nmhw-auto-part: improved nmhw-auto-part tool help and verbosity.

BugzID: 54511

Signed-off-by: Lucien Mueller <lucien.mueller@netmodule.com>
This commit is contained in:
Lucien Mueller 2018-12-05 14:31:23 +01:00
parent 6cca5429e5
commit 4f2ee73047
1 changed files with 43 additions and 34 deletions

View File

@ -1,12 +1,5 @@
#!/bin/bash #!/bin/bash
# This scripts expands the first partition on a block device and appends an additional partition to the device.
# This scripts expands 2. (otaroot) partition on a blockdevice and appends an additional partition to the device.
# Arguments:
# $1 = device to use: e.g. /dev/sdb /dev/mmcblk1
# $2 = size of the otaroot in MB
# $3 = 'data' or 'overlay' type of partition to append
# $4 = size of the appended partition in MB (set to 0 to fill)
green="\e[32m" green="\e[32m"
normal="\e[0m" normal="\e[0m"
@ -20,14 +13,14 @@ device_available(){
local partn_first local partn_first
partn_first=${partdevice_first: -1} partn_first=${partdevice_first: -1}
if (( $partn_first > 1 )) ; then if (( $partn_first > 1 )) ; then
echo "Error: Device $device has already too many partitions." echo "Device $device has already too many partitions."
echo "Please use this script only once." echo "Please use this script only once."
exit 1 exit 1
else else
return 0 return 0
fi fi
else else
echo "Error: Device $device not available" echo "Device $device not available."
return 1 return 1
fi fi
} }
@ -183,11 +176,11 @@ interactive_mode(){
d=0 d=0
while [ $d -eq 0 ]; do while [ $d -eq 0 ]; do
printf "Input device to expand $bold$green[/dev/mmcblk1]$normal:\n" printf "Enter device to expand $bold$green[/dev/mmcblk1]$normal:\n"
read device read device
if [ -z ${device} ] ; then if [ -z ${device} ] ; then
device="/dev/mmcblk1" device="/dev/mmcblk1"
printf "Using $device\n" printf "Using $device\n\n"
fi fi
device_available $device && d=1 device_available $device && d=1
done done
@ -206,31 +199,31 @@ interactive_mode(){
d=0 d=0
while [ $d -eq 0 ]; do while [ $d -eq 0 ]; do
printf "Input size of the otaroot in MB $bold$green[1024]$normal:\n" printf "Enter size of the first partition in MB. 0 = keep current size. $bold$green[1024]$normal:\n"
read newrootsize read newrootsize
if [ -z ${newrootsize} ] ; then if [ -z ${newrootsize} ] ; then
newrootsize="1024" newrootsize="1024"
printf "Using $newrootsize\n" printf "Using $newrootsize\n\n"
fi fi
rootsize_ok $devicename $otarootpartname $newrootsize && d=1 rootsize_ok $devicename $otarootpartname $newrootsize && d=1
done done
d=0 d=0
while [ $d -eq 0 ] ; do while [ $d -eq 0 ] ; do
printf "Input type of partition to append ('data' or 'overlay') $bold$green[overlay]$normal:\n" printf "Enter type of partition to append ('regular' or 'overlay') $bold$green[overlay]$normal:\n"
read ptype read ptype
if [ -z ${ptype} ] ; then if [ -z ${ptype} ] ; then
ptype="overlay" ptype="overlay"
printf "Using $ptype\n" printf "Using $ptype\n\n"
fi fi
type_available $ptype && d=1 type_available $ptype && d=1
done done
printf "Size of the appended partition in MB (set to 0 to fill) $bold$green[0]$normal:\n" printf "Enter size of the partition to append in MB. 0 = use remaining space. $bold$green[0]$normal:\n"
read datasize read datasize
if [ -z ${datasize} ] ; then if [ -z ${datasize} ] ; then
datasize="0" datasize="0"
printf "Using $datasize\n" printf "Using $datasize\n\n"
fi fi
execute execute
} }
@ -243,6 +236,7 @@ scripted_mode(){
datasize=$4 datasize=$4
if ! device_available $device ; then if ! device_available $device ; then
printf "Device $device "
exit 1 exit 1
fi fi
@ -270,23 +264,35 @@ scripted_mode(){
} }
execute(){ execute(){
printf "Expanding partition $otarootpartn of device $device. "
expand_partition $device $otarootpartn $newrootsize expand_partition $device $otarootpartn $newrootsize
printf "Done.\n\n"
printf "Appending partition with size $datasize on device $device. "
append_partition $device $datasize append_partition $device $datasize
printf "Done.\n\n"
datapart=$createdPartition datapart=$createdPartition
printf "Mounting new partition to /mnt. "
mount $datapart /mnt mount $datapart /mnt
printf "Done.\n\n"
printf "Adding new partition to /etc/fstab. "
echo "$datapart /mnt auto defaults,sync 0 0" >> /etc/fstab echo "$datapart /mnt auto defaults,sync 0 0" >> /etc/fstab
printf "Done.\n\n"
case "$ptype" in case "$ptype" in
"data")
mkdir -p /mnt/data || true
;;
"overlay") "overlay")
mkdir -p /mnt/.work mkdir -p /mnt/.work
printf "Creating overlay folder in /mnt. "
mkdir -p /mnt/overlay mkdir -p /mnt/overlay
printf "Done.\n\n"
printf "Enabling mount-overlay service. "
systemctl enable mount-overlay.service >> /tmp/log/nmhw-auto-part.log 2>&1 systemctl enable mount-overlay.service >> /tmp/log/nmhw-auto-part.log 2>&1
systemctl start mount-overlay.service systemctl start mount-overlay.service
printf "Done.\n\n"
;; ;;
*) *)
echo "Error: $ptype is not an option." echo "Error: $ptype is not an option."
@ -298,21 +304,20 @@ execute(){
} }
usage(){ usage(){
printf "\ printf "Usage: nmhw-auto-part [-h|-i] \n"
usage: nmhw-auto-part.sh [OPTION] 'devicename' 'size' 'mode' 'size' \n\n" printf " nmhw-auto-part disk size1 mode size2 \n"
} }
description(){ description(){
printf "\ printf "\
This scripts expands 1. partition on a blockdevice and appends an additional partition to the device. \n\ This scripts expands the first partition on a block device and appends an additional partition to the device. \n\
Arguments: \n\ Arguments: \n\
1. 'devicename' = device to use: e.g. /dev/sdb /dev/mmcblk1 \n\ disk : disk to use: e.g. /dev/mmcblk1 \n\
2. 'size' = size of the first partition in MB (set 0 to keep current size) \n\ size1 : size of the first partition in MB. 0 = keep current size. \n\
3. 'mode' = 'data' or 'overlay'; type of partition to append \n\ mode : regular or overlay; type of partition to append \n\
4. 'size' = size of the appended partition in MB (set to 0 to fill) \n\ size2 : size of the partition to append in MB. 0 = use remaining space. \n\
Options:\n\ -i : interactive mode\n\
-i interactive mode\n\ -h : help\n"
-h help\n"
} }
# exit when any commands fails # exit when any commands fails
@ -324,7 +329,11 @@ echo "Script run with parameters 1: $1 2: $2 3: $3 4: $4 5: $5" >> /tmp/log/nmhw
echo "" >> /tmp/log/nmhw-auto-part.log echo "" >> /tmp/log/nmhw-auto-part.log
echo "" >> /tmp/log/nmhw-auto-part.log echo "" >> /tmp/log/nmhw-auto-part.log
if (( $# == 1 )) ; then if (( $# == 0 )) ; then
usage
description
exit 0
elif (( $# == 1 )) ; then
case $1 in case $1 in
"-i") "-i")
interactive_mode interactive_mode
@ -340,7 +349,7 @@ elif (( $# == 4 )) ; then
scripted_mode $1 $2 $3 $4 scripted_mode $1 $2 $3 $4
exit 0 exit 0
else else
echo "Error: Illegal number of parameters." echo "${0##*/} : Illegal number of parameters."
usage usage
description description
exit 1 exit 1