* Don't use klibc in the initrd. It's simpler (and slightly smaller)

to use the standard (coreutils) tools.
* Use util-linux's `switch_root' to switch over to the target root
  FS.  It automatically moves over the /dev, /proc and /sys from stage
  1, so stage 2 doesn't need to set them up again.

svn path=/nixos/trunk/; revision=22085
This commit is contained in:
Eelco Dolstra 2010-06-01 15:53:24 +00:00
parent eb4c33eeca
commit dc49a0ce3f
6 changed files with 32 additions and 44 deletions

View file

@ -170,7 +170,7 @@ in
'' ''
echo "" > /proc/sys/kernel/hotplug echo "" > /proc/sys/kernel/hotplug
mkdir -pv /var/lib/udev/rules.d mkdir -p /var/lib/udev/rules.d
# Do the loading of additional stage 2 kernel modules. # Do the loading of additional stage 2 kernel modules.
# Maybe this isn't the best place... # Maybe this isn't the best place...
@ -179,7 +179,7 @@ in
${modprobe}/sbin/modprobe $i || true ${modprobe}/sbin/modprobe $i || true
done done
mkdir -pv /dev/.udev # !!! bug in udev? mkdir -p /dev/.udev # !!! bug in udev?
''; '';
daemonType = "fork"; daemonType = "fork";

View file

@ -3,7 +3,7 @@
targetRoot=/mnt-root targetRoot=/mnt-root
export LD_LIBRARY_PATH=@extraUtils@/lib export LD_LIBRARY_PATH=@extraUtils@/lib
export PATH=@extraUtils@/bin:@klibc@/bin export PATH=@extraUtils@/bin
fail() { fail() {
@ -53,6 +53,7 @@ mkdir -p /proc
mount -t proc none /proc mount -t proc none /proc
mkdir -p /sys mkdir -p /sys
mount -t sysfs none /sys mount -t sysfs none /sys
mount -t tmpfs -o "mode=0755,size=@devSize@" none /dev
# Process the kernel command line. # Process the kernel command line.
@ -144,7 +145,7 @@ checkFS() {
# Only check block devices. # Only check block devices.
if ! test -b "$device"; then return 0; fi if ! test -b "$device"; then return 0; fi
eval $(fstype "$device") FSTYPE=$(blkid -o value -s TYPE "$device" || true)
# Don't check ROM filesystems. # Don't check ROM filesystems.
if test "$FSTYPE" = iso9660 -o "$FSTYPE" = udf; then return 0; fi if test "$FSTYPE" = iso9660 -o "$FSTYPE" = udf; then return 0; fi
@ -276,33 +277,27 @@ done
# Stop udevd. # Stop udevd.
kill $(minips -C udevd -o pid=) 2> /dev/null kill -- -1
if test -n "$debug1mounts"; then fail; fi if test -n "$debug1mounts"; then fail; fi
# `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
# Restore /proc/sys/kernel/modprobe to its original value. # Restore /proc/sys/kernel/modprobe to its original value.
echo /sbin/modprobe > /proc/sys/kernel/modprobe echo /sbin/modprobe > /proc/sys/kernel/modprobe
# Start stage 2. `run-init' deletes all files in the ramfs on the # Start stage 2. `switch_root' deletes all files in the ramfs on the
# current /. Note that $stage2Init might be an absolute symlink, in # current root. It also moves the /proc, /sys and /dev mounts over to
# which case "-e" won't work because we're not in the chroot yet. # 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.
if ! test -e "$targetRoot/$stage2Init" -o -L "$targetRoot/$stage2Init"; then if ! test -e "$targetRoot/$stage2Init" -o -L "$targetRoot/$stage2Init"; then
echo "stage 2 init script not found" echo "stage 2 init script not found"
fail fail
fi fi
umount /sys mkdir -m 0755 -p $targetRoot/proc $targetRoot/sys $targetRoot/dev
umount /proc
exec run-init "$targetRoot" "$stage2Init" exec switch_root "$targetRoot" "$stage2Init"
fail # should never be reached fail # should never be reached

View file

@ -114,13 +114,17 @@ let
# Copy some utillinux stuff. # Copy some utillinux stuff.
cp -v ${pkgs.utillinux}/bin/mount ${pkgs.utillinux}/bin/umount \ cp -v ${pkgs.utillinux}/bin/mount ${pkgs.utillinux}/bin/umount \
${pkgs.utillinux}/sbin/fsck ${pkgs.utillinux}/sbin/pivot_root \ ${pkgs.utillinux}/sbin/fsck ${pkgs.utillinux}/sbin/switch_root \
${pkgs.utillinux}/sbin/blkid $out/bin ${pkgs.utillinux}/sbin/blkid $out/bin
cp -pdv ${pkgs.utillinux}/lib/libblkid*.so.* $out/lib cp -pdv ${pkgs.utillinux}/lib/libblkid*.so.* $out/lib
cp -pdv ${pkgs.utillinux}/lib/libuuid*.so.* $out/lib cp -pdv ${pkgs.utillinux}/lib/libuuid*.so.* $out/lib
# Copy some coreutils. # Copy some coreutils.
cp -v ${pkgs.coreutils}/bin/basename $out/bin cp -v ${pkgs.coreutils}/bin/basename $out/bin
cp -v ${pkgs.coreutils}/bin/mkdir $out/bin
cp -v ${pkgs.coreutils}/bin/mknod $out/bin
cp -v ${pkgs.coreutils}/bin/cat $out/bin
cp -v ${pkgs.coreutils}/bin/chroot $out/bin
# Copy e2fsck and friends. # Copy e2fsck and friends.
cp -v ${pkgs.e2fsprogs}/sbin/e2fsck $out/bin cp -v ${pkgs.e2fsprogs}/sbin/e2fsck $out/bin
@ -256,11 +260,9 @@ let
isExecutable = true; isExecutable = true;
klibc = pkgs.klibcShrunk;
inherit udevConf extraUtils; inherit udevConf extraUtils;
inherit (config.boot) resumeDevice; inherit (config.boot) resumeDevice devSize;
inherit (config.boot.initrd) checkJournalingFS inherit (config.boot.initrd) checkJournalingFS
postDeviceCommands postMountCommands kernelModules; postDeviceCommands postMountCommands kernelModules;

View file

@ -36,13 +36,12 @@ setPath "@path@"
mount -n -o remount,rw none / mount -n -o remount,rw none /
# Mount special file systems. # Mount special file systems. Note that /dev, /proc and /sys are
# already mounted by `switch_root' in the initrd.
mkdir -m 0755 -p /etc mkdir -m 0755 -p /etc
test -e /etc/fstab || touch /etc/fstab # to shut up mount test -e /etc/fstab || touch /etc/fstab # to shut up mount
test -s /etc/mtab && rm /etc/mtab # while installing a symlink is created (see man mount), if it's still there for whateever reason remove it test -s /etc/mtab && rm /etc/mtab # while installing a symlink is created (see man mount), if it's still there for whateever reason remove it
rm -f /etc/mtab* # not that we care about stale locks rm -f /etc/mtab* # not that we care about stale locks
mkdir -m 0755 -p /proc
mount -n -t proc none /proc
rm -f /etc/mtab rm -f /etc/mtab
cat /proc/mounts > /etc/mtab cat /proc/mounts > /etc/mtab
@ -79,15 +78,11 @@ done
# More special file systems, initialise required directories. # More special file systems, initialise required directories.
mkdir -m 0755 -p /sys
mount -t sysfs none /sys
mkdir -m 0755 -p /dev
mount -t tmpfs -o "mode=0755,size=@devSize@" none /dev
mkdir -m 0777 /dev/shm mkdir -m 0777 /dev/shm
mount -t tmpfs -o "rw,nosuid,nodev,size=@devShmSize@" tmpfs /dev/shm mount -t tmpfs -o "rw,nosuid,nodev,size=@devShmSize@" tmpfs /dev/shm
mkdir -m 0755 -p /dev/pts mkdir -m 0755 -p /dev/pts
mount -t devpts -o mode=0600,gid=@ttyGid@ none /dev/pts mount -t devpts -o mode=0600,gid=@ttyGid@ none /dev/pts
[ -e /proc/bus/usb ] && mount -t usbfs none /proc/bus/usb # uml doesn't have usb by default [ -e /proc/bus/usb ] && mount -t usbfs none /proc/bus/usb # UML doesn't have USB by default
mkdir -m 01777 -p /tmp mkdir -m 01777 -p /tmp
mkdir -m 0755 -p /var mkdir -m 0755 -p /var
mkdir -m 0755 -p /nix/var mkdir -m 0755 -p /nix/var
@ -113,22 +108,15 @@ rm -rf /var/lock
rm -rf /nix/var/nix/chroots # recreated in activate-configuration.sh rm -rf /nix/var/nix/chroots # recreated in activate-configuration.sh
# Create the minimal device nodes needed for the activation scripts
# and Upstart.
mknod -m 0666 /dev/null c 1 3
mknod -m 0644 /dev/urandom c 1 9 # needed for passwd
mknod -m 0644 /dev/console c 5 1
# Clear the resume device. # Clear the resume device.
if test -n "$resumeDevice"; then if test -n "$resumeDevice"; then
mkswap "$resumeDevice" || echo 'Failed to clear saved image.' mkswap "$resumeDevice" || echo 'Failed to clear saved image.'
fi fi
echo "running activation script..."
# Run the script that performs all configuration activation that does # Run the script that performs all configuration activation that does
# not have to be done at boot time. # not have to be done at boot time.
echo "running activation script..."
@activateConfiguration@ "$systemConfig" @activateConfiguration@ "$systemConfig"

View file

@ -44,7 +44,7 @@ let
src = ./stage-2-init.sh; src = ./stage-2-init.sh;
isExecutable = true; isExecutable = true;
inherit kernel activateConfiguration; inherit kernel activateConfiguration;
inherit (config.boot) devSize devShmSize; inherit (config.boot) devShmSize;
ttyGid = config.ids.gids.tty; ttyGid = config.ids.gids.tty;
upstart = config.system.build.upstart; upstart = config.system.build.upstart;
path = path =

View file

@ -160,17 +160,20 @@ in
'' ''
# We need mke2fs in the initrd. # We need mke2fs in the initrd.
cp ${pkgs.e2fsprogs}/sbin/mke2fs $out/bin cp ${pkgs.e2fsprogs}/sbin/mke2fs $out/bin
# And `ifconfig'.
cp ${pkgs.nettools}/sbin/ifconfig $out/bin
''; '';
boot.initrd.postDeviceCommands = boot.initrd.postDeviceCommands =
'' ''
# Set up networking. Needed for CIFS mounting. # Set up networking. Needed for CIFS mounting.
ipconfig 10.0.2.15:::::eth0:none ifconfig eth0 up 10.0.2.15
# If the disk image appears to be empty (fstype "unknown"; # If the disk image appears to be empty, run mke2fs to
# hacky!!!), run mke2fs to initialise. # initialise.
eval $(fstype /dev/vda) FSTYPE=$(blkid -o value -s TYPE /dev/vda || true)
if test "$FSTYPE" = unknown; then if test -z "$FSTYPE"; then
mke2fs -t ext3 /dev/vda mke2fs -t ext3 /dev/vda
fi fi
''; '';