site  contact  subhomenews

Generic script to mount partitions of an image file

June 14, 2022 — BarryK

I posted yesterday about a new EasyOS image file, with 'vmlinuz' and 'initrd' in the boot-partition, and 'easy.sfs' in the working-partition:

The current documentation to update an installation, by the manual method, is to click on the new 'easy*.img' file, and then copy across the new 'vmlinuz', 'initrd' and 'easy.sfs', for example here:

However, clicking on a 'easy*.img' file only mounts the first, vfat, boot-partition. The second ext4 working-partition also needs to be mounted, to get at the new 'easy.sfs'.

I have rewritten the image file mounting script, so that it can handle any .img file. It is /usr/sbin/mount-img and this is it:

#passed in 1st param "mount" or "unmount"
#for mount, passed in 2nd param name of .img file. may include path.

[ "$Maction" != "mount" -a "$Maction" != "unmount" ] && exit

if [ "$Maction" == "mount" ];then
[ "$ESPEC" == "" ] && exit
[ ! -f "$ESPEC" ] && exit

xESPEC="$(echo -n "$ESPEC" | sed -e 's%-%\\-%g' -e 's%\.%\\.%g' -e 's%\+%\\+%g')"
PARTS="$(fdisk -l --bytes ${ESPEC} | grep "^${xESPEC}" | tr -d '*' | tr -s ' ')"
#ex: offset in sectors, size in bytes. before the pipe operations...
# Boot Start End Sectors Size Id Type
#easy-4.0.3-amd64.img1 * 2048 32767 30720 15728640 ef EFI (FAT-12/16/32)
#easy-4.0.3-amd64.img2 32768 1638399 1605632 822083584 83 Linux

OFFs="$(echo "$PARTS" | cut -f 2 -d ' ' | tr '\n' ' ')"

for aOFFs in ${OFFs}
aOFFb=$(($aOFFs*512)) #assumption, 512 bytes per sector.
LOOPn="$(losetup -f)"
losetup -o ${aOFFb} ${LOOPn} ${ESPEC}
aFS="$(blkid ${LOOPn} | tr ' ' '\n' | grep '^TYPE' | cut -f 2 -d '"' | grep -E 'vfat|ext|f2fs')"
[ "$aFS" == "" ] && continue
mkdir -p /tmp/mount-img-${CNT}-${aFS}-${LOOPn##*/}
busybox mount -t ${aFS} ${LOOPn} /tmp/mount-img-${CNT}-${aFS}-${LOOPn##*/}
rox -x -d /tmp/mount-img-${CNT}-${aFS}-${LOOPn##*/}

for aMNT in `ls -d /tmp/mount-img-[1-9]-*`
[ "$aMNT" == "" ] && continue
[ ! -d "$aMNT" ] && continue
rox -D "$aMNT"
busybox umount "$aMNT"
rm -rf /tmp/$aDIR
losetup -d /dev/${aDIR/*-/}

Works great, brings up two ROX-Filer windows, with both partitions. Next, have to do the association, so it will run when click on an img file in ROX-Filer. As before, the plan is can click on the .img file again to unmount.    

Tags: easy