-
Notifications
You must be signed in to change notification settings - Fork 42
/
installqemu.expect
198 lines (142 loc) · 3.93 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
###############################################
#
# install alpine with docker in qemu x86_64
#
# Wait enough (forever) until a long-time boot
set timeout -1
# load the answerfile
set answerfile [exec cat ./answerfile]
# create the install dir
spawn mkdir -p "$env(INSTALLATION_DIR)"
# copy the scripts to the install dir
expect "$"
spawn cp startqemu.sh "$env(INSTALLATION_DIR)"
expect "$"
spawn cp ssh2qemu.sh "$env(INSTALLATION_DIR)"
spawn curl -v -L -o "$env(INSTALLATION_DIR)/alpine.iso" -C - $env(ALPINE_ISO_URL)
expect "left intact"
spawn rm -f "$env(INSTALLATION_DIR)/qemukey" "$env(INSTALLATION_DIR)/qemukey.pub"
expect "$"
spawn ssh-keygen -b 2048 -t rsa -N "" -f "$env(INSTALLATION_DIR)/qemukey"
expect "\[SHA256\]"
set qemukey [exec cat "$env(INSTALLATION_DIR)/qemukey.pub"]
#
# install the system
#
spawn rm -f "$env(INSTALLATION_DIR)/alpine.img"
spawn qemu-img create -f qcow2 "$env(INSTALLATION_DIR)/alpine.img" $env(DISK_SIZE)
expect "alpine.img"
sleep 5
spawn qemu-system-x86_64 -machine q35 -m 1024 -smp cpus=2 -cpu qemu64 \
-drive if=pflash,format=raw,read-only=on,file=$env(PREFIX)/share/qemu/edk2-x86_64-code.fd \
-netdev user,id=n1,hostfwd=tcp::2222-:22 -device virtio-net,netdev=n1 \
-cdrom "$env(INSTALLATION_DIR)/alpine.iso" \
-nographic "$env(INSTALLATION_DIR)/alpine.img"
set qemuID $spawn_id
expect "login:"
send "root\r"
# dns fix
expect ":~#"
send "echo 'nameserver 8.8.8.8' > /etc/resolv.conf\r"
expect ":~#"
send "sed -i 's/^#RESOLV_CONF=\"no\"/RESOLV_CONF=\"no\"/' /etc/udhcpc/udhcpc.conf\r"
# disable ipv6
expect "#"
send "echo 'net.ipv6.conf.all.disable_ipv6 = 1' >> /etc/sysctl.conf\r"
expect "#"
send "sysctl -p\r"
expect ":~#"
send "setup-interfaces\r"
expect "\[eth0\]"
send "\r"
expect "\[dhcp\]"
send "\r"
expect "\[no\]"
send "\r"
expect ":~#"
send "ifup eth0\r"
expect ":~#"
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 ":~#"
send "setup-alpine -f answerfile\r"
expect "password:"
send "$env(ROOT_PASSWORD)\n"
expect "password:"
send "$env(ROOT_PASSWORD)\n"
expect "Setup a user?"
send "\r"
expect "Erase the above disk"
send "y\r"
expect "No error reported."
expect "Please reboot"
send "poweroff\r"
expect ":~#"
# back on console
expect "$"
#
# setup the system
#
spawn qemu-system-x86_64 -machine q35 -m 1024 -smp cpus=2 -cpu qemu64 \
-drive if=pflash,format=raw,read-only=on,file=$env(PREFIX)/share/qemu/edk2-x86_64-code.fd \
-netdev user,id=n1,hostfwd=tcp::2222-:22 -device virtio-net,netdev=n1 \
-nographic "$env(INSTALLATION_DIR)/alpine.img"
set qemuID $spawn_id
expect "login:"
send "root\r"
expect "Password:"
send "$env(ROOT_PASSWORD)\n"
# only allow login with ssh key
expect "#"
send "sed -i -E 's/^#PermitRootLogin prohibit-password/PermitRootLogin prohibit-password/' /etc/ssh/sshd_config\r"
expect "#"
send "service sshd start\r"
# dns fix
expect "#"
send "echo 'nameserver 8.8.8.8' > /etc/resolv.conf\r"
expect "#"
send "sed -i 's/^#RESOLV_CONF=\"no\"/RESOLV_CONF=\"no\"/' /etc/udhcpc/udhcpc.conf\r"
# enable repo
expect "#"
send "sed -i 's|^#http://|http://|' /etc/apk/repositories\r"
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 "sed -i -E 's/^algo0=zstd/algo0=lzo/' /etc/conf.d/zram-init\r"
expect "#"
send "service zram-init start\r"
expect "#"
send "rc-update add zram-init\r"
expect "#"
send "sed -i -E 's/^algo0=zstd/algo0=lzo/' /etc/conf.d/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 "poweroff\r"
expect ":~#"
# back on console
expect "$"