#! /bin/bash # (c) 2023 by Thomas Lange, lange@debian.org # # mk-data-partition ISO | # extend an ISO file by size and create a data partition # for USB stick the data partition will take the whole size of the USB stick mntdir=/media/data add_p3() { # $1: filename or device name # $2: partition name local type if [ -b $2 ]; then echo "Partition 3 already exists on $1" exit 4 fi if [ X$fmt = Xexfat ]; then type=7 else type=83 fi # add 3th partion to the image or device { echo -e "n\np\n3\n\n\n"; sleep 2; echo -e "v\nt\n3\n$type\nw\n"; } | fdisk -w never $1 >/dev/null 2>&1 if [ "$?" -ne 0 ]; then echo "Cannot add 3th partition to $1. Error code $?" exit 3 fi udevadm settle --timeout=10 } make_fs() { if [ -b $1 ]; then if [ X$fmt = Xexfat ]; then mkfs.exfat -L MY-DATA $1 else mkfs.ext4 -q -L MY-DATA -J size=5 -E lazy_itable_init $1 fi echo "Data partition MY-DATA created" fi } mount_data() { mkdir $mntdir mount $1 $mntdir } umount_data() { umount $mntdir rmdir $mntdir } copy_data() { cp -a $* $mntdir echo "Copied files into the ISO." du --exclude=lost+found -sh $mntdir/* } usage() { cat </dev/null; then echo "Size $size needs a unit like K, M, G, T" exit 5 fi if [ -n "$1" ] && [ X$copy = X ]; then echo "If you want to copy directories, add -c" exit 3 fi if [ $(id -u) != "0" ]; then echo "Run this program as root." exit 2 fi if [ ! -b $filename ] && [ ! -f $filename ]; then echo "$filename is neither a device nor an existing file" exit 9 fi if [ -b $filename ]; then # file is a USB device if [ -n "$size" ]; then echo "Warning: Ignoring the size parameter for USB sticks." fi part=${filename}3 add_p3 $filename $part make_fs $part fi if [ -f $filename ]; then # check if the image already has a 3th partition set +e fdisk -l $filename | egrep -q ^${filename}3 if [ $? -eq 0 ]; then echo "Partition 3 already exists on $filename" exit 5 fi size=${size:-300M} echo "Extend $filename by $size" truncate -s +$size $filename if [ $? -ne 0 ]; then echo "Cannot extend $filename. Aborting" exit 9 fi loop=$(losetup -P -f --show $filename) if [ "$?" -ne 0 ]; then echo "Cannot create loop device" exit 5 fi trap "losetup -d $loop" EXIT part=${loop}p3 add_p3 $loop $part make_fs $part fi if [ X$copy = X1 ]; then mount_data $part copy_data $* umount_data $part fi