2006-12-10 22:29:44 +00:00
|
|
|
#! @staticShell@
|
2006-11-02 22:48:01 +00:00
|
|
|
|
2006-11-10 14:38:15 +00:00
|
|
|
fail() {
|
|
|
|
# If starting stage 2 failed, start an interactive shell.
|
|
|
|
echo "Stage 2 failed, starting emergency shell..."
|
2007-02-06 16:53:36 +00:00
|
|
|
echo "(Stage 1 init script is $stage2Init)"
|
2006-12-10 22:29:44 +00:00
|
|
|
exec @staticShell@
|
2006-11-10 14:38:15 +00:00
|
|
|
}
|
|
|
|
|
2006-11-12 18:48:47 +00:00
|
|
|
|
2008-08-08 17:07:04 +00:00
|
|
|
# Poor man's `basename'.
|
|
|
|
basename() {
|
|
|
|
local s="$1"
|
|
|
|
set -- $(IFS=/; echo $s)
|
|
|
|
local res
|
|
|
|
while test $# != 0; do res=$1; shift; done
|
|
|
|
echo $res
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-02 22:48:01 +00:00
|
|
|
# Print a greeting.
|
2006-11-02 22:50:30 +00:00
|
|
|
echo
|
2007-05-29 19:54:14 +00:00
|
|
|
echo "<<< NixOS Stage 1 >>>"
|
2006-11-02 22:50:30 +00:00
|
|
|
echo
|
2006-11-02 22:48:01 +00:00
|
|
|
|
2006-11-12 18:48:47 +00:00
|
|
|
|
2006-11-02 22:48:01 +00:00
|
|
|
# Set the PATH.
|
|
|
|
export PATH=/empty
|
|
|
|
for i in @path@; do
|
|
|
|
PATH=$PATH:$i/bin
|
2006-11-03 00:36:08 +00:00
|
|
|
if test -e $i/sbin; then
|
|
|
|
PATH=$PATH:$i/sbin
|
|
|
|
fi
|
2006-11-02 22:48:01 +00:00
|
|
|
done
|
|
|
|
|
2006-11-12 18:48:47 +00:00
|
|
|
|
2006-11-03 00:36:08 +00:00
|
|
|
# Mount special file systems.
|
2006-11-27 01:35:34 +00:00
|
|
|
mkdir -p /etc # to shut up mount
|
2008-08-08 15:49:57 +00:00
|
|
|
echo -n > /etc/fstab # idem
|
2006-11-27 01:35:34 +00:00
|
|
|
mkdir -p /proc
|
2006-11-04 00:18:22 +00:00
|
|
|
mount -t proc none /proc
|
2006-11-27 01:35:34 +00:00
|
|
|
mkdir -p /sys
|
2006-11-04 00:18:22 +00:00
|
|
|
mount -t sysfs none /sys
|
2006-11-03 00:36:08 +00:00
|
|
|
|
2008-03-22 16:02:57 +00:00
|
|
|
|
2006-11-24 00:04:29 +00:00
|
|
|
# Process the kernel command line.
|
2008-08-08 17:07:04 +00:00
|
|
|
stage2Init=/init
|
2006-11-24 00:04:29 +00:00
|
|
|
for o in $(cat /proc/cmdline); do
|
|
|
|
case $o in
|
|
|
|
init=*)
|
|
|
|
set -- $(IFS==; echo $o)
|
|
|
|
stage2Init=$2
|
|
|
|
;;
|
|
|
|
debugtrace)
|
|
|
|
# Show each command.
|
|
|
|
set -x
|
|
|
|
;;
|
2007-05-30 10:32:42 +00:00
|
|
|
debug1) # stop right away
|
2006-11-24 00:04:29 +00:00
|
|
|
fail
|
|
|
|
;;
|
2007-05-30 10:32:42 +00:00
|
|
|
debug1devices) # stop after loading modules and creating device nodes
|
|
|
|
debug1devices=1
|
|
|
|
;;
|
|
|
|
debug1mounts) # stop after mounting file systems
|
|
|
|
debug1mounts=1
|
|
|
|
;;
|
2006-11-24 00:04:29 +00:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
|
2006-11-03 00:36:08 +00:00
|
|
|
# Load some kernel modules.
|
2008-08-08 17:07:04 +00:00
|
|
|
for i in $(cat @modulesClosure@/insmod-list); do
|
|
|
|
echo "loading module $(basename $i)..."
|
|
|
|
insmod $i
|
2006-12-19 22:12:44 +00:00
|
|
|
done
|
2006-11-03 09:45:06 +00:00
|
|
|
|
2007-12-25 16:07:55 +00:00
|
|
|
|
2008-08-08 15:49:57 +00:00
|
|
|
# Create /dev/null.
|
|
|
|
mknod /dev/null c 1 3
|
|
|
|
|
|
|
|
|
2008-03-22 16:02:57 +00:00
|
|
|
# Try to resume - all modules are loaded now.
|
|
|
|
if test -e /sys/power/tuxonice/resume; then
|
|
|
|
if test -n "$(cat /sys/power/tuxonice/resume)"; then
|
|
|
|
echo 0 > /sys/power/tuxonice/user_interface/enabled
|
|
|
|
echo 1 > /sys/power/tuxonice/do_resume || echo "Failed to resume..."
|
|
|
|
fi
|
2007-12-25 16:07:55 +00:00
|
|
|
fi
|
2008-03-22 16:02:57 +00:00
|
|
|
|
2008-08-08 15:49:57 +00:00
|
|
|
echo "@resumeDevice@" > /sys/power/resume 2> /dev/null || echo "Failed to resume..."
|
2008-03-22 16:02:57 +00:00
|
|
|
echo shutdown > /sys/power/disk
|
|
|
|
|
2006-11-10 14:38:15 +00:00
|
|
|
|
2007-01-10 12:42:28 +00:00
|
|
|
# Create device nodes in /dev.
|
|
|
|
export UDEV_CONFIG_FILE=/udev.conf
|
2008-08-08 17:07:04 +00:00
|
|
|
echo 'udev_rules="/rules"' > $UDEV_CONFIG_FILE
|
|
|
|
mkdir /rules
|
2007-01-10 12:42:28 +00:00
|
|
|
udevd --daemon
|
2008-07-01 08:28:56 +00:00
|
|
|
udevadm trigger
|
|
|
|
udevadm settle
|
2007-01-10 12:42:28 +00:00
|
|
|
|
2007-06-28 09:57:36 +00:00
|
|
|
if type -p dmsetup > /dev/null; then
|
|
|
|
echo "dmsetup found, starting device mapper and lvm"
|
|
|
|
dmsetup mknodes
|
2007-09-01 18:05:46 +00:00
|
|
|
lvm vgscan --ignorelockingfailure
|
|
|
|
lvm vgchange -ay --ignorelockingfailure
|
2007-06-28 09:57:36 +00:00
|
|
|
fi
|
|
|
|
|
2007-05-30 10:32:42 +00:00
|
|
|
if test -n "$debug1devices"; then fail; fi
|
|
|
|
|
2007-01-10 12:42:28 +00:00
|
|
|
|
2007-02-06 16:53:36 +00:00
|
|
|
# Function for mounting a file system.
|
|
|
|
mountFS() {
|
|
|
|
local device="$1"
|
|
|
|
local mountPoint="$2"
|
|
|
|
local options="$3"
|
2007-09-01 17:59:00 +00:00
|
|
|
local fsType="$4"
|
2007-02-06 16:53:36 +00:00
|
|
|
|
|
|
|
# Check the root device, if .
|
|
|
|
mustCheck=
|
|
|
|
if test -b "$device"; then
|
|
|
|
mustCheck=1
|
|
|
|
else
|
|
|
|
case $device in
|
|
|
|
LABEL=*)
|
|
|
|
mustCheck=1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
fi
|
|
|
|
|
|
|
|
if test -n "$mustCheck"; then
|
2008-07-01 08:28:56 +00:00
|
|
|
FSTAB_FILE="/etc/mtab" fsck -V -v -C -a "$device"
|
2007-02-06 16:53:36 +00:00
|
|
|
fsckResult=$?
|
|
|
|
|
|
|
|
if test $(($fsckResult | 2)) = $fsckResult; then
|
|
|
|
echo "fsck finished, rebooting..."
|
|
|
|
sleep 3
|
2008-08-08 15:49:57 +00:00
|
|
|
reboot
|
2007-02-06 16:53:36 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if test $(($fsckResult | 4)) = $fsckResult; then
|
|
|
|
echo "$device has unrepaired errors, please fix them manually."
|
|
|
|
fail
|
|
|
|
fi
|
|
|
|
|
|
|
|
if test $fsckResult -ge 8; then
|
|
|
|
echo "fsck on $device failed."
|
|
|
|
fail
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Mount read-writable.
|
2008-08-08 15:49:57 +00:00
|
|
|
mount -t "$fsType" -o "$options" "$device" /mnt-root$mountPoint || fail
|
2007-02-06 16:53:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-12 18:48:47 +00:00
|
|
|
# Try to find and mount the root device.
|
2008-08-08 15:49:57 +00:00
|
|
|
mkdir /mnt-root
|
2006-11-12 18:48:47 +00:00
|
|
|
|
2007-05-28 14:09:04 +00:00
|
|
|
echo "mounting the root device.... (fix: sleeping 5 seconds to wait for upcoming usb drivers)"
|
|
|
|
sleep 5
|
|
|
|
|
2006-11-12 18:48:47 +00:00
|
|
|
if test -n "@autoDetectRootDevice@"; then
|
2006-11-10 14:38:15 +00:00
|
|
|
|
2006-11-12 18:48:47 +00:00
|
|
|
# Look for the root device by label.
|
|
|
|
echo "probing for the NixOS installation CD..."
|
2006-11-10 14:38:15 +00:00
|
|
|
|
2006-12-23 01:16:39 +00:00
|
|
|
for i in /sys/block/*; do
|
|
|
|
if test "$(cat $i/removable)" = "1"; then
|
2006-11-10 14:38:15 +00:00
|
|
|
|
2008-08-08 15:49:57 +00:00
|
|
|
echo " in $i..."
|
2006-11-10 14:38:15 +00:00
|
|
|
|
2006-12-23 01:16:39 +00:00
|
|
|
set -- $(IFS=: ; echo $(cat $i/dev))
|
|
|
|
major="$1"
|
|
|
|
minor="$2"
|
2006-11-10 14:38:15 +00:00
|
|
|
|
2006-12-23 01:16:39 +00:00
|
|
|
# Create a device node for this device.
|
2008-08-08 15:49:57 +00:00
|
|
|
nuke /dev/tmpdev # don't have `rm' in klibc
|
2006-12-23 01:16:39 +00:00
|
|
|
mknod /dev/tmpdev b "$major" "$minor"
|
|
|
|
|
2008-08-08 15:49:57 +00:00
|
|
|
if mount -o ro -t iso9660 /dev/tmpdev /mnt-root; then
|
|
|
|
if test -e "/mnt-root/@rootLabel@"; then
|
2006-11-12 18:48:47 +00:00
|
|
|
found=1
|
|
|
|
break
|
|
|
|
fi
|
2008-08-08 15:49:57 +00:00
|
|
|
umount /mnt-root
|
2006-11-10 14:38:15 +00:00
|
|
|
fi
|
|
|
|
|
2006-11-12 18:48:47 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if test -z "$found"; then
|
|
|
|
echo "CD not found!"
|
|
|
|
fail
|
2006-11-10 14:38:15 +00:00
|
|
|
fi
|
|
|
|
|
2006-11-12 18:48:47 +00:00
|
|
|
else
|
|
|
|
|
2007-02-06 16:53:36 +00:00
|
|
|
# Hard-coded root device(s).
|
|
|
|
mountPoints=(@mountPoints@)
|
|
|
|
devices=(@devices@)
|
|
|
|
fsTypes=(@fsTypes@)
|
|
|
|
optionss=(@optionss@)
|
|
|
|
|
|
|
|
for ((n = 0; n < ${#mountPoints[*]}; n++)); do
|
|
|
|
mountPoint=${mountPoints[$n]}
|
|
|
|
device=${devices[$n]}
|
|
|
|
fsType=${fsTypes[$n]}
|
|
|
|
options=${optionss[$n]}
|
|
|
|
|
|
|
|
# !!! Really quick hack to support bind mounts, i.e., where
|
2008-08-08 15:49:57 +00:00
|
|
|
# the "device" should be taken relative to /mnt-root, not /.
|
2007-09-01 17:59:00 +00:00
|
|
|
# Assume that every device that start with / but doesn't
|
|
|
|
# start with /dev or LABEL= is a bind mount.
|
2007-02-06 16:53:36 +00:00
|
|
|
case $device in
|
|
|
|
/dev/*)
|
|
|
|
;;
|
|
|
|
LABEL=*)
|
|
|
|
;;
|
2007-09-01 17:59:00 +00:00
|
|
|
/*)
|
2008-08-08 15:49:57 +00:00
|
|
|
device=/mnt-root$device
|
2007-02-06 16:53:36 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
echo "mounting $device on $mountPoint..."
|
|
|
|
|
2007-09-01 17:59:00 +00:00
|
|
|
mountFS "$device" "$mountPoint" "$options" "$fsType"
|
2007-02-06 16:53:36 +00:00
|
|
|
done
|
|
|
|
|
2006-11-10 14:38:15 +00:00
|
|
|
fi
|
2006-11-03 00:36:08 +00:00
|
|
|
|
2008-01-24 16:56:09 +00:00
|
|
|
|
|
|
|
# If this is a live-CD/DVD, then union-mount a tmpfs on top of the
|
|
|
|
# original root.
|
2008-08-08 15:49:57 +00:00
|
|
|
targetRoot=/mnt-root
|
2008-01-24 16:56:09 +00:00
|
|
|
if test -n "@isLiveCD@"; then
|
2008-08-08 15:49:57 +00:00
|
|
|
mkdir /mnt-tmpfs
|
|
|
|
mount -t tmpfs -o "mode=755" none /mnt-tmpfs
|
|
|
|
mkdir /mnt-union
|
|
|
|
mount -t aufs -o dirs=/mnt-tmpfs=rw:$targetRoot=ro none /mnt-union
|
|
|
|
targetRoot=/mnt-union
|
2008-01-24 16:56:09 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
2007-05-30 10:32:42 +00:00
|
|
|
if test -n "$debug1mounts"; then fail; fi
|
|
|
|
|
2006-11-24 00:04:29 +00:00
|
|
|
|
2008-08-08 15:49:57 +00:00
|
|
|
# `run-init' needs a /dev/console on the target FS.
|
|
|
|
if ! test -e $targetRoot/dev/console; then
|
|
|
|
mkdir -p $targetRoot/dev
|
|
|
|
mknod $targetRoot/dev/console c 5 1
|
|
|
|
fi
|
2006-11-24 00:04:29 +00:00
|
|
|
|
2006-11-04 00:01:13 +00:00
|
|
|
|
2008-08-08 15:49:57 +00:00
|
|
|
# Start stage 2. `run-init' deletes all files in the ramfs on the
|
|
|
|
# current /.
|
|
|
|
if test -z "$stage2Init"; then fail; fi
|
|
|
|
umount /sys
|
|
|
|
umount /proc
|
|
|
|
exec run-init "$targetRoot" "$stage2Init"
|
2006-11-10 14:38:15 +00:00
|
|
|
fail
|