diff --git a/config/board/btt_cb1.json b/config/board/btt_cb1.json index a6e53ea..3f0714f 100644 --- a/config/board/btt_cb1.json +++ b/config/board/btt_cb1.json @@ -69,9 +69,14 @@ "file": "arch/arm64/boot/dts/allwinner/overlay/sun50i-h616*.dtbo", "store_type": "boot", "subdir": "dtb/allwinner/overlay" + }, + { + "file": "", + "store_type": "boot", + "kmods": true } ], - "makeopts": "CROSS_COMPILE=%{CROSS_C}% ARCH=arm64 INSTALL_MOD_PATH=%{out_dir}%/kmods" + "makeopts": "CROSS_COMPILE=%{CROSS_C}% ARCH=arm64 INSTALL_MOD_PATH=%{out_dir}%/kmods/usr" }, { "parent": "rtl8189ES_linux", diff --git a/config/board/btt_pi2.json b/config/board/btt_pi2.json index a43b7b4..fa5c553 100644 --- a/config/board/btt_pi2.json +++ b/config/board/btt_pi2.json @@ -71,9 +71,14 @@ "file": "arch/arm64/boot/dts/%{DTB_FILE}%", "store_type": "boot", "subdir": "dtb/rockchip" + }, + { + "file": "", + "store_type": "boot", + "kmods": true } ], - "makeopts": "CROSS_COMPILE=%{CROSS_C}% ARCH=arm64 INSTALL_MOD_PATH=%{out_dir}%/kmods" + "makeopts": "CROSS_COMPILE=%{CROSS_C}% ARCH=arm64 INSTALL_MOD_PATH=%{out_dir}%/kmods/usr" } ], "install": diff --git a/config/board/opi_zero2.json b/config/board/opi_zero2.json index a5debb3..21cbdcb 100644 --- a/config/board/opi_zero2.json +++ b/config/board/opi_zero2.json @@ -70,9 +70,14 @@ "file": "arch/arm64/boot/dts/allwinner/overlay/sun50i-h616*.dtbo", "store_type": "boot", "subdir": "dtb/allwinner/overlay" + }, + { + "file": "", + "store_type": "boot", + "kmods": true } ], - "makeopts": "CROSS_COMPILE=%{CROSS_C}% ARCH=arm64 INSTALL_MOD_PATH=%{out_dir}%/kmods" + "makeopts": "CROSS_COMPILE=%{CROSS_C}% ARCH=arm64 INSTALL_MOD_PATH=%{out_dir}%/kmods/usr" } ], "install": diff --git a/config/os_aarch64.json b/config/os_aarch64.json index 0677ad6..4241bf3 100644 --- a/config/os_aarch64.json +++ b/config/os_aarch64.json @@ -1,7 +1,9 @@ { "variables": [ - "USER_LOGIN:klipper" + "USER_LOGIN:klipper", + "USER_ID:1010", + "TIME_ZONE:Europe/Warsaw" ], "stage3_info": { @@ -198,17 +200,60 @@ { "steps": [ - { "chroot": "systemctl enable NetworkManager ntpdate sshd" }, + { + "file": "/etc/systemd/system/prepare_shutdown.service", + "append": false, + "lines": [ + "[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\"" + ] + }, + { + "file": "/etc/systemd/system/sync.service", + "append": false, + "lines": [ + "[Unit]", + "Description=Sync all data", + "StartLimitIntervalSec=0", + "[Service]", + "Type=simple", + "Restart=always", + "RestartSec=10", + "User=root", + "ExecStart=/usr/local/bin/sync.sh", + "[Install]", + "WantedBy=multi-user.target" + ] + }, + { + "file": "/usr/local/bin/sync.sh", + "append": false, + "lines": [ + "#!/bin/bash", + "# regular sync to prevent data loss when direct power outage", + "while [ 1 ]; do", + " sync", + " sleep 60", + "done" + ], + "chmod": "+x" + }, + { "chroot": "systemctl enable NetworkManager ntpdate sshd prepare_shutdown sync" }, { "sudo": "sed -i -E 's/^# (%wheel ALL)/\\1/' ./etc/sudoers" }, { "sudo": "sed -i -E 's/^#(\\S+MaxUse)=$/\\1=10M/' ./etc/systemd/journald.conf" }, { "sudo": "sed -i -E 's/^#(\\S+MaxFileSize)=$/\\1=10M/' ./etc/systemd/journald.conf" }, { "copy": [ "%{ROOT_DIR}%/files/firmware/usr", "."] }, { "sudo": "chmod u+s ./usr/bin/Xorg" }, - { "sudo": "ln -sf /usr/share/zoneinfo/Europe/Warsaw ./etc/localtime" }, - { "chroot": "useradd -m -G wheel,video,audio,disk,usb %{USER_LOGIN}% --password %{USER_LOGIN}%" }, + { "sudo": "ln -sf /usr/share/zoneinfo/%{TIME_ZONE}% ./etc/localtime" }, + { "chroot": "useradd -m -G wheel,video,audio,disk,usb -g %{USER_ID}% -u %{USER_ID}% %{USER_LOGIN}% --password %{USER_LOGIN}%" }, { "chroot": "echo '%{USER_LOGIN}%:%{USER_LOGIN}%' | chpasswd" }, { "chroot": "echo 'root:root' | chpasswd" }, - { "chroot": "sudo -i -u klipper python -m venv /home/%{USER_LOGIN}%/venv" }, + { "chroot": "sudo -i -u %{USER_LOGIN}% python -m venv /home/%{USER_LOGIN}%/venv" }, { "soft_clean": "bdeps" } ] } diff --git a/files/initramfs/init b/files/initramfs/init index 7f36d6a..10f1cce 100644 --- a/files/initramfs/init +++ b/files/initramfs/init @@ -71,48 +71,43 @@ done # [ ! -d "${i}" ] && run mkdir -p "${i}" #done run mount -t squashfs -o loop,ro "${CDROOT_PATH}/${LOOP}" "${STATIC}" || run_emergency_shell -#if [ -d ${CDROOT_PATH}/modules ] -#then -# warn_msg "Adding all modules in ${CDROOT_PATH}/modules" -# for module in "${CDROOT_PATH}/modules/"*.lzm; do -# mod=${module##*/} -# mod=${mod//-/_} -# mod=${mod%.*} -# if [ ! -d "${OVERLAY}/.${mod}" ] -# then -# run mkdir -p "${OVERLAY}/.${mod}" || return -# fi -# run mount -o loop,ro "${module}" "${OVERLAY}/.${mod}" -# mod_path="${mod_path}:${OVERLAY}/.${mod}" -# # Assign variable with paths to modules mount point -# # TODO: Stop using eval -# eval ${mod}="${OVERLAY}/.${mod}" -# mods="${mods} ${mod}" -# done -#fi +if [ -d ${CDROOT_PATH}/modules ] +then + warn_msg "Adding all modules in ${CDROOT_PATH}/modules" + for module in "${CDROOT_PATH}/modules/"*.lzm; do + mod=${module##*/} + mod=${mod//-/_} + mod=${mod%.*} + if [ ! -d "${OVERLAY}/.${mod}" ] + then + run mkdir -p "${OVERLAY}/.${mod}" || return + fi + run mount -o loop,ro "${module}" "${OVERLAY}/.${mod}" + mod_path="${mod_path}:${OVERLAY}/.${mod}" + # Assign variable with paths to modules mount point + # TODO: Stop using eval + eval ${mod}="${OVERLAY}/.${mod}" + mods="${mods} ${mod}" + done +fi run mount -t overlay overlay -o lowerdir="${STATIC}${mod_path}",upperdir="${upperdir}",workdir="${workdir}" "${NEW_ROOT}" || run_emergency_shell for i in "${RW_MNT}" "${STATIC}" "${CDROOT_PATH}" do [ ! -d "${NEW_ROOT}${i}" ] && run mkdir -p "${NEW_ROOT}${i}" + run mount --move "${i}" "${NEW_ROOT}${i}" || run_emergency_shell done -#echo "overlay / overlay defaults 0 0" > "${NEW_ROOT}"/etc/fstab -#run mkdir -p "${NEW_ROOT}${OVERLAY}" -#run chmod 755 "${NEW_ROOT}${OVERLAY}" -#run mount --bind "${OVERLAY}" "${NEW_ROOT}${OVERLAY}" -#run mount --bind "${STATIC}" "${NEW_ROOT}${STATIC}" -#if [ -n "${mods}" ] -#then -# for i in ${mods} -# do -# run mount --bind "${OVERLAY}/.${i}" "${NEW_ROOT}/${OVERLAY}/.${i}" -# done -#fi -#[ ! -d "${NEW_ROOT}${CDROOT_PATH}" ] && mkdir -p "${NEW_ROOT}${CDROOT_PATH}" -#run mount --bind "${CDROOT_PATH}" "${NEW_ROOT}${CDROOT_PATH}" -#run mount --bind "${RW_MNT}" "${NEW_ROOT}${RW_MNT}" -run mount --move "${CDROOT_PATH}" "${NEW_ROOT}${CDROOT_PATH}" || run_emergency_shell -run mount --move "${RW_MNT}" "${NEW_ROOT}${RW_MNT}" || run_emergency_shell -run mount --move "${STATIC}" "${NEW_ROOT}${STATIC}" || run_emergency_shell + +#run mount --move "${CDROOT_PATH}" "${NEW_ROOT}${CDROOT_PATH}" || run_emergency_shell +#run mount --move "${RW_MNT}" "${NEW_ROOT}${RW_MNT}" || run_emergency_shell +#run mount --move "${STATIC}" "${NEW_ROOT}${STATIC}" || run_emergency_shell +if [ -n "${mods}" ] +then + for i in ${mods} + do + [ ! -d "${NEW_ROOT}${OVERLAY}/.${i}" ] && run mkdir -p "${NEW_ROOT}${OVERLAY}/.${i}" + run mount --bind "${OVERLAY}/.${i}" "${NEW_ROOT}/${OVERLAY}/.${i}" + done +fi #for m in ${MODULES}; do # run insmod "${NEW_ROOT}${m}" diff --git a/scripts/os.py b/scripts/os.py index 5615a9b..931482a 100644 --- a/scripts/os.py +++ b/scripts/os.py @@ -92,6 +92,9 @@ class OS: cmd += f" | sudo tee {is_append} {self.root_dir}{path} > /dev/null" Logger.os(f"\tCreate file {path}...") self.__sudo(cmd, shell=True, cwd=dir) + if ("chmod" in step): + mode = step["chmod"] + self.__sudo(f"chmod {mode} {dir}{path}", shell=True, cwd=dir) if ("chroot" in step): cmd = self.board.parse_variables(step["chroot"]) self.__chroot(cmd, dir=dir) @@ -239,8 +242,20 @@ class OS: def __finalize(self, dir): self.__stage3_steps(self.finalize, "Finalize system installation...", dir=dir) + def make_sqh_kmod(self): + mod_path = f"{ROOT_DIR}/out/modules" + os.makedirs(mod_path, exist_ok=True) + kmod_fn = self.board.parse_variables("%{out_dir}%/kmods/usr/lib/modules") + kmod = Path(kmod_fn) + for f in kmod.iterdir(): + sqh_name = f.name + self.__make_sqh(f"{kmod_fn}/../../..", f"{mod_path}/{sqh_name}.lzm") + break + def sqh(self): self.__relaunch_as_sudo() + self.make_sqh_kmod() + exit(0) date = datetime.datetime.today().strftime('%Y_%m_%d') temp_dir = f"{ROOT_DIR}/build/tmp" # pack full system via tar @@ -410,6 +425,7 @@ class OS: self.__copy_file(f"{self.board.out_sh}/uInitrd", f"{out_dir}/") Logger.install(f"\tCopy root.sqh") self.__sudo(["cp", "-H", f"{self.board.out_sh}/root.sqh", f"{out_dir}/"]) + self.__sudo(["cp", "-H", f"{self.board.out_sh}/modules", f"{out_dir}/"]) def __install_rw(self, out_dir): self.__sudo(["touch", f"{out_dir}/rw_part"], stdout=subprocess.DEVNULL) diff --git a/scripts/sources.py b/scripts/sources.py index 04fbbf8..c708f09 100644 --- a/scripts/sources.py +++ b/scripts/sources.py @@ -264,8 +264,15 @@ class Sources: # copy new configurtion, if exists shutil.copyfile(work_cfg_name, cfg_name) + def prepare_artifacts(self, artifacts, out_dir): + for art in artifacts: + if ("kmods" in art): + shutil.rmtree(f"{out_dir}/kmods") + def copy_artifacts(self, artifacts, out_dir): for art in artifacts: + if ("kmods" in art): + continue file_name = self.work_dir + "/" + art["file"] if ("subdir" in art): dir_o = out_dir + "/" + art["subdir"] + "/" diff --git a/scripts/target.py b/scripts/target.py index 049f811..1f98772 100644 --- a/scripts/target.py +++ b/scripts/target.py @@ -72,6 +72,7 @@ class Target: def build(self, sub_target, out_dir): self.source_sync() + self.sources.prepare_artifacts(self.artifacts, out_dir) if (not self.no_build): opts = self.makeopts.split(" ") config = ""