forked from rancher/k3os
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·345 lines (300 loc) · 7.78 KB
/
install.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/bin/bash
set -e
PROG=$0
PROGS="dd curl mkfs.ext4 mkfs.vfat fatlabel parted partprobe grub-install"
DISTRO=/run/k3os/iso
if [ "$K3OS_DEBUG" = true ]; then
set -x
fi
get_url()
{
FROM=$1
TO=$2
case $FROM in
http*)
curl -o $TO -fL ${FROM}
;;
*)
cp -f $FROM $TO
;;
esac
}
cleanup2()
{
if [ -n "${TARGET}" ]; then
umount ${TARGET}/boot/efi || true
umount ${TARGET} || true
fi
losetup -d ${ISO_DEVICE} || true
umount $DISTRO || true
}
cleanup()
{
EXIT=$?
cleanup2 2>/dev/null || true
return $EXIT
}
usage()
{
echo "Usage: $PROG [--force-efi] [--debug] [--tty TTY] [--poweroff] [--takeover] [--no-format] [--config https://.../config.yaml] DEVICE ISO_URL"
echo ""
echo "Example: $PROG /dev/vda https://github.com/rancher/k3os/releases/download/v0.2.0/k3os.iso"
echo ""
echo "DEVICE must be the disk that will be partitioned (/dev/vda). If you are using --no-format it should be the device of the K3OS_STATE partition (/dev/vda2)"
echo ""
echo "The parameters names refer to the same names used in the cmdline, refer to README.md for"
echo "more info."
echo ""
exit 1
}
do_format()
{
if [ "$K3OS_INSTALL_NO_FORMAT" = "true" ]; then
STATE=$(blkid -L K3OS_STATE || true)
if [ -z "$STATE" ] && [ -n "$DEVICE" ]; then
tune2fs -L K3OS_STATE $DEVICE
STATE=$(blkid -L K3OS_STATE)
fi
return 0
fi
dd if=/dev/zero of=${DEVICE} bs=1M count=1
parted -s ${DEVICE} mklabel ${PARTTABLE}
if [ "$PARTTABLE" = "gpt" ]; then
BOOT_NUM=1
STATE_NUM=2
parted -s ${DEVICE} mkpart primary fat32 0% 50MB
parted -s ${DEVICE} mkpart primary ext4 50MB 550MB
else
BOOT_NUM=
STATE_NUM=1
parted -s ${DEVICE} mkpart primary ext4 0% 500MB
fi
parted -s ${DEVICE} set 1 ${BOOTFLAG} on
partprobe 2>/dev/null || true
PREFIX=${DEVICE}
if [ ! -e ${PREFIX}${STATE_NUM} ]; then
PREFIX=${DEVICE}p
fi
if [ ! -e ${PREFIX}${STATE_NUM} ]; then
echo Failed to find ${PREFIX}${STATE_NUM} or ${DEVICE}${STATE_NUM} to format
exit 1
fi
if [ -n "${BOOT_NUM}" ]; then
BOOT=${PREFIX}${BOOT_NUM}
fi
STATE=${PREFIX}${STATE_NUM}
mkfs.ext4 -F -L K3OS_STATE ${STATE}
if [ -n "${BOOT}" ]; then
mkfs.vfat -F 32 ${BOOT}
fatlabel ${BOOT} K3OS_GRUB
fi
}
do_mount()
{
TARGET=/run/k3os/target
mkdir -p ${TARGET}
mount ${STATE} ${TARGET}
mkdir -p ${TARGET}/boot
if [ -n "${BOOT}" ]; then
mkdir -p ${TARGET}/boot/efi
mount ${BOOT} ${TARGET}/boot/efi
fi
mkdir -p $DISTRO
mount -t iso9660 -o ro $ISO_DEVICE $DISTRO
}
do_copy()
{
tar cf - -C ${DISTRO} k3os | tar xvf - -C ${TARGET}
if [ -n "$STATE_NUM" ]; then
echo $DEVICE $STATE_NUM > $TARGET/k3os/system/growpart
fi
if [ -n "$K3OS_INSTALL_CONFIG_URL" ]; then
get_url "$K3OS_INSTALL_CONFIG_URL" ${TARGET}/k3os/system/config.yaml
chmod 600 ${TARGET}/k3os/system/config.yaml
fi
if [ "$K3OS_INSTALL_TAKE_OVER" = "true" ]; then
touch ${TARGET}/k3os/system/takeover
fi
}
install_grub()
{
if [ "$K3OS_INSTALL_DEBUG" ]; then
GRUB_DEBUG="k3os.debug"
fi
mkdir -p ${TARGET}/boot/grub
cat > ${TARGET}/boot/grub/grub.cfg << EOF
set default=0
set timeout=10
set gfxmode=auto
set gfxpayload=keep
insmod all_video
insmod gfxterm
menuentry "k3OS Current" {
search.fs_label K3OS_STATE root
set sqfile=/k3os/system/kernel/current/kernel.squashfs
loopback loop0 /\$sqfile
set root=(\$root)
linux (loop0)/vmlinuz printk.devkmsg=on console=tty1 $GRUB_DEBUG
initrd /k3os/system/kernel/current/initrd
}
menuentry "k3OS Previous" {
search.fs_label K3OS_STATE root
set root=(\$root)
linux /k3os/system/kernel/previous/vmlinuz printk.devkmsg=on console=tty1 $GRUB_DEBUG
initrd /k3os/system/kernel/previous/initrd
}
menuentry "k3OS Rescue Shell" {
search.fs_label K3OS_STATE root
set root=(\$root)
linux /k3os/system/kernel/current/vmlinuz printk.devkmsg=on rescue console=tty1
initrd /k3os/system/kernel/current/initrd
}
EOF
if [ -z "${K3OS_INSTALL_TTY}" ]; then
TTY=$(tty | sed 's!/dev/!!')
else
TTY=$K3OS_INSTALL_TTY
fi
if [ -e "/dev/$TTY" ] && [ "$TTY" != tty1 ] && [ -n "$TTY" ]; then
sed -i "s!console=tty1!console=tty1 console=${TTY}!g" ${TARGET}/boot/grub/grub.cfg
fi
if [ "$K3OS_INSTALL_NO_FORMAT" = "true" ]; then
return 0
fi
if [ "$K3OS_INSTALL_FORCE_EFI" = "true" ]; then
GRUB_TARGET="--target=x86_64-efi"
fi
grub-install ${GRUB_TARGET} --boot-directory=${TARGET}/boot ${DEVICE}
}
get_iso()
{
ISO_DEVICE=$(blkid -L K3OS || true)
if [ -z "${ISO_DEVICE}" ]; then
for i in $(lsblk -o NAME,TYPE -n | grep -w disk | awk '{print $1}'); do
mkdir -p ${DISTRO}
if mount -t iso9660 -o ro /dev/$i ${DISTRO}; then
ISO_DEVICE="/dev/$i"
umount ${DISTRO}
break
fi
done
fi
if [ -z "${ISO_DEVICE}" ] && [ -n "$K3OS_INSTALL_ISO_URL" ]; then
TEMP_FILE=$(mktemp k3os.XXXXXXXX.iso)
get_url ${K3OS_INSTALL_ISO_URL} ${TEMP_FILE}
ISO_DEVICE=$(losetup --show -f $TEMP_FILE)
rm -f $TEMP_FILE
fi
if [ -z "${ISO_DEVICE}" ]; then
echo "#### There is no k3os ISO device"
return 1
fi
}
setup_style()
{
if [ "$K3OS_INSTALL_FORCE_EFI" = "true" ] || [ -e /sys/firmware/efi ]; then
PARTTABLE=gpt
BOOTFLAG=esp
if [ ! -e /sys/firmware/efi ]; then
echo WARNING: installing EFI on to a system that does not support EFI
fi
else
PARTTABLE=msdos
BOOTFLAG=boot
fi
}
validate_progs()
{
for i in $PROGS; do
if [ ! -x "$(which $i)" ]; then
MISSING="${MISSING} $i"
fi
done
if [ -n "${MISSING}" ]; then
echo "The following required programs are missing for installation: ${MISSING}"
exit 1
fi
}
validate_device()
{
DEVICE=$K3OS_INSTALL_DEVICE
if [ ! -b ${DEVICE} ]; then
echo "You should use an available device. Device ${DEVICE} does not exist."
exit 1
fi
}
while [ "$#" -gt 0 ]; do
case $1 in
--no-format)
K3OS_INSTALL_NO_FORMAT=true
;;
--force-efi)
K3OS_INSTALL_FORCE_EFI=true
;;
--poweroff)
K3OS_INSTALL_POWER_OFF=true
;;
--takeover)
K3OS_INSTALL_TAKE_OVER=true
;;
--debug)
set -x
K3OS_INSTALL_DEBUG=true
;;
--config)
shift 1
K3OS_INSTALL_CONFIG_URL=$1
;;
--tty)
shift 1
K3OS_INSTALL_TTY=$1
;;
-h)
usage
;;
--help)
usage
;;
*)
if [ "$#" -gt 2 ]; then
usage
fi
INTERACTIVE=true
K3OS_INSTALL_DEVICE=$1
K3OS_INSTALL_ISO_URL=$2
break
;;
esac
shift 1
done
if [ -e /etc/environment ]; then
source /etc/environment
fi
if [ -e /etc/os-release ]; then
source /etc/os-release
if [ -z "$K3OS_INSTALL_ISO_URL" ]; then
K3OS_INSTALL_ISO_URL=${ISO_URL}
fi
fi
if [ -z "$K3OS_INSTALL_DEVICE" ]; then
usage
fi
validate_progs
validate_device
trap cleanup exit
get_iso
setup_style
do_format
do_mount
do_copy
install_grub
if [ -n "$INTERACTIVE" ]; then
exit 0
fi
if [ "$K3OS_INSTALL_POWER_OFF" = true ] || grep -q 'k3os.mode=install' /proc/cmdline; then
poweroff -f
else
echo " * Rebooting system in 5 seconds (CTRL+C to cancel)"
sleep 5
reboot -f
fi