aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/tar-to-image.sh
blob: c3ce7aa25b5ddffd0480215af2f2751cecf76ece (plain)
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
#!/bin/sh

######################################################################
# defaults

qcow=""
size="4G"
tarb=""
mode="efi-grub2"

######################################################################
# create work dir

function msg() {
	local txt="$1"
	local bold="\x1b[1m"
	local normal="\x1b[0m"
	echo -e "${bold}### ${txt}${normal}"
}

function do_cleanup() {
	msg "cleaning up"
	if test "$GUESTFISH_PID" != ""; then
		guestfish --remote -- exit >/dev/null 2>&1 || true
	fi
	rm -rf "$WORK"
}

WORK="${TMPDIR-/var/tmp}/${0##*/}-$$"
mkdir "$WORK" || exit 1
trap 'do_cleanup' EXIT

# work files
script="$WORK/imagefish.script"
fstab="$WORK/fstab"
grubdef="$WORK/grub"
grubcfg="$WORK/grub.cfg"

# variables
rootfs=""
console=""

######################################################################
# parse args

function print_help() {
cat <<EOF
usage: $0 [ options ]
options:
  setup:
    --tar <tarball>
    --image <image>
    --size <size>                 (default: $size)
  mode:
    --efi-grub2                   (default)
    --rpi32
    --rpi64
EOF
}

while test "$1" != ""; do
	case "$1" in
	-i | --image)
		qcow="$2"
		shift; shift
		;;
	-s | --size)
		size="$2"
		shift; shift
		;;
	-t | --tar | --tarball)
		tarb="$2"
		shift; shift
		;;
	--efi-grub2)
		mode="efi-grub2"
		shift
		;;
	--efi-systemd)
		mode="efi-systemd"
		shift
		;;
	--rpi32)
		mode="rpi32"
		shift
		;;
	--rpi64)
		mode="rpi64"
		shift
		;;
	--force)
		allow_override="yes"
		shift
		;;
	*)	echo "ERROR: unknown arg: $1"
		exit 1
		;;
	esac
done

######################################################################
# sanity checks

if test "$qcow" = ""; then
	echo "ERROR: no image given"
	exit 1
fi
if test -f "$qcow"; then
	if test "$allow_override" = "yes"; then
		rm -f "$qcow"
	else
		echo "ERROR: image exists: $qcow"
		exit 1
	fi
fi
if test ! -f "$tarb"; then
	echo "ERROR: tarball not found: $tarb"
	exit 1
fi

######################################################################
# uuids & other names

uuid_gpt_bios="21686148-6449-6E6F-744E-656564454649"
uuid_gpt_uefi="C12A7328-F81F-11D2-BA4B-00A0C93EC93B"
uuid_gpt_swap="0657fd6d-a4ab-43c4-84e5-0933c84b4f4f"
uuid_gpt_root="FIXME"

uuid_gpt_root_ia32="44479540-f297-41b2-9af7-d131d5f0458a"
uuid_gpt_root_x64="4f68bce3-e8cd-4db1-96e7-fbcaf984b709"
uuid_gpt_root_arm="69dad710-2ce4-4e3c-b16c-21a1d49abed3"
uuid_gpt_root_a64="b921b045-1df0-41c3-af44-4c6f280d3fae"

uefi_boot_file="FIXME"
uefi_boot_file_ia32="BOOTIA32.EFI"
uefi_boot_file_x64="BOOTX64.EFI"
uefi_boot_file_arm="BOOTARM.EFI"
uefi_boot_file_a64="BOOTAA64.EFI"

######################################################################
# guestfish script helpers

function fish() {
	echo "#" "$@"
	guestfish --remote -- "$@"		|| exit 1
}

function fish_init() {
	local format

	case "$qcow" in
	*.raw)	format="raw" ;;
	*)	format="qcow2";;
	esac

	msg "creating and adding disk image"
	fish disk-create $qcow $format $size
	fish add $qcow
	fish run
}

function fish_fini() {
	msg "finishing"
	fish umount-all
	fish exit
}

function fish_partition() {
	local ptype="$1"
	local szbios="$2"
	local szuefi="$3"
	local szboot="$4"
	local szswap="$5"
	local pstart=2048
	local pend

	msg "creating partitions"
	fish part-init /dev/sda $ptype
	for size in $szbios $szuefi $szboot $szswap; do
		test "$size" = "0" && continue
		pend=$(( $pstart + $size * 2048 - 1 ))
		fish part-add /dev/sda p $pstart $pend
		pstart=$(( $pend + 1 ))
	done
	fish part-add /dev/sda p $pstart -2048
}

function fish_copy_tar() {
	msg "copying tarball to image"
	fish tar-in	$tarb	/	compress:gzip
	fish copy-in	$fstab	/etc
}

function fish_part_efi_grub2() {
	local mode="$1"
	local id_uefi id_boot id_swap id_root
	local nr_uefi nr_boot nr_swap nr_root

	if test "$mode" = "bios"; then
		fish_partition gpt 4 64 384 512
		fish part-set-gpt-type /dev/sda 1 ${uuid_gpt_bios}
		nr_uefi=2
		nr_boot=3
		nr_swap=4
		nr_root=5
	else
		fish_partition gpt 0 64 384 512
		nr_uefi=1
		nr_boot=2
		nr_swap=3
		nr_root=4
	fi

	fish part-set-gpt-type /dev/sda ${nr_uefi} ${uuid_gpt_uefi}
	fish part-set-bootable /dev/sda ${nr_uefi} true
	fish part-set-gpt-type /dev/sda ${nr_swap} ${uuid_gpt_swap}
	fish part-set-gpt-type /dev/sda ${nr_root} ${uuid_gpt_root}

	msg "creating filesystems"
	fish mkfs fat	/dev/sda${nr_uefi}	label:UEFI
	fish mkfs ext2	/dev/sda${nr_boot}	label:boot
	fish mkswap	/dev/sda${nr_swap}	label:swap
	fish mkfs ext4	/dev/sda${nr_root}	label:root

	id_uefi=$(guestfish --remote -- vfs-uuid /dev/sda${nr_uefi})
	id_boot=$(guestfish --remote -- vfs-uuid /dev/sda${nr_boot})
	id_swap=$(guestfish --remote -- vfs-uuid /dev/sda${nr_swap})
	id_root=$(guestfish --remote -- vfs-uuid /dev/sda${nr_root})
	rootfs="UUID=${id_root}"

	msg "mounting filesystems"
	fish mount	/dev/sda${nr_root}	/
	fish mkdir				/boot
	fish mount	/dev/sda${nr_boot}	/boot
	fish mkdir				/boot/efi
	fish mount	/dev/sda${nr_uefi}	/boot/efi

	cat <<-EOF > "$fstab"
	UUID=${id_root}	/		ext4	defaults	0 0
	UUID=${id_boot}	/boot		ext2	defaults	0 0
	UUID=${id_uefi}	/boot/efi	vfat	defaults	0 0
	UUID=${id_swap}	swap		swap	defaults	0 0
EOF
}

function fish_grub2_efi() {
	local term="${1-console}"
	local havegrubby haveboot grubefi

	msg "boot setup (root=${rootfs})"
	kver=$(guestfish --remote -- ls /boot \
		| grep -e "^vmlinuz-" | grep -v rescue \
		| sed -e "s/vmlinuz-//")
	echo "### kernel version is $kver"

	echo "### rebuilding initramfs"
	fish command "dracut --force /boot/initramfs-${kver}.img ${kver}"

	havegrubby=$(guestfish --remote -- is-file /usr/sbin/grubby)
	if test "$havegrubby" = "true"; then
		echo "### create grub2 boot loader config (grubby mode)"
		cat <<-EOF > "$grubdef"
			GRUB_TIMEOUT="5"
			GRUB_TERMINAL_OUTPUT="${term}"
			GRUB_DISABLE_SUBMENU="true"
			GRUB_DISABLE_RECOVERY="true"
			GRUB_CMDLINE_LINUX="ro root=${rootfs} ${console}"
EOF
		fish copy-in $grubdef /etc/default
		fish command "sh -c 'grub2-mkconfig > /etc/grub2-efi.cfg'"
		fish command "sed -i -c -e s/linux16/linuxefi/ /etc/grub2-efi.cfg"
		fish command "sed -i -c -e s/initrd16/initrdefi/ /etc/grub2-efi.cfg"
	else
		echo "### create grub2 boot loader config (bls mode)"
		cat <<-EOF > "$grubdef"
			GRUB_TIMEOUT="5"
			GRUB_TERMINAL_OUTPUT="${term}"
			GRUB_DISABLE_SUBMENU="true"
			GRUB_DISABLE_RECOVERY="true"
			GRUB_CMDLINE_LINUX="ro root=${rootfs} ${console}"
			GRUB_ENABLE_BLSCFG="true"
EOF
		fish copy-in $grubdef /etc/default
		fish command "sh -c 'grub2-mkconfig > /etc/grub2-efi.cfg'"
	fi

	haveboot=$(guestfish --remote -- is-file /boot/efi/EFI/BOOT/${uefi_boot_file})
	if test "$haveboot" = "true"; then
		echo "### have ${uefi_boot_file}, good"
	else
		grubefi=$(guestfish --remote -- glob-expand /boot/efi/EFI/*/grub*.efi)
		echo "### install ${grubefi} as ${uefi_boot_file}"
		fish cp	${grubefi} /boot/efi/EFI/BOOT/${uefi_boot_file}
	fi
}

function fish_part_efi_systemd() {
	local mode="$1"
	local id_uefi id_swap id_root
	local nr_uefi nr_swap nr_root

	if test "$mode" = "bios"; then
		fish_partition gpt 4 512 0 512
		fish part-set-gpt-type /dev/sda 1 ${uuid_gpt_bios}
		nr_uefi=2
		nr_swap=3
		nr_root=4
	else
		fish_partition gpt 0 512 0 512
		nr_uefi=1
		nr_swap=2
		nr_root=3
	fi

	fish part-set-gpt-type /dev/sda ${nr_uefi} ${uuid_gpt_uefi}
	fish part-set-bootable /dev/sda ${nr_uefi} true
	fish part-set-gpt-type /dev/sda ${nr_swap} ${uuid_gpt_swap}
	fish part-set-gpt-type /dev/sda ${nr_root} ${uuid_gpt_root}

	msg "creating filesystems"
	fish mkfs fat	/dev/sda${nr_uefi}	label:UEFI
	fish mkswap	/dev/sda${nr_swap}	label:swap
	fish mkfs ext4	/dev/sda${nr_root}	label:root

	id_uefi=$(guestfish --remote -- vfs-uuid /dev/sda${nr_uefi})
	id_swap=$(guestfish --remote -- vfs-uuid /dev/sda${nr_swap})
	id_root=$(guestfish --remote -- vfs-uuid /dev/sda${nr_root})
	rootfs="UUID=${id_root}"

	msg "mounting filesystems"
	fish mount	/dev/sda${nr_root}	/
	fish mkdir				/boot
	fish mount	/dev/sda${nr_uefi}	/boot

	cat <<-EOF > "$fstab"
	UUID=${id_root}	/		ext4	defaults	0 0
	UUID=${id_uefi}	/boot		vfat	defaults	0 0
	UUID=${id_swap}	swap		swap	defaults	0 0
EOF
}

function fish_systemd_boot() {
	msg "boot setup (root=${rootfs})"
	kver=$(guestfish --remote -- ls /lib/modules)
	echo "### kernel version is $kver"

	havegrub=$(guestfish --remote -- is-dir /usr/lib/grub/i386-pc)
	if test "$havegrub" = "true"; then
		#
		# install grub2 for bios, teach it to use the boot
		# loader spec entries created by systemd-boot.  This
		# gives us an image which boots with both bios and
		# efi.
		#
		# Then uninstall the rpms so the grub2 scripts don't
		# get into the way when updating the systemd-boot
		# config on kernel updates.  Also cleanup files
		# already created by grub2 scripts.
		#
		echo "### grub2 bios boot hack"
		fish command "grub2-install --target=i386-pc /dev/sda"
		grubrpms=$(guestfish --remote -- command "rpm -qa 'grub2*'")
		grubrpms=$(echo $grubrpms)
		fish command "rpm -e -v $grubrpms"
		cat <<-EOF > "$grubcfg"
		function load_video {
			insmod all_video
		}

		insmod part_gpt
		insmod fat
		insmod serial
		insmod terminal
		insmod blscfg

		serial --unit=0 --speed=115200
		terminal_output console serial
		terminal_input console serial

		set boot='hd0,gpt2'
		set timeout=3
		blscfg
EOF
		fish copy-in $grubcfg /boot/grub2
		fish glob rm /boot/*$kver
	fi

	echo "### init systemd-boot"
	fish mkdir-p /etc/kernel
	fish write /etc/kernel/cmdline "ro root=${rootfs} ${console}"
	fish command "bootctl --no-variables install"
	fish command "kernel-install remove ${kver} /lib/modules/${kver}/vmlinuz"
	fish command "kernel-install add ${kver} /lib/modules/${kver}/vmlinuz"
	fish command "sed -i -e '/timeout/s/^#//' /boot/loader/loader.conf"
}

function fish_part_rpi() {
	local bootpart="${1-2}"

	local id_firm id_boot id_swap id_root
	fish_partition mbr 0 64 384 512

	fish part-set-bootable /dev/sda $bootpart true
	fish part-set-mbr-id /dev/sda 1 0x0c
	fish part-set-mbr-id /dev/sda 2 0x83
	fish part-set-mbr-id /dev/sda 3 0x82
	fish part-set-mbr-id /dev/sda 4 0x83

	msg "creating filesystems"
	fish mkfs fat	/dev/sda1	label:firm
	fish mkfs ext2	/dev/sda2	label:boot
	fish mkswap	/dev/sda3	label:swap
	fish mkfs ext4	/dev/sda4	label:root

	id_firm=$(guestfish --remote -- vfs-uuid /dev/sda1)
	id_boot=$(guestfish --remote -- vfs-uuid /dev/sda2)
	id_swap=$(guestfish --remote -- vfs-uuid /dev/sda3)
	id_root=$(guestfish --remote -- vfs-uuid /dev/sda4)
	#rootfs="LABEL=root"
	rootfs="UUID=${id_root}"

	msg "mounting filesystems"
	fish mount	/dev/sda4	/
	fish mkdir			/boot
	fish mount	/dev/sda2	/boot
	fish mkdir			/boot/efi
	fish mount	/dev/sda1	/boot/efi

	cat <<-EOF > "$fstab"
	UUID=${id_root}	/		ext4	defaults	0 0
	UUID=${id_boot}	/boot		ext2	defaults	0 0
	UUID=${id_firm}	/boot/efi	vfat	defaults	0 0
	#UUID=${id_swap}	swap		swap	defaults	0 0
EOF
}

function fish_firmware_rpi32() {
	msg "rpi 32bit firmware setup"
	fish glob cp-a "/usr/share/bcm283x-firmware/*"	/boot/efi
	fish cp	/usr/share/uboot/rpi_2/u-boot.bin	/boot/efi/rpi2-u-boot.bin
	fish cp	/usr/share/uboot/rpi_3_32b/u-boot.bin	/boot/efi/rpi3-u-boot.bin
}

function fish_firmware_rpi64() {
	msg "rpi 64bit firmware setup"
	fish glob cp-a "/usr/share/bcm283x-firmware/*"	/boot/efi
	fish cp	/usr/share/uboot/rpi_3/u-boot.bin	/boot/efi/rpi3-u-boot.bin
	fish rename /boot/efi/config.txt		/boot/efi/config-32.txt
	fish rename /boot/efi/config-64.txt		/boot/efi/config.txt

	# copy kernel dtb to efi partition
	fish mkdir					/boot/efi/dtb
	fish glob cp-a "/boot/dtb-*/broadcom"		/boot/efi/dtb

	# copy grub to boot (workaround u-boot bug)
	fish mkdir					/boot/efi/efi/boot
	fish cp /boot/efi/efi/fedora/grubaa64.efi	/boot/efi/efi/boot
}

function fish_extlinux_rpi32() {
	local cmdline="ro root=${rootfs} ${console}"
	local kver

	msg "boot setup (root=${rootfs})"
	kver=$(guestfish --remote -- ls /boot \
		| grep -e "^vmlinuz-" | grep -v rescue \
		| sed -e "s/vmlinuz-//")
	echo "### kernel version is $kver"

	echo "### creating extlinux.conf"
	cat <<-EOF >> "$WORK/extlinux.conf"
	menu title Fedora boot menu
	timeout 30
	label Fedora (${kver})
	  kernel /vmlinuz-${kver}
	  append ${cmdline}
	  fdtdir /dtb-${kver}/
	  initrd /initramfs-${kver}.img
EOF
	fish copy-in "$WORK/extlinux.conf" /boot/extlinux

	echo "### rebuilding initramfs"
	fish command "dracut --force /boot/initramfs-${kver}.img ${kver}"
}

function fish_extlinux_rpi64() {
	local cmdline="ro root=${rootfs} ${console}"
	local kernel kver

	msg "boot setup (root=${rootfs})"
	kernel=$(guestfish --remote -- ls /boot \
		| grep -e "^vmlinu[xz]-" | grep -v rescue)
	echo "### kernel image is $kernel"
	kver=$(echo $kernel | sed -e "s/vmlinu[xz]-//")
	echo "### kernel version is $kver"

	echo "### creating extlinux.conf"
	cat <<-EOF >> "$WORK/extlinux.conf"
	menu title Fedora boot menu
	timeout 30
	label Fedora (${kver})
	  kernel /vmlinux-${kver}
	  append ${cmdline}
	  fdtdir /dtb-${kver}/
	  initrd /initramfs-${kver}.img
EOF
	fish copy-in "$WORK/extlinux.conf" /boot/extlinux

	echo "### rebuilding initramfs"
	fish command "dracut --force /boot/initramfs-${kver}.img ${kver}"
}

######################################################################
# go!

export LIBGUESTFS_BACKEND=direct
eval $(guestfish --listen)
if test "$GUESTFISH_PID" = ""; then
	echo "ERROR: starting guestfish failed"
	exit 1
fi

case "$(uname -m)" in
armv7*)
	console="console=ttyAMA0,115200 console=tty1"
	uuid_gpt_root="$uuid_gpt_root_arm"
	uefi_boot_file="$uefi_boot_file_arm"
	uefi_part_mode="pure"
	;;
aarch64)
	console="console=ttyAMA0,115200 console=tty1"
	uuid_gpt_root="$uuid_gpt_root_a64"
	uefi_boot_file="$uefi_boot_file_a64"
	uefi_part_mode="pure"
	;;
i?86)
	console="console=ttyS0,115200 console=tty1"
	uuid_gpt_root="$uuid_gpt_root_ia32"
	uefi_boot_file="$uefi_boot_file_ia32"
	uefi_part_mode="bios"
	;;
x86_64)
	console="console=ttyS0,115200 console=tty1"
	uuid_gpt_root="$uuid_gpt_root_x64"
	uefi_boot_file="$uefi_boot_file_x64"
	uefi_part_mode="bios"
	;;
esac

case "$mode" in
efi-grub2)
	fish_init
	fish_part_efi_grub2 ${uefi_part_mode}
	fish_copy_tar
	fish_grub2_efi
	fish_fini
	;;
efi-systemd)
	fish_init
	fish_part_efi_systemd ${uefi_part_mode}
	fish_copy_tar
	fish_systemd_boot
	fish_fini
	;;
rpi32)
	fish_init
	fish_part_rpi	2
	fish_copy_tar
	fish_firmware_rpi32
	fish_extlinux_rpi32
	fish_fini
	;;
rpi64)
	fish_init
	fish_part_rpi	1
	fish_copy_tar
	fish_firmware_rpi64
#	fish_extlinux_rpi64
	fish_grub2_efi	gfxterm
	fish_fini
	;;
*)
	# should not happen
	echo "Oops"
	exit 1
	;;
esac

msg "all done."