From 6dd651ff99e7e1e2aa612ce0603de6fcb914c1dc Mon Sep 17 00:00:00 2001 From: Jonatan Schlag Date: Sun, 23 Jun 2019 16:25:53 +0200 Subject: [PATCH] Improve how we execute commands in chroot Signed-off-by: Jonatan Schlag --- functions.sh | 20 ++++++++++++++++++++ generate_image.sh | 33 +++++++++++---------------------- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/functions.sh b/functions.sh index f666c30..ad46a30 100644 --- a/functions.sh +++ b/functions.sh @@ -20,6 +20,7 @@ FALSE=1 IMAGE_RELEASE=${FALSE} +CHROOT_SCRIPT="/build/chroot.sh" # Functions @@ -356,3 +357,22 @@ chroot_wrapper() { umount "${chroot_dir}/run" umount "${chroot_dir}/tmp" } + +chroot_script_init(){ + echo "#!/bin/bash" > "${IMAGE_MOUNT_DIR}${CHROOT_SCRIPT}" +} + +chroot_script_add_cmd(){ + local command + for command in "$@" + do + echo "${command}" >> "${IMAGE_MOUNT_DIR}${CHROOT_SCRIPT}" + done +} + + +chroot_script_exec() { + chmod +x "${IMAGE_MOUNT_DIR}${CHROOT_SCRIPT}" + + chroot_wrapper ${IMAGE_MOUNT_DIR} "${CHROOT_SCRIPT}" +} diff --git a/generate_image.sh b/generate_image.sh index 88e4506..674cc22 100755 --- a/generate_image.sh +++ b/generate_image.sh @@ -172,40 +172,29 @@ reset_root_password "${IMAGE_MOUNT_DIR}" clone_git_repos "${IMAGE_MOUNT_DIR}/build" ${GIT_REPOS} + # ## Prepare chrooting into the image. # -# Check if the network stack should be build. -if [ "${USE_GIT_NETWORK_STACK}" == "True" ]; then - BUILD_NETWORK_CMDS="cd network/ && ./autogen.sh && ./configure && make && make install" -fi - -ENABLE_GETTY="/bin/systemctl enable getty@.service" +chroot_script_init -# Check if the arch uses grub if [ "${HAVE_GRUB}" == "True" ]; then - GENERATE_GRUB_CONF="grub-install --boot-directory=${IMAGE_MOUNT_DIR}/boot/ --modules="${FILESYSTEM} part_msdos" ${outlo} && \ - grub-mkconfig -o /boot/grub/grub.cfg" + chroot_script_add_cmd "grub-install --boot-directory=/boot/ ${outlo}" "grub-mkconfig -o /boot/grub/grub.cfg" fi -# Use systemd-nspawn to spawn a chroot environment and execute -# commands inside it. -# -# The first command enables the terminal on TTY1. -# The second command generates the configuration file for grub2. + +# ENABLE Serial Console +chroot_script_add_cmd "/bin/systemctl enable getty@.service" -systemd-nspawn -D ${IMAGE_MOUNT_DIR} --bind /dev --capability=CAP_SYS_ADMIN,CAP_SYS_RAWIO --bind /proc --bind /sys << END - echo "Execute commands inside chroot" - ${ENABLE_GETTY} - ${GENERATE_GRUB_CONF} - cd /build/ && ls - ${BUILD_NETWORK_CMDS} - echo "All commands executed" -END +# Check if the network stack should be build. +if [ "${USE_GIT_NETWORK_STACK}" == "True" ]; then + chroot_script_add_cmd "cd /build/network" "./autogen.sh" "./configure" "make" "make install" +fi +chroot_script_exec # Insert the UUID because grub-mkconfig often fails to -- 2.39.2