From 94bc0f5bb0dff8b98597f1eb66a7156469932221 Mon Sep 17 00:00:00 2001 From: Felix Uhl Date: Tue, 22 Oct 2024 23:05:22 +0200 Subject: [PATCH] mode destroy: Add confirmation dialogue The new confirmation dialogue is only shown for the new outputs introduced in the previous commits. The existing outputs do not change behavior to keep backwards compatibility. Fixes #725 --- disko | 16 +++++++++-- docs/reference.md | 2 ++ lib/default.nix | 62 +++++++++++++++++++++++++++++++++++++---- lib/make-disk-image.nix | 2 +- lib/tests.nix | 4 +-- 5 files changed, 76 insertions(+), 10 deletions(-) diff --git a/disko b/disko index 28a79e5..97700b6 100755 --- a/disko +++ b/disko @@ -9,6 +9,7 @@ declare disko_config # mount was chosen as the default mode because it's less destructive mode=mount nix_args=() +skip_destroy_safety_check=false # DISKO_VERSION is set by the wrapper in package.nix DISKO_VERSION="${DISKO_VERSION:="unknown! This is a bug, please report it!"}" @@ -44,6 +45,8 @@ Options: * --no-deps avoid adding another dependency closure to an in-memory installer requires all necessary dependencies to be available in the environment +* --yes-wipe-all-disks + skip the safety check for destroying partitions, useful for automation * --debug run with set -x * --help @@ -95,6 +98,9 @@ while [[ $# -gt 0 ]]; do --no-deps) nix_args+=(--arg noDeps true) ;; + --yes-wipe-all-disks) + skip_destroy_safety_check=true + ;; --show-trace) nix_args+=("$1") ;; @@ -169,8 +175,14 @@ script=$(nixBuild "${libexec_dir}"/cli.nix \ --argstr mode "$mode" \ "${nix_args[@]}" ) + +command=("$(echo "$script"/bin/*)") +if [[ $mode = "destroy,format,mount" && $skip_destroy_safety_check = true ]]; then + command+=("--yes-wipe-all-disks") +fi + if [[ -n "${dry_run+x}" ]]; then - echo "$script" + echo "${command[@]}" else - exec "$script" + exec "${command[@]}" fi diff --git a/docs/reference.md b/docs/reference.md index bb1a0ba..3d390b9 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -42,4 +42,6 @@ Options: requires all necessary dependencies to be available in the environment * --debug run with set -x +* --yes-wipe-all-disks + skip the safety check for destroying partitions, useful for automation ``` diff --git a/lib/default.nix b/lib/default.nix index 78dcb95..9100555 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -462,7 +462,7 @@ let ''; destroyFormatMount = (diskoLib.writeCheckedBash { inherit pkgs checked; }) "/bin/disko-destroy-format-mount" '' export PATH=${lib.makeBinPath ((cfg.config._packages pkgs) ++ [ pkgs.bash ] ++ destroyDependencies)}:$PATH - ${cfg.config._disko} + ${cfg.config._destroyFormatMount} ''; # These are useful to skip copying executables uploading a script to an in-memory installer @@ -479,7 +479,7 @@ let ${cfg.config._formatMount} ''; destroyFormatMountNoDeps = (diskoLib.writeCheckedBash { inherit pkgs checked; noDeps = true; }) "/bin/disko-destroy-format-mount" '' - ${cfg.config._disko} + ${cfg.config._destroyFormatMount} ''; @@ -490,7 +490,7 @@ let # `user.users..packages` and `home.packages`, see https://github.com/nix-community/disko/issues/454 destroyScript = (diskoLib.writeCheckedBash { inherit pkgs checked; }) "disko-destroy" '' export PATH=${lib.makeBinPath destroyDependencies}:$PATH - ${cfg.config._destroy} + ${cfg.config._legacyDestroy} ''; formatScript = (diskoLib.writeCheckedBash { inherit pkgs checked; }) "disko-format" '' @@ -510,7 +510,7 @@ let # These are useful to skip copying executables uploading a script to an in-memory installer destroyScriptNoDeps = (diskoLib.writeCheckedBash { inherit pkgs checked; noDeps = true; }) "disko-destroy" '' - ${cfg.config._destroy} + ${cfg.config._legacyDestroy} ''; formatScriptNoDeps = (diskoLib.writeCheckedBash { inherit pkgs checked; noDeps = true; }) "disko-format" '' @@ -526,11 +526,12 @@ let ''; }; }; - _destroy = lib.mkOption { + _legacyDestroy = lib.mkOption { internal = true; type = lib.types.str; description = '' The script to unmount (& destroy) all devices defined by disko.devices + Does not ask for confirmation! Depracated in favor of _destroy ''; default = '' umount -Rv "${rootMountPoint}" || : @@ -541,6 +542,44 @@ let done ''; }; + _destroy = lib.mkOption { + internal = true; + type = lib.types.str; + description = '' + The script to unmount (& destroy) all devices defined by disko.devices + ''; + default = + let + selectedDisks = lib.escapeShellArgs (lib.catAttrs "device" (lib.attrValues devices.disk)); + in + '' + if [ "$1" != "--yes-wipe-all-disks" ]; then + echo "WARNING: This will destroy all data on the disks defined in disko.devices, which are:" + echo + # shellcheck disable=SC2043 + for dev in ${selectedDisks}; do + echo " - $dev" + done + echo + echo " (If you want to skip this dialogue, pass --yes-wipe-all-disks)" + echo + echo "Are you sure you want to wipe the devices listed above?" + read -rp "Type 'yes' to continue, anything else to abort: " confirmation + + if [ "$confirmation" != "yes" ]; then + echo "Aborted." + exit 1 + fi + fi + + umount -Rv "${rootMountPoint}" || : + + # shellcheck disable=SC2043 + for dev in ${selectedDisks}; do + $BASH ${../disk-deactivate}/disk-deactivate "$dev" + done + ''; + }; _create = lib.mkOption { internal = true; type = lib.types.str; @@ -586,6 +625,19 @@ let type = lib.types.str; description = '' The script to umount, create and mount all devices defined by disko.devices + Deprecated in favor of _destroyFormatMount + ''; + default = '' + ${cfg.config._legacyDestroy} + ${cfg.config._create} + ${cfg.config._mount} + ''; + }; + _destroyFormatMount = lib.mkOption { + internal = true; + type = lib.types.str; + description = '' + The script to unmount, create and mount all devices defined by disko.devices ''; default = '' ${cfg.config._destroy} diff --git a/lib/make-disk-image.nix b/lib/make-disk-image.nix index 55d7d25..8ff1cd1 100644 --- a/lib/make-disk-image.nix +++ b/lib/make-disk-image.nix @@ -84,7 +84,7 @@ let ${lib.optionalString diskoCfg.testMode '' export IN_DISKO_TEST=1 ''} - ${lib.getExe systemToInstall.config.system.build.destroyFormatMount} + ${lib.getExe systemToInstall.config.system.build.destroyFormatMount} --yes-wipe-all-disks ''; installer = lib.optionalString cfg.copyNixStore '' diff --git a/lib/tests.nix b/lib/tests.nix index 5160cff..65bfd1a 100644 --- a/lib/tests.nix +++ b/lib/tests.nix @@ -263,7 +263,7 @@ let machine.succeed("${lib.getExe tsp-format}") machine.succeed("${lib.getExe tsp-mount}") machine.succeed("${lib.getExe tsp-mount}") # verify that mount is idempotent - machine.succeed("${lib.getExe tsp-disko}") # verify that we can destroy and recreate + machine.succeed("${lib.getExe tsp-disko} --yes-wipe-all-disks") # verify that we can destroy and recreate machine.succeed("mkdir -p /mnt/home") machine.succeed("touch /mnt/home/testfile") machine.succeed("${lib.getExe tsp-format}") # verify that format is idempotent @@ -274,7 +274,7 @@ let machine.succeed("${lib.getExe nodes.machine.system.build.format}") machine.succeed("${lib.getExe nodes.machine.system.build.mount}") machine.succeed("${lib.getExe nodes.machine.system.build.mount}") # verify that mount is idempotent - machine.succeed("${lib.getExe nodes.machine.system.build.destroyFormatMount}") # verify that we can destroy and recreate again + machine.succeed("${lib.getExe nodes.machine.system.build.destroyFormatMount} --yes-wipe-all-disks") # verify that we can destroy and recreate again machine.succeed("mkdir -p /mnt/home") machine.succeed("touch /mnt/home/testfile") machine.succeed("${lib.getExe nodes.machine.system.build.format}") # verify that format is idempotent