-
Notifications
You must be signed in to change notification settings - Fork 3
/
create_vbox_image.sh
executable file
·84 lines (54 loc) · 1.94 KB
/
create_vbox_image.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/bash -xe
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
cd $DIR
apt-get install -y virtualbox sshpass
if [[ "$#" -ne 1 ]]; then
echo "Usage: $0 vbox_image_file"
exit 1
fi
IMAGE_NAME=$1
VM='Syncloud-VM'
SSH_PORT=3333
chmod a=r ${IMAGE_NAME}.img
VBoxManage controlvm ${VM} poweroff || true
VBoxManage unregistervm ${VM} --delete || true
rm -rf ${IMAGE_NAME}.vdi
rm -rf ${IMAGE_NAME}.vdi.xz
rm -rf ${IMAGE_NAME}-test.vdi
VBoxManage convertdd ${IMAGE_NAME}.img ${IMAGE_NAME}.vdi --format VDI
chmod a=r ${IMAGE_NAME}.vdi
cp ${IMAGE_NAME}.vdi ${IMAGE_NAME}-test.vdi
echo "testing"
xz -0 ${IMAGE_NAME}.vdi -k
rm -rf $HOME/"VirtualBox VMs"/${VM}
VBoxManage createvm --name ${VM} --ostype "Debian_64" --register
VBoxManage storagectl ${VM} --name "SATA Controller" --add sata --controller IntelAHCI
VBoxManage list hdds
ls -la
VBoxManage storageattach ${VM} --storagectl "SATA Controller" --port 0 --device 0 --type hdd --medium ${IMAGE_NAME}-test.vdi
VBoxManage modifyvm ${VM} --ioapic on
VBoxManage modifyvm ${VM} --boot1 dvd --boot2 disk --boot3 none --boot4 none
VBoxManage modifyvm ${VM} --memory 1024 --vram 128
VBoxManage modifyvm ${VM} --natpf1 "guestssh,tcp,,${SSH_PORT},,22"
VBoxHeadless --startvm ${VM} &
ATTEMPT=0
TOTAL_ATTEMPTS=10
set +e
ssh-keygen -f "/root/.ssh/known_hosts" -R [localhost]:${SSH_PORT}
sshpass -p syncloud ssh -o StrictHostKeyChecking=no -p ${SSH_PORT} root@localhost date
while test $? -gt 0
do
sleep 1
echo "Waiting for SSH ..."
ATTEMPT=$((ATTEMPT +1))
echo "attempt $ATTEMPT of $TOTAL_ATTEMPTS"
if [[ ${ATTEMPT} -gt ${TOTAL_ATTEMPTS} ]]; then
VBoxManage controlvm ${VM} screenshotpng ${DIR}/console.png
echo "unable to connect to vbox instance"
exit 1
fi
sshpass -p syncloud ssh -o StrictHostKeyChecking=no -p ${SSH_PORT} root@localhost date
done
set -e
sshpass -p syncloud ssh -o StrictHostKeyChecking=no -p ${SSH_PORT} root@localhost journalctl
VBoxManage controlvm ${VM} poweroff