-
Notifications
You must be signed in to change notification settings - Fork 96
/
mender-convert-modify
executable file
·341 lines (282 loc) · 12.1 KB
/
mender-convert-modify
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
#! /usr/bin/env bash
#
# Copyright 2021 Northern.tech AS
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Default that can be overridden by providing this method in a
# configuration file passed with '--config'
function platform_pre_modify() {
true
}
PLATFORM_PRE_MODIFY_HOOKS=(platform_pre_modify)
function platform_modify() {
true
}
PLATFORM_MODIFY_HOOKS=(platform_modify)
function user_local_modify() {
true
}
USER_LOCAL_MODIFY_HOOKS=(user_local_modify)
function overlay_modify() {
true
}
OVERLAY_MODIFY_HOOKS=(overlay_modify)
function trap_exit() {
echo "mender-convert-modify has finished. Cleaning up..."
sudo umount -f work/boot
sudo umount -f work/rootfs
}
function trap_term() {
true
}
trap trap_term INT TERM
trap trap_exit EXIT
echo "Running $(basename $0): $@"
source modules/bootstrap.sh
source modules/disk.sh
source modules/probe.sh
source modules/deb.sh
source modules/testscfg.sh
source modules/grub.sh
# The mender_convert_config is always used and provides all the defaults
declare -a configs=("configs/mender_convert_config")
while (("$#")); do
case "$1" in
-o | --overlay)
overlays+=("${2}")
shift 2
;;
-c | --config)
configs+=("${2}")
shift 2
;;
-d | --disk-image)
disk_image="${2}"
shift 2
;;
*)
log_fatal "Sorry, but the provided option is not supported: $1"
;;
esac
done
# Note the use of %q formatting here. This is a bash feature to add
# proper quoting to the strings so that spaces and special characters
# will be treated properly. Primarily for supporting spaces in
# pathnames and avoid splitting those into multiple parameters.
source modules/config.sh $(printf "%q " "${configs[@]}")
boot_part=$(disk_boot_part)
root_part=$(disk_root_part)
# Sysfs device paths
boot_part_device=$(disk_boot_part_device)
data_part_device=$(disk_data_part_device)
root_part_a_device=$(disk_root_part_a_device)
root_part_b_device=$(disk_root_part_b_device)
# Create mount points
mkdir -p work/boot
mkdir -p work/rootfs
sudo mount ${boot_part} work/boot
sudo mount ${root_part} work/rootfs
if [ -z "${MENDER_DEVICE_TYPE}" ]; then
# Observed systems who do not have this file, e.g images generated with mkosi
if [ -f work/rootfs/etc/hostname ]; then
MENDER_DEVICE_TYPE=$(cat work/rootfs/etc/hostname)
else
MENDER_DEVICE_TYPE="default"
fi
fi
log_info "Performing platform specific pre-modifications (if any)"
for hook in "${PLATFORM_PRE_MODIFY_HOOKS[@]}"; do
log_info "Running hook: $hook"
eval $hook
done
# Do this unconditionally even if not installing add-ons. The reason is that if
# the Debian package is installed later, this folder has to preexist. It is part
# of the rootfs setup, independently of the software.
log_info "Creating state folder in the data partition for Mender add-ons"
run_and_log_cmd "sudo mkdir -p work/rootfs/var/lib"
run_and_log_cmd "sudo mkdir -p work/rootfs/data/mender-configure"
run_and_log_cmd "sudo ln -sf /data/mender-configure work/rootfs/var/lib"
run_and_log_cmd "sudo mkdir -p work/rootfs/data/mender-monitor"
run_and_log_cmd "sudo ln -sf /data/mender-monitor work/rootfs/var/lib"
if [ "${MENDER_GRUB_EFI_INTEGRATION}" == "y" ]; then
# Check for known U-Boot problems in all files on the boot partition.
check_for_broken_uboot_uefi_support work/boot
if [ "$MENDER_GRUB_D_INTEGRATION" = y ] || ( [ "$MENDER_GRUB_D_INTEGRATION" = auto ] \
&& supports_grub_d_and_efi "work/boot" "work/rootfs" ); then
# No need to install Grub, use the one already present, and only install
# our tools.
log_info "Not installing GRUB EFI bootloader, relying on platform provided one."
log_info "Installing Mender GRUB tools..."
grub_install_grub_editenv_binary
log_info "Generating grub config using update-grub..."
grub_create_grub_config
grub_install_grub_d_config
else
log_info "Installing GRUB EFI bootloader..."
grub_install_mender_grub
log_info "Generating the mender-grub config..."
grub_create_grub_config
grub_install_standalone_grub_config
fi
fi
run_and_log_cmd "wget --quiet -O work/mender-data-dir.service $MENDER_CLIENT_DATA_DIR_SERVICE_URL"
run_and_log_cmd "sudo install -m 644 work/mender-data-dir.service work/rootfs/lib/systemd/system/mender-data-dir.service"
for service_name in mender-client mender-updated; do
run_and_log_cmd "sudo mkdir -p work/rootfs/lib/systemd/system/${service_name}.service.wants"
run_and_log_cmd "sudo ln -sf /lib/systemd/system/mender-data-dir.service \
work/rootfs/lib/systemd/system/${service_name}.service.wants/mender-data-dir.service"
done
run_and_log_cmd "sudo mkdir -p work/rootfs/data/mender"
run_and_log_cmd "sudo mkdir -p work/rootfs/var/lib"
run_and_log_cmd "sudo ln -sf /data/mender work/rootfs/var/lib"
log_info "Using root device A in mender.conf: $root_part_a_device"
log_info "Using root device B in mender.conf: $root_part_b_device"
cat <<- EOF > work/mender.conf.data
{
"RootfsPartA": "${root_part_a_device}",
"RootfsPartB": "${root_part_b_device}"
}
EOF
run_and_log_cmd "sudo cp work/mender.conf.data work/rootfs/data/mender/mender.conf"
run_and_log_cmd "sudo chmod 600 work/rootfs/data/mender/mender.conf"
run_and_log_cmd "echo 'device_type=${MENDER_DEVICE_TYPE}' > work/device_type"
run_and_log_cmd "sudo install -m 0444 work/device_type work/rootfs/data/mender/"
log_info "Creating state scripts version file."
case "${MENDER_CLIENT_VERSION}" in
1*) VERSION_STRING='2' ;;
*) VERSION_STRING='3' ;;
esac
run_and_log_cmd "sudo mkdir -p work/rootfs/etc/mender/scripts/"
run_and_log_cmd "echo -n ${VERSION_STRING} | sudo tee work/rootfs/etc/mender/scripts/version"
log_info "Installing a custom /etc/fstab (see ${MENDER_CONVERT_LOG_FILE} for more info)"
if [ -z "${MENDER_BOOT_PART_MOUNT_LOCATION:-}" ]; then
if [ "${MENDER_GRUB_EFI_INTEGRATION}" == "y" ]; then
MENDER_BOOT_PART_MOUNT_LOCATION="/boot/efi"
else
MENDER_BOOT_PART_MOUNT_LOCATION="/uboot"
fi
fi
run_and_log_cmd "sudo mkdir -p work/rootfs/${MENDER_BOOT_PART_MOUNT_LOCATION}"
if [ "${MENDER_DATA_PART_GROWFS}" == "y" ]; then
MENDER_DATA_PART_FSTAB_OPTS="${MENDER_DATA_PART_FSTAB_OPTS},x-systemd.growfs"
fi
if echo ${MENDER_ROOT_PART_FSTAB_OPTS} | tr ',' '\n' | egrep -q "^ro$"; then
if ! echo "${MENDER_ROOT_PART_MKFS_OPTS}" | fgrep -q -- "^has_journal"; then
log_warn "EXT4 journal feature is known to create unstable checksums on read-only file systems, add \"-O ^has_journal\" to MENDER_ROOT_PART_MKFS_OPTS to remove it"
fi
fi
log_info "Using boot partition device in fstab: $boot_part_device"
log_info "Using data partition device in fstab: $data_part_device"
fstab=$(sed '/\/boot/d' work/rootfs/etc/fstab)
fstab=$(sed '/.*\s\/\s.*/d' <<< "$fstab")
cat <<- EOF > work/rootfs/etc/fstab
${boot_part_device} ${MENDER_BOOT_PART_MOUNT_LOCATION} auto ${MENDER_BOOT_PART_FSTAB_OPTS} 0 0
/dev/root / auto ${MENDER_ROOT_PART_FSTAB_OPTS} 1 ${MENDER_ROOT_PART_FS_PASSNO}
${data_part_device} /data auto ${MENDER_DATA_PART_FSTAB_OPTS} 0 0
# entries kept from original fstab
$fstab
EOF
log_info "Installing Mender client and related files"
if [ "${MENDER_CLIENT_INSTALL}" = "y" ]; then
mender_client_deb_version="$MENDER_CLIENT_VERSION"
if [ "$MENDER_CLIENT_VERSION" = "auto" ]; then
deb_distro=$(probe_debian_distro_name)
deb_codename=$(probe_debian_distro_codename)
if ([ "$deb_distro" = "debian" ] && [ "$deb_codename" = "bullseye" ]) \
|| ([ "$deb_distro" = "debian" ] && [ "$deb_codename" = "buster" ]) \
|| ([ "$deb_distro" = "ubuntu" ] && [ "$deb_codename" = "jammy" ]) \
|| ([ "$deb_distro" = "ubuntu" ] && [ "$deb_codename" = "focal" ]); then
MENDER_CLIENT_VERSION="3.5.x"
mender_client_deb_version="latest"
else
MENDER_CLIENT_VERSION="latest"
mender_client_deb_version="latest"
fi
fi
case "${MENDER_CLIENT_VERSION}" in
1.* | 2.* | 3.*)
log_info "Installing Mender client version ${mender_client_deb_version}"
deb_get_and_install_package mender-client mender-client "${mender_client_deb_version}"
mender_client_deb_name="${DEB_NAME}"
;;
*)
# For 4.x, master and latest(*) install the new packages
log_info "Installing Mender Auth and Mender Update version ${mender_client_deb_version}"
deb_get_and_install_package mender-client4 mender-auth "${mender_client_deb_version}"
deb_get_and_install_package mender-client4 mender-update "${mender_client_deb_version}"
mender_client_deb_name="${DEB_NAME}"
log_info "Installing Mender Flash version ${MENDER_FLASH_VERSION}"
deb_get_and_install_package mender-flash mender-flash "${MENDER_FLASH_VERSION}"
if [ "$MENDER_SETUP_INSTALL" = "auto" ]; then
MENDER_SETUP_INSTALL=y
fi
if [ "$MENDER_SNAPSHOT_INSTALL" = "auto" ]; then
MENDER_SNAPSHOT_INSTALL=y
fi
;;
esac
if [ "$MENDER_SETUP_INSTALL" = "y" ]; then
log_info "Installing Mender Setup version ${MENDER_SETUP_VERSION}"
deb_get_and_install_package mender-setup mender-setup "${MENDER_SETUP_VERSION}"
fi
if [ "$MENDER_SNAPSHOT_INSTALL" = "y" ]; then
log_info "Installing Mender Snapshot version ${MENDER_SNAPSHOT_VERSION}"
deb_get_and_install_package mender-snapshot mender-snapshot "${MENDER_SNAPSHOT_VERSION}"
fi
# Save installed client version for tests in Yocto variable format
testscfg_add "PREFERRED_VERSION:mender-client" "$(echo ${mender_client_deb_name} | sed -r 's/.*_([0-9]+\.[0-9]+\.[0-9]+).*/\1/')"
fi
if [ "${MENDER_ADDON_CONNECT_INSTALL}" = "y" ]; then
log_info "Installing Mender Connect addon"
deb_get_and_install_package mender-connect mender-connect "${MENDER_ADDON_CONNECT_VERSION}"
fi
if [ "${MENDER_ADDON_CONFIGURE_INSTALL}" = "y" ]; then
log_info "Installing Mender Configure addon"
deb_get_and_install_package mender-configure mender-configure "${MENDER_ADDON_CONFIGURE_VERSION}" "true"
fi
if [ "${MENDER_INSTALL_INPUT_PACKAGES}" = "y" ]; then
deb_get_and_install_input_package ${MENDER_INSTALL_INPUT_PACKAGES_PATH}
fi
# Done with package installations.
deb_cleanup_package_cache work/rootfs
#
# Make sure to re-label rootfs when selinux is in enforcing mode
# e.g. CentOS8 after conversion cannot start login shell due selinux
# inspired by: https://forums.centos.org/viewtopic.php?t=48714
#
if [ -f work/rootfs/etc/selinux/config ]; then
grep -r 'SELINUX=Enforcing' work/rootfs/etc/selinux/config || true
if [ $? -eq 0 ]; then
log_info "Selinux is in enforcing mode. Enable autorelabel"
touch work/rootfs/.autorelabel
fi
fi
log_info "Performing platform specific modifications (if any)"
for hook in "${PLATFORM_MODIFY_HOOKS[@]}"; do
log_info "Running hook: $hook"
eval $hook
done
log_info "Performing user/local specific modifications (if any)"
for hook in "${USER_LOCAL_MODIFY_HOOKS[@]}"; do
log_info "Running hook: $hook"
eval $hook
done
for overlay in "${overlays[@]}"; do
log_info "Applying rootfs overlay: ${overlay}"
run_and_log_cmd "sudo rsync --archive --keep-dirlinks --verbose ${overlay}/ work/rootfs/"
done
log_info "Performing overlay specific modifications (if any)"
for hook in "${OVERLAY_MODIFY_HOOKS[@]}"; do
log_info "Running hook: $hook"
eval $hook
done