forked from plus3it/amigen8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMkChrootTree.sh
executable file
·403 lines (358 loc) · 10.7 KB
/
MkChrootTree.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
#!/bin/bash
set -eu -o pipefail
#
# Setup build-chroot's physical and virtual storage
#
#######################################################################
PROGNAME=$(basename "$0")
CHROOTDEV=""
CHROOTMNT="${CHROOT:-/mnt/ec2-root}"
DEBUG="${DEBUG:-UNDEF}"
DEFGEOMARR=(
/:rootVol:4
swap:swapVol:2
/home:homeVol:1
/var:varVol:2
/var/tmp:varTmpVol:2
/var/log:logVol:2
/var/log/audit:auditVol:100%FREE
)
DEFGEOMSTR="${DEFGEOMSTR:-$( IFS=$',' ; echo "${DEFGEOMARR[*]}" )}"
FSTYPE="${DEFFSTYPE:-xfs}"
GEOMETRYSTRING="${DEFGEOMSTR}"
SAVIFS="${IFS}"
read -ra VALIDFSTYPES <<< "$( awk '!/^nodev/{ print $1}' /proc/filesystems | tr '\n' ' ' )"
# 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
local PART
SCRIPTEXIT="${1:-1}"
(
echo "Usage: ${0} [GNU long option] [option] ..."
echo " Options:"
printf '\t%-4s%s\n' '-d' 'Device to contain the OS partition(s) (e.g., "/dev/xvdf")'
printf '\t%-4s%s\n' '-f' 'Filesystem-type used chroot-dev device(s) (default: "xfs")'
printf '\t%-4s%s\n' '-h' 'Print this message'
printf '\t%-4s%s\n' '-m' 'Where to mount chroot-dev (default: "/mnt/ec2-root")'
printf '\t%-4s%s\n' '-p' 'Comma-delimited string of colon-delimited partition-specs'
printf '\t%-6s%s\n' '' 'Default layout:'
for PART in "${DEFGEOMARR[@]}"
do
printf '\t%-8s%s\n' '' "${PART}"
done
echo " GNU long options:"
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' '--mountpoint' 'See "-m" short-option'
printf '\t%-20s%s\n' '--no-lvm' 'LVM2 objects not used'
printf '\t%-20s%s\n' '--partition-string' 'See "-p" short-option'
)
exit "${SCRIPTEXIT}"
}
# Try to ensure good chroot mount-point exists
function ValidateTgtMnt {
# Ensure chroot mount-point exists
if [[ -d ${CHROOTMNT} ]]
then
if [[ $( mountpoint -q "${CHROOTMNT}" )$? -eq 0 ]]
then
err_exit "Selected mount-point [${CHROOTMNT}] already in use. Aborting."
else
err_exit "Requested mount-point available for use. Proceeding..." NONE
fi
elif [[ -e ${CHROOTMNT} ]] && [[ ! -d ${CHROOTMNT} ]]
then
err_exit "Selected mount-point [${CHROOTMNT}] is not correct type. Aborting"
else
err_exit "Requested mount-point [${CHROOTMNT}] not found. Creating... " NONE
install -Ddm 000755 "${CHROOTMNT}" || \
err_exit "Failed to create mount-point"
err_exit "Succeeded creating mount-point [${CHROOTMNT}]" NONE
fi
}
# Mount VG elements
function DoLvmMounts {
local ELEM
local MOUNTPT
local PARTITIONARRAY
local PARTITIONSTR
local -A MOUNTINFO
PARTITIONSTR="${GEOMETRYSTRING}"
# Convert ${PARTITIONSTR} to iterable partition-info array
IFS=',' read -ra PARTITIONARRAY <<< "${PARTITIONSTR}"
IFS="${SAVIFS}"
# Create associative-array with mountpoints as keys
for ELEM in "${PARTITIONARRAY[@]}"
do
MOUNTINFO[${ELEM//:*/}]=${ELEM#*:}
done
# Ensure all LVM volumes are active
vgchange -a y "${VGNAME}" || err_exit "Failed to activate LVM"
# Mount volumes
for MOUNTPT in $( echo "${!MOUNTINFO[*]}" | tr " " "\n" | sort )
do
# Ensure mountpoint exists
if [[ ! -d ${CHROOTMNT}/${MOUNTPT} ]]
then
install -dDm 000755 "${CHROOTMNT}/${MOUNTPT}"
fi
# Mount the filesystem
if [[ ${MOUNTPT} == /* ]]
then
err_exit "Mounting '${CHROOTMNT}${MOUNTPT}'..." NONE
mount -t "${FSTYPE}" "/dev/${VGNAME}/${MOUNTINFO[${MOUNTPT}]//:*/}" \
"${CHROOTMNT}${MOUNTPT}" || \
err_exit "Unable to mount /dev/${VGNAME}/${MOUNTINFO[${MOUNTPT}]//:*/}"
else
err_exit "Skipping '${MOUNTPT}'..." NONE
fi
done
}
# mount /boot and /boot/efi partitions
function MountBootFSes {
# Create /boot mountpoint as needed
if [[ ! -d "${CHROOTMNT}/boot" ]]
then
mkdir "${CHROOTMNT}/boot"
fi
# Mount BIOS-boot partition
mount -t "${FSTYPE}" "${CHROOTDEV}${PARTPRE}3" "${CHROOTMNT}/boot"
# Create /boot/efi mountpoint as needed
if [[ ! -d "${CHROOTMNT}/boot/efi" ]]
then
mkdir "${CHROOTMNT}/boot/efi"
fi
# Mount UEFI-boot partition
mount -t vfat "${CHROOTDEV}${PARTPRE}2" "${CHROOTMNT}/boot/efi"
}
# Create block/character-special files
function PrepSpecialDevs {
local BINDDEV
local -a CHARDEVS
local DEVICE
local DEVMAJ
local DEVMIN
local DEVPRM
local DEVOWN
CHARDEVS=(
/dev/null:1:3:000666
/dev/zero:1:5:000666
/dev/random:1:8:000666
/dev/urandom:1:9:000666
/dev/tty:5:0:000666:tty
/dev/console:5:1:000600
/dev/ptmx:5:2:000666:tty
)
# Prep for loopback mounts
mkdir -p "${CHROOTMNT}"/{proc,sys,dev/{pts,shm}}
# Create character-special files
for DEVSTR in "${CHARDEVS[@]}"
do
DEVICE=$( cut -d: -f 1 <<< "${DEVSTR}" )
DEVMAJ=$( cut -d: -f 2 <<< "${DEVSTR}" )
DEVMIN=$( cut -d: -f 3 <<< "${DEVSTR}" )
DEVPRM=$( cut -d: -f 4 <<< "${DEVSTR}" )
DEVOWN=$( cut -d: -f 5 <<< "${DEVSTR}" )
# Create any missing device-nodes as needed
if [[ -e ${CHROOTMNT}${DEVICE} ]]
then
err_exit "${CHROOTMNT}${DEVICE} exists" NONE
else
err_exit "Making ${CHROOTMNT}${DEVICE}... " NONE
mknod -m "${DEVPRM}" "${CHROOTMNT}${DEVICE}" c "${DEVMAJ}" "${DEVMIN}" || \
err_exit "Failed making ${CHROOTMNT}${DEVICE}"
# Set an alternate group-owner where appropriate
if [[ ${DEVOWN:-} != '' ]]
then
err_exit "Setting ownership on ${CHROOTMNT}${DEVICE}..." NONE
chown root:"${DEVOWN}" "${CHROOTMNT}${DEVICE}" || \
err_exit "Failed setting ownership on ${CHROOTMNT}${DEVICE}..."
fi
fi
done
# Bind-mount pseudo-filesystems
grep -v "${CHROOTMNT}" /proc/mounts | \
sed '{
/^none/d
/\/tmp/d
/rootfs/d
/dev\/sd/d
/dev\/xvd/d
/dev\/nvme/d
/\/user\//d
/\/mapper\//d
/^cgroup/d
}' | awk '{ print $2 }' | sort -u | while read -r BINDDEV
do
# Create mountpoints in chroot-env
if [[ ! -d ${CHROOTMNT}${BINDDEV} ]]
then
err_exit "Creating mountpoint: ${CHROOTMNT}${BINDDEV}" NONE
install -Ddm 000755 "${CHROOTMNT}${BINDDEV}" || \
err_exit "Failed creating mountpoint: ${CHROOTMNT}${BINDDEV}"
fi
err_exit "Mounting ${CHROOTMNT}${BINDDEV}..." NONE
mount -o bind "${BINDDEV}" "${CHROOTMNT}${BINDDEV}" || \
err_exit "Failed mounting ${CHROOTMNT}${BINDDEV}"
done
}
######################
## Main program-flow
######################
OPTIONBUFR=$( getopt \
-o d:f:hm:p: \
--long disk:,fstype:,help,mountpoint:,no-lvm,partition-string: \
-n "${PROGNAME}" -- "$@")
eval set -- "${OPTIONBUFR}"
###################################
# Parse contents of ${OPTIONBUFR}
###################################
while true
do
case "$1" in
-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
;;
*)
FSTYPE="${2}"
if [[ $( grep -qw "${FSTYPE}" <<< "${VALIDFSTYPES[*]}" ) -ne 0 ]]
then
err_exit "Invalid fstype [${FSTYPE}] requested"
fi
shift 2;
;;
esac
;;
-h|--help)
UsageMsg 0
;;
-m|--mountpoint)
case "$2" in
"")
err_exit "Error: option required but not specified"
shift 2;
exit 1
;;
*)
CHROOTMNT=${2}
shift 2;
;;
esac
;;
--no-lvm)
NOLVM="true"
shift 1;
;;
-p|--partition-string)
case "$2" in
"")
err_exit "Error: option required but not specified"
shift 2;
exit 1
;;
*)
GEOMETRYSTRING=${2}
shift 2;
;;
esac
;;
--)
shift
break
;;
*)
err_exit "Internal error!"
exit 1
;;
esac
done
# *MUST* supply a disk-device
if [[ -z ${CHROOTDEV:-} ]]
then
err_exit "No block-device specified. Aborting"
elif [[ ! -b ${CHROOTDEV} ]]
then
err_exit "No such block-device [${CHROOTDEV}]. Aborting"
else
if [[ ${CHROOTDEV} =~ /dev/nvme ]]
then
PARTPRE="p"
else
PARTPRE=""
fi
fi
# Ensure build-target mount-hierarchy is available
ValidateTgtMnt
## Mount partition(s) from second slice
if [[ -d /sys/firmware/efi ]]
then
# Locate LVM2 volume-group name (EFI)
read -r VGNAME <<< "$( pvs --noheading -o vg_name "${CHROOTDEV}${PARTPRE}4" )"
else
# Locate LVM2 volume-group name (no EFI)
read -r VGNAME <<< "$( pvs --noheading -o vg_name "${CHROOTDEV}${PARTPRE}2" )"
fi
# Do partition-mount if 'no-lvm' explicitly requested
if [[ ${NOLVM:-} == "true" ]]
then
mount -t "${FSTYPE}" "${CHROOTDEV}${PARTPRE}2" "${CHROOTMNT}"
# Bail if not able to find a LVM2 vg-name
elif [[ -z ${VGNAME:-} ]]
then
err_exit "No LVM2 volume group found on ${CHROOTDEV}${PARTPRE}2 and" NONE
err_exit "The '--no-lvm' option not set. Aborting"
# Attempt mount of LVM2 volumes
else
DoLvmMounts
fi
# Mount BIOS and UEFI boot-devices as needed
if [[ -d /sys/firmware/efi ]]
then
MountBootFSes
fi
# Make block/character-special files
PrepSpecialDevs