-
Notifications
You must be signed in to change notification settings - Fork 4
/
installqemu.expect
158 lines (112 loc) · 2.74 KB
/
installqemu.expect
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
###############################################
#
# install alpine with docker in qemu x86_64
#
# Wait enough (forever) until a long-time boot
set timeout -1
spawn curl -v -L -o alpine.iso -C - $env(ALPINE_ISO_URL)
expect "left intact"
spawn rm -f ./qemukey ./qemukey.pub
sleep 5
spawn ssh-keygen -b 2048 -t rsa -N "" -f ./qemukey
expect "\[SHA256\]"
set qemukey [exec cat ./qemukey.pub]
set answerfile [exec cat ./answerfile]
#
# install the system
#
spawn rm -f alpine.img
spawn qemu-img create -f qcow2 alpine.img $env(DISK_SIZE)
sleep 5
spawn qemu-system-x86_64 -machine q35 -m 1024 -smp cpus=2 -cpu qemu64 \
-drive if=pflash,format=raw,read-only,file=$env(PREFIX)/share/qemu/edk2-x86_64-code.fd \
-netdev user,id=n1,hostfwd=tcp::2222-:22 -device virtio-net,netdev=n1 \
-cdrom alpine.iso \
-nographic alpine.img
set qemuID $spawn_id
expect "login:"
send "root\r"
#expect "Password:"
#send "\n"
expect "localhost:~#"
send "setup-interfaces\r"
expect "\[eth0\]"
send "\r"
expect "\[dhcp\]"
send "\r"
expect "\[no\]"
send "\r"
expect "localhost:~#"
send "ifup eth0\r"
expect "localhost:~#"
send "sed -i -E 's/(local kernel_opts)=.*/\\1=\"console=ttyS0\"/' /sbin/setup-disk\r"
expect "localhost:~#"
send "vi /root/answerfile\r"
sleep 3
send "i"
sleep 3
send "$answerfile\r"
# Escape
sleep 3
send [format "%c(" 27]
sleep 3
send ":wq\r"
sleep 3
send "cat /root/answerfile\r"
sleep 3
expect "localhost:~#"
send "setup-alpine -f answerfile\r"
expect "password:"
send "$env(ROOT_PASSWORD)\n"
expect "password:"
send "$env(ROOT_PASSWORD)\n"
expect "\[n\]"
send "y\r"
expect "Please reboot"
send "halt\r"
expect "System halted"
# Ctrl-a + x (qemu machine monitor halt)
send "\x01"
send "x"
close -i $qemuID
#
# setup the system
#
sleep 5
spawn qemu-system-x86_64 -machine q35 -m 1024 -smp cpus=2 -cpu qemu64 \
-drive if=pflash,format=raw,read-only,file=$env(PREFIX)/share/qemu/edk2-x86_64-code.fd \
-netdev user,id=n1,hostfwd=tcp::2222-:22 -device virtio-net,netdev=n1 \
-nographic alpine.img
set qemuID $spawn_id
expect "login:"
send "root\r"
expect "Password:"
send "$env(ROOT_PASSWORD)\n"
expect "#"
send "apk update && apk add docker ip6tables\r"
expect "#"
send "service docker start\r"
expect "#"
send "rc-update add docker\r"
expect "#"
send "apk add zram-init\r"
# fix broken zram init script
expect "#"
send "sed -i -E 's/num_devices=2/num_devices=1/' /etc/conf.d/zram-init\r"
expect "#"
send "service zram-init start\r"
expect "#"
send "rc-update add zram-init\r"
expect "#"
send "mkdir -p /root/.ssh\r"
expect "#"
send "chmod 700 /root/.ssh\r"
expect "#"
send "echo $qemukey >> /root/.ssh/authorized_keys\r"
expect "#"
send "halt\r"
expect "System halted"
# Ctrl-a + x (qemu machine monitor halt)
send "\x01"
send "x"
close -i $qemuID