switch-to-configuration-ng: add more logging

Adds more logging to the non-systemd related parts of
switch-to-configuration.

Changed files
+24 -2
nixos
pkgs
by-name
sw
switch-to-configuration-ng
src
src
+3 -1
nixos/doc/manual/development/what-happens-during-a-system-switch.chapter.md
···
By default, some units are filtered from the outputs to make it less spammy.
This can be disabled for development or testing by setting the environment variable
-
`STC_DISPLAY_ALL_UNITS=1`
Most of these actions are either self-explaining but some of them have to do
with our units or the activation script. For this reason, these topics are
···
By default, some units are filtered from the outputs to make it less spammy.
This can be disabled for development or testing by setting the environment variable
+
`STC_DISPLAY_ALL_UNITS=1`.
+
+
More detailed output can be displayed by setting `STC_DEBUG=1`.
Most of these actions are either self-explaining but some of them have to do
with our units or the activation script. For this reason, these topics are
+21 -1
pkgs/by-name/sw/switch-to-configuration-ng/src/src/main.rs
···
submitted_jobs: &Rc<RefCell<HashMap<dbus::Path<'static>, Job>>>,
) {
while !submitted_jobs.borrow().is_empty() {
_ = conn.process(Duration::from_millis(500));
}
}
···
.restart_unit("nixos-activation.service", "replace")
.context("Failed to restart nixos-activation.service")?;
while !*nixos_activation_done.borrow() {
_ = dbus_conn
.process(Duration::from_secs(500))
···
/// Performs switch-to-configuration functionality for the entire system
fn do_system_switch(action: Action) -> anyhow::Result<()> {
let out = PathBuf::from(required_env("OUT")?);
let toplevel = PathBuf::from(required_env("TOPLEVEL")?);
let distro_id = required_env("DISTRO_ID")?;
···
let install_bootloader = required_env("INSTALL_BOOTLOADER")?;
let locale_archive = required_env("LOCALE_ARCHIVE")?;
let new_systemd = PathBuf::from(required_env("SYSTEMD")?);
let action = ACTION.get_or_init(|| action);
// The action that is to be performed (like switch, boot, test, dry-activate) Also exposed via
// environment variable from now on
···
std::fs::set_permissions("/run/nixos", perms)
.context("Failed to set permissions on /run/nixos directory")?;
let Ok(lock) = std::fs::OpenOptions::new()
.append(true)
.create(true)
···
die();
};
let Ok(_lock) = Flock::lock(lock, FlockArg::LockExclusiveNonblock) else {
eprintln!("Could not acquire lock");
die();
};
-
if syslog::init(Facility::LOG_USER, LevelFilter::Debug, Some("nixos")).is_err() {
bail!("Failed to initialize logger");
}
···
!= "1"
{
do_pre_switch_check(&pre_switch_check, &toplevel, action)?;
}
if *action == Action::Check {
···
// Install or update the bootloader.
if matches!(action, Action::Switch | Action::Boot) {
do_install_bootloader(&install_bootloader, &toplevel)?;
}
// Just in case the new configuration hangs the system, do a sync now.
···
eprintln!("restarting systemd...");
_ = systemd.reexecute(); // we don't get a dbus reply here
while !*systemd_reload_status.borrow() {
_ = dbus_conn
.process(Duration::from_millis(500))
···
// Make systemd reload its units.
_ = systemd.reload(); // we don't get a dbus reply here
while !*systemd_reload_status.borrow() {
_ = dbus_conn
.process(Duration::from_millis(500))
···
.canonicalize()
.context("Failed to get full path to /proc/self/exe")?;
std::process::Command::new(&myself)
.uid(uid)
.gid(gid)
···
submitted_jobs: &Rc<RefCell<HashMap<dbus::Path<'static>, Job>>>,
) {
while !submitted_jobs.borrow().is_empty() {
+
log::debug!(
+
"waiting for submitted jobs to finish, still have {} job(s)",
+
submitted_jobs.borrow().len()
+
);
_ = conn.process(Duration::from_millis(500));
}
}
···
.restart_unit("nixos-activation.service", "replace")
.context("Failed to restart nixos-activation.service")?;
+
log::debug!("waiting for nixos activation to finish");
while !*nixos_activation_done.borrow() {
_ = dbus_conn
.process(Duration::from_secs(500))
···
/// Performs switch-to-configuration functionality for the entire system
fn do_system_switch(action: Action) -> anyhow::Result<()> {
+
log::debug!("Performing system switch");
+
let out = PathBuf::from(required_env("OUT")?);
let toplevel = PathBuf::from(required_env("TOPLEVEL")?);
let distro_id = required_env("DISTRO_ID")?;
···
let install_bootloader = required_env("INSTALL_BOOTLOADER")?;
let locale_archive = required_env("LOCALE_ARCHIVE")?;
let new_systemd = PathBuf::from(required_env("SYSTEMD")?);
+
let log_level = if std::env::var("STC_DEBUG").is_ok() {
+
LevelFilter::Debug
+
} else {
+
LevelFilter::Info
+
};
let action = ACTION.get_or_init(|| action);
+
log::debug!("Using action {:?}", action);
// The action that is to be performed (like switch, boot, test, dry-activate) Also exposed via
// environment variable from now on
···
std::fs::set_permissions("/run/nixos", perms)
.context("Failed to set permissions on /run/nixos directory")?;
+
log::debug!("Creating lock file /run/nixos/switch-to-configuration.lock");
let Ok(lock) = std::fs::OpenOptions::new()
.append(true)
.create(true)
···
die();
};
+
log::debug!("Acquiring lock on file /run/nixos/switch-to-configuration.lock");
let Ok(_lock) = Flock::lock(lock, FlockArg::LockExclusiveNonblock) else {
eprintln!("Could not acquire lock");
die();
};
+
if syslog::init(Facility::LOG_USER, log_level, Some("nixos")).is_err() {
bail!("Failed to initialize logger");
}
···
!= "1"
{
do_pre_switch_check(&pre_switch_check, &toplevel, action)?;
+
log::debug!("Done performing pre-switch checks");
}
if *action == Action::Check {
···
// Install or update the bootloader.
if matches!(action, Action::Switch | Action::Boot) {
do_install_bootloader(&install_bootloader, &toplevel)?;
+
log::debug!("Done performing bootloader installation");
}
// Just in case the new configuration hangs the system, do a sync now.
···
eprintln!("restarting systemd...");
_ = systemd.reexecute(); // we don't get a dbus reply here
+
log::debug!("waiting for systemd restart to finish");
while !*systemd_reload_status.borrow() {
_ = dbus_conn
.process(Duration::from_millis(500))
···
// Make systemd reload its units.
_ = systemd.reload(); // we don't get a dbus reply here
+
log::debug!("waiting for systemd reload to finish");
while !*systemd_reload_status.borrow() {
_ = dbus_conn
.process(Duration::from_millis(500))
···
.canonicalize()
.context("Failed to get full path to /proc/self/exe")?;
+
log::debug!("Performing user switch for {name}");
std::process::Command::new(&myself)
.uid(uid)
.gid(gid)