-
Notifications
You must be signed in to change notification settings - Fork 7
/
DiskSetup.sh
executable file
·463 lines (421 loc) · 12.8 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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#!/bin/bash
set -eu -o pipefail
#
# Script to automate basic setup of CHROOT device
#
#################################################################
PROGNAME="$( basename "$0" )"
PROGDIR="$( dirname "${0}" )"
BOOTDEVSZMIN="768"
BOOTDEVSZ="${BOOTDEVSZ:-${BOOTDEVSZMIN}}"
UEFIDEVSZ="${UEFIDEVSZ:-128}"
CHROOTDEV="${CHROOTDEV:-UNDEF}"
DEBUG="${DEBUG:-UNDEF}"
FSTYPE="${FSTYPE:-xfs}"
LABEL_BOOT="${LABEL_BOOT:-boot_disk}"
LABEL_UEFI="${LABEL_UEFI:-UEFI_DISK}"
# Import shared error-exit function
source "${PROGDIR}/err_exit.bashlib"
# Ensure appropriate SEL mode is set
source "${PROGDIR}/no_sel.bashlib"
# Print out a basic usage message
function UsageMsg {
local SCRIPTEXIT
SCRIPTEXIT="${1:-1}"
(
echo "Usage: ${0} [GNU long option] [option] ..."
echo " Options:"
printf '\t%-4s%s\n' '-B' 'Boot-partition size (default: 768MiB)'
printf '\t%-4s%s\n' '-d' 'Base dev-node used for build-device'
printf '\t%-4s%s\n' '-f' 'Filesystem-type used for root filesystems (default: xfs)'
printf '\t%-4s%s\n' '-h' 'Print this message'
printf '\t%-4s%s\n' '-l' ' for /boot filesystem (default: boot_disk)'
printf '\t%-4s%s\n' '-L' ' for /boot/efi filesystem (default: UEFI_DISK)'
printf '\t%-4s%s\n' '-p' 'Comma-delimited string of colon-delimited partition-specs'
printf '\t%-6s%s\n' '' 'Default layout:'
printf '\t%-8s%s\n' '' '/:rootVol:4'
printf '\t%-8s%s\n' '' 'swap:swapVol:2'
printf '\t%-8s%s\n' '' '/home:homeVol:1'
printf '\t%-8s%s\n' '' '/var:varVol:2'
printf '\t%-8s%s\n' '' '/var/tmp:varTmpVol:2'
printf '\t%-8s%s\n' '' '/var/log:logVol:2'
printf '\t%-8s%s\n' '' '/var/log/audit:auditVol:100%FREE'
printf '\t%-4s%s\n' '-r' 'Label to apply to root-partition if not using LVM (default: root_disk)'
printf '\t%-4s%s\n' '-v' 'Name assigned to root volume-group (default: VolGroup00)'
printf '\t%-4s%s\n' '-U' 'UEFI-partition size (default: 256MiB)'
echo " GNU long options:"
printf '\t%-20s%s\n' '--boot-size' 'See "-B" short-option'
printf '\t%-20s%s\n' '--disk' 'See "-d" short-option'
printf '\t%-20s%s\n' '--fstype' 'See "-f" short-option'
printf '\t%-20s%s\n' '--help' 'See "-h" short-option'
printf '\t%-20s%s\n' '--label-boot' 'See "-l" short-option'
printf '\t%-20s%s\n' '--label-uefi' 'See "-L" short-option'
printf '\t%-20s%s\n' '--partition-string' 'See "-p" short-option'
printf '\t%-20s%s\n' '--rootlabel' 'See "-r" short-option'
printf '\t%-20s%s\n' '--uefi-size' 'See "-U" short-option'
printf '\t%-20s%s\n' '--vgname' 'See "-v" short-option'
)
exit "${SCRIPTEXIT}"
}
# 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/tmp:varTmpVol: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 target-disk of partitioning and other structural data
CleanChrootDiskPrtTbl
# Lay down the base partitions
err_exit "Laying down new partition-table..." NONE
parted -s "${CHROOTDEV}" -- mktable gpt \
mkpart primary "${FSTYPE}" 1049k 2m \
mkpart primary fat16 4096s $(( 2 + UEFIDEVSZ ))m \
mkpart primary xfs $((
2 + UEFIDEVSZ ))m $(( ( 2 + UEFIDEVSZ ) + BOOTDEVSZ
))m \
mkpart primary xfs $(( ( 2 + UEFIDEVSZ ) + BOOTDEVSZ ))m 100% \
set 1 bios_grub on \
set 2 esp on \
set 3 bls_boot on \
set 4 lvm on || \
err_exit "Failed laying down new partition-table"
## Create LVM objects
# Let's only attempt this if we're a secondary EBS
if [[ ${CHROOTDEV} == /dev/xvda ]] || [[ ${CHROOTDEV} == /dev/nvme0n1 ]]
then
err_exit "Skipping explicit pvcreate opertion... " NONE
else
err_exit "Creating LVM2 PV ${CHROOTDEV}${PARTPRE:-}4..." NONE
pvcreate "${CHROOTDEV}${PARTPRE:-}4" || \
err_exit "PV creation failed. Aborting!"
fi
# Create root VolumeGroup
err_exit "Creating LVM2 volume-group ${VGNAME}..." NONE
vgcreate -y "${VGNAME}" "${CHROOTDEV}${PARTPRE:-}4" || \
err_exit "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
err_exit "Creating swap filesystem..." NONE
mkswap "/dev/${VGNAME}/${VOLNAME}" || \
err_exit "Failed creating swap filesystem..."
else
err_exit "Creating filesystem for ${MOUNTPT}..." NONE
mkfs -t "${FSTYPE}" "${MKFSFORCEOPT}" "/dev/${VGNAME}/${VOLNAME}" || \
err_exit "Failure creating filesystem for '${MOUNTPT}'"
fi
(( ITER+=1 ))
done
}
# Partition with no LVM
function CarveBare {
# Clear the target-disk of partitioning and other structural data
CleanChrootDiskPrtTbl
# Lay down the base partitions
err_exit "Laying down new partition-table..." NONE
parted -s "${CHROOTDEV}" -- mklabel gpt \
mkpart primary "${FSTYPE}" 1049k 2m \
mkpart primary fat16 4096s $(( 2 + UEFIDEVSZ ))m \
mkpart primary xfs $((
2 + UEFIDEVSZ ))m $(( ( 2 + UEFIDEVSZ ) + BOOTDEVSZ
))m \
mkpart primary xfs $(( ( 2 + UEFIDEVSZ ) + BOOTDEVSZ ))m 100% \
set 1 bios_grub on \
set 2 esp on \
set 3 bls_boot on || \
err_exit "Failed laying down new partition-table"
# Create FS on partitions
err_exit "Creating filesystem on ${CHROOTDEV}${PARTPRE:-}4..." NONE
mkfs -t "${FSTYPE}" "${MKFSFORCEOPT}" -L "${ROOTLABEL}" \
"${CHROOTDEV}${PARTPRE:-}4" || \
err_exit "Failed creating filesystem"
}
function CleanChrootDiskPrtTbl {
local HAS_PARTS
local PART_NUM
local PDEV
HAS_PARTS="$(
parted -s "${CHROOTDEV}" print | \
sed -e '1,/^Number/d' \
-e '/^$/d'
)"
# Ensure there's actually partitions to clear
if [[ -z ${HAS_PARTS:-} ]]
then
echo "Disk has no partitions to clear"
return
fi
# Iteratively nuke partitions from NVMe devices
if [[ ${CHROOTDEV} == "/dev/nvme"* ]]
then
for PDEV in $( blkid | grep "${CHROOTDEV}" | sed 's/:.*$//' )
do
PART_NUM="${PDEV//*p/}"
printf "Deleting partition %s from %s... " "${PART_NUM}" "${CHROOTDEV}"
parted -sf "${CHROOTDEV}" rm "${PART_NUM}"
echo SUCCESS
done
# Iteratively nuke partitions from Xen Virtual Disk devices
elif [[ ${CHROOTDEV} == "/dev/xvd"* ]]
then
for PDEV in $( blkid | grep "${CHROOTDEV}" | sed 's/:.*$//' )
do
PART_NUM="${PDEV//*xvd?/}"
printf "Deleting partition %s from %s... " "${PART_NUM}" "${CHROOTDEV}"
parted -sf "${CHROOTDEV}" rm "${PART_NUM}"
echo SUCCESS
done
fi
# Ask kernel to update its partition-map of target-disk
partprobe "${CHROOTDEV}"
# Null-out any lingering disk structs
err_exit "Clearing existing partition-tables..." NONE
dd if=/dev/zero of="${CHROOTDEV}" bs=512 count=1000 > /dev/null 2>&1 || \
err_exit "Failed clearing existing partition-tables"
# Ask kernel, again, to update its partition-map of target-disk
partprobe "${CHROOTDEV}" || true
}
function SetupBootParts {
# Make filesystem for /boot/efi
err_exit "Creating filesystem on ${CHROOTDEV}${PARTPRE:-}2..." NONE
mkfs -t vfat -n "${LABEL_UEFI}" "${CHROOTDEV}${PARTPRE:-}2" || \
err_exit "Failed creating filesystem"
# Make filesystem for /boot
err_exit "Creating filesystem on ${CHROOTDEV}${PARTPRE:-}3..." NONE
mkfs -t "${FSTYPE}" "${MKFSFORCEOPT}" -L "${LABEL_BOOT}" \
"${CHROOTDEV}${PARTPRE:-}3" || \
err_exit "Failed creating filesystem"
}
######################
## Main program-flow
######################
OPTIONBUFR=$( getopt \
-o b:B:d:f:hl:L:p:r:U:v: \
--long boot-size:,disk:,fstype:,help,label-boot:,label-uefi:,partition-string:,rootlabel:,uefi-size:,vgname: \
-n "${PROGNAME}" -- "$@")
eval set -- "${OPTIONBUFR}"
###################################
# Parse contents of ${OPTIONBUFR}
###################################
while true
do
case "$1" in
-B|--boot-size)
case "$2" in
"")
err_exit "Error: option required but not specified"
shift 2;
exit 1
;;
*)
BOOTDEVSZ=${2}
if [[ ${BOOTDEVSZ} -lt ${BOOTDEVSZMIN} ]]
then
err_exit "Requested size for '/boot' filesystem is too small" 1
fi
shift 2;
;;
esac
;;
-d|--disk)
case "$2" in
"")
err_exit "Error: option required but not specified"
shift 2;
exit 1
;;
*)
CHROOTDEV=${2}
shift 2;
;;
esac
;;
-f|--fstype)
case "$2" in
"")
err_exit "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;
;;
*)
err_exit "Error: unrecognized/unsupported FSTYPE. Aborting..."
shift 2;
exit 1
;;
esac
;;
-h|--help)
UsageMsg 0
;;
-l|--label-boot)
case "$2" in
"")
err_exit "Error: option required but not specified"
shift 2;
exit 1
;;
*)
LABEL_BOOT=${2}
shift 2;
;;
esac
;;
-L|--label-uefi)
case "$2" in
"")
err_exit "Error: option required but not specified"
shift 2;
exit 1
;;
*)
LABEL_UEFI=${2}
shift 2;
;;
esac
;;
-p|--partition-string)
case "$2" in
"")
err_exit "Error: option required but not specified"
shift 2;
exit 1
;;
*)
GEOMETRYSTRING=${2}
shift 2;
;;
esac
;;
-r|--rootlabel)
case "$2" in
"")
err_exit "Error: option required but not specified"
shift 2;
exit 1
;;
*)
ROOTLABEL=${2}
shift 2;
;;
esac
;;
-U|--uefi-size)
case "$2" in
"")
err_exit "Error: option required but not specified"
shift 2;
exit 1
;;
*)
UEFIDEVSZ=${2}
shift 2;
;;
esac
;;
-v|--vgname)
case "$2" in
"")
err_exit "Error: option required but not specified"
shift 2;
exit 1
;;
*)
VGNAME=${2}
shift 2;
;;
esac
;;
--)
shift
break
;;
*)
err_exit "Internal error!"
exit 1
;;
esac
done
# Bail if not root
if [[ ${EUID} != 0 ]]
then
err_exit "Must be root to execute disk-carving actions"
fi
# See if our carve-target is an NVMe
if [[ ${CHROOTDEV} == "UNDEF" ]]
then
err_exit "Failed to specify partitioning-target. Aborting"
elif [[ ${CHROOTDEV} =~ /dev/nvme ]]
then
PARTPRE="p"
else
PARTPRE=""
fi
# Determine how we're formatting the disk
if [[ -z ${ROOTLABEL+xxx} ]] && [[ -n ${VGNAME+xxx} ]]
then
CarveLVM
elif [[ -n ${ROOTLABEL+xxx} ]] && [[ -z ${VGNAME+xxx} ]]
then
CarveBare
elif [[ -z ${ROOTLABEL+xxx} ]] && [[ -z ${VGNAME+xxx} ]]
then
err_exit "Failed to specifiy a partitioning-method. Aborting"
else
err_exit "The '-r'/'--rootlabel' and '-v'/'--vgname' flag-options are mutually-exclusive. Exiting." 0
fi
# Take care of /boot/... paritions
SetupBootParts