-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmkpartitions
More file actions
102 lines (76 loc) · 3.13 KB
/
Copy pathmkpartitions
File metadata and controls
102 lines (76 loc) · 3.13 KB
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
#!/usr/bin/env bash
##################################################
#### Wipe device, then partition with several same-sized partitions
##################################################
##################################################
#### partname is not optional, the way this script invokes parted
#### specifying too many or too large partitions results in fewer created
partcount=10
partgb=5
partname=iscsi
device=/dev/nvme0n1xxx
createremainder=0
uniquesuffix=0
##################################################
#### SCRIPT BEGIN
##################################################
echo "Checking device..."
[[ -b "${device}" ]] || { echo "ERROR: ${device} invalid"; exit 1; }
{ lsblk -l "${device}" | awk -v device=$(basename "${device}") '$1 == device && $6 !~ /disk/ {print "ERROR: /dev/" device " is a " $6", not a disk"; exit 1}'; } || exit 1
### note wipefs and parted both seem to refuse
### reasonable endeavor check anyway
echo "Checking mounts..."
{ lsblk -l "${device}" | awk -v device=$(basename "${device}") '$1 ~ device && $7 ~ /\// {print "ERROR: mounted at "$7; exit 1}'; } || exit 1
if ! command -v pvs &> /dev/null
then
echo "Skipping LVM check..."
else
echo "Checking LVM..."
for i in $(lsblk -l "${device}" | awk -v device=$(basename "${device}") '$1 ~ device {print $1}'); do
{ pvs | awk -v checkpv="/dev/${i}" '$1 == checkpv {print "ERROR: LVM uses "$1; exit 1}'; } || exit 1
done
fi
echo
read -t 2 -r -n 2 -s -p "Press any key to wipe ${device} and re-partition with ${partcount} * ${partgb}GiB partitions " || { echo -e "\nTimed out"; exit 1; }
echo
echo
echo -n "Continuing in a few seconds..."
for i in {1..3}; do echo -n .; sleep "${i}"; done; echo -en '.\n'
echo "Wiping..."
for i in $(lsblk -l "${device}" | awk '$6 ~/^part$/ {print $1}'); do
wipefs -a /dev/"${i}" > /dev/null ||
{ echo "ERROR: partition ${device} not wiped"; exit 1; }
done
wipefs -a "${device}" > /dev/null ||
{ echo "ERROR: device ${device} not wiped"; exit 1; }
echo "Partitioning..."
### try to be balanced
generate_uniq_suffix() {
if [[ "${uniquesuffix}" -ne 0 ]]; then
[[ -z "${mid}" ]] &&
[[ -f /etc/machine-id ]] &&
mid=$(</etc/machine-id)
[[ -z "${mid}" ]] &&
mid="${RANDOM:0:4}${RANDOM:0:4}"
echo "$(date +%s%N)${mid}${device}" |
cksum |
cut -d ' ' -f1
fi
}
parted --align=optimal "${device}" --script -- \
$(echo "'mklabel gpt'") \
$(echo "'mkpart ${partname}$(generate_uniq_suffix) 0% ${partgb}GiB'") \
$(for i in $(seq 1 $(("${partcount}"-1))); do
echo -e "'mkpart ${partname}$(generate_uniq_suffix) $(( $i*${partgb} ))GiB $(( $i*${partgb}+${partgb} ))GiB'"
done)
echo "mkpart ${partname} exit: $?"
if [[ "${createremainder}" -eq 1 ]]; then
echo "Partitioning extra space (will fail if number requested not created)..."
extraargs=$(echo "'mkpart extra$(generate_uniq_suffix) $(( ${partgb}*${partcount} ))GiB 100%'")
parted --align=optimal "${device}" --script -- \
$(echo "${extraargs}")
echo "mkpart extra softexit: $?"
fi
echo
echo "Final state:"
parted --align=optimal "${device}" p | grep --color=always '$\|[[:space:]]extra[0-9]*$'