3
0
Fork 0
forked from mirrors/nixpkgs

nixos-rebuild: Add ‘dry-activate’ command

‘nixos-rebuild dry-activate’ builds the new configuration and then
prints what systemd services would be stopped, restarted etc. if the
configuration were actually activated. This could be extended later to
show other activation actions (like uids being deleted).

To prevent confusion, ‘nixos-rebuild dry-run’ has been renamed to
‘nixos-rebuild dry-build’.
This commit is contained in:
Eelco Dolstra 2015-03-09 16:23:23 +01:00
parent e8b33876af
commit a574065a81
3 changed files with 107 additions and 52 deletions

View file

@ -1,7 +1,7 @@
<refentry xmlns="http://docbook.org/ns/docbook" <refentry xmlns="http://docbook.org/ns/docbook"
xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xi="http://www.w3.org/2001/XInclude"> xmlns:xi="http://www.w3.org/2001/XInclude">
<refmeta> <refmeta>
<refentrytitle><command>nixos-rebuild</command></refentrytitle> <refentrytitle><command>nixos-rebuild</command></refentrytitle>
<manvolnum>8</manvolnum> <manvolnum>8</manvolnum>
@ -22,7 +22,8 @@
<arg choice='plain'><option>boot</option></arg> <arg choice='plain'><option>boot</option></arg>
<arg choice='plain'><option>test</option></arg> <arg choice='plain'><option>test</option></arg>
<arg choice='plain'><option>build</option></arg> <arg choice='plain'><option>build</option></arg>
<arg choice='plain'><option>dry-run</option></arg> <arg choice='plain'><option>dry-build</option></arg>
<arg choice='plain'><option>dry-activate</option></arg>
<arg choice='plain'><option>build-vm</option></arg> <arg choice='plain'><option>build-vm</option></arg>
<arg choice='plain'><option>build-vm-with-bootloader</option></arg> <arg choice='plain'><option>build-vm-with-bootloader</option></arg>
</group> </group>
@ -114,10 +115,22 @@ $ nix-build /path/to/nixpkgs/nixos -A system
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>
<term><option>dry-run</option></term> <term><option>dry-build</option></term>
<listitem> <listitem>
<para>Simply show what store paths would be built or downloaded <para>Show what store paths would be built or downloaded by any
by any of the operations above.</para> of the operations above, but otherwise do nothing.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>dry-activate</option></term>
<listitem>
<para>Build the new configuration, but instead of activating it,
show what changes would be performed by the activation (i.e. by
<command>nixos-rebuild test</command>). For
instance, this command will print which systemd units would be
restarted. The list of changes is not guaranteed to be
complete.</para>
</listitem> </listitem>
</varlistentry> </varlistentry>

View file

@ -26,7 +26,8 @@ while [ "$#" -gt 0 ]; do
--help) --help)
showSyntax showSyntax
;; ;;
switch|boot|test|build|dry-run|build-vm|build-vm-with-bootloader) switch|boot|test|build|dry-build|dry-run|dry-activate|build-vm|build-vm-with-bootloader)
if [ "$i" = dry-run ]; then i=dry-build; fi
action="$i" action="$i"
;; ;;
--install-grub) --install-grub)
@ -137,7 +138,7 @@ fi
# First build Nix, since NixOS may require a newer version than the # First build Nix, since NixOS may require a newer version than the
# current one. # current one.
if [ -n "$rollback" -o "$action" = dry-run ]; then if [ -n "$rollback" -o "$action" = dry-build ]; then
buildNix= buildNix=
fi fi
@ -180,7 +181,7 @@ if [ -n "$canRun" ]; then
fi fi
if [ "$action" = dry-run ]; then if [ "$action" = dry-build ]; then
extraBuildFlags+=(--dry-run) extraBuildFlags+=(--dry-run)
fi fi
@ -193,7 +194,7 @@ if [ -z "$rollback" ]; then
if [ "$action" = switch -o "$action" = boot ]; then if [ "$action" = switch -o "$action" = boot ]; then
nix-env "${extraBuildFlags[@]}" -p "$profile" -f '<nixpkgs/nixos>' --set -A system nix-env "${extraBuildFlags[@]}" -p "$profile" -f '<nixpkgs/nixos>' --set -A system
pathToConfig="$profile" pathToConfig="$profile"
elif [ "$action" = test -o "$action" = build -o "$action" = dry-run ]; then elif [ "$action" = test -o "$action" = build -o "$action" = dry-build -o "$action" = dry-activate ]; then
nix-build '<nixpkgs/nixos>' -A system -k "${extraBuildFlags[@]}" > /dev/null nix-build '<nixpkgs/nixos>' -A system -k "${extraBuildFlags[@]}" > /dev/null
pathToConfig=./result pathToConfig=./result
elif [ "$action" = build-vm ]; then elif [ "$action" = build-vm ]; then
@ -224,7 +225,7 @@ fi
# If we're not just building, then make the new configuration the boot # If we're not just building, then make the new configuration the boot
# default and/or activate it now. # default and/or activate it now.
if [ "$action" = switch -o "$action" = boot -o "$action" = test ]; then if [ "$action" = switch -o "$action" = boot -o "$action" = test -o "$action" = dry-activate ]; then
if ! $pathToConfig/bin/switch-to-configuration "$action"; then if ! $pathToConfig/bin/switch-to-configuration "$action"; then
echo "warning: error(s) occured while switching to the new configuration" >&2 echo "warning: error(s) occured while switching to the new configuration" >&2
exit 1 exit 1

View file

@ -9,19 +9,21 @@ use Cwd 'abs_path';
my $out = "@out@"; my $out = "@out@";
# To be robust against interruption, record what units need to be started etc.
my $startListFile = "/run/systemd/start-list"; my $startListFile = "/run/systemd/start-list";
my $restartListFile = "/run/systemd/restart-list"; my $restartListFile = "/run/systemd/restart-list";
my $reloadListFile = "/run/systemd/reload-list"; my $reloadListFile = "/run/systemd/reload-list";
my $action = shift @ARGV; my $action = shift @ARGV;
if (!defined $action || ($action ne "switch" && $action ne "boot" && $action ne "test")) { if (!defined $action || ($action ne "switch" && $action ne "boot" && $action ne "test" && $action ne "dry-activate")) {
print STDERR <<EOF; print STDERR <<EOF;
Usage: $0 [switch|boot|test] Usage: $0 [switch|boot|test]
switch: make the configuration the boot default and activate now switch: make the configuration the boot default and activate now
boot: make the configuration the boot default boot: make the configuration the boot default
test: activate the configuration, but don\'t make it the boot default test: activate the configuration, but don\'t make it the boot default
dry-activate: show what would be done if this configuration were activated
EOF EOF
exit 1; exit 1;
} }
@ -56,8 +58,6 @@ EOF
exit 100; exit 100;
} }
syslog(LOG_NOTICE, "switching to system configuration $out");
# Ignore SIGHUP so that we're not killed if we're running on (say) # Ignore SIGHUP so that we're not killed if we're running on (say)
# virtual console 1 and we restart the "tty1" unit. # virtual console 1 and we restart the "tty1" unit.
$SIG{PIPE} = "IGNORE"; $SIG{PIPE} = "IGNORE";
@ -116,6 +116,11 @@ sub boolIsTrue {
return $s eq "yes" || $s eq "true"; return $s eq "yes" || $s eq "true";
} }
sub recordUnit {
my ($fn, $unit) = @_;
write_file($fn, { append => 1 }, "$unit\n") if $action ne "dry-activate";
}
# As a fingerprint for determining whether a unit has changed, we use # As a fingerprint for determining whether a unit has changed, we use
# its absolute path. If it has an override file, we append *its* # its absolute path. If it has an override file, we append *its*
# absolute path as well. # absolute path as well.
@ -124,9 +129,18 @@ sub fingerprintUnit {
return abs_path($s) . (-f "${s}.d/overrides.conf" ? " " . abs_path "${s}.d/overrides.conf" : ""); return abs_path($s) . (-f "${s}.d/overrides.conf" ? " " . abs_path "${s}.d/overrides.conf" : "");
} }
# Stop all services that no longer exist or have changed in the new # Figure out what units need to be stopped, started, restarted or reloaded.
# configuration. my (%unitsToStop, %unitsToSkip, %unitsToStart, %unitsToRestart, %unitsToReload);
my (@unitsToStop, @unitsToSkip);
$unitsToStart{$_} = 1 foreach
split('\n', read_file($startListFile, err_mode => 'quiet') // "");
$unitsToRestart{$_} = 1 foreach
split('\n', read_file($restartListFile, err_mode => 'quiet') // "");
$unitsToReload{$_} = 1 foreach
split '\n', read_file($reloadListFile, err_mode => 'quiet') // "";
my $activePrev = getActiveUnits; my $activePrev = getActiveUnits;
while (my ($unit, $state) = each %{$activePrev}) { while (my ($unit, $state) = each %{$activePrev}) {
my $baseUnit = $unit; my $baseUnit = $unit;
@ -141,7 +155,7 @@ while (my ($unit, $state) = each %{$activePrev}) {
if (-e $prevUnitFile && ($state->{state} eq "active" || $state->{state} eq "activating")) { if (-e $prevUnitFile && ($state->{state} eq "active" || $state->{state} eq "activating")) {
if (! -e $newUnitFile || abs_path($newUnitFile) eq "/dev/null") { if (! -e $newUnitFile || abs_path($newUnitFile) eq "/dev/null") {
push @unitsToStop, $unit; $unitsToStop{$unit} = 1;
} }
elsif ($unit =~ /\.target$/) { elsif ($unit =~ /\.target$/) {
@ -155,7 +169,8 @@ while (my ($unit, $state) = each %{$activePrev}) {
# should not be the case. Just ignore it. # should not be the case. Just ignore it.
if ($unit ne "suspend.target" && $unit ne "hibernate.target" && $unit ne "hybrid-sleep.target") { if ($unit ne "suspend.target" && $unit ne "hibernate.target" && $unit ne "hybrid-sleep.target") {
unless (boolIsTrue($unitInfo->{'RefuseManualStart'} // "no")) { unless (boolIsTrue($unitInfo->{'RefuseManualStart'} // "no")) {
write_file($startListFile, { append => 1 }, "$unit\n"); $unitsToStart{$unit} = 1;
recordUnit($startListFile, $unit);
} }
} }
@ -171,7 +186,7 @@ while (my ($unit, $state) = each %{$activePrev}) {
# (unless there is a PartOf dependency), so this is just a # (unless there is a PartOf dependency), so this is just a
# bookkeeping thing to get systemd to do the right thing. # bookkeeping thing to get systemd to do the right thing.
if (boolIsTrue($unitInfo->{'X-StopOnReconfiguration'} // "no")) { if (boolIsTrue($unitInfo->{'X-StopOnReconfiguration'} // "no")) {
push @unitsToStop, $unit; $unitsToStop{$unit} = 1;
} }
} }
@ -180,16 +195,18 @@ while (my ($unit, $state) = each %{$activePrev}) {
# Do nothing. These cannot be restarted directly. # Do nothing. These cannot be restarted directly.
} elsif ($unit =~ /\.mount$/) { } elsif ($unit =~ /\.mount$/) {
# Reload the changed mount unit to force a remount. # Reload the changed mount unit to force a remount.
write_file($reloadListFile, { append => 1 }, "$unit\n"); $unitsToReload{$unit} = 1;
recordUnit($reloadListFile, $unit);
} elsif ($unit =~ /\.socket$/ || $unit =~ /\.path$/ || $unit =~ /\.slice$/) { } elsif ($unit =~ /\.socket$/ || $unit =~ /\.path$/ || $unit =~ /\.slice$/) {
# FIXME: do something? # FIXME: do something?
} else { } else {
my $unitInfo = parseUnit($newUnitFile); my $unitInfo = parseUnit($newUnitFile);
if (boolIsTrue($unitInfo->{'X-ReloadIfChanged'} // "no")) { if (boolIsTrue($unitInfo->{'X-ReloadIfChanged'} // "no")) {
write_file($reloadListFile, { append => 1 }, "$unit\n"); $unitsToReload{$unit} = 1;
recordUnit($reloadListFile, $unit);
} }
elsif (!boolIsTrue($unitInfo->{'X-RestartIfChanged'} // "yes") || boolIsTrue($unitInfo->{'RefuseManualStop'} // "no") ) { elsif (!boolIsTrue($unitInfo->{'X-RestartIfChanged'} // "yes") || boolIsTrue($unitInfo->{'RefuseManualStop'} // "no") ) {
push @unitsToSkip, $unit; $unitsToSkip{$unit} = 1;
} else { } else {
# If this unit is socket-activated, then stop the # If this unit is socket-activated, then stop the
# socket unit(s) as well, and restart the # socket unit(s) as well, and restart the
@ -202,8 +219,9 @@ while (my ($unit, $state) = each %{$activePrev}) {
} }
foreach my $socket (@sockets) { foreach my $socket (@sockets) {
if (defined $activePrev->{$socket}) { if (defined $activePrev->{$socket}) {
push @unitsToStop, $socket; $unitsToStop{$unit} = 1;
write_file($startListFile, { append => 1 }, "$socket\n"); $unitsToStart{$unit} = 1;
recordUnit($startListFile, $socket);
$socketActivated = 1; $socketActivated = 1;
} }
} }
@ -213,7 +231,8 @@ while (my ($unit, $state) = each %{$activePrev}) {
# This unit should be restarted instead of # This unit should be restarted instead of
# stopped and started. # stopped and started.
write_file($restartListFile, { append => 1 }, "$unit\n"); $unitsToRestart{$unit} = 1;
recordUnit($restartListFile, $unit);
} else { } else {
@ -222,10 +241,11 @@ while (my ($unit, $state) = each %{$activePrev}) {
# We write this to a file to ensure that the # We write this to a file to ensure that the
# service gets restarted if we're interrupted. # service gets restarted if we're interrupted.
if (!$socketActivated) { if (!$socketActivated) {
write_file($startListFile, { append => 1 }, "$unit\n"); $unitsToStart{$unit} = 1;
recordUnit($startListFile, $unit);
} }
push @unitsToStop, $unit; $unitsToStop{$unit} = 1;
} }
} }
@ -268,14 +288,16 @@ foreach my $mountPoint (keys %$prevFss) {
my $unit = pathToUnitName($mountPoint) . ".mount"; my $unit = pathToUnitName($mountPoint) . ".mount";
if (!defined $new) { if (!defined $new) {
# Filesystem entry disappeared, so unmount it. # Filesystem entry disappeared, so unmount it.
push @unitsToStop, $unit; $unitsToStop{$unit} = 1;
} elsif ($prev->{fsType} ne $new->{fsType} || $prev->{device} ne $new->{device}) { } elsif ($prev->{fsType} ne $new->{fsType} || $prev->{device} ne $new->{device}) {
# Filesystem type or device changed, so unmount and mount it. # Filesystem type or device changed, so unmount and mount it.
write_file($startListFile, { append => 1 }, "$unit\n"); $unitsToStop{$unit} = 1;
push @unitsToStop, $unit; $unitsToStart{$unit} = 1;
recordUnit($startListFile, $unit);
} elsif ($prev->{options} ne $new->{options}) { } elsif ($prev->{options} ne $new->{options}) {
# Mount options changes, so remount it. # Mount options changes, so remount it.
write_file($reloadListFile, { append => 1 }, "$unit\n"); $unitsToReload{$unit} = 1;
recordUnit($reloadListFile, $unit);
} }
} }
@ -294,14 +316,36 @@ foreach my $device (keys %$prevSwaps) {
# FIXME: update swap options (i.e. its priority). # FIXME: update swap options (i.e. its priority).
} }
if (scalar @unitsToStop > 0) {
@unitsToStop = unique(@unitsToStop); # Should we have systemd re-exec itself?
print STDERR "stopping the following units: ", join(", ", sort(@unitsToStop)), "\n"; my $restartSystemd = abs_path("/proc/1/exe") ne abs_path("@systemd@/lib/systemd/systemd");
system("systemctl", "stop", "--", @unitsToStop); # FIXME: ignore errors?
# Show dry-run actions.
if ($action eq "dry-activate") {
print STDERR "would stop the following units: ", join(", ", sort(keys %unitsToStop)), "\n"
if scalar(keys %unitsToStop) > 0;
print STDERR "would NOT stop the following changed units: ", join(", ", sort(keys %unitsToSkip)), "\n"
if scalar(keys %unitsToSkip) > 0;
print STDERR "would restart systemd\n" if $restartSystemd;
print STDERR "would restart the following units: ", join(", ", sort(keys %unitsToRestart)), "\n"
if scalar(keys %unitsToRestart) > 0;
print STDERR "would start the following units: ", join(", ", sort(keys %unitsToStart)), "\n";
print STDERR "would reload the following units: ", join(", ", sort(keys %unitsToReload)), "\n"
if scalar(keys %unitsToReload) > 0;
exit 0;
} }
print STDERR "NOT restarting the following units: ", join(", ", sort(@unitsToSkip)), "\n"
if scalar @unitsToSkip > 0; syslog(LOG_NOTICE, "switching to system configuration $out");
if (scalar (keys %unitsToStop) > 0) {
print STDERR "stopping the following units: ", join(", ", sort(keys %unitsToStop)), "\n";
system("systemctl", "stop", "--", sort(keys %unitsToStop)); # FIXME: ignore errors?
}
print STDERR "NOT restarting the following changed units: ", join(", ", sort(keys %unitsToSkip)), "\n"
if scalar(keys %unitsToSkip) > 0;
# Activate the new configuration (i.e., update /etc, make accounts, # Activate the new configuration (i.e., update /etc, make accounts,
# and so on). # and so on).
@ -310,7 +354,7 @@ print STDERR "activating the configuration...\n";
system("$out/activate", "$out") == 0 or $res = 2; system("$out/activate", "$out") == 0 or $res = 2;
# Restart systemd if necessary. # Restart systemd if necessary.
if (abs_path("/proc/1/exe") ne abs_path("@systemd@/lib/systemd/systemd")) { if ($restartSystemd) {
print STDERR "restarting systemd...\n"; print STDERR "restarting systemd...\n";
system("@systemd@/bin/systemctl", "daemon-reexec") == 0 or $res = 2; system("@systemd@/bin/systemctl", "daemon-reexec") == 0 or $res = 2;
} }
@ -327,10 +371,9 @@ system("@systemd@/bin/systemctl", "reload-or-restart", "dbus.service");
# Restart changed services (those that have to be restarted rather # Restart changed services (those that have to be restarted rather
# than stopped and started). # than stopped and started).
my @restart = unique(split('\n', read_file($restartListFile, err_mode => 'quiet') // "")); if (scalar(keys %unitsToRestart) > 0) {
if (scalar @restart > 0) { print STDERR "restarting the following units: ", join(", ", sort(keys %unitsToRestart)), "\n";
print STDERR "restarting the following units: ", join(", ", sort(@restart)), "\n"; system("@systemd@/bin/systemctl", "restart", "--", sort(keys %unitsToRestart)) == 0 or $res = 4;
system("@systemd@/bin/systemctl", "restart", "--", @restart) == 0 or $res = 4;
unlink($restartListFile); unlink($restartListFile);
} }
@ -340,17 +383,15 @@ if (scalar @restart > 0) {
# that are symlinks to other units. We shouldn't start both at the # that are symlinks to other units. We shouldn't start both at the
# same time because we'll get a "Failed to add path to set" error from # same time because we'll get a "Failed to add path to set" error from
# systemd. # systemd.
my @start = unique("default.target", "timers.target", "sockets.target", split('\n', read_file($startListFile, err_mode => 'quiet') // "")); print STDERR "starting the following units: ", join(", ", sort(keys %unitsToStart)), "\n";
print STDERR "starting the following units: ", join(", ", sort(@start)), "\n"; system("@systemd@/bin/systemctl", "start", "--", sort(keys %unitsToStart)) == 0 or $res = 4;
system("@systemd@/bin/systemctl", "start", "--", @start) == 0 or $res = 4;
unlink($startListFile); unlink($startListFile);
# Reload units that need it. This includes remounting changed mount # Reload units that need it. This includes remounting changed mount
# units. # units.
my @reload = unique(split '\n', read_file($reloadListFile, err_mode => 'quiet') // ""); if (scalar(keys %unitsToReload) > 0) {
if (scalar @reload > 0) { print STDERR "reloading the following units: ", join(", ", sort(keys %unitsToReload)), "\n";
print STDERR "reloading the following units: ", join(", ", sort(@reload)), "\n"; system("@systemd@/bin/systemctl", "reload", "--", sort(keys %unitsToReload)) == 0 or $res = 4;
system("@systemd@/bin/systemctl", "reload", "--", @reload) == 0 or $res = 4;
unlink($reloadListFile); unlink($reloadListFile);
} }