From e8d73c03a06d330425a6a99034f3b0f13eccef37 Mon Sep 17 00:00:00 2001 From: ading2210 Date: Tue, 3 Oct 2023 23:56:57 -0700 Subject: [PATCH] try to swich_root into the rootfs --- README.md | 9 +++++++- bootloader/bin/bootstrap.sh | 44 +++++++++++++++++++++++++++++++++++-- build.sh | 9 +++----- 3 files changed, 53 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index bf54b4a..b892dd5 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,10 @@ # Chrome OS RMA Shim Bootloader -This is a set of scripts for patching a Chrome OS RMA shim to serve as a bootloader for a standard Linux disto. \ No newline at end of file +This is a set of scripts for patching a Chrome OS RMA shim to serve as a bootloader for a standard Linux disto. + +## Current Development Roadmap: +- ~~build the image automatically~~ +- ~~boot to a shell~~ +- switch_root into an actual rootfs +- start X11 in the actual rootfs +- ui improvements in the bootloader \ No newline at end of file diff --git a/bootloader/bin/bootstrap.sh b/bootloader/bin/bootstrap.sh index 7cfda0c..69cadfe 100644 --- a/bootloader/bin/bootstrap.sh +++ b/bootloader/bin/bootstrap.sh @@ -38,16 +38,56 @@ find_rootfs_partitions() { if [ ! "${partitions}" ]; then continue fi - echo $partitions + echo "${disk}${partitions}" done } +#from original bootstrap.sh +move_mounts() { + local BASE_MOUNTS="/sys /proc /dev" + local NEWROOT_MNT="$1" + for mnt in $BASE_MOUNTS; do + # $mnt is a full path (leading '/'), so no '/' joiner + mkdir -p "$NEWROOT_MNT$mnt" + mount -n -o move "$mnt" "$NEWROOT_MNT$mnt" + done +} + +#from original bootstrap.sh +use_new_root() { + local NEWROOT_MNT="$1" + move_mounts $NEWROOT_MNT + # Chroot into newroot, erase the contents of the old /, and exec real init. + echo "About to switch root... Check VT2/3/4 if you stuck for a long time." + # If you have problem getting console after switch_root, try to debug by: + # 1. Try a simple shell. + # exec <"${TTY}" >"${TTY}" 2>&1 + # exec switch_root "${NEWROOT_MNT}" /bin/sh + # 2. Try to invoke factory installer directly + # exec switch_root "${NEWROOT_MNT}" /usr/sbin/factory_shim_service.sh + # -v prints upstart info in kmsg (available in INFO_TTY). + exec switch_root "${NEWROOT_MNT}" /sbin/init +} + main() { echo "...:::||| Bootstrapping ChromeOS Factory Shim |||:::..." echo "TTY: ${TTY}, LOG: ${LOG_TTY}, echo: ${echo_TTY}, DEBUG: ${DEBUG_TTY}" echo "idk please work" + + sleep 5 - find_rootfs_partitions + local rootfs_partitions=$(find_rootfs_partitions) + for rootfs_partition in $rootfs_partitions; do + local IFS=: read -r part_path part_name <<< $rootfs_partition + echo "found bootable partition ${part_path}: ${part_name}" + done + + sleep 5 + + mkdir /newroot + mount /dev/sda4 /newroot + use_new_root /newroot + enable_debug_console "/dev/pts/0" } diff --git a/build.sh b/build.sh index 4ce519e..d5fd7f1 100755 --- a/build.sh +++ b/build.sh @@ -11,7 +11,7 @@ fi . ./build_image.sh print_help() { - echo "Usage: ./build.sh output_path shim_path" + echo "Usage: ./build.sh output_path shim_path rootfs_dir" } check_deps() { @@ -43,6 +43,7 @@ fi output_path=$(realpath "${1}") shim_path=$(realpath "${2}") +rootfs_dir=$(realpath "${3}") echo "created loop device for shim" shim_loop=$(create_loop "${shim_path}") @@ -87,7 +88,7 @@ echo "patching initramfs" patch_initramfs $initramfs_dir echo "creating disk image" -create_image $output_path 20 200 +create_image $output_path 20 1200 echo "creating loop device for the image" image_loop=$(create_loop ${output_path}) @@ -96,10 +97,6 @@ echo "creating partitions on the disk image" create_partitions $image_loop "${kernel_dir}/kernel.bin" echo "copying data into the image" -rootfs_dir=/tmp/rootfs -rm $rootfs_dir -rf -mkdir $rootfs_dir -touch $rootfs_dir/testfile populate_partitions $image_loop $initramfs_dir $rootfs_dir echo "cleaning up loop devices"