capture
[rrq/rescue-boot.git] / minbase-strap-mkimg.sh
1 # Include snippet for minbase-strap.sh
2 #
3 # Prepare image $IMG with 2 partitions:
4 # 1) a FAT/UEFI partition using directory $TMP, and
5 # 2) an squashfs partition using FS.sqfs
6
7 [ -e "$IMG" ] && read -p "** Will overwrite $IMG (or ^C here and now)" x
8 rm -f "$IMG"
9
10 # First partition sectors (round up to a Mb number
11 DUM1=$(( ( $(du -sB1 $TMP | sed 's/\s.*//') / 1048576 + 2 ) ))
12
13 # Second partition is a squashfs packing of $FS
14 [ -r FS.sqfs ] || ( cd $FS && mksquashfs * ../FS.sqfs )
15 DUM2=$(( $(stat -c %s FS.sqfs) / 1048576 + 2 ))
16
17 # Estimate the required disk image size in Mib
18 DUM=$(( DUM1 + DUM2 + 1 ))
19
20 # Create the image, with the partition marked as EFI parition and
21 # bootable; acutal format is FAT{12,16,32}
22 dd if=/dev/zero of="$IMG" bs=${DUM}M count=0 seek=1 status=none
23 cat <<EOF | tee -a /proc/self/fd/2 | sfdisk -q "$IMG"
24 label: dos
25
26 2048 $(( DUM1 * 2048 )) 0xef *
27 - - 0x83
28 EOF
29 PART=( $(fdisk -l -ostart,sectors $IMG | tail -n 2) )
30
31 # First sector is 2048 by default, which is byte address 1048576
32 TMPIMG=$(mktemp XXXX)
33 fusefile $TMPIMG $IMG/0+$(( ( ${PART[0]} + ${PART[1]} ) * 512 ))
34 mkfs.fat --offset ${PART[0]} "$TMPIMG"  >/dev/null
35 umount $TMPIMG
36 rm $TMPIMG
37 mcopy -i "$IMG@@1048576" -s $TMP/* ::/
38 [ -z "$SUDO_USER" ] || chown $SUDO_USER: "$IMG"
39
40 dd if=FS.sqfs of=$IMG seek=${PART[2]} conv=notrunc status=none