When mount local filesystem in os starting, we will create or use a local existed logical volume to replace remote filesystem.
Signed-off-by: Xu Xijian hdxuxijian@163.com --- .../dracut-initrd/bin/set-local-sysroot.sh | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 container/dracut-initrd/bin/set-local-sysroot.sh
diff --git a/container/dracut-initrd/bin/set-local-sysroot.sh b/container/dracut-initrd/bin/set-local-sysroot.sh new file mode 100644 index 0000000..65e6d3e --- /dev/null +++ b/container/dracut-initrd/bin/set-local-sysroot.sh @@ -0,0 +1,78 @@ +#!/bin/sh + +analyse_kernel_cmdline_params() { + rootfs="$(getarg root=)" + rootfs_src=$(echo $"$rootfs" | sed 's////') + timestamp="$(basename $root)" + export rootfs_src timestamp +} + +rsync_src_lv() { + local src_lv="$1" + + [ -e "$src_lv" ] && return # /dev/mapper/os-openeuler-aarch64-20210226104900 + + # create logical volume + src_lv_devname="$(basename $src_lv)" # os-openeuler_aarch64_20210226104900 + lvm lvcreate -L 10G --name "${src_lv_devname#os-}" os # openeuler_aarch64_20210226104900 + mke2fs -t ext4 -F "$src_lv" + + # sync nfsroot to $src_lv + mkdir -p /mnt1 && mount -t nfs "$rootfs_src" /mnt1 + mkdir -p /mnt2 && mount "$src_lv" /mnt2 + cp -a /mnt1/. /mnt2/ + umount /mnt1 /mnt2 + + # change permission of "$src_lv" to readonly + lvm lvchange --permission r "$src_lv" +} + +snapshot_boot_lv() { + local src_lv="$1" + local boot_lv="$2" + + [ "$src_lv" == "$boot_lv" ] && return + + lvm lvremove --force "$boot_lv" + boot_lv_devname="$(basename $boot_lv)" + lvm lvcreate --size 10G --name ${boot_lv_devname#os-} --snapshot "$src_lv" +} + +set_sysroot() { + boot_lv="$1" + umount "$NEWROOT" + mount "$boot_lv" "$NEWROOT" +} + +if ! getargbool 0 local; then + exit 0 +fi + +analyse_kernel_cmdline_params + +sed -i "s/^locking_type = .*/locking_type = 1/" /etc/lvm/lvm.conf + +use_root_partition="$(getarg use_root_partition=)" +if [ -n "$use_root_partition" ]; then + src_lv="$use_root_partition" # "$use_root_partition" "$root_$timestamp", root from getarg root= + rsync_src_lv "$src_lv" +else + src_lv="/dev/mapper/os-openeuler_aarch64_$timestamp" # "os-openeuler_aarch64_$timestamp" + [ -e "src_lv" ] || { + echo "warn dracut: FATAL: no src_lv with local mount, reboot" + reboot + } +fi + +save_root_partition="$(getarg save_root_partition=)" +if [ -n "$save_root_partition" ]; then + boot_lv="$save_root_partition" # "os-openeuler_aarch64_$save_partition" +else + boot_lv="/dev/mapper/os-openeuler_aarch64_default" +fi + +snapshot_boot_lv "$src_lv" "$boot_lv" + +set_sysroot "$boot_lv" + +exit 0