Fix multiple issues

- part_auto error on devices without existing partitions
- offer gparted if it's installed and running in a graphical environment
- errshow properly shift off fatal level arg and cleanup
This commit is contained in:
natemaia 2019-11-02 10:50:52 -07:00
parent 184a5aa38a
commit 071e2fe55d

View File

@ -8,7 +8,7 @@
# check for syntax errors # check for syntax errors
# set -n # set -n
VER=2.1.3 VER=2.1.4
# default values { # default values {
@ -576,7 +576,7 @@ part_menu()
dlg choice menu "Edit Partitions" "$_part\n\n$(lsblk -no NAME,MODEL,SIZE,TYPE,FSTYPE $device)" \ dlg choice menu "Edit Partitions" "$_part\n\n$(lsblk -no NAME,MODEL,SIZE,TYPE,FSTYPE $device)" \
"auto" "Whole device automatic partitioning" \ "auto" "Whole device automatic partitioning" \
"cfdisk" "Curses based variant of fdisk" \ "cfdisk" "Curses based variant of fdisk" \
"parted" "GNU partition editor" \ "parted" "GNU partition editor" $([[ $DISPLAY ]] && hash gparted >/dev/null 2>&1 && printf "gparted 'A gui front end to parted'") \
"fdisk" "Dialog-driven creation and manipulation of partitions" \ "fdisk" "Dialog-driven creation and manipulation of partitions" \
"done" "Return to the main menu" "done" "Return to the main menu"
# "shrink" "Shrink an existing ext or ntfs partition" \ # "shrink" "Shrink an existing ext or ntfs partition" \
@ -586,18 +586,18 @@ part_menu()
elif [[ $choice == 'shrink' ]]; then elif [[ $choice == 'shrink' ]]; then
part_shrink "$device" part_shrink "$device"
elif [[ $choice == 'auto' ]]; then elif [[ $choice == 'auto' ]]; then
local root_size txt table boot_fs local root_size txt label boot_fs
root_size=$(lsblk -lno SIZE "$device" | awk 'NR == 1 {if ($1 ~ "G") {sub(/G/, ""); print ($1 * 1000 - 512) / 1000 "G"} else {sub(/M/, ""); print ($1 - 512) "M"}}') root_size=$(lsblk -lno SIZE "$device" | awk 'NR == 1 {if ($1 ~ "G") {sub(/G/, ""); print ($1 * 1000 - 512) / 1000 "G"} else {sub(/M/, ""); print ($1 - 512) "M"}}')
txt="\nWARNING:\n\nALL data on $device will be destroyed and the following partitions will be created\n\n- " txt="\nWARNING:\n\nALL data on $device will be destroyed and the following partitions will be created\n\n- "
if [[ $SYS == 'BIOS' ]]; then if [[ $SYS == 'BIOS' ]]; then
table="msdos" boot_fs="ext4" label="msdos" boot_fs="ext4" boot_type="primary"
txt+="An $boot_fs boot partition with the boot flag enabled (512M)\n- " txt+="An $boot_fs boot partition with the boot flag enabled (512M)\n- "
else else
table="gpt" boot_fs="fat32" label="gpt" boot_fs="fat32" boot_type="ESP"
txt+="A $boot_fs efi boot partition (512M)\n- " txt+="A $boot_fs efi boot partition (512M)\n- "
fi fi
txt+="An ext4 partition using all remaining space ($root_size)\n\nDo you want to continue?\n" txt+="An ext4 partition using all remaining space ($root_size)\n\nDo you want to continue?\n"
yesno "Auto Partition" "$txt" && part_auto "$device" "$table" "$boot_fs" "$root_size" yesno "Auto Partition" "$txt" && part_auto "$device" "$label" "$boot_fs" "$root_size"
else else
clear clear
tput cnorm tput cnorm
@ -624,28 +624,24 @@ part_show()
part_auto() part_auto()
{ {
local device="$1" table="$2" boot_fs="$3" size="$4" dev_info="" local device="$1" label="$2" boot_fs="$3" size="$4" boot_type="$5" dev_info=""
msg "Auto Partition" "\nRemoving partitions on $device and setting table to $table\n" 1 msg "Auto Partition" "\nRemoving partitions on $device and setting label to $label\n" 1
dev_info="$(parted -s "$device" print 2> /dev/null)" dev_info="$(parted -s "$device" print 2> /dev/null)"
swapoff -a swapoff -a
while read -r PART; do while read -r PART; do
[[ $PART ]] || continue
parted -s "$device" rm "$PART" > /dev/null 2> "$ERR" parted -s "$device" rm "$PART" > /dev/null 2> "$ERR"
errshow 0 "parted -s $device rm $PART" || return 1 errshow 0 "parted -s $device rm $PART" || return 1
done <<< "$(awk '/^ [1-9][0-9]?/ {print $1}' <<< "$dev_info" | sort -r)" done <<< "$(awk '/^ [1-9][0-9]?/ {print $1}' <<< "$dev_info" | sort -r)"
[[ $(awk '/Table:/ {print $3}' <<< "$dev_info") != "$table" ]] && parted -s "$device" mklabel "$table" > /dev/null 2> "$ERR" [[ $(awk '/Table:/ {print $3}' <<< "$dev_info") != "$label" ]] && parted -s "$device" mklabel "$label" > /dev/null 2> "$ERR"
msg "Auto Partition" "\nCreating a 512M $boot_fs boot partition.\n" 1 msg "Auto Partition" "\nCreating a 512M $boot_fs boot partition.\n" 1
if [[ $SYS == "BIOS" ]]; then parted -s "$device" mkpart "$boot_type" "$boot_fs" 1MiB 513MiB > /dev/null 2> "$ERR"
parted -s "$device" mkpart primary "$boot_fs" 1MiB 513MiB > /dev/null 2> "$ERR" errshow 0 "parted -s $device mkpart $boot_type $boot_fs 1MiB 513MiB" || return 1
errshow 0 "parted -s $device mkpart primary $boot_fs 1MiB 513MiB" || return 1
else
parted -s "$device" mkpart ESP "$boot_fs" 1MiB 513MiB > /dev/null 2> "$ERR"
errshow 0 "parted -s $device mkpart ESP $boot_fs 1MiB 513MiB" || return 1
fi
sleep 0.5 sleep 0.5
BOOT_DEV="$device" BOOT_DEV="$device"
@ -703,7 +699,7 @@ part_shrink()
ntfs) ntfs)
if ntfsresize -fc "$part"; then if ntfsresize -fc "$part"; then
ntfsresize -ff --size $(( (size * 1024) / 1000 ))k "$part" 2> "$ERR" # k=10^3 bytes ntfsresize -ff --size $(( (size * 1024) / 1000 ))k "$part" 2> "$ERR" # k=10^3 bytes
errshow "ntfsresize -f -s $(( (size * 1024) / 1000 ))k $part" || return 1 errshow 0 "ntfsresize -f -s $(( (size * 1024) / 1000 ))k $part" || return 1
else else
msg "Resize" "\nThe ntfs partition $part cannot be resized because it is scheduled for a consistency check.\n\nTo do a consistency check in windows open command prompt as admin and run:\n\n\tchkdsk /f /r /x\n" msg "Resize" "\nThe ntfs partition $part cannot be resized because it is scheduled for a consistency check.\n\nTo do a consistency check in windows open command prompt as admin and run:\n\n\tchkdsk /f /r /x\n"
return 1 return 1
@ -712,7 +708,7 @@ part_shrink()
*) *)
e2fsck -f "$part"; sleep 0.5 e2fsck -f "$part"; sleep 0.5
resize2fs -f "$part" ${size}K 2> "$ERR" # K=2^10 bytes resize2fs -f "$part" ${size}K 2> "$ERR" # K=2^10 bytes
errshow "resize2fs -f $part ${size}K" || return 1 errshow 0 "resize2fs -f $part ${size}K" || return 1
;; ;;
esac esac
sleep 0.5 sleep 0.5
@ -721,10 +717,10 @@ part_shrink()
sleep 0.5 sleep 0.5
if [[ $devsize == "$end" ]]; then if [[ $devsize == "$end" ]]; then
parted -s "$device" mkpart primary ext4 ${size}KiB 100% 2> "$ERR" parted -s "$device" mkpart primary ext4 ${size}KiB 100% 2> "$ERR"
errshow "parted -s $device mkpart primary ext4 ${size}KiB 100%" || return 1 errshow 0 "parted -s $device mkpart primary ext4 ${size}KiB 100%" || return 1
else else
parted -s "$device" mkpart primary ext4 ${size}KiB ${end}KiB 2> "$ERR" parted -s "$device" mkpart primary ext4 ${size}KiB ${end}KiB 2> "$ERR"
errshow "parted -s $device mkpart primary ext4 ${size}KiB ${end}KiB" || return 1 errshow 0 "parted -s $device mkpart primary ext4 ${size}KiB ${end}KiB" || return 1
fi fi
msg "Resize Complete" "\n$part has been successfully resized to $((size / 1024))M.\n" 1 msg "Resize Complete" "\n$part has been successfully resized to $((size / 1024))M.\n" 1
;; ;;
@ -779,14 +775,14 @@ part_swap()
{ {
if [[ $1 == "$MNT/swapfile" && $SWAP_SIZE ]]; then if [[ $1 == "$MNT/swapfile" && $SWAP_SIZE ]]; then
fallocate -l $SWAP_SIZE "$1" 2> "$ERR" fallocate -l $SWAP_SIZE "$1" 2> "$ERR"
errshow "fallocate -l $SWAP_SIZE $1" errshow 0 "fallocate -l $SWAP_SIZE $1"
chmod 600 "$1" 2> "$ERR" chmod 600 "$1" 2> "$ERR"
errshow "chmod 600 $1" errshow 0 "chmod 600 $1"
fi fi
mkswap "$1" > /dev/null 2> "$ERR" mkswap "$1" > /dev/null 2> "$ERR"
errshow "mkswap $1" errshow 0 "mkswap $1"
swapon "$1" > /dev/null 2> "$ERR" swapon "$1" > /dev/null 2> "$ERR"
errshow "swapon $1" errshow 0 "swapon $1"
return 0 return 0
} }
@ -814,7 +810,7 @@ part_format()
msg "Format" "\nFormatting $part as $fs\n" 0 msg "Format" "\nFormatting $part as $fs\n" 0
mkfs.$fs ${FS_CMD_FLAGS[$fs]} "$part" > /dev/null 2> "$ERR" mkfs.$fs ${FS_CMD_FLAGS[$fs]} "$part" > /dev/null 2> "$ERR"
errshow "mkfs.$fs ${FS_CMD_FLAGS[$fs]} "$part"" || return 1 errshow 0 "mkfs.$fs ${FS_CMD_FLAGS[$fs]} "$part"" || return 1
FORMATTED+="$part " FORMATTED+="$part "
sleep $delay sleep $delay
} }
@ -1893,7 +1889,7 @@ lvm_detect()
if [[ $(lvs -o vg_name,lv_name --noheading --separator - 2> /dev/null) && $(pvs -o pv_name --noheading 2> /dev/null) ]]; then if [[ $(lvs -o vg_name,lv_name --noheading --separator - 2> /dev/null) && $(pvs -o pv_name --noheading 2> /dev/null) ]]; then
msg "LVM Setup" "\nActivating existing logical volume management.\n" 0 msg "LVM Setup" "\nActivating existing logical volume management.\n" 0
modprobe dm-mod > /dev/null 2> "$ERR" modprobe dm-mod > /dev/null 2> "$ERR"
errshow 'modprobe dm-mod' errshow 0 'modprobe dm-mod'
vgscan > /dev/null 2>&1 vgscan > /dev/null 2>&1
vgchange -ay > /dev/null 2>&1 vgchange -ay > /dev/null 2>&1
fi fi
@ -1912,7 +1908,7 @@ lvm_create()
lvm_volume_name "$_lvmlvname\nNOTE: This LV will use up all remaining space in the volume group (${VGROUP_MB}MB)" || return 1 lvm_volume_name "$_lvmlvname\nNOTE: This LV will use up all remaining space in the volume group (${VGROUP_MB}MB)" || return 1
msg "$_lvmnew (LV:$VOL_COUNT)" "\nCreating volume $VNAME from remaining space in $VGROUP\n" 0 msg "$_lvmnew (LV:$VOL_COUNT)" "\nCreating volume $VNAME from remaining space in $VGROUP\n" 0
lvcreate -l +100%FREE "$VGROUP" -n "$VNAME" > /dev/null 2> "$ERR" lvcreate -l +100%FREE "$VGROUP" -n "$VNAME" > /dev/null 2> "$ERR"
errshow "lvcreate -l +100%FREE $VGROUP -n $VNAME" || return 1 errshow 0 "lvcreate -l +100%FREE $VGROUP -n $VNAME" || return 1
LVM='logical volume'; sleep 0.5 LVM='logical volume'; sleep 0.5
txt="\nDone, volume: $VGROUP-$VNAME (${VOLUME_SIZE:-${VGROUP_MB}MB}) has been created.\n" txt="\nDone, volume: $VGROUP-$VNAME (${VOLUME_SIZE:-${VGROUP_MB}MB}) has been created.\n"
msg "$_lvmnew (LV:$VOL_COUNT)" "$txt\n$(lsblk -o NAME,MODEL,TYPE,FSTYPE,SIZE $LVM_PARTS)\n" msg "$_lvmnew (LV:$VOL_COUNT)" "$txt\n$(lsblk -o NAME,MODEL,TYPE,FSTYPE,SIZE $LVM_PARTS)\n"
@ -1971,7 +1967,7 @@ lvm_mkgroup()
msg "$_lvmnew" "\nCreating volume group: $VGROUP\n" 0 msg "$_lvmnew" "\nCreating volume group: $VGROUP\n" 0
vgcreate -f "$VGROUP" $LVM_PARTS > /dev/null 2> "$ERR" vgcreate -f "$VGROUP" $LVM_PARTS > /dev/null 2> "$ERR"
errshow "vgcreate -f $VGROUP $LVM_PARTS" || return 1 errshow 0 "vgcreate -f $VGROUP $LVM_PARTS" || return 1
SIZE=$(vgdisplay "$VGROUP" | awk '/VG Size/ { gsub(/[^0-9.]/, ""); print int($0) }') SIZE=$(vgdisplay "$VGROUP" | awk '/VG Size/ { gsub(/[^0-9.]/, ""); print int($0) }')
SIZE_UNIT="$(vgdisplay "$VGROUP" | awk '/VG Size/ { print substr($NF, 0, 1) }')" SIZE_UNIT="$(vgdisplay "$VGROUP" | awk '/VG Size/ { print substr($NF, 0, 1) }')"
@ -2026,7 +2022,7 @@ lvm_extra_lvs()
lvm_volume_name "$_lvmlvname" && lvm_lv_size || return 1 lvm_volume_name "$_lvmlvname" && lvm_lv_size || return 1
msg "$_lvmnew (LV:$VOL_COUNT)" "\nCreating a $VOLUME_SIZE volume $VNAME in $VGROUP\n" 0 msg "$_lvmnew (LV:$VOL_COUNT)" "\nCreating a $VOLUME_SIZE volume $VNAME in $VGROUP\n" 0
lvcreate -L "$VOLUME_SIZE" "$VGROUP" -n "$VNAME" > /dev/null 2> "$ERR" lvcreate -L "$VOLUME_SIZE" "$VGROUP" -n "$VNAME" > /dev/null 2> "$ERR"
errshow "lvcreate -L $VOLUME_SIZE $VGROUP -n $VNAME" || return 1 errshow 0 "lvcreate -L $VOLUME_SIZE $VGROUP -n $VNAME" || return 1
msg "$_lvmnew (LV:$VOL_COUNT)" "\nDone, logical volume (LV) $VNAME ($VOLUME_SIZE) has been created.\n" msg "$_lvmnew (LV:$VOL_COUNT)" "\nDone, logical volume (LV) $VNAME ($VOLUME_SIZE) has been created.\n"
(( VOL_COUNT-- )) (( VOL_COUNT-- ))
done done
@ -2112,7 +2108,7 @@ luks_open()
luks_pass "$_luksopen" || return 1 luks_pass "$_luksopen" || return 1
msg "$_luksopen" "\nOpening encrypted partition: $LUKS_NAME\n\nUsing device/volume: $LUKS_PART\n" 0 msg "$_luksopen" "\nOpening encrypted partition: $LUKS_NAME\n\nUsing device/volume: $LUKS_PART\n" 0
cryptsetup open --type luks "$LUKS_PART" "$LUKS_NAME" <<< "$LUKS_PASS" 2> "$ERR" cryptsetup open --type luks "$LUKS_PART" "$LUKS_NAME" <<< "$LUKS_PASS" 2> "$ERR"
errshow "cryptsetup open --type luks $LUKS_PART $LUKS_NAME" || return 1 errshow 0 "cryptsetup open --type luks $LUKS_PART $LUKS_NAME" || return 1
LUKS='encrypted'; luks_show LUKS='encrypted'; luks_show
return 0 return 0
} }
@ -2175,9 +2171,9 @@ luks_basic()
luks_setup || return 1 luks_setup || return 1
msg "$_luksnew" "\nCreating encrypted partition: $LUKS_NAME\n\nDevice or volume used: $LUKS_PART\n" 0 msg "$_luksnew" "\nCreating encrypted partition: $LUKS_NAME\n\nDevice or volume used: $LUKS_PART\n" 0
cryptsetup -q luksFormat "$LUKS_PART" <<< "$LUKS_PASS" 2> "$ERR" cryptsetup -q luksFormat "$LUKS_PART" <<< "$LUKS_PASS" 2> "$ERR"
errshow "cryptsetup -q luksFormat $LUKS_PART" || return 1 errshow 0 "cryptsetup -q luksFormat $LUKS_PART" || return 1
cryptsetup open "$LUKS_PART" "$LUKS_NAME" <<< "$LUKS_PASS" 2> "$ERR" cryptsetup open "$LUKS_PART" "$LUKS_NAME" <<< "$LUKS_PASS" 2> "$ERR"
errshow "cryptsetup open $LUKS_PART $LUKS_NAME" || return 1 errshow 0 "cryptsetup open $LUKS_PART $LUKS_NAME" || return 1
LUKS='encrypted'; luks_show LUKS='encrypted'; luks_show
return 0 return 0
} }
@ -2190,9 +2186,9 @@ luks_advanced()
[[ $cipher ]] || return 1 [[ $cipher ]] || return 1
msg "$_luksadv" "\nCreating encrypted partition: $LUKS_NAME\n\nDevice or volume used: $LUKS_PART\n" 0 msg "$_luksadv" "\nCreating encrypted partition: $LUKS_NAME\n\nDevice or volume used: $LUKS_PART\n" 0
cryptsetup -q $cipher luksFormat "$LUKS_PART" <<< "$LUKS_PASS" 2> "$ERR" cryptsetup -q $cipher luksFormat "$LUKS_PART" <<< "$LUKS_PASS" 2> "$ERR"
errshow "cryptsetup -q $cipher luksFormat $LUKS_PART" || return 1 errshow 0 "cryptsetup -q $cipher luksFormat $LUKS_PART" || return 1
cryptsetup open "$LUKS_PART" "$LUKS_NAME" <<< "$LUKS_PASS" 2> "$ERR" cryptsetup open "$LUKS_PART" "$LUKS_NAME" <<< "$LUKS_PASS" 2> "$ERR"
errshow "cryptsetup open $LUKS_PART $LUKS_NAME" || return 1 errshow 0 "cryptsetup open $LUKS_PART $LUKS_NAME" || return 1
luks_show luks_show
return 0 return 0
fi fi
@ -2418,12 +2414,13 @@ errshow()
{ {
[ $? -eq 0 ] && return 0 [ $? -eq 0 ] && return 0
local fatal=0 err="" local fatal=$1
shift # always shift off the fatal level arg
local err=""
err="$(sed 's/[^[:print:]]//g; s/\[[0-9\;:]*\?m//g; s/==> //g; s/] ERROR:/]\nERROR:/g' "$ERR")" err="$(sed 's/[^[:print:]]//g; s/\[[0-9\;:]*\?m//g; s/==> //g; s/] ERROR:/]\nERROR:/g' "$ERR")"
[[ -z $err ]] && err="no error message was found" [[ -z $err ]] && err="no error message was found"
(( $1 == 1 )) && { fatal=1; shift; }
local txt="\nCommand: $1\n\n\n\nError: $err\n\n" local txt="\nCommand: $1\n\n\n\nError: $err\n\n"
if (( fatal )); then if (( fatal )); then