-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdisk-menu
140 lines (122 loc) · 4.03 KB
/
disk-menu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/bin/bash
REQUIRED_PKG="dialog"
PKG_OK=$(dpkg-query -W --showformat='${Status}\n' $REQUIRED_PKG|grep "install ok installed")
echo Checking for $REQUIRED_PKG: "$PKG_OK"
if [ "" = "$PKG_OK" ]; then
echo "No $REQUIRED_PKG. Setting up $REQUIRED_PKG."
apt install -y $REQUIRED_PKG
fi
shopt -s lastpipe
width=120
height=40
menu_height=20
backtitle='Disk-menu'
title='Device selection'
# Will store the menu numbers and (nicely formatted) lsblk lines as pairs.
# Later given to dialog as individual arguments.
choices=()
# Arrays to store the properties of a device according to a particular lsblk
# field. At the very least, one needs to be able to look up the kname from the
# choice number.
knames=()
uuids=()
mountpoints=()
sizes=()
# Array indexes normally start from 0 in bash but, for the benefit of dialog,
# let's start from 1.
i=1
# This is a three part pipeline. The third part is the while loop.
lsblk -rno type,kname,uuid,mountpoint,size |
column -t -s ' ' |
while read -r line; do
# We just read the (nicely formatted) line. Now split the line into
# individual fields, assigning to bash variables.
echo "$line" | read -r type kname uuid size mountpoint
if [[ $type != part ]]; then
# It's not a partition. Proceed directly to the next line.
continue
fi
# Insert the line into the choices array for later use by dialog. Note that
# two items are being appended to the array - the index number and the line
# itself.
choices+=("$i" "$line")
# Stash the fields so that they be later looked up by the choice number.
knames[i]=$kname
uuids[i]=$uuid
if [[ -z "$size" ]]; then
sizes[i]=$mountpoint
mountpoints[i]="<none>"
else
sizes[i]=$size
# The mountpoint column output by lsblk may contain backslash-escape
# sequences. This happens when the mountpoint contains whitespace or other
# unusual characters. bash is able to decode properly with printf.
mountpoints[i]=$(printf %b "$mountpoint")
fi
# Increment the index number for the next added device, if any.
i=$((i + 1))
done
i=$(
dialog \
--clear \
--stdout \
--backtitle "$backtitle" \
--title "$title" \
--menu "Select partition (1-${#knames[@]}): " \
"$height" "$width" "$menu_height" "${choices[@]}"
)
# Check whether i is empty, indicating that no choice was made.
if [[ ! $i ]]; then
echo "No partition was selected. Aborting" >&2
exit 1
fi
# dialog --title "You selected ${knames[i]}" \
# --msgbox "Which has a size of ${sizes[i]}" 10 30;
width=120
height=40
menu_height=10
backtitle='Disk-menu'
title='Device selection'
menu='Choose one of the following options:'
ID="UUID=${uuids[i]}"
MP="$ID none swap swap 0 0"
options=(1 'Replacement of default swap'
2 'Show current partition info'
3 'Partitioning tool cfdisk'
4 'Unmount partition'
x reboot
q quit )
result=$(dialog --clear \
--backtitle "$backtitle" \
--title "$title" \
--menu "$menu" \
"$height" "$width" "$menu_height" \
"${options[@]}" \
2>&1 >/dev/tty)
case "$result" in
1) echo 'Replacement of swap';
cp -v /etc/fstab /etc/fstab.backup;
sed -i /swap/c "${MP[i]}" fstab;
printf 'RESUME=%s\n' "$ID" > resume;
echo "/sbin/mkswap /dev/""${kname[i]}""";
echo "swapon -U ""${uuid[i]}"" /dev/""${kname[i]}""";
echo update-initramfs -u -k all;;
2) dialog --title "Selected device: ${knames[i]}" \
--msgbox "Here's a list of variables
Mountpoint : ${mountpoints[i]}
Size : ${sizes[i]}
ID : ${uuids[i]}
"$ID" " 10 50;;
# For the unmounted partition: (option 2)
# Size shown at the wrong place.
# Last update needs to be tested on another
# configuration with an unmounted partition
##################################################
3) echo test;;
# cfdisk /dev/"$knames" > debug.txt;;
4) umount "/dev/""${kname[i]}""";;
x) echo 'Reboot';
reboot;;
q) clear;
exit ;;
esac