-
Notifications
You must be signed in to change notification settings - Fork 1
/
qpart
executable file
·189 lines (176 loc) · 4.28 KB
/
qpart
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/bash
# Quickly partition and format drive
# Depends: kpartx cloud-utils parted e2fsprogs xfsprogs bash
if [ "$USER" != "root" ]; then
echo "Please run this as root or with sudo privileges."
exit 1
fi
BLD="\033[1m"
ITL="\033[3m"
FIN="\033[0m"
# CURRENT
echo -e ""
echo -e "${BLD}${ITL}CURRENT PARTITION SCHEME${FIN}"
if [[ `findmnt -v -n -o SOURCE /boot` ]]; then
CB_NODE=`findmnt -v -n -o SOURCE /boot`
CB_FSTYPE=`findmnt -v -n -o FSTYPE /boot`
echo -en "BOOT: ${BLD}${CB_NODE}${FIN} "
echo -e "FSTYPE: ${BLD}${CB_FSTYPE}${FIN}"
fi
if [[ `findmnt -v -n -o SOURCE /` ]]; then
CR_NODE=`findmnt -v -n -o SOURCE /`
CR_FSTYPE=`findmnt -v -n -o FSTYPE /`
echo -en "ROOT: ${BLD}${CR_NODE}${FIN} "
echo -e "FSTYPE: ${BLD}${CR_FSTYPE}${FIN}"
fi
# AVAILABLE
if [[ `ls /dev/sd*[a-z]` ]] > /dev/null 2>&1 || \
[[ `ls /dev/nvme*[0-9]` ]] > /dev/null 2>&1 || \
[[ `ls /dev/mmcblk[0-9]` ]] > /dev/null 2>&1; then
echo -e ""
echo -e "${BLD}${ITL}AVAILABLE DRIVES${FIN}"
if [[ `ls /dev/sd*[a-z]` ]] > /dev/null 2>&1; then
HD_CHK=`findmnt -v -n -o SOURCE / | sed 's/.$//'`
ls /dev/sd*[a-z]
fi
if [[ `ls /dev/nvme*[0-9]` ]] > /dev/null 2>&1; then
NVME_CHK=`findmnt -v -n -o SOURCE / | sed 's/..$//'`
ls /dev/nvme*[0-9]n1
fi
if [[ `ls /dev/mmcblk[0-9]` ]] > /dev/null 2>&1; then
MMC_CHK=`findmnt -v -n -o SOURCE / | sed 's/..$//'`
ls /dev/mmcblk[0-9]
fi
else
echo -e ""
echo -e "There are no available drives to format."
exit 0
fi
# CHOOSE DRIVE
echo -e ""
echo -e "${BLD}Write in the device node you want to format:${FIN} "
echo -en "/dev/"
read DEVICE_NODE
NODE="/dev/${DEVICE_NODE}"
if [[ -e "$NODE" ]]; then
:;
else
echo -e
echo -e "${BLD}WARNING:${FIN} The drive you selected is not available or invalid."
exit 1
fi
if [[ "$HD_CHK" == "$NODE" || "$NVME_CHK" == "$NODE" || "$MMC_CHK" == "$NODE" ]]; then
echo -e
echo -e "${BLD}WARNING:${FIN} The drive you selected is currently in use."
exit 1
fi
PART="p1"
if [[ `ls $NODE | grep "/dev/sd"` ]]; then PART=`echo $PART | sed 's/p//g'`; fi
# Filesystem Types
echo -e ""
echo -e "${BLD}Filesystem Types${FIN}"
echo -e "${ITL}OPTIONS: ${FIN}ext4 xfs"
echo -e ""
echo -en "Filesystem: "
read FSTYPE
if [[ "$FSTYPE" =~ ^(ext4|xfs)$ ]]; then :; else echo -e "$FSTYPE not supported!"; exit 1; fi
# GUID Partition Table
echo -e ""
echo -e "${BLD}GUID Partition Table${FIN}"
echo -e "${ITL}OPTIONS: ${FIN}true false"
echo -e ""
echo -en "GPT: "
read GPT
if [[ "$GPT" =~ ^(true|false|yes|no)$ ]]; then :; else echo -e "Unknown option chosen?"; exit 1; fi
# Partition Offset
echo -e ""
echo -e "${BLD}Partition Offset${FIN}"
echo -e "${ITL}If unsure choose: 1: ${FIN}"
echo -e ""
echo -en "OFFSET: "
read PART_OFFSET
OFFSET="${PART_OFFSET}MiB"
# Partition Label
echo -e ""
echo -e "${BLD}Partition Label${FIN}"
echo -e "${ITL}EXAMPLE: ${FIN}EXTERNAL"
echo -e ""
echo -en "LABEL: "
read LABEL
# SUMMARY
echo -e ""
echo -e "${BLD}Summary List${FIN}"
echo -e "Drive: $NODE"
echo -e "Fstype: $FSTYPE"
echo -e "GPT: $GPT"
echo -e "Offset: $OFFSET"
echo -e "Label: $LABEL"
echo -e ""
options=("Continue" "Exit")
select opt in "${options[@]}"
do
case $opt in
"Continue")
break
;;
"Exit")
echo -e "Program Terminated."
exit 0
;;
*) echo "invalid option $REPLY";;
esac
done
if [[ "$FSTYPE" == "ext4" ]]; then
MKFS="mkfs.ext4 -L $LABEL ${NODE}${PART}"
RESIZEFS="resize2fs ${NODE}${PART}"
fi
if [[ "$FSTYPE" == "xfs" ]]; then
MKFS="mkfs.xfs -f -L $LABEL ${NODE}${PART}"
RESIZEFS="xfs_growfs -d /mnt/${PART}"
fi
if [[ -e "${NODE}" ]]; then
# delete
sfdisk --delete ${NODE}
sleep 1
kpartx ${NODE}
sleep .25
kpartx -u ${NODE}
sleep 1
# partition
if [[ "$GPT" =~ ^(true|yes)$ ]]; then
parted --script "${NODE}" \
mklabel gpt \
mkpart 1 ext2 ${OFFSET} 100%
else
parted --script ${NODE} \
mklabel msdos \
mkpart primary ext2 ${OFFSET} 100%
fi
partprobe ${NODE}
sleep 1.25
else
echo -e "Drive not found: ${NODE}"
exit 1
fi
if [[ -e "${NODE}${PART}" ]]; then
mkdir -p /mnt/${PART}
echo 'y' | $MKFS
sleep .50
mount ${NODE}${PART} /mnt/${PART}
sync
umount /mnt/${PART}
sleep .50
mount -o defaults,noatime ${NODE}${PART} /mnt/${PART}
bash growpart ${NODE} 1
sleep .75
$RESIZEFS
sleep .50
umount /mnt/${PART}
if [[ -d "/mnt/${PART}" ]]; then
rm -fd /mnt/${PART}
fi
else
echo -e "Partition not found: ${NODE}${PART}"
exit 1
fi
exit 0