This repository has been archived by the owner on Dec 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathDiskSetup.sh
executable file
·353 lines (321 loc) · 9.87 KB
/
DiskSetup.sh
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/bin/bash
# shellcheck disable=SC2181,SC2236
#
# Script to automate basic setup of CHROOT device
#
#################################################################
PROGNAME=$(basename "$0")
BOOTDEVSZ="${BOOTDEVSZ:-500m}"
FSTYPE="${FSTYPE:-ext4}"
# Function-abort hooks
trap "exit 1" TERM
export TOP_PID=$$
# Error-logging
function err_exit {
echo "${1}" > /dev/stderr
logger -t "${PROGNAME}" -p kern.crit "${1}"
exit 1
}
# Print out a basic usage message
function UsageMsg {
(
echo "Usage: ${0} [GNU long option] [option] ..."
echo " Options:"
printf "\t-b <BOOT_LABEL>\n"
printf '\t%-4s%s\n' '-B' 'Boot-partition size (default: 500MiB)'
printf "\t-d <BOOT_DEV_PATH>\n"
printf "\t-f <FSTYPE>\n"
printf "\t-p <PARTITION_STRING>\n"
printf "\t-r <ROOT_FS_LABEL>\n"
printf "\t-v <ROOT_VG_NAME>\n"
echo " GNU long options:"
printf "\t--bootlabel <BOOT_LABEL>\n"
printf "\t--disk <BOOT_DEV_PATH>\n"
printf "\t--fstype <FSTYPE>\n"
printf "\t--partitioning <PARTITION_STRING>\n"
printf "\t--rootlabel <ROOT_FS_LABEL>\n"
printf "\t--vgname <ROOT_VG_NAME>\n"
)
exit 1
}
# Check for arguments
if [[ $# -lt 1 ]]
then
(
printf "Missing parameter(s). Valid flags/parameters are:\n"
printf "\t-b|--bootlabel: FS-label applied to '/boot' filesystem\n"
printf "\t-d|--disk: dev-path to disk to be partitioned\n"
printf "\t-r|--rootlabel: FS-label to apply to '/' filesystem (no LVM in use)\n"
printf "\t-v|--vgname: LVM2 Volume-Group name for root volumes\n"
printf "Aborting...\n"
) > /dev/stderr
exit 1
fi
function LogBrk {
echo "${2}" > /dev/stderr
exit "${1}"
}
# Partition as LVM
function CarveLVM {
local ITER
local MOUNTPT
local PARTITIONARRAY
local PARTITIONSTR
local VOLFLAG
local VOLNAME
local VOLSIZE
# Whether to use flag-passed partition-string or default values
if [ -z ${GEOMETRYSTRING+x} ]
then
# This is fugly but might(??) be easier for others to follow/update
PARTITIONSTR="/:rootVol:4"
PARTITIONSTR+=",swap:swapVol:2"
PARTITIONSTR+=",/home:homeVol:1"
PARTITIONSTR+=",/var:varVol:2"
PARTITIONSTR+=",/var/log:logVol:2"
PARTITIONSTR+=",/var/log/audit:auditVol:100%FREE"
else
PARTITIONSTR="${GEOMETRYSTRING}"
fi
# Convert ${PARTITIONSTR} to iterable array
IFS=',' read -r -a PARTITIONARRAY <<< "${PARTITIONSTR}"
# Clear the MBR and partition table
dd if=/dev/zero of="${CHROOTDEV}" bs=512 count=1000 > /dev/null 2>&1
# Lay down the base partitions
parted -s "${CHROOTDEV}" -- mklabel msdos mkpart primary "${FSTYPE}" 2048s "${BOOTDEVSZ}" \
mkpart primary "${FSTYPE}" "${BOOTDEVSZ}" 100% set 2 lvm
# Gather info to diagnose seeming /boot race condition
if [[ $(grep -q "${BOOTLABEL}" /proc/mounts)$? -eq 0 ]]
then
tail -n 100 /var/log/messages
sleep 3
fi
# Stop/umount boot device, in case parted/udev/systemd managed to remount it
# again.
systemctl stop boot.mount || true
# Create /boot filesystem
mkfs -t "${FSTYPE}" "${MKFSFORCEOPT}" -L "${BOOTLABEL}" \
"${CHROOTDEV}${PARTPRE}1" || \
err_exit "Failure creating filesystem - /boot"
## Create LVM objects
# Let's only attempt this if we're a secondary EBS
if [[ ${CHROOTDEV} == /dev/xvda ]] || [[ ${CHROOTDEV} == /dev/nvme0n1 ]]
then
echo "Skipping explicit pvcreate opertion... "
else
pvcreate "${CHROOTDEV}${PARTPRE}2" || LogBrk 5 "PV creation failed. Aborting!"
fi
# Create root VolumeGroup
vgcreate -y "${VGNAME}" "${CHROOTDEV}${PARTPRE}2" || LogBrk 5 "VG creation failed. Aborting!"
# Create LVM2 volume-objects by iterating ${PARTITIONARRAY}
ITER=0
while [[ ${ITER} -lt ${#PARTITIONARRAY[*]} ]]
do
MOUNTPT="$( cut -d ':' -f 1 <<< "${PARTITIONARRAY[${ITER}]}")"
VOLNAME="$( cut -d ':' -f 2 <<< "${PARTITIONARRAY[${ITER}]}")"
VOLSIZE="$( cut -d ':' -f 3 <<< "${PARTITIONARRAY[${ITER}]}")"
# Create LVs
if [[ ${VOLSIZE} =~ FREE ]]
then
# Make sure 'FREE' is given as last list-element
if [[ $(( ITER += 1 )) -eq ${#PARTITIONARRAY[*]} ]]
then
VOLFLAG="-l"
VOLSIZE="100%FREE"
else
echo "Using 'FREE' before final list-element. Aborting..."
kill -s TERM " ${TOP_PID}"
fi
else
VOLFLAG="-L"
VOLSIZE+="g"
fi
lvcreate --yes -W y "${VOLFLAG}" "${VOLSIZE}" -n "${VOLNAME}" "${VGNAME}" || \
err_exit "Failure creating LVM2 volume '${VOLNAME}'"
# Create FSes on LVs
if [[ ${MOUNTPT} == swap ]]
then
mkswap "/dev/${VGNAME}/${VOLNAME}"
else
mkfs -t "${FSTYPE}" "${MKFSFORCEOPT}" "/dev/${VGNAME}/${VOLNAME}" \
|| err_exit "Failure creating filesystem for '${MOUNTPT}'"
fi
(( ITER+=1 ))
done
# shellcheck disable=SC2053
if [[ ${FSTYPE} == ext3 ]] || [[ ${FSTYPE} == ext4 ]]
then
if [[ $( e2label "${CHROOTDEV}${PARTPRE}1" ) != ${BOOTLABEL} ]]
then
e2label "${CHROOTDEV}${PARTPRE}1" "${BOOTLABEL}" || \
err_exit "Failed to apply desired label to ${CHROOTDEV}${PARTPRE}1"
fi
elif [[ ${FSTYPE} == xfs ]]
then
if [[ $( xfs_admin -l "${CHROOTDEV}${PARTPRE}1" | sed -e 's/"$//' -e 's/^.*"//' ) != ${BOOTLABEL} ]]
then
xfs_admin -L "${CHROOTDEV}${PARTPRE}1" "${BOOTLABEL}" || \
err_exit "Failed to apply desired label to ${CHROOTDEV}${PARTPRE}1"
fi
else
err_exit "Unrecognized fstype [${FSTYPE}] specified. Aborting... "
fi
}
# Partition with no LVM
function CarveBare {
# Clear the MBR and partition table
dd if=/dev/zero of="${CHROOTDEV}" bs=512 count=1000 > /dev/null 2>&1
# Lay down the base partitions
parted -s "${CHROOTDEV}" -- mklabel msdos mkpart primary "${FSTYPE}" 2048s "${BOOTDEVSZ}" \
mkpart primary "${FSTYPE}" "${BOOTDEVSZ}" 100%
# Create FS on partitions
mkfs -t "${FSTYPE}" "${MKFSFORCEOPT}" -L "${BOOTLABEL}" "${CHROOTDEV}${PARTPRE}1"
mkfs -t "${FSTYPE}" "${MKFSFORCEOPT}" -L "${ROOTLABEL}" "${CHROOTDEV}${PARTPRE}2"
}
######################
## Main program-flow
######################
OPTIONBUFR=$(getopt -o b:B:d:f:hp:r:v: --long bootlabel:,boot-size:,disk:,fstype:,help,partitioning:,rootlabel:,vgname: -n "${PROGNAME}" -- "$@")
eval set -- "${OPTIONBUFR}"
###################################
# Parse contents of ${OPTIONBUFR}
###################################
while true
do
case "$1" in
-b|--bootlabel)
case "$2" in
"")
LogBrk 1 "Error: option required but not specified"
shift 2;
exit 1
;;
*)
BOOTLABEL=${2}
shift 2;
;;
esac
;;
-B|--boot-size)
case "$2" in
"")
err_exit "Error: option required but not specified"
shift 2;
exit 1
;;
*)
BOOTDEVSZ=${2}
shift 2;
;;
esac
;;
-d|--disk)
case "$2" in
"")
LogBrk 1 "Error: option required but not specified"
shift 2;
exit 1
;;
*)
CHROOTDEV=${2}
shift 2;
;;
esac
;;
-f|--fstype)
case "$2" in
"")
LogBrk 1 "Error: option required but not specified"
shift 2;
exit 1
;;
ext3|ext4)
FSTYPE=${2}
MKFSFORCEOPT="-F"
shift 2;
;;
xfs)
FSTYPE=${2}
MKFSFORCEOPT="-f"
shift 2;
;;
*)
LogBrk 1 "Error: unrecognized/unsupported FSTYPE. Aborting..."
shift 2;
exit 1
;;
esac
;;
-h|--help)
UsageMsg
;;
-p|--partitioning)
case "$2" in
"")
LogBrk 1"Error: option required but not specified"
shift 2;
exit 1
;;
*)
GEOMETRYSTRING=${2}
shift 2;
;;
esac
;;
-r|--rootlabel)
case "$2" in
"")
LogBrk 1"Error: option required but not specified"
shift 2;
exit 1
;;
*)
ROOTLABEL=${2}
shift 2;
;;
esac
;;
-v|--vgname)
case "$2" in
"")
LogBrk 1 "Error: option required but not specified"
shift 2;
exit 1
;;
*)
VGNAME=${2}
shift 2;
;;
esac
;;
--)
shift
break
;;
*)
LogBrk 1 "Internal error!"
exit 1
;;
esac
done
# See if our carve-target is an NVMe
if [[ ${CHROOTDEV} =~ /dev/nvme ]]
then
PARTPRE="p"
else
PARTPRE=""
fi
# Ensure BOOTLABEL has been specified
if [[ -z ${BOOTLABEL+xxx} ]]
then
LogBrk 1 "Cannot continue without 'bootlabel' being specified. Aborting..."
elif [[ ! -z ${ROOTLABEL+xxx} ]] && [[ ! -z ${VGNAME+xxx} ]]
then
LogBrk 1 "The 'rootlabel' and 'vgname' arguments are mutually exclusive. Exiting."
elif [[ -z ${ROOTLABEL+xxx} ]] && [[ ! -z ${VGNAME+xxx} ]]
then
CarveLVM
elif [[ ! -z ${ROOTLABEL+xxx} ]] && [[ -z ${VGNAME+xxx} ]]
then
CarveBare
fi