2006-11-04 00:01:13 +00:00
|
|
|
#! @shell@
|
|
|
|
|
2010-09-13 22:10:25 +00:00
|
|
|
systemConfig=@systemConfig@
|
2006-11-04 00:01:13 +00:00
|
|
|
|
2017-04-04 20:50:48 +00:00
|
|
|
export HOME=/root PATH="@path@"
|
2013-01-23 19:16:33 +00:00
|
|
|
|
2006-11-13 11:41:27 +00:00
|
|
|
|
2017-04-04 22:27:25 +00:00
|
|
|
# Process the kernel command line.
|
2017-04-04 22:34:54 +00:00
|
|
|
for o in $(</proc/cmdline); do
|
2017-04-04 22:27:25 +00:00
|
|
|
case $o in
|
|
|
|
boot.debugtrace)
|
|
|
|
# Show each command.
|
|
|
|
set -x
|
|
|
|
;;
|
|
|
|
resume=*)
|
|
|
|
set -- $(IFS==; echo $o)
|
|
|
|
resumeDevice=$2
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
|
2006-11-04 00:01:13 +00:00
|
|
|
# Print a greeting.
|
|
|
|
echo
|
2009-11-06 21:51:28 +00:00
|
|
|
echo -e "\e[1;32m<<< NixOS Stage 2 >>>\e[0m"
|
2006-11-04 00:01:13 +00:00
|
|
|
echo
|
|
|
|
|
2006-11-13 11:41:27 +00:00
|
|
|
|
2010-01-20 18:06:33 +00:00
|
|
|
# Normally, stage 1 mounts the root filesystem read/writable.
|
2012-11-02 16:07:53 +00:00
|
|
|
# However, in some environments, stage 2 is executed directly, and the
|
|
|
|
# root is read-only. So make it writable here.
|
2016-09-07 07:38:35 +00:00
|
|
|
if [ -z "$container" ]; then
|
2014-08-12 00:51:20 +00:00
|
|
|
mount -n -o remount,rw none /
|
|
|
|
fi
|
2010-01-20 18:06:33 +00:00
|
|
|
|
|
|
|
|
2017-01-10 16:29:19 +00:00
|
|
|
# Likewise, stage 1 mounts /proc, /dev and /sys, so if we don't have a
|
2010-07-14 10:55:45 +00:00
|
|
|
# stage 1, we need to do that here.
|
2017-01-10 16:29:19 +00:00
|
|
|
if [ ! -e /proc/1 ]; then
|
|
|
|
specialMount() {
|
|
|
|
local device="$1"
|
|
|
|
local mountPoint="$2"
|
|
|
|
local options="$3"
|
|
|
|
local fsType="$4"
|
|
|
|
|
2018-04-04 11:01:29 +00:00
|
|
|
install -m 0755 -d "$mountPoint"
|
2017-01-10 16:29:19 +00:00
|
|
|
mount -n -t "$fsType" -o "$options" "$device" "$mountPoint"
|
|
|
|
}
|
|
|
|
source @earlyMountScript@
|
|
|
|
fi
|
2010-07-14 10:55:45 +00:00
|
|
|
|
|
|
|
|
2013-02-18 14:39:02 +00:00
|
|
|
echo "booting system configuration $systemConfig" > /dev/kmsg
|
|
|
|
|
|
|
|
|
2012-09-25 20:33:21 +00:00
|
|
|
# Make /nix/store a read-only bind mount to enforce immutability of
|
2012-10-04 20:15:30 +00:00
|
|
|
# the Nix store. Note that we can't use "chown root:nixbld" here
|
|
|
|
# because users/groups might not exist yet.
|
2015-02-16 18:41:08 +00:00
|
|
|
# Silence chown/chmod to fail gracefully on a readonly filesystem
|
|
|
|
# like squashfs.
|
|
|
|
chown -f 0:30000 /nix/store
|
2015-04-07 11:15:48 +00:00
|
|
|
chmod -f 1775 /nix/store
|
2012-09-25 20:33:21 +00:00
|
|
|
if [ -n "@readOnlyStore@" ]; then
|
2017-04-04 21:27:51 +00:00
|
|
|
if ! [[ "$(findmnt --noheadings --output OPTIONS /nix/store)" =~ ro(,|$) ]]; then
|
|
|
|
# FIXME when linux < 4.5 is EOL, switch to atomic bind mounts
|
|
|
|
#mount /nix/store /nix/store -o bind,remount,ro
|
2013-03-08 00:00:21 +00:00
|
|
|
mount --bind /nix/store /nix/store
|
|
|
|
mount -o remount,ro,bind /nix/store
|
|
|
|
fi
|
2012-09-25 20:33:21 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
2010-07-14 10:55:45 +00:00
|
|
|
# Provide a /etc/mtab.
|
2018-04-04 11:01:29 +00:00
|
|
|
install -m 0755 -d /etc
|
2008-01-24 16:56:09 +00:00
|
|
|
test -e /etc/fstab || touch /etc/fstab # to shut up mount
|
2009-10-09 16:09:56 +00:00
|
|
|
rm -f /etc/mtab* # not that we care about stale locks
|
2012-06-02 00:15:07 +00:00
|
|
|
ln -s /proc/mounts /etc/mtab
|
2006-12-12 00:08:26 +00:00
|
|
|
|
2006-11-24 00:18:14 +00:00
|
|
|
|
|
|
|
# More special file systems, initialise required directories.
|
2014-06-05 11:10:35 +00:00
|
|
|
[ -e /proc/bus/usb ] && mount -t usbfs usbfs /proc/bus/usb # UML doesn't have USB by default
|
2018-04-04 11:01:29 +00:00
|
|
|
install -m 01777 -d /tmp
|
|
|
|
install -m 0755 -d /var/{log,lib,db} /nix/var /etc/nixos/ \
|
2017-04-04 22:28:24 +00:00
|
|
|
/run/lock /home /bin # for the /bin/sh symlink
|
2008-01-24 16:56:09 +00:00
|
|
|
|
2006-11-06 22:21:50 +00:00
|
|
|
|
2006-12-10 22:29:44 +00:00
|
|
|
# Miscellaneous boot time cleanup.
|
2012-10-26 13:15:08 +00:00
|
|
|
rm -rf /var/run /var/lock
|
2013-03-28 17:40:13 +00:00
|
|
|
rm -f /etc/{group,passwd,shadow}.lock
|
2008-10-29 13:10:49 +00:00
|
|
|
|
2006-11-19 18:16:29 +00:00
|
|
|
|
2010-08-25 11:59:04 +00:00
|
|
|
# Also get rid of temporary GC roots.
|
2014-04-18 16:37:07 +00:00
|
|
|
rm -rf /nix/var/nix/gcroots/tmp /nix/var/nix/temproots
|
2010-08-25 11:59:04 +00:00
|
|
|
|
|
|
|
|
2011-07-16 19:27:45 +00:00
|
|
|
# For backwards compatibility, symlink /var/run to /run, and /var/lock
|
|
|
|
# to /run/lock.
|
|
|
|
ln -s /run /var/run
|
|
|
|
ln -s /run/lock /var/lock
|
2010-06-09 22:29:06 +00:00
|
|
|
|
|
|
|
|
2008-03-22 16:03:43 +00:00
|
|
|
# Clear the resume device.
|
|
|
|
if test -n "$resumeDevice"; then
|
|
|
|
mkswap "$resumeDevice" || echo 'Failed to clear saved image.'
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
2014-04-18 14:40:27 +00:00
|
|
|
# Use /etc/resolv.conf supplied by systemd-nspawn, if applicable.
|
2017-04-04 22:34:54 +00:00
|
|
|
if [ -n "@useHostResolvConf@" ] && [ -e /etc/resolv.conf ]; then
|
|
|
|
resolvconf -m 1000 -a host </etc/resolv.conf
|
2014-04-18 14:40:27 +00:00
|
|
|
fi
|
|
|
|
|
2016-02-24 07:54:25 +00:00
|
|
|
# Log the script output to /dev/kmsg or /run/log/stage-2-init.log.
|
2016-02-22 05:42:51 +00:00
|
|
|
# Only at this point are all the necessary prerequisites ready for these commands.
|
2016-02-23 10:53:37 +00:00
|
|
|
exec {logOutFd}>&1 {logErrFd}>&2
|
|
|
|
if test -w /dev/kmsg; then
|
2016-02-24 07:54:25 +00:00
|
|
|
exec > >(tee -i /proc/self/fd/"$logOutFd" | while read -r line; do
|
2016-02-23 10:53:37 +00:00
|
|
|
if test -n "$line"; then
|
|
|
|
echo "<7>stage-2-init: $line" > /dev/kmsg
|
|
|
|
fi
|
|
|
|
done) 2>&1
|
|
|
|
else
|
|
|
|
mkdir -p /run/log
|
|
|
|
exec > >(tee -i /run/log/stage-2-init.log) 2>&1
|
2016-02-22 05:42:51 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
2006-12-09 19:25:23 +00:00
|
|
|
# Run the script that performs all configuration activation that does
|
|
|
|
# not have to be done at boot time.
|
2010-06-01 15:53:24 +00:00
|
|
|
echo "running activation script..."
|
2010-09-13 22:10:25 +00:00
|
|
|
$systemConfig/activate
|
2007-03-20 13:01:53 +00:00
|
|
|
|
|
|
|
|
2014-03-18 22:14:34 +00:00
|
|
|
# Restore the system time from the hardware clock. We do this after
|
|
|
|
# running the activation script to be sure that /etc/localtime points
|
|
|
|
# at the current time zone.
|
2015-05-11 14:22:49 +00:00
|
|
|
if [ -e /dev/rtc ]; then
|
|
|
|
hwclock --hctosys
|
|
|
|
fi
|
2014-03-18 22:14:34 +00:00
|
|
|
|
|
|
|
|
2010-06-20 20:54:31 +00:00
|
|
|
# Record the boot configuration.
|
2012-07-16 15:30:37 +00:00
|
|
|
ln -sfn "$systemConfig" /run/booted-system
|
2010-09-13 22:10:25 +00:00
|
|
|
|
2019-10-09 18:29:58 +00:00
|
|
|
# Prevent the booted system from being garbage-collected. If it weren't
|
2010-09-13 22:10:25 +00:00
|
|
|
# a gcroot, if we were running a different kernel, switched system,
|
|
|
|
# and garbage collected all, we could not load kernel modules anymore.
|
2012-07-16 15:30:37 +00:00
|
|
|
ln -sfn /run/booted-system /nix/var/nix/gcroots/booted-system
|
2006-12-09 02:51:42 +00:00
|
|
|
|
|
|
|
|
2007-03-06 00:45:33 +00:00
|
|
|
# Run any user-specified commands.
|
2009-06-05 16:02:58 +00:00
|
|
|
@shell@ @postBootCommands@
|
2007-03-06 00:45:33 +00:00
|
|
|
|
2010-06-08 12:48:16 +00:00
|
|
|
|
nixos/stage-2: create empty machine-id at boot
Previously, the activation script was responsible for ensuring that
/etc/machine-id exists. However, the only time it could not already
exist is during stage-2-init, not while switching configurations,
because one of the first things systemd does when starting up as PID 1
is to create this file. So I've moved the initialization to
stage-2-init.
Furthermore, since systemd will do the equivalent of
systemd-machine-id-setup if /etc/machine-id doesn't have valid contents,
we don't need to do that ourselves.
We _do_, however, want to ensure that the file at least exists, because
systemd also uses the non-existence of this file to guess that this is a
first-boot situation. In that case, systemd tries to create some
symlinks in /etc/systemd/system according to its presets, which it can't
do because we've already populated /etc according to the current NixOS
configuration.
This is not necessary for any other activation script snippets, so it's
okay to do it after stage-2-init runs the activation script. None of
them declare a dependency on the "systemd" snippet. Also, most of them
only create files or directories in ways that obviously don't need the
machine-id set.
2018-09-29 18:35:00 +00:00
|
|
|
# Ensure systemd doesn't try to populate /etc, by forcing its first-boot
|
|
|
|
# heuristic off. It doesn't matter what's in /etc/machine-id for this purpose,
|
|
|
|
# and systemd will immediately fill in the file when it starts, so just
|
|
|
|
# creating it is enough. This `: >>` pattern avoids forking and avoids changing
|
|
|
|
# the mtime if the file already exists.
|
|
|
|
: >> /etc/machine-id
|
|
|
|
|
|
|
|
|
2016-02-23 10:53:37 +00:00
|
|
|
# Reset the logging file descriptors.
|
|
|
|
exec 1>&$logOutFd 2>&$logErrFd
|
|
|
|
exec {logOutFd}>&- {logErrFd}>&-
|
2016-02-22 05:42:51 +00:00
|
|
|
|
|
|
|
|
2012-06-02 00:15:07 +00:00
|
|
|
# Start systemd.
|
|
|
|
echo "starting systemd..."
|
2021-03-15 18:34:09 +00:00
|
|
|
|
2018-08-22 01:39:27 +00:00
|
|
|
PATH=/run/current-system/systemd/lib/systemd:@fsPackagesPath@ \
|
2021-03-15 18:34:09 +00:00
|
|
|
LOCALE_ARCHIVE=/run/current-system/sw/lib/locale/locale-archive @systemdUnitPathEnvVar@ \
|
2020-07-14 12:10:38 +00:00
|
|
|
exec @systemdExecutable@
|