nmhw-auto-part: improved script.
Script now automatically aborts, when an error occours. Signed-off-by: Lucien Mueller <lucien.mueller@netmodule.com>
This commit is contained in:
parent
45e0d28fdf
commit
212421128a
|
|
@ -7,17 +7,18 @@
|
|||
# $3 = 'data' or 'overlay' type of partition to append
|
||||
# $4 = size of the appended partition in MB (set to 0 to fill)
|
||||
|
||||
red="\e[31m"
|
||||
|
||||
green="\e[32m"
|
||||
underlined="\e[4m"
|
||||
normal="\e[0m"
|
||||
bold="\e[1m"
|
||||
|
||||
device_available(){
|
||||
local device=$1
|
||||
if [ -e $device ] ; then
|
||||
local partdevice_first=$(ls $device*[1-9] | tail -n 1)
|
||||
local partn_first=${partdevice_first: -1}
|
||||
local partdevice_first
|
||||
partdevice_first=$(ls $device*[1-9] | tail -n 1)
|
||||
local partn_first
|
||||
partn_first=${partdevice_first: -1}
|
||||
if (( $partn_first > 1 )) ; then
|
||||
echo "Error: Device $device has already too many partitions."
|
||||
echo "Please use this script only once."
|
||||
|
|
@ -35,7 +36,8 @@ rootsize_ok(){
|
|||
local devicename=$1
|
||||
local partname=$2
|
||||
local newsize=$3
|
||||
local size=$(( $(cat /sys/block/$devicename/$partname/size) * $(cat /sys/block/$devicename/queue/hw_sector_size) / 1024 / 1024 ))
|
||||
local size
|
||||
size=$(( $(cat /sys/block/$devicename/$partname/size) * $(cat /sys/block/$devicename/queue/hw_sector_size) / 1024 / 1024 ))
|
||||
|
||||
if [ $newsize -eq 0 ] ; then
|
||||
return 0
|
||||
|
|
@ -79,29 +81,32 @@ expand_partition(){
|
|||
fi
|
||||
|
||||
# Get the partiton device e.g. /dev/mmcblk1p2
|
||||
local partdevice=$(ls $device*[$partn] | tail -n 1)
|
||||
local partdevice
|
||||
partdevice=$(ls $device*[$partn] | tail -n 1)
|
||||
|
||||
# get the name e.g. mmcblk1
|
||||
local devicename=$(echo $device | cut -d '/' -f 3)
|
||||
local devicename
|
||||
devicename=$(echo $device | cut -d '/' -f 3)
|
||||
|
||||
# get the name of the otaroot partition e.g. mmcblk1p2
|
||||
local partname=$(echo $partdevice | cut -d '/' -f 3)
|
||||
local partname
|
||||
partname=$(echo $partdevice | cut -d '/' -f 3)
|
||||
|
||||
# first sector
|
||||
local start=$(cat /sys/block/$devicename/$partname/start)
|
||||
local start
|
||||
start=$(cat /sys/block/$devicename/$partname/start)
|
||||
|
||||
# last sector
|
||||
local end=$(($start+$(cat /sys/block/$devicename/$partname/size)))
|
||||
local end
|
||||
end=$(($start+$(cat /sys/block/$devicename/$partname/size)))
|
||||
|
||||
# calculate new last sector
|
||||
local newend=$(($start + $newsize * 1024 * 1024 / $(cat /sys/block/$devicename/queue/hw_sector_size)))
|
||||
|
||||
# calculate current size
|
||||
local currentsize=$(( $(cat /sys/block/$devicename/$partname/size) / 1024 / 1024 * $(cat /sys/block/$devicename/queue/hw_sector_size) ))
|
||||
local newend
|
||||
newend=$(($start + $newsize * 1024 * 1024 / $(cat /sys/block/$devicename/queue/hw_sector_size)))
|
||||
|
||||
echo -e "d\n$partn\nn\np\n$partn\n$start\n$newend\nw\n" | fdisk $device > /dev/null 2>&1
|
||||
partprobe $device > /dev/null 2>&1
|
||||
resize2fs $partdevice > /dev/null 2>&1
|
||||
partprobe $device >> /var/log/nmhw-auto-part.log 2>&1 || true
|
||||
resize2fs $partdevice >> /var/log/nmhw-auto-part.log 2>&1
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -114,31 +119,40 @@ append_partition(){
|
|||
local size=$2
|
||||
|
||||
# Get the previous partiton device e.g. /dev/mmcblk1p2
|
||||
local partdevice_prev=$(ls $device*[1-9] | tail -n 1)
|
||||
local partn_prev=${partdevice_prev: -1}
|
||||
local partdevice_prev
|
||||
partdevice_prev=$(ls $device*[1-9] | tail -n 1)
|
||||
local partn_prev
|
||||
partn_prev=${partdevice_prev: -1}
|
||||
|
||||
if [ $partn_prev -eq 4 ] ; then
|
||||
echo "Error:You can only have a maximum of 4 partitions."
|
||||
return 1
|
||||
fi
|
||||
|
||||
local partn=$(($partn_prev + 1))
|
||||
local partn
|
||||
partn=$(($partn_prev + 1))
|
||||
|
||||
# get the name e.g. mmcblk1
|
||||
local devicename=$(echo $device | cut -d '/' -f 3)
|
||||
local devicename
|
||||
devicename=$(echo $device | cut -d '/' -f 3)
|
||||
|
||||
# get the name of the previous partition e.g. mmcblk1p2
|
||||
local partname_prev=$(echo $partdevice_prev | cut -d '/' -f 3)
|
||||
local partname_prev
|
||||
partname_prev=$(echo $partdevice_prev | cut -d '/' -f 3)
|
||||
|
||||
# first sector
|
||||
local start_prev=$(cat /sys/block/$devicename/$partname_prev/start)
|
||||
local start_prev
|
||||
start_prev=$(cat /sys/block/$devicename/$partname_prev/start)
|
||||
|
||||
# last sector of previous partition
|
||||
local end_prev=$(($start_prev+$(cat /sys/block/$devicename/$partname_prev/size)))
|
||||
local end_prev
|
||||
end_prev=$(($start_prev+$(cat /sys/block/$devicename/$partname_prev/size)))
|
||||
|
||||
local start=$(($end_prev + 1))
|
||||
local start
|
||||
start=$(($end_prev + 1))
|
||||
|
||||
local end_device=$(($(cat /sys/block/$devicename/size)-8))
|
||||
local end_device
|
||||
end_device=$(($(cat /sys/block/$devicename/size)-8))
|
||||
|
||||
if [ $size -eq 0 ] ; then
|
||||
local end=$end_device
|
||||
|
|
@ -150,11 +164,12 @@ append_partition(){
|
|||
fi
|
||||
|
||||
echo -e "n\np\n$partn\n$start\n$end\nw\n" | fdisk $device > /dev/null 2>&1
|
||||
partprobe $device > /dev/null 2>&1
|
||||
partprobe $device >> /var/log/nmhw-auto-part.log 2>&1 || true
|
||||
|
||||
# Get the partiton device e.g. /dev/mmcblk1p2
|
||||
local partdevice=$(ls $device*[$partn] | tail -n 1)
|
||||
mkfs.ext4 $partdevice > /dev/null 2>&1
|
||||
local partdevice
|
||||
partdevice=$(ls $device*[$partn] | tail -n 1)
|
||||
mkfs.ext4 $partdevice >> /var/log/nmhw-auto-part.log 2>&1
|
||||
|
||||
createdPartition=$partdevice
|
||||
}
|
||||
|
|
@ -166,15 +181,15 @@ interactive_mode(){
|
|||
printf "|$bold It's advised, that you use the recommended values in the$green [square brackets]$normal.\n"
|
||||
printf "|$bold Press enter to automatically apply the recommended values.$normal\n"
|
||||
|
||||
false
|
||||
while ! [ $? -eq 0 ]; do
|
||||
d=0
|
||||
while [ $d -eq 0 ]; do
|
||||
printf "Input device to expand $bold$green[/dev/mmcblk1]$normal:\n"
|
||||
read device
|
||||
if [ -z ${device} ] ; then
|
||||
device="/dev/mmcblk1"
|
||||
printf "Using $device\n"
|
||||
fi
|
||||
device_available $device
|
||||
device_available $device && d=1
|
||||
done
|
||||
|
||||
# number of the otaroot partiton
|
||||
|
|
@ -189,26 +204,26 @@ interactive_mode(){
|
|||
# get the name of the otaroot partition e.g. mmcblk1p2
|
||||
otarootpartname=$(echo $otarootpart | cut -d '/' -f 3)
|
||||
|
||||
false
|
||||
while ! [ $? -eq 0 ]; do
|
||||
d=0
|
||||
while [ $d -eq 0 ]; do
|
||||
printf "Input size of the otaroot in MB $bold$green[1024]$normal:\n"
|
||||
read newrootsize
|
||||
if [ -z ${newrootsize} ] ; then
|
||||
newrootsize="1024"
|
||||
printf "Using $newrootsize\n"
|
||||
fi
|
||||
rootsize_ok $devicename $otarootpartname $newrootsize
|
||||
rootsize_ok $devicename $otarootpartname $newrootsize && d=1
|
||||
done
|
||||
|
||||
false
|
||||
while ! [ $? -eq 0 ] ; do
|
||||
d=0
|
||||
while [ $d -eq 0 ] ; do
|
||||
printf "Input type of partition to append ('data' or 'overlay') $bold$green[overlay]$normal:\n"
|
||||
read ptype
|
||||
if [ -z ${ptype} ] ; then
|
||||
ptype="overlay"
|
||||
printf "Using $ptype\n"
|
||||
fi
|
||||
type_available $ptype
|
||||
type_available $ptype && d=1
|
||||
done
|
||||
|
||||
printf "Size of the appended partition in MB (set to 0 to fill) $bold$green[0]$normal:\n"
|
||||
|
|
@ -260,24 +275,26 @@ execute(){
|
|||
append_partition $device $datasize
|
||||
datapart=$createdPartition
|
||||
|
||||
mkdir -p /data
|
||||
mount $datapart /mnt
|
||||
echo "$datapart /mnt auto defaults,sync 0 0" >> /etc/fstab
|
||||
|
||||
case "$ptype" in
|
||||
"data")
|
||||
mkdir /mnt/data
|
||||
mkdir -p /mnt/data
|
||||
;;
|
||||
"overlay")
|
||||
mkdir -p /mnt/.work
|
||||
mkdir -p /mnt/overlay
|
||||
systemctl enable mount-overlay.service > /dev/null 2>&1
|
||||
systemctl enable mount-overlay.service >> /var/log/nmhw-auto-part.log 2>&1
|
||||
systemctl start mount-overlay.service
|
||||
;;
|
||||
*)
|
||||
echo "Error: $ptype is not an option."
|
||||
;;
|
||||
esac
|
||||
|
||||
echo "Partitioning successful."
|
||||
echo "Partitioning successful." >> /var/log/nmhw-auto-part.log
|
||||
}
|
||||
|
||||
usage(){
|
||||
|
|
@ -298,6 +315,14 @@ description(){
|
|||
-h help\n"
|
||||
}
|
||||
|
||||
# exit when any commands fails
|
||||
set -e
|
||||
|
||||
echo "Script started $(date)." >> /var/log/nmhw-auto-part.log
|
||||
echo "Script run with parameters 1: $1 2: $2 3: $3 4: $4 5: $5" >> /var/log/nmhw-auto-part.log
|
||||
echo "" >> /var/log/nmhw-auto-part.log
|
||||
echo "" >> /var/log/nmhw-auto-part.log
|
||||
|
||||
if (( $# == 1 )) ; then
|
||||
case $1 in
|
||||
"-i")
|
||||
|
|
|
|||
Loading…
Reference in New Issue