-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathinstall.sh
executable file
·578 lines (561 loc) · 23.9 KB
/
install.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
#! /bin/bash
PKG_DIR="${PWD}"
TEMP_DIR="${PKG_DIR}/temp"
MNT_DIR="${TEMP_DIR}/mnt"
LINUX_REQD_TOOLS="dd md5sum sha256sum grep egrep blockdev lsblk findfs tar"
MACOSX_REQD_TOOLS="dd md5 shasum cat cut sed diff grep egrep diskutil tar"
MD5_CHECKSUM="dist_md5sum.txt"
SHA256_CHECKSUM="dist_sha256sum.txt"
LINUX="LINUX"
MACOSX="MACOSX"
SYNC_LOCK="/var/lock/conf_sync"
WIPE_TRAN_TYPES="sata|sas|nvme"
# Optional installation block device path parameter
install_dev="${1}"
# Optional installation device transport type parameter
install_tran="${2}"
# Optional installation device model string parameter (can be an egrep pattern)
install_model="${3}"
echo "*** $(cat VERSION) Install Script ***" && echo
# Need root privileges for installer
if [ "x$(whoami)" != "xroot" ]; then
echo "### This installer requires root privileges."
exit 1
fi
# What operating system is this
if [ "x$(uname -s)" = "xLinux" ]; then
this_os="${LINUX}"
elif [ "x$(uname -s)" = "xDarwin" ]; then
this_os="${MACOSX}"
else
echo "ERROR: Only Linux and Mac OS X are supported with this script!" 1>&2
exit 1
fi
# Make sure the required tools/utilities are available
echo "### Checking for required tools..."
reqd_tools=${this_os}_REQD_TOOLS
for i in ${!reqd_tools}; do
if ! which ${i} > /dev/null 2>&1; then
echo "ERROR: The '${i}' utility is required to use this" \
"installation script." 1>&2
exit 1
fi
done
echo
# Checksums
echo "### Verifying checksums..."
if [ "${this_os}" = "${LINUX}" ]; then
md5sum -w -c ${MD5_CHECKSUM} || exit 1
sha256sum -w -c ${SHA256_CHECKSUM} || exit 1
elif [ "${this_os}" = "${MACOSX}" ]; then
chksum_img_file="$(cat ${MD5_CHECKSUM} | cut -d' ' -f2 | sed -e 's/\*//')"
if md5 -r ${chksum_img_file} | sed 's/ / */' | \
diff ${MD5_CHECKSUM} - > /dev/null 2>&1; then
echo "${chksum_img_file}: OK"
else
echo "${chksum_img_file}: The MD5 checksum doesn't match!"
exit 1
fi
shasum -a 256 -c ${SHA256_CHECKSUM} || exit 1
fi
echo
# Locate the image file
image_file="$(ls *.img.tar.bz2)" || exit 1
# Check if we're doing an upgrade
if test -f "/etc/esos-release" && test -z "${install_dev}" && \
test -z "${install_tran}" && test -z "${install_model}" && \
! grep esos_iso /proc/cmdline > /dev/null 2>&1; then
# Prevent conf_sync.sh from running
exec 200> "${SYNC_LOCK}"
flock --timeout 300 -E 200 -x 200
RC=${?}
if [ ${RC} -ne 0 ]; then
echo "ERROR: Could not acquire conf_sync lock (RC=${RC})!" 1>&2
exit ${RC}
fi
while true; do
# Look up the ESOS block device node
esos_root="$(findfs LABEL=esos_root)"
if [ ${?} -ne 0 ]; then
echo "ERROR: We couldn't find the LABEL=esos_root" \
"file system!" 1>&2
exit 1
fi
# If the extracted package resides in /tmp we may need to increase size
if echo "${PKG_DIR}" | grep -q -E '^/tmp'; then
curr_tmp_size="$(df --block-size=1 --output=size /tmp | tail -1)"
if [ "${curr_tmp_size}" -lt "6442450944" ]; then
echo "### Increasing the /tmp file system..."
mount -o remount,size=6G /tmp || exit 1
echo
fi
fi
echo "### Extracting the image file..."
mkdir -p ${TEMP_DIR} || exit 1
extracted_img="${TEMP_DIR}/$(basename ${image_file} .tar.bz2)"
tar xfj ${image_file} -C ${TEMP_DIR}/ --sparse || exit 1
echo
# We only support upgrading if using a MD RAID boot drive
if echo ${esos_root} | grep -q "/dev/md"; then
using_md=1
else
using_md=0
fi
if [ ${using_md} -eq 1 ]; then
# Set block device variables for below
esos_blk_root="$(findfs LABEL=esos_root)"
esos_blk_boot="$(findfs LABEL=ESOS_BOOT)"
if [ -n "${FORCE_UPGRADE}" ]; then
confirm="Y"
else
# Get confirmation for an upgrade (only option for MD boot)
echo "### This installation script is running on a live ESOS" \
"host. We've detected ESOS is using a MD RAID boot drive." \
"Upgrading in-place is the only supported install option," \
"please type 'yes' to continue the upgrade. If you" \
"decline, this installation script will exit." && \
read confirm
echo
fi
else
if [ -z "${FORCE_UPGRADE}" ]; then
# Make sure the image disk label and the ESOS boot drive match
esos_blk_dev="$(echo ${esos_root} | sed -e '/\/dev\/sd/s/2//' \
-e '/\/dev\/nvme/s/p2//' -e '/\/dev\/vd/s/2//')"
image_mbr="$(mktemp -u -t mbr.XXXXXX)" || exit 1
disk_parts="$(mktemp -u -t disk_parts.XXXXXX)" || exit 1
image_parts="$(mktemp -u -t image_parts.XXXXXX)" || exit 1
dd if=${extracted_img} of=${image_mbr} bs=512 count=1 > \
/dev/null 2>&1 || exit 1
fdisk -u -l ${esos_blk_dev} | egrep "^${esos_blk_dev}" | \
sed -e 's/\*//' | awk '{ print $2 }' > ${disk_parts}
fdisk -u -l ${image_mbr} | egrep "^${image_mbr}" | \
sed -e 's/\*//' | awk '{ print $2 }' > ${image_parts}
if ! diff ${disk_parts} ${image_parts} > /dev/null 2>&1; then
echo "### The image file and current ESOS disk labels" \
"do not match! An in-place upgrade is not supported," \
"continuing..." 1>&2
echo
rm -f ${image_mbr} ${disk_parts} ${image_parts}
break
fi
fi
if echo ${esos_blk_dev} | grep -q "/dev/nvme"; then
# For NVMe drives
esos_blk_root="${esos_blk_dev}p2"
esos_blk_boot="${esos_blk_dev}p1"
elif echo ${esos_blk_dev} | grep -q "/dev/vd"; then
# For VirtIO drives
esos_blk_root="${esos_blk_dev}2"
esos_blk_boot="${esos_blk_dev}1"
else
# For SCSI drives
esos_blk_root="${esos_blk_dev}2"
esos_blk_boot="${esos_blk_dev}1"
fi
if [ -n "${FORCE_UPGRADE}" ]; then
confirm="Y"
else
# Get confirmation for an upgrade
echo "### This installation script is running on a live ESOS" \
"host. Okay to perform an in-place upgrade (yes/no)? Log" \
"file system data will persist and you will not be" \
"prompted to install propietary CLI tools. If you" \
"decline, a full installation will continue." && \
read confirm
echo
fi
fi
if [[ ${confirm} =~ [Yy]|[Yy][Ee][Ss] ]]; then
if grep -q esos_persist /proc/cmdline; then
usb_esos_root="/mnt/root"
usb_esos_boot="/boot"
else
echo "### Mounting the ESOS boot drive file systems..."
usb_esos_root="${TEMP_DIR}/old_esos_root"
usb_esos_boot="${TEMP_DIR}/old_esos_boot"
mkdir -p ${usb_esos_root} || exit 1
mkdir -p ${usb_esos_boot} || exit 1
mount ${esos_blk_root} ${usb_esos_root} || exit 1
mount ${esos_blk_boot} ${usb_esos_boot} || exit 1
echo
fi
echo "### Mounting the image file partitions..."
img_esos_root="${TEMP_DIR}/new_esos_root"
img_esos_boot="${TEMP_DIR}/new_esos_boot"
mkdir -p ${img_esos_root} || exit 1
mkdir -p ${img_esos_boot} || exit 1
loop_dev="$(losetup -f)"
losetup ${loop_dev} ${extracted_img} || exit 1
kpartx -a -s ${loop_dev} || exit 1
boot_dev="$(echo ${loop_dev} | sed 's/^\/dev/\/dev\/mapper/')p1"
root_dev="$(echo ${loop_dev} | sed 's/^\/dev/\/dev\/mapper/')p2"
mount ${root_dev} ${img_esos_root} || exit 1
mount ${boot_dev} ${img_esos_boot} || exit 1
# Ensure the root FS has space for both image slots
new_root_img_bytes="$(stat -c "%s" \
"${img_esos_root}/PRIMARY-root.sqsh")"
if [ -f "${usb_esos_root}/PRIMARY-root.cpio.bz2" ]; then
curr_pri_img_file="${usb_esos_root}/PRIMARY-root.cpio.bz2"
else
curr_pri_img_file="${usb_esos_root}/PRIMARY-root.sqsh"
fi
curr_pri_img_bytes="$(stat -c "%s" "${curr_pri_img_file}")"
both_img_bytes="$(expr "${new_root_img_bytes}" + \
"${curr_pri_img_bytes}")"
curr_usb_root_size="$(df --block-size=1 --output=size \
"${usb_esos_root}" | tail -1)"
if [ "${both_img_bytes}" -lt "${curr_usb_root_size}" ]; then
# Both images will fit, proceed with PRIMARY/SECONDARY setup
echo
echo "### Moving the current primary image to the" \
"secondary slot..."
usb_ver="$(cat ${usb_esos_boot}/PRIMARY-version | cut -d= -f2)"
mv -f ${usb_esos_boot}/PRIMARY-version \
${usb_esos_boot}/SECONDARY-version || exit 1
mv -f ${usb_esos_boot}/PRIMARY-initramfs.cpio.gz \
${usb_esos_boot}/SECONDARY-initramfs.cpio.gz || exit 1
mv -f ${usb_esos_boot}/PRIMARY-bzImage-esos.prod \
${usb_esos_boot}/SECONDARY-bzImage-esos.prod || exit 1
mv -f ${usb_esos_boot}/PRIMARY-bzImage-esos.debug \
${usb_esos_boot}/SECONDARY-bzImage-esos.debug || exit 1
# Handle the root cpio -> squashfs transition
if [ -f "${usb_esos_root}/PRIMARY-root.sqsh" ]; then
mv -f ${usb_esos_root}/PRIMARY-root.sqsh \
${usb_esos_root}/SECONDARY-root.sqsh || exit 1
# There might be an old cpio archive in the SECONDARY slot
if [ -f "${usb_esos_root}/SECONDARY-root.cpio.bz2" ]; then
rm -f ${usb_esos_root}/SECONDARY-root.cpio.bz2 || exit 1
fi
fi
if [ -f "${usb_esos_root}/PRIMARY-root.cpio.bz2" ]; then
mv -f ${usb_esos_root}/PRIMARY-root.cpio.bz2 \
${usb_esos_root}/SECONDARY-root.cpio.bz2 || exit 1
fi
else
# Only one image will fit, zap SECONDARY + overwrite PRIMARY
echo
echo "### Removing secondary slot image (if any)..."
rm -f "${usb_esos_boot}/SECONDARY-version" || exit 1
rm -f "${usb_esos_boot}/SECONDARY-initramfs.cpio.gz" || exit 1
rm -f "${usb_esos_boot}/SECONDARY-bzImage-esos.prod" || exit 1
rm -f "${usb_esos_boot}/SECONDARY-bzImage-esos.debug" || exit 1
rm -f "${usb_esos_root}/SECONDARY-root.sqsh" || exit 1
# There might be an old cpio archive in the PRIMARY slot
rm -f "${usb_esos_root}/PRIMARY-root.cpio.bz2" || exit 1
# There might be an old cpio archive in the SECONDARY slot
rm -f "${usb_esos_root}/SECONDARY-root.cpio.bz2" || exit 1
fi
echo
echo "### Copying the new image to the primary slot..."
img_ver="$(cat ${img_esos_boot}/PRIMARY-version | cut -d= -f2)"
cp -fp ${img_esos_boot}/PRIMARY-version \
${usb_esos_boot}/PRIMARY-version || exit 1
cp -fp ${img_esos_boot}/PRIMARY-initramfs.cpio.gz \
${usb_esos_boot}/PRIMARY-initramfs.cpio.gz || exit 1
cp -fp ${img_esos_boot}/PRIMARY-bzImage-esos.prod \
${usb_esos_boot}/PRIMARY-bzImage-esos.prod || exit 1
cp -fp ${img_esos_boot}/PRIMARY-bzImage-esos.debug \
${usb_esos_boot}/PRIMARY-bzImage-esos.debug || exit 1
cp -fp ${img_esos_root}/PRIMARY-root.sqsh \
${usb_esos_root}/PRIMARY-root.sqsh || exit 1
echo
echo "### Cleaning up..."
umount ${img_esos_boot} || exit 1
umount ${img_esos_root} || exit 1
if ! grep -q esos_persist /proc/cmdline; then
umount ${usb_esos_boot} || exit 1
umount ${usb_esos_root} || exit 1
fi
sync
if ! kpartx -d ${loop_dev}; then
sleep 5
kpartx -v -d ${loop_dev} || exit 1
fi
if ! losetup -d ${loop_dev}; then
sleep 5
losetup -v -d ${loop_dev} || exit 1
fi
rm -rf ${TEMP_DIR}
echo
echo "### The ESOS upgrade succeeded! Here are the details:"
echo "Primary slot version: ${img_ver}"
if [ -n "${usb_ver}" ]; then
echo "Secondary slot version: ${usb_ver}"
fi
exit 0
else
if [ ${using_md} -eq 1 ]; then
exit 1
else
break
fi
fi
done
fi
# For new installs directly on ESOS, we may want to wipe all devices first
if [ -f "/etc/esos-release" ] && [ "x${WIPE_DEVS}" = "x1" ]; then
while : ; do
if [ -n "${NO_PROMPT}" ] && [ ${NO_PROMPT} -eq 1 ]; then
# Don't get confirmation, print a warning and continue
confirm="Y"
echo "NO_PROMPT=1 is set, continuing..."
echo
else
echo "### Okay to wipe all block devices matching transport" \
"types '${WIPE_TRAN_TYPES}' (yes/no)?" && read confirm
echo
fi
if [[ ${confirm} =~ [Yy]|[Yy][Ee][Ss] ]]; then
while read -r line; do
blk_dev="$(echo ${line} | awk '{print $1}')"
if [ -n "${blk_dev}" ]; then
echo "### Attempting to wipe '${blk_dev}' via" \
"'blkdiscard'..."
if ! blkdiscard --force ${blk_dev}; then
echo "WARNING: Discarding device sectors failed," \
"attempting to wipe any residual metadata" \
"using 'dd'..."
dd if=/dev/zero of=${blk_dev} bs=1M count=128
echo
else
echo
fi
fi
done <<< "$(lsblk -p -d -o NAME,TYPE,TRAN | \
grep -E "${WIPE_TRAN_TYPES}\$")"
break
elif [[ ${confirm} =~ [Nn]|[Nn][Oo] ]]; then
echo "WARNING: Not wiping block devices may result in first" \
"boot issues!"
echo
break
fi
done
fi
if [ -z "${install_dev}" ] && [ -z "${install_tran}" ] && \
[ -z "${install_model}" ]; then
# Print out a list of disk devices
echo "### Here is a list of disk devices on this machine:"
if [ "${this_os}" = "${LINUX}" ]; then
lsblk --nodeps --paths --exclude 1,11,251,252 \
--output NAME,VENDOR,MODEL,REV,SIZE,TRAN,SUBSYSTEMS
elif [ "${this_os}" = "${MACOSX}" ]; then
diskutil list
fi
echo
fi
# Get desired install target device node and perform a few checks
if [ -n "${install_dev}" ]; then
dev_node="${install_dev}"
echo "### Using block device '${dev_node}' given via argument..."
echo
elif [ -n "${install_tran}" ]; then
if [ -n "${install_model}" ]; then
# Using device transport + model to locate install target
tran_model_dev=$(lsblk -p -d -o NAME,TYPE,MODEL,TRAN | \
grep "${install_tran}\$" | grep -E "${install_model}" | \
head -1 | awk '{print $1}')
if [ "x${tran_model_dev}" = "x" ]; then
echo "ERROR: Unable to resolve any devices for transport" \
"'${install_tran}' and model '${install_model}'." 1>&2
exit 1
fi
dev_node="${tran_model_dev}"
echo "### Using block device '${dev_node}' resolved via" \
"transport '${install_tran}' and" \
"model '${install_model}' arguments..."
echo
else
# Only use the device transport type to locate install target
tran_dev=$(lsblk -p -d -o NAME,TYPE,TRAN | grep "${install_tran}\$" | \
head -1 | awk '{print $1}')
if [ "x${tran_dev}" = "x" ]; then
echo "ERROR: Unable to resolve any devices for transport" \
"'${install_tran}'." 1>&2
exit 1
fi
dev_node="${tran_dev}"
echo "### Using block device '${dev_node}' resolved via" \
"transport '${install_tran}' argument..."
echo
fi
elif [ -n "${install_model}" ]; then
model_dev=$(lsblk -p -d -o NAME,TYPE,MODEL | grep -E "${install_model}" | \
head -1 | awk '{print $1}')
if [ "x${model_dev}" = "x" ]; then
echo "ERROR: Unable to resolve any devices for model" \
"'${install_model}'." 1>&2
exit 1
fi
dev_node="${model_dev}"
echo "### Using block device '${dev_node}' resolved via" \
"model '${install_model}' argument..."
echo
else
while : ; do
echo "### Please type the full path of your destination device node" \
"(eg, /dev/sdz):" && read dev_node
echo
if [ -n "${dev_node}" ]; then
break
fi
done
fi
if [ "x${dev_node}" = "x" ] || [ ! -e ${dev_node} ]; then
echo "ERROR: That device node doesn't seem to exist." 1>&2
exit 1
fi
if mount | grep ${dev_node} > /dev/null; then
echo "ERROR: It looks like that device is mounted; unmount it" \
"and try again." 1>&2
exit 1
fi
if [ "${this_os}" = "${LINUX}" ]; then
dev_sectors=$(blockdev --getsz ${dev_node}) || exit 1
dev_sect_sz=$(blockdev --getss ${dev_node}) || exit 1
dev_bytes=$(expr ${dev_sectors} \* ${dev_sect_sz})
elif [ "${this_os}" = "${MACOSX}" ]; then
dev_bytes=$(diskutil info -plist ${dev_node} | \
grep -A1 "<key>TotalSize</key>" | grep -o '<integer>'.*'</integer>' | \
grep -o [^'<'integer'>'].*[^'<''/'integer'>']) || exit 1
fi
if [ ${dev_bytes} -lt 8589934592 ]; then
echo "ERROR: Your target install drive isn't large enough;" \
"it must be at least 8192 MiB." 1>&2
exit 1
fi
if [ "${this_os}" = "${LINUX}" ]; then
suffix="M"
real_dev_node=${dev_node}
elif [ "${this_os}" = "${MACOSX}" ]; then
suffix="m"
real_dev_node=${dev_node/\/dev\//\/dev\/r}
fi
# Get a final confirmation before writing the image
while : ; do
if [ -n "${NO_PROMPT}" ] && [ ${NO_PROMPT} -eq 1 ]; then
# Don't get confirmation, print a warning and continue
confirm="Y"
echo "NO_PROMPT=1 is set, continuing..."
echo
else
echo "### Proceeding will completely wipe the '${real_dev_node}'" \
"device. Are you sure (yes/no)?" && read confirm
echo
fi
if [[ ${confirm} =~ [Yy]|[Yy][Ee][Ss] ]]; then
echo "### Writing '${image_file}' to '${real_dev_node}'; this may" \
"take a while..."
tar xfOj ${image_file} | dd of=${real_dev_node} bs=1${suffix} || \
exit 1
if [ ${PIPESTATUS[0]} -ne 0 ]; then
exit 1
fi
if [ "${this_os}" = "${LINUX}" ]; then
# Re-read the partition table (not fatal)
if ! echo ${dev_node} | grep -E -q 'loop'; then
if ! blockdev --rereadpt ${dev_node}; then
# Might as well retry once
sleep 10
blockdev --rereadpt ${dev_node}
fi
fi
fi
echo
echo "### It appears the image was successfully written to disk" \
"(no errors reported)!"
if [ "${this_os}" != "${LINUX}" ]; then
break
fi
blk_dev_bytes="$(blockdev --getsize64 ${dev_node})"
blk_dev_sector="$(blockdev --getss ${dev_node})"
if [ "${blk_dev_bytes}" -gt "137438953472" ]; then
# Devices that are larger than 128 GiB get a "data" file system
echo
echo "### Large installation target detected; adding the" \
"'esos_data' file system..."
udevadm settle --timeout=30
if echo ${dev_node} | grep -E -q 'loop'; then
kpartx -a "${dev_node}"
else
if ! blockdev --rereadpt "${dev_node}"; then
sleep 5
blockdev --rereadpt "${dev_node}"
fi
fi
if echo ${dev_node} | grep -E -q 'nvme|nbd'; then
# For NVMe/NBD drives
old_logs_part_dev="${dev_node}p4"
new_logs_part_dev="${dev_node}p5"
new_data_part_dev="${dev_node}p6"
elif echo ${dev_node} | grep -E -q 'loop'; then
# For loop devices
short_dev="$(basename "${dev_node}")"
old_logs_part_dev="/dev/mapper/${short_dev}p4"
new_logs_part_dev="/dev/mapper/${short_dev}p5"
new_data_part_dev="/dev/mapper/${short_dev}p6"
else
# For everything else (eg, SCSI drives)
old_logs_part_dev="${dev_node}4"
new_logs_part_dev="${dev_node}5"
new_data_part_dev="${dev_node}6"
fi
free_end="$(parted -m -s ${dev_node} unit s print free | \
egrep ':free;$' | tail -n+2 | cut -d: -f3 | tr -d 's')"
orig_start="$(parted -m -s ${dev_node} unit s print | \
egrep '^4:' | cut -d: -f2 | tr -d 's')"
# Zap the original 'esos_logs' file system
if ! wipefs --all "${old_logs_part_dev}"; then
sleep 5
wipefs --all "${old_logs_part_dev}"
fi
parted -m -s "${dev_node}" rm 4 || exit 1
# Add a 64 GiB partition for the new 'esos_logs' FS
esos_logs_sectors="$(echo "68719476736 / ${blk_dev_sector}" | bc)"
# TODO: Make sure the sectors unit for 'parted' is the logical
# block size and not "Linux sectors" (512 bytes).
parted -m -s ${dev_node} mkpart extended \
${orig_start}s ${free_end}s || exit 1
one_mib_sectors="$(echo "1048576 / ${blk_dev_sector}" | bc)"
two_mib_sectors="$(echo "2 * (1048576 / ${blk_dev_sector})" | bc)"
esos_logs_start="$(echo "${orig_start} + ${one_mib_sectors}" \
| bc)"
esos_logs_end="$(echo "${esos_logs_start} + ${esos_logs_sectors}" \
| bc)"
parted -m -s ${dev_node} mkpart logical \
${esos_logs_start}s ${esos_logs_end}s || exit 1
# Add a partition for the 'esos_data' FS using remaining space
esos_data_start="$(echo "${esos_logs_end} + ${two_mib_sectors}" \
| bc)"
esos_data_end="$(echo "${free_end} - ${one_mib_sectors}" \
| bc)"
parted -m -s ${dev_node} mkpart logical \
${esos_data_start}s ${esos_data_end}s || exit 1
# Create the file systems
udevadm settle --timeout=30
if echo ${dev_node} | grep -E -q 'loop'; then
kpartx -a "${dev_node}"
else
if ! blockdev --rereadpt "${dev_node}"; then
sleep 5
blockdev --rereadpt "${dev_node}"
fi
fi
mkfs.ext4 -I 256 -L esos_logs "${new_logs_part_dev}" || exit 1
mkfs.ext4 -I 256 -L esos_data "${new_data_part_dev}" || exit 1
if echo ${dev_node} | grep -E -q 'loop'; then
kpartx -d "${dev_node}"
fi
fi
break
elif [[ ${confirm} =~ [Nn]|[Nn][Oo] ]]; then
exit 1
fi
done
# We finished successfully
echo
echo "### ESOS boot device installation complete!"
# Done
rm -rf ${TEMP_DIR}
exit 0