2009-02-22 17:08:13 +01:00
|
|
|
#! @shell@
|
2006-11-02 23:48:01 +01:00
|
|
|
|
2008-08-16 02:59:12 +02:00
|
|
|
targetRoot=/mnt/root
|
2008-08-26 14:45:36 +02:00
|
|
|
|
2009-02-22 17:08:13 +01:00
|
|
|
export LD_LIBRARY_PATH=@extraUtils@/lib
|
|
|
|
|
2008-08-26 14:45:36 +02:00
|
|
|
|
|
|
|
errorDialog() {
|
2008-08-16 02:59:12 +02:00
|
|
|
timeout=15
|
|
|
|
echo
|
|
|
|
echo "Press within $timeout seconds:"
|
2008-08-26 14:45:36 +02:00
|
|
|
echo " i) to launch an interactive shell"
|
|
|
|
echo " f) to start an interactive shell having pid 1"
|
|
|
|
echo " (needed if you want to start Stage 2 manually)"
|
2008-08-16 02:59:12 +02:00
|
|
|
echo " *) to continue immediately (ignoring the failing command)"
|
|
|
|
read -t $timeout reply
|
|
|
|
case $reply in
|
2008-08-26 14:45:36 +02:00
|
|
|
f)
|
2009-02-22 17:08:13 +01:00
|
|
|
exec @shell@;;
|
2008-08-26 14:45:36 +02:00
|
|
|
i)
|
|
|
|
echo
|
|
|
|
echo "Quit interactive shell with exit status of"
|
|
|
|
echo " 0 : to continue"
|
|
|
|
echo " non-zero : to get this dialog again"
|
2009-02-22 17:08:13 +01:00
|
|
|
@shell@ || fail
|
2008-08-26 14:45:36 +02:00
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo continuing ignoring error;;
|
2008-08-16 02:59:12 +02:00
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2008-08-26 14:45:36 +02:00
|
|
|
|
|
|
|
fail() {
|
2006-11-10 15:38:15 +01:00
|
|
|
# If starting stage 2 failed, start an interactive shell.
|
2008-08-16 02:59:12 +02:00
|
|
|
echo "error while running Stage 1"
|
|
|
|
echo "Stage 1 should mount the root partition containing the nix store on \`$targetRoot'";
|
|
|
|
echo
|
2008-08-26 14:45:36 +02:00
|
|
|
errorDialog
|
2006-11-10 15:38:15 +01:00
|
|
|
}
|
2008-08-16 02:59:12 +02:00
|
|
|
trap 'fail' ERR;
|
|
|
|
|
2006-11-10 15:38:15 +01:00
|
|
|
|
2006-11-12 19:48:47 +01:00
|
|
|
|
2008-08-08 19:07:04 +02: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 23:48:01 +01:00
|
|
|
# Print a greeting.
|
2006-11-02 23:50:30 +01:00
|
|
|
echo
|
2007-05-29 21:54:14 +02:00
|
|
|
echo "<<< NixOS Stage 1 >>>"
|
2006-11-02 23:50:30 +01:00
|
|
|
echo
|
2006-11-02 23:48:01 +01:00
|
|
|
|
2006-11-12 19:48:47 +01:00
|
|
|
|
2006-11-02 23:48:01 +01:00
|
|
|
# Set the PATH.
|
|
|
|
export PATH=/empty
|
|
|
|
for i in @path@; do
|
|
|
|
PATH=$PATH:$i/bin
|
2006-11-03 01:36:08 +01:00
|
|
|
if test -e $i/sbin; then
|
|
|
|
PATH=$PATH:$i/sbin
|
|
|
|
fi
|
2006-11-02 23:48:01 +01:00
|
|
|
done
|
|
|
|
|
2006-11-12 19:48:47 +01:00
|
|
|
|
2006-11-03 01:36:08 +01:00
|
|
|
# Mount special file systems.
|
2006-11-27 02:35:34 +01:00
|
|
|
mkdir -p /etc # to shut up mount
|
2008-08-08 17:49:57 +02:00
|
|
|
echo -n > /etc/fstab # idem
|
2006-11-27 02:35:34 +01:00
|
|
|
mkdir -p /proc
|
2006-11-04 01:18:22 +01:00
|
|
|
mount -t proc none /proc
|
2006-11-27 02:35:34 +01:00
|
|
|
mkdir -p /sys
|
2006-11-04 01:18:22 +01:00
|
|
|
mount -t sysfs none /sys
|
2006-11-03 01:36:08 +01:00
|
|
|
|
2008-03-22 17:02:57 +01:00
|
|
|
|
2006-11-24 01:04:29 +01:00
|
|
|
# Process the kernel command line.
|
2008-08-08 19:07:04 +02:00
|
|
|
stage2Init=/init
|
2006-11-24 01:04:29 +01: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 12:32:42 +02:00
|
|
|
debug1) # stop right away
|
2006-11-24 01:04:29 +01:00
|
|
|
fail
|
|
|
|
;;
|
2007-05-30 12:32:42 +02:00
|
|
|
debug1devices) # stop after loading modules and creating device nodes
|
|
|
|
debug1devices=1
|
|
|
|
;;
|
|
|
|
debug1mounts) # stop after mounting file systems
|
|
|
|
debug1mounts=1
|
|
|
|
;;
|
2006-11-24 01:04:29 +01:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
|
2006-11-03 01:36:08 +01:00
|
|
|
# Load some kernel modules.
|
2008-08-08 19:07:04 +02:00
|
|
|
for i in $(cat @modulesClosure@/insmod-list); do
|
|
|
|
echo "loading module $(basename $i)..."
|
2008-10-29 14:38:43 +01:00
|
|
|
insmod $i || true
|
2006-12-19 23:12:44 +01:00
|
|
|
done
|
2006-11-03 10:45:06 +01:00
|
|
|
|
2007-12-25 17:07:55 +01:00
|
|
|
|
2008-08-08 17:49:57 +02:00
|
|
|
# Create /dev/null.
|
|
|
|
mknod /dev/null c 1 3
|
|
|
|
|
|
|
|
|
2008-03-22 17:02:57 +01: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 17:07:55 +01:00
|
|
|
fi
|
2008-03-22 17:02:57 +01:00
|
|
|
|
2008-08-08 17:49:57 +02:00
|
|
|
echo "@resumeDevice@" > /sys/power/resume 2> /dev/null || echo "Failed to resume..."
|
2008-03-22 17:02:57 +01:00
|
|
|
echo shutdown > /sys/power/disk
|
|
|
|
|
2006-11-10 15:38:15 +01:00
|
|
|
|
2007-01-10 13:42:28 +01:00
|
|
|
# Create device nodes in /dev.
|
2008-08-09 00:44:45 +02:00
|
|
|
export UDEV_CONFIG_FILE=@udevConf@
|
2007-01-10 13:42:28 +01:00
|
|
|
udevd --daemon
|
2008-07-01 10:28:56 +02:00
|
|
|
udevadm trigger
|
|
|
|
udevadm settle
|
2007-01-10 13:42:28 +01:00
|
|
|
|
2007-06-28 11:57:36 +02:00
|
|
|
if type -p dmsetup > /dev/null; then
|
|
|
|
echo "dmsetup found, starting device mapper and lvm"
|
|
|
|
dmsetup mknodes
|
2007-09-01 20:05:46 +02:00
|
|
|
lvm vgscan --ignorelockingfailure
|
|
|
|
lvm vgchange -ay --ignorelockingfailure
|
2007-06-28 11:57:36 +02:00
|
|
|
fi
|
|
|
|
|
2007-05-30 12:32:42 +02:00
|
|
|
if test -n "$debug1devices"; then fail; fi
|
|
|
|
|
2007-01-10 13:42:28 +01:00
|
|
|
|
2009-01-02 20:41:39 +01:00
|
|
|
# Return true if the machine is on AC power, or if we can't determine
|
|
|
|
# whether it's on AC power.
|
2009-02-22 17:08:00 +01:00
|
|
|
onACPower() {
|
2009-01-02 20:41:39 +01:00
|
|
|
if test -d "/proc/acpi/battery"; then
|
2009-02-22 17:08:00 +01:00
|
|
|
if ls /proc/acpi/battery/BAT[0-9]* > /dev/null 2>&1; then
|
|
|
|
if cat /proc/acpi/battery/BAT*/state \
|
|
|
|
| grep "^charging state" \
|
|
|
|
| grep -q "discharg" ; then
|
|
|
|
false
|
|
|
|
else
|
|
|
|
true
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
true
|
|
|
|
fi
|
2009-01-02 20:41:39 +01:00
|
|
|
else
|
2009-02-22 17:08:00 +01:00
|
|
|
true
|
2009-01-02 20:41:39 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2007-02-06 17:53:36 +01:00
|
|
|
|
2009-02-22 17:08:00 +01:00
|
|
|
# Check the specified file system, if appropriate.
|
|
|
|
checkFS() {
|
|
|
|
# Only check block devices.
|
|
|
|
if ! test -b "$device"; then return 0; fi
|
|
|
|
|
|
|
|
# For unclean ext3 file systems, fsck.ext3 should just replay the
|
|
|
|
# journal and exit, but in practice this takes *much* longer than
|
|
|
|
# letting the kernel recover the FS. So, don't run fsck on
|
|
|
|
# journalling file systems.
|
|
|
|
eval $(fstype "$device")
|
|
|
|
if test "$FSTYPE" = ext3 -o "$FSTYPE" = ext4 -o "$FSTYPE" = reiserfs -o "$FSTYPE" = xfs -o "$FSTYPE" = jfs; then
|
|
|
|
return 0;
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Don't run `fsck' if the machine is on battery power. !!! Is
|
|
|
|
# this a good idea?
|
|
|
|
if ! onACPower; then
|
|
|
|
echo "on battery power, so \`fsck' not run on \`$device'"
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
FSTAB_FILE="/etc/mtab" fsck -V -v -C -a "$device"
|
|
|
|
fsckResult=$?
|
|
|
|
|
|
|
|
if test $(($fsckResult | 2)) = $fsckResult; then
|
|
|
|
echo "fsck finished, rebooting..."
|
|
|
|
sleep 3
|
|
|
|
reboot
|
2007-02-06 17:53:36 +01:00
|
|
|
fi
|
|
|
|
|
2009-02-22 17:08:00 +01:00
|
|
|
if test $(($fsckResult | 4)) = $fsckResult; then
|
|
|
|
echo "$device has unrepaired errors, please fix them manually."
|
|
|
|
fail
|
|
|
|
fi
|
2009-01-02 20:41:39 +01:00
|
|
|
|
2009-02-22 17:08:00 +01:00
|
|
|
if test $fsckResult -ge 8; then
|
|
|
|
echo "fsck on $device failed."
|
|
|
|
fail
|
|
|
|
fi
|
2009-01-02 20:41:39 +01:00
|
|
|
|
2009-02-22 17:08:00 +01:00
|
|
|
return 0
|
|
|
|
}
|
2009-01-02 20:41:39 +01:00
|
|
|
|
2007-02-06 17:53:36 +01:00
|
|
|
|
2009-02-22 17:08:00 +01:00
|
|
|
# Function for mounting a file system.
|
|
|
|
mountFS() {
|
|
|
|
local device="$1"
|
|
|
|
local mountPoint="$2"
|
|
|
|
local options="$3"
|
|
|
|
local fsType="$4"
|
|
|
|
|
|
|
|
checkFS "$device"
|
|
|
|
|
2007-02-06 17:53:36 +01:00
|
|
|
# Mount read-writable.
|
2008-08-08 17:49:57 +02:00
|
|
|
mount -t "$fsType" -o "$options" "$device" /mnt-root$mountPoint || fail
|
2007-02-06 17:53:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-12 19:48:47 +01:00
|
|
|
# Try to find and mount the root device.
|
2008-08-08 17:49:57 +02:00
|
|
|
mkdir /mnt-root
|
2006-11-12 19:48:47 +01:00
|
|
|
|
2008-08-09 01:01:30 +02: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-22 17:08:00 +01:00
|
|
|
# that every device that starts with / but doesn't start with /dev
|
|
|
|
# is a bind mount.
|
2008-08-09 01:01:30 +02:00
|
|
|
case $device in
|
|
|
|
/dev/*)
|
|
|
|
;;
|
|
|
|
/*)
|
|
|
|
device=/mnt-root$device
|
|
|
|
;;
|
|
|
|
esac
|
2007-02-06 17:53:36 +01:00
|
|
|
|
2008-08-09 01:15:36 +02: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"
|
|
|
|
# that we don't properly recognise (like NFS mounts).
|
|
|
|
if ! test -e $device; then
|
|
|
|
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-09 01:01:30 +02:00
|
|
|
echo "mounting $device on $mountPoint..."
|
2007-02-06 17:53:36 +01:00
|
|
|
|
2008-08-09 01:01:30 +02:00
|
|
|
mountFS "$device" "$mountPoint" "$options" "$fsType"
|
|
|
|
done
|
2006-11-03 01:36:08 +01:00
|
|
|
|
2008-01-24 17:56:09 +01:00
|
|
|
|
|
|
|
# If this is a live-CD/DVD, then union-mount a tmpfs on top of the
|
|
|
|
# original root.
|
2008-08-08 17:49:57 +02:00
|
|
|
targetRoot=/mnt-root
|
2008-01-24 17:56:09 +01:00
|
|
|
if test -n "@isLiveCD@"; then
|
2008-08-08 17:49:57 +02: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 17:56:09 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
2008-08-09 00:44:45 +02:00
|
|
|
# Stop udevd.
|
|
|
|
kill $(minips -C udevd -o pid=)
|
|
|
|
|
|
|
|
|
2007-05-30 12:32:42 +02:00
|
|
|
if test -n "$debug1mounts"; then fail; fi
|
|
|
|
|
2006-11-24 01:04:29 +01:00
|
|
|
|
2008-08-08 17:49:57 +02: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 01:04:29 +01:00
|
|
|
|
2006-11-04 01:01:13 +01:00
|
|
|
|
2008-08-08 17:49:57 +02: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"
|
2008-08-16 02:59:12 +02:00
|
|
|
|
|
|
|
echo
|
2009-02-22 17:08:13 +01:00
|
|
|
echo "$1: failed running $stage2Init"
|
|
|
|
echo "Dropping into a root shell..."
|
|
|
|
export $stage2Init; exec @shell@
|