From 2fb4756a7f071f23dc971ee8f9a7454b19ef1e44 Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Thu, 28 Mar 2024 23:45:10 +0000 Subject: [PATCH] add soft restart option to liminix-rebuild instead of doing a full reboot, it runs activate / and uses s6-rc-update to install the new service database --- pkgs/min-copy-closure/liminix-rebuild.sh | 29 +++++++++++---- pkgs/s6-rc-database/default.nix | 15 +++++++- pkgs/systemconfig/default.nix | 4 +- pkgs/systemconfig/restart-services.fnl | 37 +++++++++++++++++++ pkgs/systemconfig/test-hashes | 12 ++++++ pkgs/systemconfig/test-hashes2 | 12 ++++++ .../min-copy-closure/test-liminix-rebuild.sh | 24 +++++++++--- .../wait-for-soft-restart.expect | 13 +++++++ tests/min-copy-closure/with-figlet.nix | 7 ++++ 9 files changed, 138 insertions(+), 15 deletions(-) create mode 100644 pkgs/systemconfig/restart-services.fnl create mode 100644 pkgs/systemconfig/test-hashes create mode 100644 pkgs/systemconfig/test-hashes2 create mode 100644 tests/min-copy-closure/wait-for-soft-restart.expect diff --git a/pkgs/min-copy-closure/liminix-rebuild.sh b/pkgs/min-copy-closure/liminix-rebuild.sh index 5dda92c..16ec7f1 100755 --- a/pkgs/min-copy-closure/liminix-rebuild.sh +++ b/pkgs/min-copy-closure/liminix-rebuild.sh @@ -2,12 +2,18 @@ ssh_command=${SSH_COMMAND-ssh} -if [ "$1" = "--no-reboot" ] ; then - reboot="true" - shift -else - reboot="reboot" -fi +reboot="reboot" + +case "$1" in + "--no-reboot") + unset reboot + shift + ;; + "--fast") + reboot="soft" + shift + ;; +esac target_host=$1 shift @@ -21,7 +27,16 @@ if toplevel=$(nix-build "$@" -A outputs.systemConfiguration --no-out-link); then echo systemConfiguration $toplevel min-copy-closure $target_host $toplevel $ssh_command $target_host $toplevel/bin/install - $ssh_command $target_host "sync; source /etc/profile; reboot" + case "$reboot" in + reboot) + $ssh_command $target_host "sync; source /etc/profile; reboot" + ;; + soft) + $ssh_command $target_host $toplevel/bin/restart-services + ;; + *) + ;; + esac else echo Rebuild failed fi diff --git a/pkgs/s6-rc-database/default.nix b/pkgs/s6-rc-database/default.nix index 9983bbf..1fabe41 100644 --- a/pkgs/s6-rc-database/default.nix +++ b/pkgs/s6-rc-database/default.nix @@ -21,11 +21,22 @@ in stdenvNoCC.mkDerivation { if test -d $i; then for j in $i/* ; do if test -f $j/type ; then + case $(cat $j/type) in + longrun|oneshot) + # s6-rc-update only wants oneshots in its + # restarts file + echo $(basename $j) " " $i >> $out/hashes + ;; + *) + ;; + esac srcs="$srcs $i" fi done fi done s6-rc-compile $out/compiled $srcs - ''; - } + s6-rc-db -c $out/compiled contents default + mv $out/hashes $out/compiled + ''; +} diff --git a/pkgs/systemconfig/default.nix b/pkgs/systemconfig/default.nix index f6f38b1..f6a6f9d 100644 --- a/pkgs/systemconfig/default.nix +++ b/pkgs/systemconfig/default.nix @@ -6,6 +6,7 @@ { writeText +, writeFennelScript , lib , s6-init-bin , closureInfo @@ -52,7 +53,7 @@ let chown = if uid>0 || gid>0 then "\nCHOWN(${qpathname},${toString uid},${toString gid});\n" else ""; - in "${cmd} ${chown}"; + in "unlink(${qpathname}); ${cmd} ${chown}"; in mapAttrsToList (makeFile prefix) attrset; activateScript = attrset: writeText "makedevs.c" '' #include "defs.h" @@ -80,6 +81,7 @@ in attrset: cp $closure/store-paths $out/etc/nix-store-paths $STRIP --remove-section=.note --remove-section=.comment --strip-all makedevs -o $out/bin/activate ln -s ${s6-init-bin}/bin/init $out/bin/init + cp -p ${writeFennelScript "restart-services" [] ./restart-services.fnl} $out/bin/restart-services cat > $out/bin/install < rootfs -truncate -s 24M rootfs +truncate -s 32M rootfs resize2fs rootfs dd if=rootfs of=disk-image bs=512 seek=4 conv=sync @@ -51,9 +62,12 @@ echo "READY" touch known_hosts export SSH_COMMAND="ssh -o UserKnownHostsFile=${work}/known_hosts -o StrictHostKeyChecking=no -p 2022 -i ${here}/id" -(cd ${top} && liminix-rebuild root@localhost -I liminix-config=${here}/with-figlet.nix --arg device "import ./devices/qemu-armv7l") +if test -n "$FAST"; then + (cd ${top} && liminix-rebuild --fast root@localhost -I liminix-config=${here}/with-figlet.nix --argstr deviceName qemu-armv7l) + cd ${work} && expect $here/wait-for-soft-restart.expect +else + (cd ${top} && liminix-rebuild root@localhost -I liminix-config=${here}/with-figlet.nix --arg device "import ./devices/qemu-armv7l") + cd ${work} && expect $here/wait-for-reboot.expect +fi -ls -l vm - -cd ${work} && expect $here/wait-for-reboot.expect cd / ; rm -rf $work diff --git a/tests/min-copy-closure/wait-for-soft-restart.expect b/tests/min-copy-closure/wait-for-soft-restart.expect new file mode 100644 index 0000000..381f7a2 --- /dev/null +++ b/tests/min-copy-closure/wait-for-soft-restart.expect @@ -0,0 +1,13 @@ +proc chat {instr outstr} { + expect { + $instr { send $outstr } + timeout { exit 1 } + } +} +spawn socat unix-connect:vm/console - + +send "exit\r" +chat "BusyBox" "\r" +chat "#" "stty -echo; type -p figlet\r" +chat "figlet-armv7l-unknown-linux" "s6-rc -a list |grep w\\inkle\r" +chat "winkle" "poweroff\r" diff --git a/tests/min-copy-closure/with-figlet.nix b/tests/min-copy-closure/with-figlet.nix index 5614d92..09ec47c 100644 --- a/tests/min-copy-closure/with-figlet.nix +++ b/tests/min-copy-closure/with-figlet.nix @@ -4,4 +4,11 @@ defaultProfile.packages = with pkgs; [ figlet ]; + services.ripvanwinkle = pkgs.liminix.services.longrun { + name = "winkle"; + run = '' + echo SLEEPING > /dev/console + sleep 3600 + ''; + }; }