2009-02-05 15:57:07 +00:00
|
|
|
|
#! @shell@
|
2006-11-02 22:48:01 +00:00
|
|
|
|
|
2009-06-10 16:29:48 +00:00
|
|
|
|
targetRoot=/mnt-root
|
2008-08-26 12:45:36 +00:00
|
|
|
|
|
2009-02-05 15:57:07 +00:00
|
|
|
|
export LD_LIBRARY_PATH=@extraUtils@/lib
|
2010-06-01 15:53:24 +00:00
|
|
|
|
export PATH=@extraUtils@/bin
|
2009-02-05 15:57:07 +00:00
|
|
|
|
|
2008-08-26 12:45:36 +00:00
|
|
|
|
|
2009-06-10 15:02:39 +00:00
|
|
|
|
fail() {
|
2010-01-04 18:04:57 +00:00
|
|
|
|
if [ -n "$panicOnFail" ]; then exit 1; fi
|
|
|
|
|
|
2009-06-10 15:02:39 +00:00
|
|
|
|
# If starting stage 2 failed, allow the user to repair the problem
|
|
|
|
|
# in an interactive shell.
|
|
|
|
|
cat <<EOF
|
|
|
|
|
|
|
|
|
|
An error occured in stage 1 of the boot process, which must mount the
|
|
|
|
|
root filesystem on \`$targetRoot' and then start stage 2. Press one
|
2010-01-04 18:04:57 +00:00
|
|
|
|
of the following keys:
|
2009-06-10 15:02:39 +00:00
|
|
|
|
|
|
|
|
|
i) to launch an interactive shell;
|
|
|
|
|
f) to start an interactive shell having pid 1 (needed if you want to
|
|
|
|
|
start stage 2's init manually); or
|
|
|
|
|
*) to ignore the error and continue.
|
|
|
|
|
EOF
|
|
|
|
|
|
2009-08-10 09:20:05 +00:00
|
|
|
|
read reply
|
2009-06-10 15:02:39 +00:00
|
|
|
|
|
2008-08-16 00:59:12 +00:00
|
|
|
|
case $reply in
|
2008-08-26 12:45:36 +00:00
|
|
|
|
f)
|
2009-02-05 15:57:07 +00:00
|
|
|
|
exec @shell@;;
|
2008-08-26 12:45:36 +00:00
|
|
|
|
i)
|
2009-06-10 15:02:39 +00:00
|
|
|
|
echo "Starting interactive shell..."
|
2009-02-05 15:57:07 +00:00
|
|
|
|
@shell@ || fail
|
2008-08-26 12:45:36 +00:00
|
|
|
|
;;
|
|
|
|
|
*)
|
2009-06-10 15:02:39 +00:00
|
|
|
|
echo "Continuing...";;
|
2008-08-16 00:59:12 +00:00
|
|
|
|
esac
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-10 15:02:39 +00:00
|
|
|
|
trap 'fail' ERR
|
2008-08-08 17:07:04 +00:00
|
|
|
|
|
|
|
|
|
|
2006-11-02 22:48:01 +00:00
|
|
|
|
# Print a greeting.
|
2006-11-02 22:50:30 +00:00
|
|
|
|
echo
|
2009-11-06 21:51:28 +00:00
|
|
|
|
echo "[1;32m<<< NixOS Stage 1 >>>[0m"
|
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-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
|
2010-06-01 15:53:24 +00:00
|
|
|
|
mount -t tmpfs -o "mode=0755,size=@devSize@" none /dev
|
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.
|
2009-08-27 11:57:43 +00:00
|
|
|
|
export 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
|
|
|
|
|
;;
|
2010-01-04 18:04:57 +00:00
|
|
|
|
stage1panic)
|
|
|
|
|
panicOnFail=1
|
|
|
|
|
;;
|
2010-08-07 14:16:18 +00:00
|
|
|
|
root=*)
|
|
|
|
|
# If a root device is specified on the kernel command
|
|
|
|
|
# line, make it available through the symlink /dev/root.
|
|
|
|
|
# Recognise LABEL= and UUID= to support UNetbootin.
|
|
|
|
|
set -- $(IFS==; echo $o)
|
|
|
|
|
if [ $2 = "LABEL" ]; then
|
|
|
|
|
root="/dev/disk/by-label/$3"
|
|
|
|
|
elif [ $2 = "UUID" ]; then
|
|
|
|
|
root="/dev/disk/by-uuid/$3"
|
|
|
|
|
else
|
|
|
|
|
root=$2
|
|
|
|
|
fi
|
|
|
|
|
ln -s "$root" /dev/root
|
|
|
|
|
;;
|
2006-11-24 00:04:29 +00:00
|
|
|
|
esac
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 16:38:20 +00:00
|
|
|
|
# Load the required kernel modules.
|
|
|
|
|
echo @extraUtils@/bin/modprobe > /proc/sys/kernel/modprobe
|
|
|
|
|
for i in @kernelModules@; do
|
2008-08-08 17:07:04 +00:00
|
|
|
|
echo "loading module $(basename $i)..."
|
2009-12-15 16:38:20 +00:00
|
|
|
|
modprobe $i || true
|
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
|
|
|
|
|
|
|
|
|
|
|
2007-01-10 12:42:28 +00:00
|
|
|
|
# Create device nodes in /dev.
|
2009-12-15 16:38:20 +00:00
|
|
|
|
echo "running udev..."
|
2008-08-08 22:44:45 +00:00
|
|
|
|
export UDEV_CONFIG_FILE=@udevConf@
|
2009-08-11 21:12:37 +00:00
|
|
|
|
mkdir -p /dev/.udev # !!! bug in udev?
|
2010-05-16 20:40:04 +00:00
|
|
|
|
mkdir -p /dev/.mdadm
|
2007-01-10 12:42:28 +00:00
|
|
|
|
udevd --daemon
|
2010-01-10 19:00:29 +00:00
|
|
|
|
udevadm control --env=STARTUP=1
|
2010-05-16 19:02:45 +00:00
|
|
|
|
udevadm trigger --action=add
|
2008-07-01 08:28:56 +00:00
|
|
|
|
udevadm settle
|
2010-01-10 20:08:35 +00:00
|
|
|
|
udevadm control --env=STARTUP=
|
2007-01-10 12:42:28 +00:00
|
|
|
|
|
2010-01-10 16:32:30 +00:00
|
|
|
|
echo "starting device mapper and LVM..."
|
2010-01-10 19:00:29 +00:00
|
|
|
|
lvm vgscan
|
|
|
|
|
lvm vgchange -ay
|
|
|
|
|
|
2007-05-30 10:32:42 +00:00
|
|
|
|
if test -n "$debug1devices"; then fail; fi
|
|
|
|
|
|
2007-01-10 12:42:28 +00:00
|
|
|
|
|
2009-06-18 16:47:00 +00:00
|
|
|
|
@postDeviceCommands@
|
|
|
|
|
|
|
|
|
|
|
2010-08-14 20:12:05 +00:00
|
|
|
|
# Try to resume - all modules are loaded now, and devices exist
|
|
|
|
|
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
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if test -e /sys/power/resume -a -e /sys/power/disk; then
|
|
|
|
|
echo "@resumeDevice@" > /sys/power/resume 2> /dev/null || echo "failed to resume..."
|
|
|
|
|
echo shutdown > /sys/power/disk
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
2008-11-28 12:03:56 +00:00
|
|
|
|
# Return true if the machine is on AC power, or if we can't determine
|
|
|
|
|
# whether it's on AC power.
|
2009-02-01 19:53:59 +00:00
|
|
|
|
onACPower() {
|
2009-06-10 15:02:39 +00:00
|
|
|
|
! test -d "/proc/acpi/battery" ||
|
|
|
|
|
! ls /proc/acpi/battery/BAT[0-9]* > /dev/null 2>&1 ||
|
|
|
|
|
! cat /proc/acpi/battery/BAT*/state | grep "^charging state" | grep -q "discharg"
|
2008-11-28 12:03:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-02-06 16:53:36 +00:00
|
|
|
|
|
2009-02-01 19:53:59 +00:00
|
|
|
|
# Check the specified file system, if appropriate.
|
|
|
|
|
checkFS() {
|
|
|
|
|
# Only check block devices.
|
|
|
|
|
if ! test -b "$device"; then return 0; fi
|
|
|
|
|
|
2010-06-01 15:53:24 +00:00
|
|
|
|
FSTYPE=$(blkid -o value -s TYPE "$device" || true)
|
2009-06-15 15:50:36 +00:00
|
|
|
|
|
|
|
|
|
# Don't check ROM filesystems.
|
|
|
|
|
if test "$FSTYPE" = iso9660 -o "$FSTYPE" = udf; then return 0; fi
|
|
|
|
|
|
2009-06-15 16:47:37 +00:00
|
|
|
|
# Optionally, skip fsck on journaling filesystems. This option is
|
|
|
|
|
# a hack - it's mostly because e2fsck on ext3 takes much longer to
|
|
|
|
|
# recover the journal than the ext3 implementation in the kernel
|
|
|
|
|
# does (minutes versus seconds).
|
|
|
|
|
if test -z "@checkJournalingFS@" -a \
|
|
|
|
|
\( "$FSTYPE" = ext3 -o "$FSTYPE" = ext4 -o "$FSTYPE" = reiserfs \
|
|
|
|
|
-o "$FSTYPE" = xfs -o "$FSTYPE" = jfs \)
|
|
|
|
|
then
|
|
|
|
|
return 0
|
|
|
|
|
fi
|
|
|
|
|
|
2009-02-01 19:53:59 +00:00
|
|
|
|
# Don't run `fsck' if the machine is on battery power. !!! Is
|
|
|
|
|
# this a good idea?
|
|
|
|
|
if ! onACPower; then
|
2009-06-10 15:02:39 +00:00
|
|
|
|
echo "on battery power, so no \`fsck' will be performed on \`$device'"
|
2009-02-01 19:53:59 +00:00
|
|
|
|
return 0
|
|
|
|
|
fi
|
2009-06-15 15:50:36 +00:00
|
|
|
|
|
2010-07-07 14:25:16 +00:00
|
|
|
|
FSTAB_FILE="/etc/mtab" fsck -V -C -a "$device"
|
2009-02-01 19:53:59 +00:00
|
|
|
|
fsckResult=$?
|
|
|
|
|
|
|
|
|
|
if test $(($fsckResult | 2)) = $fsckResult; then
|
|
|
|
|
echo "fsck finished, rebooting..."
|
|
|
|
|
sleep 3
|
|
|
|
|
reboot
|
2007-02-06 16:53:36 +00:00
|
|
|
|
fi
|
|
|
|
|
|
2009-02-01 19:53:59 +00:00
|
|
|
|
if test $(($fsckResult | 4)) = $fsckResult; then
|
|
|
|
|
echo "$device has unrepaired errors, please fix them manually."
|
|
|
|
|
fail
|
|
|
|
|
fi
|
2008-11-28 12:03:56 +00:00
|
|
|
|
|
2009-02-01 19:53:59 +00:00
|
|
|
|
if test $fsckResult -ge 8; then
|
|
|
|
|
echo "fsck on $device failed."
|
|
|
|
|
fail
|
|
|
|
|
fi
|
2008-11-28 12:03:56 +00:00
|
|
|
|
|
2009-02-01 19:53:59 +00:00
|
|
|
|
return 0
|
|
|
|
|
}
|
2008-11-28 12:03:56 +00:00
|
|
|
|
|
2007-02-06 16:53:36 +00:00
|
|
|
|
|
2009-02-01 19:53:59 +00:00
|
|
|
|
# Function for mounting a file system.
|
|
|
|
|
mountFS() {
|
|
|
|
|
local device="$1"
|
|
|
|
|
local mountPoint="$2"
|
|
|
|
|
local options="$3"
|
|
|
|
|
local fsType="$4"
|
|
|
|
|
|
|
|
|
|
checkFS "$device"
|
2009-06-10 15:02:39 +00:00
|
|
|
|
|
|
|
|
|
mkdir -p "/mnt-root$mountPoint" || true
|
2010-01-06 00:25:14 +00:00
|
|
|
|
|
|
|
|
|
# For CIFS mounts, retry a few times before giving up.
|
|
|
|
|
local n=0
|
|
|
|
|
while true; do
|
|
|
|
|
if mount -t "$fsType" -o "$options" "$device" /mnt-root$mountPoint; then
|
|
|
|
|
break
|
|
|
|
|
fi
|
|
|
|
|
if [ "$fsType" != cifs -o "$n" -ge 10 ]; then fail; break; fi
|
|
|
|
|
echo "retrying..."
|
|
|
|
|
n=$((n + 1))
|
|
|
|
|
done
|
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
|
|
|
|
|
2008-08-08 23:01:30 +00:00
|
|
|
|
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 the
|
|
|
|
|
# "device" should be taken relative to /mnt-root, not /. Assume
|
2009-02-01 19:53:59 +00:00
|
|
|
|
# that every device that starts with / but doesn't start with /dev
|
|
|
|
|
# is a bind mount.
|
2009-06-22 14:44:48 +00:00
|
|
|
|
pseudoDevice=
|
2008-08-08 23:01:30 +00:00
|
|
|
|
case $device in
|
|
|
|
|
/dev/*)
|
|
|
|
|
;;
|
2009-06-18 16:47:00 +00:00
|
|
|
|
//*)
|
|
|
|
|
# Don't touch SMB/CIFS paths.
|
2009-06-22 14:44:48 +00:00
|
|
|
|
pseudoDevice=1
|
2009-06-18 16:47:00 +00:00
|
|
|
|
;;
|
2008-08-08 23:01:30 +00:00
|
|
|
|
/*)
|
|
|
|
|
device=/mnt-root$device
|
|
|
|
|
;;
|
2009-06-22 14:44:48 +00:00
|
|
|
|
*)
|
|
|
|
|
# Not an absolute path; assume that it's a pseudo-device
|
|
|
|
|
# like an NFS path (e.g. "server:/path").
|
|
|
|
|
pseudoDevice=1
|
|
|
|
|
;;
|
2008-08-08 23:01:30 +00:00
|
|
|
|
esac
|
2007-02-06 16:53:36 +00:00
|
|
|
|
|
2008-08-08 23:15:36 +00:00
|
|
|
|
# USB storage devices tend to appear with some delay. It would be
|
|
|
|
|
# great if we had a way to synchronously wait for them, but
|
|
|
|
|
# alas... So just wait for a few seconds for the device to
|
|
|
|
|
# appear. If it doesn't appear, try to mount it anyway (and
|
|
|
|
|
# probably fail). This is a fallback for non-device "devices"
|
2009-06-22 14:44:48 +00:00
|
|
|
|
# that we don't properly recognise.
|
|
|
|
|
if test -z "$pseudoDevice" -a ! -e $device; then
|
2008-08-08 23:15:36 +00:00
|
|
|
|
echo -n "waiting for device $device to appear..."
|
|
|
|
|
for ((try = 0; try < 10; try++)); do
|
|
|
|
|
sleep 1
|
|
|
|
|
if test -e $device; then break; fi
|
|
|
|
|
echo -n "."
|
|
|
|
|
done
|
|
|
|
|
echo
|
|
|
|
|
fi
|
|
|
|
|
|
2008-08-08 23:01:30 +00:00
|
|
|
|
echo "mounting $device on $mountPoint..."
|
2007-02-06 16:53:36 +00:00
|
|
|
|
|
2008-08-08 23:01:30 +00:00
|
|
|
|
mountFS "$device" "$mountPoint" "$options" "$fsType"
|
|
|
|
|
done
|
2006-11-03 00:36:08 +00:00
|
|
|
|
|
2008-01-24 16:56:09 +00:00
|
|
|
|
|
2009-06-18 16:03:18 +00:00
|
|
|
|
@postMountCommands@
|
|
|
|
|
|
|
|
|
|
|
2008-08-08 22:44:45 +00:00
|
|
|
|
# Stop udevd.
|
2010-06-01 15:53:24 +00:00
|
|
|
|
kill -- -1
|
2008-08-08 22:44:45 +00:00
|
|
|
|
|
|
|
|
|
|
2007-05-30 10:32:42 +00:00
|
|
|
|
if test -n "$debug1mounts"; then fail; fi
|
|
|
|
|
|
2006-11-24 00:04:29 +00:00
|
|
|
|
|
2009-12-15 18:31:21 +00:00
|
|
|
|
# Restore /proc/sys/kernel/modprobe to its original value.
|
|
|
|
|
echo /sbin/modprobe > /proc/sys/kernel/modprobe
|
|
|
|
|
|
|
|
|
|
|
2010-06-01 15:53:24 +00:00
|
|
|
|
# Start stage 2. `switch_root' deletes all files in the ramfs on the
|
|
|
|
|
# current root. It also moves the /proc, /sys and /dev mounts over to
|
|
|
|
|
# the new root. Note that $stage2Init might be an absolute symlink,
|
|
|
|
|
# in which case "-e" won't work because we're not in the chroot yet.
|
2010-07-22 14:40:29 +00:00
|
|
|
|
if ! test -e "$targetRoot/$stage2Init" -o -L "$targetRoot/$stage2Init"; then
|
|
|
|
|
echo "stage 2 init script ($targetRoot/$stage2Init) not found"
|
|
|
|
|
fail
|
|
|
|
|
fi
|
2009-06-10 15:02:39 +00:00
|
|
|
|
|
2010-06-01 15:53:24 +00:00
|
|
|
|
mkdir -m 0755 -p $targetRoot/proc $targetRoot/sys $targetRoot/dev
|
|
|
|
|
|
|
|
|
|
exec switch_root "$targetRoot" "$stage2Init"
|
2008-08-16 00:59:12 +00:00
|
|
|
|
|
2009-06-10 15:02:39 +00:00
|
|
|
|
fail # should never be reached
|