-
Notifications
You must be signed in to change notification settings - Fork 14
/
DiskSetup.sh
executable file
·579 lines (529 loc) · 17.5 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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
#!/bin/bash
set -eu -o pipefail
#
# Script to automate basic setup of CHROOT device
#
#################################################################
PROGNAME=$(basename "$0")
BOOTDEVSZMIN="1024"
BOOTDEVSZ="${BOOTDEVSZ:-${BOOTDEVSZMIN}}"
UEFIDEVSZ="${UEFIDEVSZ:-100}"
CHROOTDEV="${CHROOTDEV:-UNDEF}"
DEBUG="${DEBUG:-UNDEF}"
FSTYPE="${FSTYPE:-xfs}"
LABEL_BOOT="${LABEL_BOOT:-boot_disk}"
LABEL_UEFI="${LABEL_UEFI:-UEFI_DISK}"
# Make interactive-execution more-verbose unless explicitly told not to
if [[ $( tty -s ) -eq 0 ]] && [[ ${DEBUG} == "UNDEF" ]]
then
DEBUG="true"
fi
# Error handler function
function err_exit {
local ERRSTR
local ISNUM
local SCRIPTEXIT
ERRSTR="${1}"
ISNUM='^[0-9]+$'
SCRIPTEXIT="${2:-1}"
if [[ ${DEBUG} == true ]]
then
# Our output channels
logger -i -t "${PROGNAME}" -p kern.crit -s -- "${ERRSTR}"
else
logger -i -t "${PROGNAME}" -p kern.crit -- "${ERRSTR}"
fi
# Only exit if requested exit is numerical
if [[ ${SCRIPTEXIT} =~ ${ISNUM} ]]
then
exit "${SCRIPTEXIT}"
fi
}
# 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' 'Size of /boot partition (default/minimum: 1024MiB)'
printf '\t%-4s%s\n' '-B' 'Boot-block size (default: 16MiB)'
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' 'Filesystem label for /boot partition (default: boot_disk)'
printf '\t%-4s%s\n' '-L' 'Filesystem label for /boot/efi partition (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' '-U' 'Size of /boot/efi partition (default: 100MiB)'
printf '\t%-4s%s\n' '-v' 'Name assigned to root volume-group (default: VolGroup00)'
echo " GNU long options:"
printf '\t%-20s%s\n' '--bootprt-size' 'See "-b" short-option'
printf '\t%-20s%s\n' '--bootblk-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 (no EFI)
function CarveLVM_Standard {
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 MBR and partition table
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"
# Lay down the base partitions
err_exit "Laying down new partition-table..." NONE
parted -s "${CHROOTDEV}" -- mktable gpt \
mkpart primary "${FSTYPE}" 2048s "${BOOTBLKSZ}m" \
mkpart primary "${FSTYPE}" "${BOOTBLKSZ}m" 100% \
set 1 bios_grub on \
set 2 lvm || \
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:-}2..." NONE
pvcreate "${CHROOTDEV}${PARTPRE:-}2" || \
err_exit "PV creation failed. Aborting!"
fi
# Create root VolumeGroup
err_exit "Creating LVM2 volume-group ${VGNAME}..." NONE
vgcreate -y "${VGNAME}" "${CHROOTDEV}${PARTPRE:-}2" || \
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 as LVM (with EFI)
function CarveLVM_Efi {
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 MBR and partition table
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"
# 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 4 lvm on || \
err_exit "Failed laying down new partition-table"
## Create LVM objects
# 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 (no EFI)
function CarveBare_Standard {
# Clear the MBR and partition table
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"
# Lay down the base partitions
err_exit "Laying down new partition-table..." NONE
parted -s "${CHROOTDEV}" -- mklabel gpt \
mkpart primary "${FSTYPE}" 2048s "${BOOTBLKSZ}m" \
mkpart primary "${FSTYPE}" "${BOOTBLKSZ}m" 100% \
set 1 bios_grub on || \
err_exit "Failed laying down new partition-table"
# Create FS on partitions
err_exit "Creating filesystem on ${CHROOTDEV}${PARTPRE:-}2..." NONE
mkfs -t "${FSTYPE}" "${MKFSFORCEOPT}" -L "${ROOTLABEL}" \
"${CHROOTDEV}${PARTPRE:-}2" || \
err_exit "Failed creating filesystem"
}
# Partition with no LVM (with EFI)
function CarveBare_Efi {
# Clear the MBR and partition table
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"
# 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 || \
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 SetupBootParts_Efi {
# 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 bootlabel:,bootblk-size:,bootprt-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|--bootprt-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
;;
-B|--bootblk-size)
case "$2" in
"")
err_exit "Error: option required but not specified"
shift 2;
exit 1
;;
*)
BOOTBLKSZ=${2}
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 [[ -d /sys/firmware/efi ]]
then
if [[ -z ${ROOTLABEL:-} ]] && [[ -n ${VGNAME:-} ]]
then
CarveLVM_Efi
elif [[ -n ${ROOTLABEL:-} ]] && [[ -z ${VGNAME:-} ]]
then
CarveBare_Efi
elif [[ -z ${ROOTLABEL:-} ]] && [[ -z ${VGNAME:-} ]]
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
SetupBootParts_Efi
else
if [[ -z ${ROOTLABEL:-} ]] && [[ -n ${VGNAME:-} ]]
then
CarveLVM_Standard
elif [[ -n ${ROOTLABEL:-} ]] && [[ -z ${VGNAME:-} ]]
then
CarveBare_Standard
elif [[ -z ${ROOTLABEL:-} ]] && [[ -z ${VGNAME:-} ]]
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
fi