[patch] Add partition support to usb_inst.sh

Post there if you plan to contribute to this SystemCd
Post Reply
JoeKar
Posts: 1
Joined: 30 Dec 2013, 11:40

[patch] Add partition support to usb_inst.sh

Post by JoeKar » 30 Dec 2013, 11:59

Hi,

because it was annoying to me too, that usb_inst.sh will destroy the whole content of my USB device, I've adapted the included script to support selective partition formatting as well.
Maybe the script will find his way into the next release ;). If not, then it could help some one else, who needs this feature.

Code: Select all

--- usb_inst.sh
+++ usb_inst.sh
@@ -5,12 +5,13 @@

 ###############################################################################

-logfile="/var/tmp/usb_inst.log"
-TMPDIR="/var/tmp/usb_inst.tmp"
+tmp="/tmp"
+logfile="${tmp}/usb_inst.log"
+TMPDIR="${tmp}/usb_inst.tmp"
 MINSIZEMB=512
 PROGIMG="${0}"
 PROGLOC="$(dirname ${0})"
-CDFILES=('sysrcd.dat' 'sysrcd.md5' 'version' '???linux/initram.igz'
+CDFILES=('sysrcd.dat' 'sysrcd.md5' 'version' '???linux/initram.igz'
 	'???linux/rescue32' '???linux/rescue64' '???linux/f1boot.msg'
 	'???linux/???linux.bin' '???linux/???linux.cfg')

@@ -86,7 +87,7 @@
 	then
 		die "${devname} is not a valid device name (1)"
 	fi
-
+
 	partcnt=0
 	firstpart=0
 	for i in $(seq 1 4)
@@ -98,13 +99,37 @@
 			partcnt=$((partcnt+1))
 		fi
 	done
-
+
 	if [ "${partcnt}" = '1' ]
 	then
 		return ${partcnt}
 	else
 		return 0
 	fi
+}
+
+# count partitions at the valid block-device $1
+count_partitions()
+{
+    devname="$1"
+    if [ -z "${devname}" ] || [ ! -d "/sys/block/$(basename ${devname})" ]
+    then
+        die "${devname} is not a valid device name (1)"
+    fi
+
+    partcnt=0
+    firstpart=0
+    for i in $(seq 1 4)
+    do
+        partname="${devname}${i}"
+        if [ -b "${partname}" ]
+        then
+            [ "${firstpart}" = '0' ] && firstpart="$i"
+            partcnt=$((partcnt+1))
+        fi
+    done
+
+    return ${partcnt}
 }

 # check $1 is a valid partition name
@@ -119,7 +144,7 @@
 	then
 		die "${partname} is not a valid partition name"
 	fi
-
+
 	if ! echo "${partname}" | grep -qE '^/dev/[a-z]*[1-4]+$'
 	then
 		die "device [${partname}] is not a valid partition. Expect something like [/dev/sdf1]"
@@ -129,7 +154,7 @@
 	then
 		die "${partname} is already mounted, cannot continue"
 	fi
-
+
 	return 0
 }

@@ -140,17 +165,17 @@
 	then
 		die "you have to provide a valid device name as argument of this command"
 	fi
-
+
 	if [ ! -b "${devname}" ] || [ ! -d "/sys/block/$(basename ${devname})" ]
 	then
 		die "${devname} is not a valid device name (2)"
 	fi
-
+
 	if is_dev_mounted "${devname}"
 	then
 		die "${devname} is already mounted, cannot continue"
 	fi
-
+
 	return 0
 }

@@ -172,9 +197,9 @@
 # returns 0 if the device is big enough
 check_sizeof_dev()
 {
-	devname="$1"
-
-	if [ -z "${devname}" ]
+	fulldevname="$1"
+
+	if [ -z "${fulldevname}" ]
 	then
 		die "check_sizeof_dev(): devname is empty"
 	fi
@@ -184,14 +209,14 @@
 		echo "blockdev not found, assuming the size is ok"
 		return 0
 	fi
-
-	secsizeofdev="$(blockdev --getsz ${devname})"
+
+	secsizeofdev="$(blockdev --getsz ${fulldevname})"
 	mbsizeofdev="$((secsizeofdev/2048))"
 	if [ "${mbsizeofdev}" -lt "${MINSIZEMB}" ]
 	then
-		die "The device [${devname}] is only ${mbsizeofdev} MB. It is too small to copy all the files, an USB-stick of at least ${MINSIZEMB}MB is recommended"
-	else
-		echo "The device [${devname}] seems to be big enough: ${mbsizeofdev} MB."
+		die "The device [${fulldevname}] is only ${mbsizeofdev} MB. It is too small to copy all the files, an USB-stick of at least ${MINSIZEMB}MB is recommended"
+	else
+		echo "The device [${fulldevname}] seems to be big enough: ${mbsizeofdev} MB."
 		return 0
 	fi
 }
@@ -215,7 +240,7 @@
 is_dev_usb_stick()
 {
 	curdev="$1"
-
+
 	remfile="/sys/block/${curdev}/removable"
 	vendor="$(cat /sys/block/${curdev}/device/vendor 2>/dev/null)"
 	model="$(cat /sys/block/${curdev}/device/model 2>/dev/null)"
@@ -231,48 +256,63 @@
 do_writembr()
 {
 	devname="$1"
+	bootable="$2"
+	partcnt="$3"
 	shortname="$(echo ${devname} | sed -e 's!/dev/!!g')"
-
+
 	check_valid_blkdevname "${devname}"
 	if ! is_dev_usb_stick "${shortname}"
 	then
 		die "Device [${devname}] does not seem to be an usb-stick. Cannot continue."
 	fi
-
-	check_sizeof_dev "${devname}"
-
+
+	check_sizeof_dev "${devname}${bootable}"
+
 	if [ ! -x "${PROG_INSTMBR}" ] || [ ! -x "${PROG_PARTED}" ]
 	then
 		die "install-mbr and parted must be installed, check these programs first."
 	fi
-
+
 	cmd="${PROG_INSTMBR} ${devname} --force"
 	echo "--> ${cmd}"
 	if ! ${cmd}
 	then
 		die "${cmd} --> failed"
 	fi
-
-	cmd="${PROG_PARTED} -s ${devname} mklabel msdos"
+
+	if [ ${partcnt} -le 1 ]
+    then
+        cmd="${PROG_PARTED} -s ${devname} mklabel msdos"
+        echo "--> ${cmd}"
+        if ! ${cmd} 2>/dev/null
+        then
+            die "${cmd} --> failed"
+        fi
+
+        cmd="${PROG_PARTED} -s ${devname}${bootable} mkpart primary fat32 0 100%"
+        echo "--> ${cmd}"
+        if ! ${cmd} 2>/dev/null
+        then
+            die "${cmd} --> failed"
+        fi
+    fi
+
+    for i in $(seq 1 ${partcnt})
+    do
+        cmd="${PROG_PARTED} -s ${devname} set ${i} boot off"
+        echo "--> ${cmd}"
+        if ! ${cmd} 2>/dev/null
+        then
+            die "${cmd} --> failed"
+        fi
+    done
+
+	cmd="${PROG_PARTED} -s ${devname} set ${bootable} boot on"
 	echo "--> ${cmd}"
 	if ! ${cmd} 2>/dev/null
 	then
 		die "${cmd} --> failed"
 	fi
-
-	cmd="${PROG_PARTED} -s ${devname} mkpart primary fat32 0 100%"
-	echo "--> ${cmd}"
-	if ! ${cmd} 2>/dev/null
-	then
-		die "${cmd} --> failed"
-	fi
-
-	cmd="${PROG_PARTED} -s ${devname} set 1 boot on"
-	echo "--> ${cmd}"
-	if ! ${cmd} 2>/dev/null
-	then
-		die "${cmd} --> failed"
-	fi
 }

 do_format()
@@ -286,7 +326,7 @@
 	then
 		die "mkfs.vfat not found on your system, please install dosfstools first."
 	fi
-
+
 	if ${PROG_MKVFATFS} -F 32 -n SYSRESC ${partname}
 	then
 		echo "Partition ${partname} has been successfully formatted"
@@ -301,38 +341,38 @@
 {
 	partname="$1"
 	check_valid_partname "${partname}"
-
+
 	# check the important files are available in ${LOCATION}
 	check_sysresccd_files "${LOCATION}"
-
+
 	check_sizeof_dev "${partname}"
-
+
 	mkdir -p /mnt/usbstick 2>/dev/null
 	if ! mount -t vfat ${partname} /mnt/usbstick
 	then
 		die "cannot mount ${partname} on /mnt/usbstick"
 	fi
 	echo "${partname} successfully mounted on /mnt/usbstick"
-
+
 	check_disk_freespace "/mnt/usbstick"
-
-	echo "cp -v -r --remove-destination ${LOCATION}/* /mnt/usbstick/"
+
+	echo "cp -v -r --remove-destination ${LOCATION}/* /mnt/usbstick/"
 	if cp -v -r --remove-destination ${LOCATION}/* /mnt/usbstick/ && sync
 	then
 		echo "Files have been successfully copied to ${partname}"
 	else
 		echo "Cannot copy files to ${partname}"
 	fi
-
+
 	if ! ls -l /mnt/usbstick/???linux/???linux.cfg >/dev/null 2>&1
 	then
 		umount /mnt/usbstick
 		die "isolinux/syslinux configuration file not found, cannot continue"
 	fi
-
+
 	# check the important files have been copied
 	check_sysresccd_files "/mnt/usbstick"
-
+
 	# move isolinux files to syslinux files
 	if [ -f /mnt/usbstick/isolinux/isolinux.cfg ]
 	then
@@ -345,10 +385,10 @@
 		fi
 		sed -i -e 's!/isolinux/!/syslinux/!g' /mnt/usbstick/boot/grub/grub*.cfg
 	fi
-
+
 	# add scandelay option which allows the usb devices to be detected
 	sed -i -e 's!scandelay=.!scandelay=5!g' /mnt/usbstick/syslinux/syslinux.cfg
-
+
 	umount /mnt/usbstick
 }

@@ -356,12 +396,12 @@
 {
 	partname="$1"
 	check_valid_partname "${partname}"
-
+
 	if [ ! -x "${PROG_SYSLINUX}" ]
 	then
 		die "syslinux not found on your system, please install syslinux first."
 	fi
-
+
 	${PROG_SYSLINUX} --install --directory syslinux ${partname}
 	res=$?
 	sync
@@ -377,7 +417,7 @@
 is_dev_mounted()
 {
 	curdev="$1"
-
+
 	if cat /proc/mounts | grep -q "^${curdev}"
 	then
 		return 0
@@ -425,7 +465,7 @@
 	if [ ${devsallcnt} -eq 0 ]
 	then
 		echo "No valid USB/Removable device has been detected on your system"
-		return 1
+		return 1
 	fi
 	if [ ${devsokcnt} -eq 0 ]
 	then
@@ -457,14 +497,37 @@
 			status="${status}Installation on ${devname2} in progress\n\n"
 			status="${status}details will be written in ${logfile}\n"
 			dialog_status "${status}"
-			status="${status}* Writing MBR on ${devname2}\n"
+			sleep 1
+			count_partitions ${devname2}
+			partcnt=$?
+			if [ ${partcnt} -gt 1 ]
+            then
+                for i in $(seq 1 ${partcnt})
+                do
+                    parttxt="${parttxt} \"${i}\" \"${devname2}${i}\" off"
+                done
+                message="The selected device contains more than 1 partition.\n"
+                message="${message}Please select one of the found partitions to proceed with the installation:\n"
+
+                pselection="/tmp/usb_inst_partsel.tmp"
+                [ ! -d /tmp ] && mkdir -p /tmp
+                [ -f ${pselection} ] && rm -f ${pselection}
+                selection='${PROG_DIALOG} --backtitle "Select desired partition" --checklist "${message}" 20 70 4'
+                eval "${selection} ${parttxt}" 2>$pselection
+                if [ -s $pselection ]
+                then
+                    partsel=$(cat ${pselection} | tr -d \" | sort)
+                fi
+                rm -f $pselection
+            else
+                partsel=${partcnt}
+            fi
+            status="${status}* Writing MBR on ${devname2} with bootable ${partsel}. partition...\n"
+            dialog_status "${status}"
+            do_writembr ${devname2} ${partsel} ${partcnt} >> ${logfile} 2>&1
+            [ $? -ne 0 ] && dialog_die "Failed to write the MBR on ${devname2}"
 			dialog_status "${status}"
-			do_writembr ${devname2} >> ${logfile} 2>&1
-			[ $? -ne 0 ] && dialog_die "Failed to write the MBR on ${devname2}"
-			sleep 1
-			output="$(find_first_partition ${devname2})\n"
-			devname2="${devname2}$?"
-			dialog_status "${status}"
+			devname2="${devname2}${partsel}"
 			sleep 5
 			status="${status}* Creating filesystem on ${devname2}...\n"
 			dialog_status "${status}"
@@ -514,7 +577,7 @@
 				secsizeofdev="$(blockdev --getsz /dev/${curdev})"
 				mbsizeofdev="$((secsizeofdev/2048))"
 				sizemsg=" and size=${mbsizeofdev}MB"
-			fi
+			fi
 			echo "Device [${devname}] detected as [${vendor} ${model}] is removable${sizemsg}"
 			if is_dev_mounted "${devname}"
 			then

Post Reply