vuls: init at 0.27.0
[NixPkgs.git] / nixos / modules / system / boot / stage-2-init.sh
blobb5627ec8e5717d2f7801f0c392b44ca8eb61d747
1 #! @shell@
3 systemConfig=@systemConfig@
5 export HOME=/root PATH="@path@"
8 if [ "${IN_NIXOS_SYSTEMD_STAGE1:-}" != true ]; then
9 # Process the kernel command line.
10 for o in $(</proc/cmdline); do
11 case $o in
12 boot.debugtrace)
13 # Show each command.
14 set -x
16 esac
17 done
20 # Print a greeting.
21 echo
22 echo -e "\e[1;32m<<< @distroName@ Stage 2 >>>\e[0m"
23 echo
26 # Normally, stage 1 mounts the root filesystem read/writable.
27 # However, in some environments, stage 2 is executed directly, and the
28 # root is read-only. So make it writable here.
29 if [ -z "$container" ]; then
30 mount -n -o remount,rw none /
35 # Likewise, stage 1 mounts /proc, /dev and /sys, so if we don't have a
36 # stage 1, we need to do that here.
37 if [ ! -e /proc/1 ]; then
38 specialMount() {
39 local device="$1"
40 local mountPoint="$2"
41 local options="$3"
42 local fsType="$4"
44 # We must not overwrite this mount because it's bind-mounted
45 # from stage 1's /run
46 if [ "${IN_NIXOS_SYSTEMD_STAGE1:-}" = true ] && [ "${mountPoint}" = /run ]; then
47 return
50 install -m 0755 -d "$mountPoint"
51 mount -n -t "$fsType" -o "$options" "$device" "$mountPoint"
53 source @earlyMountScript@
57 if [ "${IN_NIXOS_SYSTEMD_STAGE1:-}" = true ] || [ ! -c /dev/kmsg ] ; then
58 echo "booting system configuration ${systemConfig}"
59 else
60 echo "booting system configuration $systemConfig" > /dev/kmsg
64 # Make /nix/store a read-only bind mount to enforce immutability of
65 # the Nix store. Note that we can't use "chown root:nixbld" here
66 # because users/groups might not exist yet.
67 # Silence chown/chmod to fail gracefully on a readonly filesystem
68 # like squashfs.
69 chown -f 0:30000 /nix/store
70 chmod -f 1775 /nix/store
71 if [ -n "@readOnlyNixStore@" ]; then
72 if ! [[ "$(findmnt --noheadings --output OPTIONS /nix/store)" =~ ro(,|$) ]]; then
73 if [ -z "$container" ]; then
74 mount --bind /nix/store /nix/store
75 else
76 mount --rbind /nix/store /nix/store
78 mount -o remount,ro,bind /nix/store
83 if [ "${IN_NIXOS_SYSTEMD_STAGE1:-}" != true ]; then
84 # Use /etc/resolv.conf supplied by systemd-nspawn, if applicable.
85 if [ -n "@useHostResolvConf@" ] && [ -e /etc/resolv.conf ]; then
86 resolvconf -m 1000 -a host </etc/resolv.conf
90 # Log the script output to /dev/kmsg or /run/log/stage-2-init.log.
91 # Only at this point are all the necessary prerequisites ready for these commands.
92 exec {logOutFd}>&1 {logErrFd}>&2
93 if test -w /dev/kmsg; then
94 exec > >(tee -i /proc/self/fd/"$logOutFd" | while read -r line; do
95 if test -n "$line"; then
96 echo "<7>stage-2-init: $line" > /dev/kmsg
98 done) 2>&1
99 else
100 mkdir -p /run/log
101 exec > >(tee -i /run/log/stage-2-init.log) 2>&1
106 # Required by the activation script
107 install -m 0755 -d /etc
108 if [ ! -h "/etc/nixos" ]; then
109 install -m 0755 -d /etc/nixos
111 install -m 01777 -d /tmp
114 # Run the script that performs all configuration activation that does
115 # not have to be done at boot time.
116 echo "running activation script..."
117 $systemConfig/activate
120 # Record the boot configuration.
121 ln -sfn "$systemConfig" /run/booted-system
124 # Run any user-specified commands.
125 @shell@ @postBootCommands@
128 # No need to restore the stdout/stderr streams we never redirected and
129 # especially no need to start systemd
130 if [ "${IN_NIXOS_SYSTEMD_STAGE1:-}" != true ]; then
131 # Reset the logging file descriptors.
132 exec 1>&$logOutFd 2>&$logErrFd
133 exec {logOutFd}>&- {logErrFd}>&-
136 # Start systemd in a clean environment.
137 echo "starting systemd..."
138 exec @systemdExecutable@ "$@"