Add a shutdown phase for correctly unmount all FS.

This commit is contained in:
andreili 2025-07-16 15:03:04 +02:00
parent 691e0d0ba4
commit 99175af0d6
8 changed files with 91 additions and 9 deletions

View File

@ -63,14 +63,6 @@ then
echo ${CONSOLE_LOGLEVEL} > /proc/sys/kernel/printk
fi
NEW_ROOT='/newroot'
CDROOT_PATH="/mnt/cdrom"
RW_MNT="/mnt/rw_part"
STATIC="/mnt/livecd"
OVERLAY="/mnt/overlay"
LOOP="/root.sqh"
LOOPTYPE="squashfs"
good_msg "Initial ramdisk script. Linux kernel ${KV}"
log_msg "COMMAND: 'echo "" > /proc/sys/kernel/hotplug'"
echo "" > /proc/sys/kernel/hotplug
@ -196,5 +188,9 @@ echo ${CONSOLE_LOGLEVEL} > /proc/sys/kernel/printk && log_msg "COMMAND: 'echo \"
good_msg "Switching to real root: switch_root ${CHROOT} ${init} ${init_opts}"
#sh
exec switch_root "${CHROOT}" "${init}" ${init_opts}
#exec pivot_root "${CHROOT}" "${init}" ${init_opts}
good_msg "Return from real root, finishing..."
run_emergency_shell
exit 1

View File

@ -1,3 +1,11 @@
NEW_ROOT='/newroot'
CDROOT_PATH="/mnt/cdrom"
RW_MNT="/mnt/rw_part"
STATIC="/mnt/livecd"
OVERLAY="/mnt/overlay"
LOOP="/root.sqh"
LOOPTYPE="squashfs"
BACK_UP="\033[1K\033[0G"
WARN="\033[33;1m"
BAD="\033[31;1m"

View File

@ -222,3 +222,11 @@ run_shell() {
run_emergency_shell() {
run_shell
}
find_mount() {
# $1 = mount point
local mnt_p=$1
local res
res=$(cat /proc/self/mountinfo | cut -d' ' -f5 | grep "^${mnt_p}/")
echo ${res}
}

View File

@ -64,4 +64,5 @@ file /bin/resize2fs build/common/initrd/resize2fs 755 0 0
file /etc/init.def files/initramfs/init.def 755 0 0
file /etc/init.script files/initramfs/init.script 755 0 0
file /init files/initramfs/init 755 0 0
file /shutdown files/initramfs/shutdown 755 0 0
#file /etc/udev/hwdb.bin ../CB1/root/boot/init_last/etc/udev/hwdb.bin 755 0 0

43
files/initramfs/shutdown Executable file
View File

@ -0,0 +1,43 @@
#!/bin/busybox sh
. /etc/init.def
. /etc/init.script
# Set up symlinks
run busybox --install -s
ln -s /proc/self/mounts /etc/mtab
ACTION="${1:-halt}"
good_msg "Shutdown ramdisk script. Linux kernel ${KV}"
log_msg "COMMAND: 'echo "" > /proc/sys/kernel/hotplug'"
echo "" > /proc/sys/kernel/hotplug
good_msg "Move all to temporary..."
sh_mnt=$(cat /proc/self/mountinfo | cut -d' ' -f5 | grep "^/oldroot/run/shutdown/mounts/")
good_msg ${sh_mnt}
for ddir in ${sh_mnt}
do
run mount --move "${ddir}" "/tmp"
done
good_msg "Move system mounts..."
for ddir in /dev /proc /sys /run
do
run mount --move "/oldroot${ddir}" "${ddir}"
done
good_msg "Unmount old root..."
for ddir in /oldroot /tmp
do
run umount "${ddir}"
done
good_msg "Finished"
#for debug - uncomment
#run_emergency_shell
# send sysrq
case "${ACTION}" in
reboot) reboot -f; break;;
poweroff) poweroff -f; break;;
halt) halt -f; break;;
esac
# this should never happen
run_emergency_shell

View File

@ -0,0 +1,9 @@
[Unit]
Description=Prepare a shutdown script to correctly unmount all filesystems
[Install]
WantedBy=multi-user.target
[Service]
Type=oneshot
ExecStart=sh -c "mkdir -p /run/initramfs && cd /run/initramfs && tar xf /usr/shutdown.tar.xz"

View File

@ -106,12 +106,29 @@ class Initramfs:
f"{self.out_dir}/uInitrd"])
p.wait()
def __mkshutdown(self):
Logger.build(f"\tShutdown image")
dir_tmp = f"{self.build_dir}/shutdown_img"
dir_ch = Path(dir_tmp)
if (dir_ch.is_dir()):
p = subprocess.Popen(["sudo", "rm", "-rf", dir_tmp])
p.wait()
p = subprocess.Popen(["mkdir", "-p", dir_tmp])
p.wait()
p = subprocess.Popen(f"sudo cat {self.files_dir}/init.cpio | sudo cpio -idm && sudo tar cJpf {self.out_dir}/shutdown.tar.xz .", shell=True, cwd=dir_tmp)
p.wait()
p = subprocess.Popen(["sudo", "cp", f"{self.out_dir}/shutdown.tar.xz", f"{ROOT_DIR}/root/usr/"])
p.wait()
p = subprocess.Popen(["sudo", "rm", "-rf", dir_tmp])
p.wait()
def __initrd(self):
Logger.build(f"Make uInitrd")
self.__cpio()
self.__compress_gzip()
self.__compress_lzma()
self.__mkimage()
self.__mkshutdown()
def build(self, os):
#self.__prepare()

View File

@ -71,7 +71,7 @@ class Target:
self.sources.do_patch(self.board_name, self.patch_dir)
def build(self, sub_target, out_dir):
self.source_sync()
#self.source_sync()
if (not self.no_build):
opts = self.makeopts.split(" ")
config = ""