-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
disko
executable file
·193 lines (173 loc) · 4.99 KB
/
disko
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env bash
set -euo pipefail
readonly libexec_dir="${0%/*}"
# a file with the disko config
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!"}"
onlyPrintVersion=false
showUsage() {
cat <<USAGE
Usage: $0 [options] disk-config.nix
or $0 [options] --flake github:somebody/somewhere#disk-config
With flakes, disk-config is discovered first under the .diskoConfigurations top level attribute
or else from the disko module of a NixOS configuration of that name under .nixosConfigurations.
Options:
* -m, --mode mode
set the mode, either distroy, format, mount, format,mount or destroy,format,mount
destroy: unmount filesystems and destroy partition tables of the selected disks
format: create partition tables, zpools, lvms, raids and filesystems if they don't exist yet
mount: mount the partitions at the specified root-mountpoint
format,mount: run format and mount in sequence
destroy,format,mount: run all three modes in sequence. Previously known as --mode disko
* -f, --flake uri
fetch the disko config relative to this flake's root
* --arg name value
pass value to nix-build. can be used to set disk-names for example
* --argstr name value
pass value to nix-build as string
* --root-mountpoint /some/other/mnt
where to mount the device tree (default: /mnt)
* --dry-run
just show the path to the script instead of running it
* --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
show this help
USAGE
}
abort() {
echo "aborted: $*" >&2
exit 1
}
## Main ##
[[ $# -eq 0 ]] && {
showUsage
exit 1
}
while [[ $# -gt 0 ]]; do
case "$1" in
--debug)
set -x
;;
-m | --mode)
mode=$2
shift
;;
-f | --flake)
flake=$2
shift
;;
--argstr | --arg)
nix_args+=("$1" "$2" "$3")
shift
shift
;;
-h | --help)
showUsage
exit 0
;;
--dry-run)
dry_run=y
;;
--root-mountpoint)
nix_args+=(--argstr rootMountPoint "$2")
shift
;;
--no-deps)
nix_args+=(--arg noDeps true)
;;
--yes-wipe-all-disks)
skip_destroy_safety_check=true
;;
--show-trace)
nix_args+=("$1")
;;
--version)
onlyPrintVersion=true
;;
*)
if [ -z ${disko_config+x} ]; then
disko_config=$1
else
showUsage
exit 1
fi
;;
esac
shift
done
if [[ "$onlyPrintVersion" = true ]]; then
echo "$DISKO_VERSION"
exit 0
fi
# Always print version information to help with debugging
echo "disko version $DISKO_VERSION"
nixBuild() {
if command -v nom-build > /dev/null; then
nom-build "$@"
else
nix-build "$@"
fi
}
if ! {
# Base modes
[[ $mode = "destroy" ]] || [[ $mode = "format" ]] || [[ $mode = "mount" ]] ||
# Combined modes
[[ $mode = "format,mount" ]] ||
[[ $mode = "destroy,format,mount" ]] || # Replaces --mode disko
# Legacy modes, will be removed in next major version
[[ $mode = "disko" ]] || [[ $mode = "create" ]] || [[ $mode = "zap_create_mount" ]] ;
}; then
abort 'mode must be one of "destroy", "format", "mount", "destroy,format,mount" or "format,mount"'
fi
if [[ -n "${flake+x}" ]]; then
if [[ $flake =~ ^(.*)\#([^\#\"]*)$ ]]; then
flake="${BASH_REMATCH[1]}"
flakeAttr="${BASH_REMATCH[2]}"
fi
if [[ -z "${flakeAttr-}" ]]; then
echo "Please specify the name of the NixOS configuration to be installed, as a URI fragment in the flake-uri."
echo "For example, to use the output diskoConfigurations.foo from the flake.nix, append \"#foo\" to the flake-uri."
exit 1
fi
if [[ -e "$flake" ]]; then
flake="$(realpath "$flake")"
fi
nix_args+=("--arg" "flake" "\"$flake\"")
nix_args+=("--argstr" "flakeAttr" "$flakeAttr")
nix_args+=(--extra-experimental-features flakes)
elif [[ -n "${disko_config+x}" ]] && [[ -e "$disko_config" ]]; then
nix_args+=("--arg" "diskoFile" "$(realpath "$disko_config")")
else
abort "disko config must be an existing file or flake must be set"
fi
# The "--impure" is still pure, as the path is within the nix store.
script=$(nixBuild "${libexec_dir}"/cli.nix \
--no-out-link \
--impure \
--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
# Legacy modes don't support --yes-wipe-all-disks and are not in `$script/bin`
if [[ $mode = "disko" ]] || [[ $mode = "create" ]] || [[ $mode = "zap_create_mount" ]] ; then
command=("$script")
fi
if [[ -n "${dry_run+x}" ]]; then
echo "${command[@]}"
else
exec "${command[@]}"
fi