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 28
/
Copy pathMkChrootTree.sh
executable file
·338 lines (296 loc) · 9.24 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
#!/bin/bash
# shellcheck disable=SC2015,SC2207
#
# Setup/mount chroot'ed volumes/partitions
# * Takes the dev-path hosting the /boot and LVM partitions as argument
#
#######################################################################
PROGNAME=$(basename "$0")
CHROOTDEV=${1:-UNDEF}
CHROOTMNT="${AMIGENCHROOT:-/mnt/ec2-root}"
FSTYPE="${DEFFSTYPE:-ext4}"
DEFGEOMARR=(
/:rootVol:4
swap:swapVol:2
/home:homeVol:1
/var:varVol:2
/var/log:logVol:2
/var/log/audit:auditVol:100%FREE
)
DEFGEOMSTR="${DEFGEOMSTR:-$( IFS=$',' ; echo "${DEFGEOMARR[*]}" )}"
GEOMETRYSTRING="${DEFGEOMSTR}"
# 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: "ext4")'
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' '--partition-string' 'See "-p" short-option'
)
exit "${SCRIPTEXIT}"
}
# Generic logging outputter - extend to increase output destinations
function err_out() {
echo "${2}"
exit "${1}"
}
# Set up base mounts using info from SORTEDARRAY
function FlexMount {
local ELEM
local MOUNTINFO
local MOUNTPT
local PARTITIONARRAY
local PARTITIONSTR
declare -A MOUNTINFO
PARTITIONSTR="${GEOMETRYSTRING}"
# Convert ${PARTITIONSTR} to iterable partition-info array
IFS=',' read -r -a PARTITIONARRAY <<< "${PARTITIONSTR}"
unset IFS
# 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_out 2 "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
echo "Mounting '${CHROOTMNT}${MOUNTPT}'..."
mount -t "${FSTYPE}" "/dev/${VGNAME}/${MOUNTINFO[${MOUNTPT}]//:*/}" \
"${CHROOTMNT}${MOUNTPT}" || \
err_out 1 "Unable to mount /dev/${VGNAME}/${MOUNTINFO[${MOUNTPT}]//:*/}"
else
echo "Skipping '${MOUNTPT}'..."
fi
done
# Ensure next-level mountpoints in / all exist
mkdir -p "${CHROOTMNT}"/{var,opt,home,boot,etc} || err_out 3 "Mountpoint Create Failed"
# Ensure next-level mountpoints in /var all exist
mkdir -p "${CHROOTMNT}"/var/{cache,log,lib/{,rpm},tmp}
# Ensure /var/run is a link to /run
if [[ -L /var/run ]]
then
(
cd "${CHROOTMNT}"/var/ &&
ln -s ../run run
)
if [[ $( readlink "${CHROOTMNT}/var/run" )$? -eq 1 ]]
then
echo "************************************************"
echo "** WARNING: /var/run is not a symlink to /run **"
echo "************************************************"
fi
fi
# Mount the boot-root
echo "Mounting ${BOOTDEV} to ${CHROOTMNT}/boot"
mount "${BOOTDEV}" "${CHROOTMNT}/boot/" || err_out 2 "Mount Failed"
}
##########
## MAIN ##
##########
OPTIONBUFR=$( getopt \
-o d:f:hm:p: \
--long disk:,fstype:,help,mountpoint:,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
;;
-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
# Set a partition-prefix as necessary
if [[ ${CHROOTDEV} =~ /dev/nvme ]]
then
PARTPRE="p"
else
PARTPRE=""
fi
BOOTDEV=${CHROOTDEV}${PARTPRE}1
LVMDEV=${CHROOTDEV}${PARTPRE}2
# Can't do anything if we don't have an EBS to operate on
if [[ ${CHROOTDEV} = UNDEF ]]
then
err_out 1 "Must supply name of device to use (e.g., /dev/xvdg)"
fi
if [ -d "${CHROOTMNT}" ]
then
echo "Found ${CHROOTMNT}: proceeding..."
elif [ -e "${CHROOTMNT}" ] && [ ! -d "${CHROOTMNT}" ]
then
err_out 1 "Found ${CHROOTMNT} but it's not usable as mount-point"
else
printf "Requested chroot [%s] not found. Attempting to create... " "${CHROOTMNT}"
install -d -m 0755 "${CHROOTMNT}" || err_out 1 "Failed to create ${CHROOTMNT}."
echo "Success!"
fi
# Ensure that we can find mountable LVM objects
VGNAME=$(lsblk -i -o NAME,TYPE "${LVMDEV}" | grep -w lvm | \
sed 's/^ *.-//' | cut -d "-" -f 1 | uniq)
# Mount filesystems
if [[ ${#VGNAME} -gt 0 ]]
then
# Offload mounting LVM2 objects to function
FlexMount
else
########################################################
## NOTE: This section assumes a simple, two-partition ##
## disk with "/boot" on primary-partition 1 and ##
## "/" on primary-partition 2. This script also ##
## assumes that each partition is labeled. ##
########################################################
MNTPTS=(/boot /)
IFS=$'\n'; PARTS=( $(lsblk -i "${CHROOTDEV}" | awk '/ part *$/{ print $1}' | \
sed 's/^.-//') )
# Iterate partitions and mount
for (( IDX=${#PARTS[@]}-1 ; IDX>=0 ; IDX-- ))
do
# Get partition-label
if [[ ${FSTYPE} == ext3 ]] ||
[[ ${FSTYPE} == ext4 ]]
then
LABEL=$(e2label "/dev/${PARTS[IDX]}")
elif [[ ${FSTYPE} == xfs ]]
then
LABEL=$( xfs_admin -l "/dev/${PARTS[IDX]}" | sed -e 's/"$//' -e 's/^.*"//' )
else
err_out 1 "Unable to determine fstype of /dev/${PARTS[IDX]}"
fi
# Ensure mount-point exists
if [[ ! -d ${CHROOTMNT}${MNTPTS[IDX]} ]]
then
mkdir -p "${CHROOTMNT}${MNTPTS[IDX]}"
fi
# Mount partition by label
mount LABEL="${LABEL}" "${CHROOTMNT}${MNTPTS[IDX]}"
done
fi
# Prep for loopback mounts
mkdir -p "${CHROOTMNT}"/{proc,sys,dev/{pts,shm}}
# Create base dev-nodes
mknod -m 600 "${CHROOTMNT}"/dev/console c 5 1
mknod -m 666 "${CHROOTMNT}"/dev/null c 1 3
mknod -m 666 "${CHROOTMNT}"/dev/zero c 1 5
mknod -m 666 "${CHROOTMNT}"/dev/random c 1 8
mknod -m 666 "${CHROOTMNT}"/dev/urandom c 1 9
mknod -m 666 "${CHROOTMNT}"/dev/tty c 5 0
mknod -m 666 "${CHROOTMNT}"/dev/ptmx c 5 2
chown root:tty "${CHROOTMNT}"/dev/ptmx
# Bind-mount everything else
BINDSOURCES=( $(grep -v "${CHROOTMNT}" /proc/mounts | sed '{
/^none/d
/\/tmp/d
/rootfs/d
/\/ /d
/\/boot /d
/dev\/xvd/d
/dev\/nvme/d
/\/user\//d
/\/mapper\//d
/^cgroup/d
}' | awk '{print $2}' | sort -u) )
for MOUNT in "${BINDSOURCES[@]}"
do
if [[ ! -d ${CHROOTMNT}${MOUNT} ]]
then
mkdir -p "${CHROOTMNT}${MOUNT}" && \
echo "Creating ${CHROOTMNT}${MOUNT}" || break
fi
echo "Bind-mounting ${MOUNT} to ${CHROOTMNT}${MOUNT}"
mount -o bind "${MOUNT}" "${CHROOTMNT}${MOUNT}"
done