This repository was archived by the owner on Jun 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_seo_partitions.sh
More file actions
198 lines (177 loc) · 7.09 KB
/
create_seo_partitions.sh
File metadata and controls
198 lines (177 loc) · 7.09 KB
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
#!/bin/bash
# INTEL CONFIDENTIAL
#
# Copyright 2019-2022 Intel Corporation.
#
# This software and the related documents are Intel copyrighted materials, and your use of
# them is governed by the express license under which they were provided to you ("License").
# Unless the License provides otherwise, you may not use, modify, copy, publish, distribute,
# disclose or transmit this software or the related documents without Intel's prior written permission.
#
# This software and the related documents are provided as is, with no express or implied warranties,
# other than those that are expressly stated in the License.
set -e
MY_NAME=$(basename "$0")
# shellcheck disable=SC2251,SC2162
! read -d '' usage <<EOF
Usage: ${MY_NAME} [-h] [-d DEVICE]
Partition a block device to match the Smart Edge Open expectations. The device is selected automatically unless the
-d option is used.
WARNING: Be sure that you know what you do. This script may wipe out your machine's disk if used incorrectly.
-h Display this message
-d DEVICE The block DEVICE to partition.
EOF
DEVICE=''
while getopts "hd:" CURR_OPT; do
case $CURR_OPT in
h) echo "$usage"; exit 0;;
d) DEVICE=$OPTARG;;
*) echo "${MY_NAME}: ERROR: Unrecognized option. You can use the -h option to see the help message"; exit 1;;
esac
done
if [ -z "$DEVICE" ]; then
echo "INFO: The target device was not specified and will be selected automatically" 2>&1 | tee -a /dev/console
# shellcheck disable=SC2144
if [[ $(ls -l /sys/block/nvme[0-9]n[0-9] 2>/dev/null) ]]; then
# shellcheck disable=SC2010
DRIVE="/dev/$(ls -l /sys/block/nvme* | grep -v usb | head -n1 | sed 's/^.*\(nvme[a-z0-1]\+\).*$/\1/')"
export DRIVE
export BOOT_PARTITION=${DRIVE}p1
export ROOT_PARTITION=${DRIVE}p2
elif [[ $(ls -l /sys/block/[vsh]d[a-z] 2>/dev/null) ]]; then
# shellcheck disable=SC2010
DRIVE="/dev/$(ls -l /sys/block/[vsh]d[a-z] | grep -v usb | head -n1 | sed 's/^.*\([vsh]d[a-z]\+\).*$/\1/')"
export DRIVE
export BOOT_PARTITION=${DRIVE}1
export ROOT_PARTITION=${DRIVE}2
elif [[ $(ls -l /sys/block/mmcblk[0-9] 2>/dev/null) ]]; then
# shellcheck disable=SC2010
DRIVE="/dev/$(ls -l /sys/block/mmcblk[0-9] | grep -v usb | head -n1 | sed 's/^.*\(mmcblk[0-9]\+\).*$/\1/')"
export DRIVE
export BOOT_PARTITION=${DRIVE}p1
export ROOT_PARTITION=${DRIVE}p2
else
echo "No supported drives found!" 2>&1 | tee -a /dev/console
sleep 300
reboot
fi
else
echo "INFO: The target device was specified through the '-d' command line argument" 2>&1 | tee -a /dev/console
case "${DEVICE}" in
/dev/[vhs]d[a-z])
# shellcheck disable=SC2010,SC2086
DRIVE="/dev/$(ls -l /sys/block/[vsh]d[a-z] | grep -v usb | head -n1 | sed 's/^.*\([vsh]d[a-z]\+\).*$/\1/' | grep ${DEVICE##/*/})"
export DRIVE
export BOOT_PARTITION=${DRIVE}1
export ROOT_PARTITION=${DRIVE}2 ;;
/dev/nvme)
# shellcheck disable=SC2010,SC2086
DRIVE="/dev/$(ls -l /sys/block/nvme* | grep -v usb | head -n1 | sed 's/^.*\(nvme[a-z0-1]\+\).*$/\1/' | grep ${DEVICE##/*/})"
export DRIVE
export BOOT_PARTITION=${DRIVE}p1
export ROOT_PARTITION=${DRIVE}p2 ;;
/dev/mmcblk)
# shellcheck disable=SC2010,SC2086
DRIVE="/dev/$(ls -l /sys/block/mmcblk[0-9] | grep -v usb | head -n1 | sed 's/^.*\(mmcblk[0-9]\+\).*$/\1/' | grep ${DEVICE##/*/})"
export DRIVE
export BOOT_PARTITION=${DRIVE}p1
export ROOT_PARTITION=${DRIVE}p2 ;;
*)
echo "Unsupported drive specified!" 2>&1 | tee -a /dev/console
exit 1
esac
if [[ "$DRIVE" != "$DEVICE" ]]; then
echo "${DEVICE} not found!" 2>&1 | tee -a /dev/console
exit 1
fi
fi
# Define a minimal replacement of the 'run' function if it is not defined. In the ESP/uOS environment it is provided by
# the ESP bootstraping scripts. The replacement definition is needed to run the script stand-alone:
if [ "$(type -t run)" != 'function' ]; then
echo "INFO: The 'run' function is not defined"
run() {
local msg=$1
local runThis=$2
echo "$msg"
unbuffer "$runThis"
}
fi
# --- Create Volume Group/Name ---
VOLUME_GROUP="system-vg"
LOGICAL_VOLUME="root-lv"
# --- Create Boot/Root targets ---
export BOOTFS=/target/boot
export ROOTFS=/target/root
mkdir -p $BOOTFS
mkdir -p $ROOTFS
echo "" 2>&1 | tee -a /dev/console
echo "" 2>&1 | tee -a /dev/console
echo "Installing on ${DRIVE}" 2>&1 | tee -a /dev/console
echo "" 2>&1 | tee -a /dev/console
echo "" 2>&1 | tee -a /dev/console
# shellcheck disable=SC2269 # variable origin: pre.sh
param_parttype=${param_parttype}
# shellcheck disable=SC2269 # variable origin: pre.sh
param_validationmode=${param_validationmode}
# --- Partition HDD ---
run "Partitioning drive ${DRIVE}" \
"if [[ $param_parttype == 'efi' ]]; then
parted --script ${DRIVE} \
mklabel gpt \
mkpart ESP fat32 1MiB 551MiB \
set 1 esp on \
mkpart primary 551MiB 100% \
set 2 lvm on;
else
parted --script ${DRIVE} \
mklabel msdos \
mkpart primary ext4 1MiB 551MiB \
set 1 boot on \
mkpart primary 551MiB 100% \
set 2 lvm on;
fi" \
"${PROVISION_LOG}"
# --- Create Physical Volume ---
run "Creating Physical Volume on ${ROOT_PARTITION}" \
"pvcreate -ff --yes ${ROOT_PARTITION}" \
"${PROVISION_LOG}"
# --- Create Volume Group ---
run "Creating Volume Group on ${ROOT_PARTITION}" \
"vgcreate ${VOLUME_GROUP} ${ROOT_PARTITION}" \
"${PROVISION_LOG}"
# --- Create Logical Volume ---
# shellcheck disable=SC2086
diskspace=$(parted ${DRIVE} unit GiB print | grep 'Disk /' | awk '{ print $3 }')
diskspace=${diskspace::-3}
threshold=500
if [[ $param_validationmode == true ]] && [[ $diskspace -gt $threshold ]]; then
run "Creating Logical Volume on ${VOLUME_GROUP}" \
"lvcreate --yes -l +50%FREE ${VOLUME_GROUP} -n ${LOGICAL_VOLUME}" \
"${PROVISION_LOG}"
else
run "Creating Logical Volume on ${VOLUME_GROUP}" \
"lvcreate --yes -l 100%FREE ${VOLUME_GROUP} -n ${LOGICAL_VOLUME}" \
"${PROVISION_LOG}"
fi
# --- Create file systems ---
if [[ $param_parttype == 'efi' ]]; then
run "Creating boot partition on drive ${DRIVE}" \
"mkfs -t vfat -n BOOT ${BOOT_PARTITION} && \
mkdir -p $BOOTFS && \
mount ${BOOT_PARTITION} $BOOTFS" \
"${PROVISION_LOG}"
else
run "Creating boot partition on drive ${DRIVE}" \
"mkfs -O ^metadata_csum -t ext4 -L BOOT -F ${BOOT_PARTITION} && \
e2label ${BOOT_PARTITION} BOOT && \
mkdir -p $BOOTFS && \
mount ${BOOT_PARTITION} $BOOTFS" \
"${PROVISION_LOG}"
fi
# --- Create ROOT file system ---
export ROOT_PARTITION=/dev/${VOLUME_GROUP}/${LOGICAL_VOLUME}
run "Creating root file system" \
"mkfs -O ^metadata_csum -t ext4 ${ROOT_PARTITION} && \
mount ${ROOT_PARTITION} $ROOTFS && \
e2label ${ROOT_PARTITION} STATE_PARTITION" \
"${PROVISION_LOG}"