removed
[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 DUM2=$(( $(stat -c %s FS.sqfs) / 1048576 + 2 ))
13
14 # Estimate the required disk image size in Mib
15 DUM=$(( DUM1 + DUM2 + 1 ))
16
17 # Create the image, with the partition marked as EFI parition and
18 # bootable; acutal format is FAT{12,16,32}
19 dd if=/dev/zero of="$IMG" bs=${DUM}M count=0 seek=1 status=none
20 cat <<EOF | tee -a /proc/self/fd/2 | sfdisk -q "$IMG"
21 label: dos
22
23 2048 $(( DUM1 * 2048 )) 0xef *
24 - - 0x83
25 EOF
26 PART=( $(fdisk -l -ostart,sectors $IMG | tail -n 2) )
27 echo "${PART[*]}"
28
29 # First sector is 2048 by default, which is byte address 1048576
30 TMPIMG=$(mktemp XXXX)
31 fusefile $TMPIMG $IMG/0+$(( ( ${PART[0]} + ${PART[1]} ) * 512 ))
32 mkfs.fat --offset ${PART[0]} "$TMPIMG"  >/dev/null
33 umount $TMPIMG
34 rm $TMPIMG
35 mcopy -i "$IMG@@1048576" -s $TMP/* ::/
36 [ -z "$SUDO_USER" ] || chown $SUDO_USER: "$IMG"
37
38 dd if=FS.sqfs of=$IMG seek=${PART[2]} conv=notrunc status=none