Advertisement
Guest User

Untitled

a guest
Jul 28th, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 11.48 KB | None | 0 0
  1. #! /nix/store/d94yl3ab8nh76q9px1c66i4spg42d60b-extra-utils/bin/ash
  2.  
  3. targetRoot=/mnt-root
  4. console=tty1
  5.  
  6. export LD_LIBRARY_PATH=/nix/store/d94yl3ab8nh76q9px1c66i4spg42d60b-extra-utils/lib
  7. export PATH=/nix/store/d94yl3ab8nh76q9px1c66i4spg42d60b-extra-utils/bin:/nix/store/d94yl3ab8nh76q9px1c66i4spg42d60b-extra-utils/sbin
  8.  
  9.  
  10. fail() {
  11.     if [ -n "$panicOnFail" ]; then exit 1; fi
  12.  
  13.     # If starting stage 2 failed, allow the user to repair the problem
  14.     # in an interactive shell.
  15.     cat <<EOF
  16.  
  17. An error occurred in stage 1 of the boot process, which must mount the
  18. root filesystem on \`$targetRoot' and then start stage 2.  Press one
  19. of the following keys:
  20.  
  21. EOF
  22.     if [ -n "$allowShell" ]; then cat <<EOF
  23.   i) to launch an interactive shell
  24.   f) to start an interactive shell having pid 1 (needed if you want to
  25.      start stage 2's init manually)
  26. EOF
  27.     fi
  28.     cat <<EOF
  29.   r) to reboot immediately
  30.   *) to ignore the error and continue
  31. EOF
  32.  
  33.     read reply
  34.  
  35.     if [ -n "$allowShell" -a "$reply" = f ]; then
  36.         exec setsid /nix/store/d94yl3ab8nh76q9px1c66i4spg42d60b-extra-utils/bin/ash -c "/nix/store/d94yl3ab8nh76q9px1c66i4spg42d60b-extra-utils/bin/ash < /dev/$console >/dev/$console 2>/dev/$console"
  37.     elif [ -n "$allowShell" -a "$reply" = i ]; then
  38.         echo "Starting interactive shell..."
  39.         setsid /nix/store/d94yl3ab8nh76q9px1c66i4spg42d60b-extra-utils/bin/ash -c "/nix/store/d94yl3ab8nh76q9px1c66i4spg42d60b-extra-utils/bin/ash < /dev/$console >/dev/$console 2>/dev/$console" || fail
  40.     elif [ "$reply" = r ]; then
  41.         echo "Rebooting..."
  42.         reboot -f
  43.     else
  44.         echo "Continuing..."
  45.     fi
  46. }
  47.  
  48. trap 'fail' 0
  49.  
  50.  
  51. # Print a greeting.
  52. echo
  53. echo "<<< NixOS Stage 1 >>>"
  54. echo
  55.  
  56.  
  57. # Mount special file systems.
  58. mkdir -p /etc
  59. touch /etc/fstab # to shut up mount
  60. touch /etc/mtab # to shut up mke2fs
  61. mkdir -p /proc
  62. mount -t proc proc /proc
  63. mkdir -p /sys
  64. mount -t sysfs sysfs /sys
  65. mount -t devtmpfs -o "size=5%" devtmpfs /dev
  66. mkdir -p /run
  67. mount -t tmpfs -o "mode=0755,size=25%" tmpfs /run
  68.  
  69.  
  70. # Process the kernel command line.
  71. export stage2Init=/init
  72. for o in $(cat /proc/cmdline); do
  73.     case $o in
  74.         console=*)
  75.             set -- $(IFS==; echo $o)
  76.             params=$2
  77.             set -- $(IFS=,; echo $params)
  78.             console=$1
  79.             ;;
  80.         init=*)
  81.             set -- $(IFS==; echo $o)
  82.             stage2Init=$2
  83.             ;;
  84.         boot.trace|debugtrace)
  85.             # Show each command.
  86.             set -x
  87.             ;;
  88.         boot.shell_on_fail)
  89.             allowShell=1
  90.             ;;
  91.         boot.debug1|debug1) # stop right away
  92.             allowShell=1
  93.             fail
  94.             ;;
  95.         boot.debug1devices) # stop after loading modules and creating device nodes
  96.             allowShell=1
  97.             debug1devices=1
  98.             ;;
  99.         boot.debug1mounts) # stop after mounting file systems
  100.             allowShell=1
  101.             debug1mounts=1
  102.             ;;
  103.         boot.panic_on_fail|stage1panic=1)
  104.             panicOnFail=1
  105.             ;;
  106.         root=*)
  107.             # If a root device is specified on the kernel command
  108.             # line, make it available through the symlink /dev/root.
  109.             # Recognise LABEL= and UUID= to support UNetbootin.
  110.             set -- $(IFS==; echo $o)
  111.             if [ $2 = "LABEL" ]; then
  112.                 root="/dev/disk/by-label/$3"
  113.             elif [ $2 = "UUID" ]; then
  114.                 root="/dev/disk/by-uuid/$3"
  115.             else
  116.                 root=$2
  117.             fi
  118.             ln -s "$root" /dev/root
  119.             ;;
  120.     esac
  121. done
  122.  
  123.  
  124. # Load the required kernel modules.
  125. mkdir -p /lib
  126. ln -s /nix/store/jap5gy43p1cg73cb4aknpl6jbs2x7wv1-kernel-modules-shrunk/lib/modules /lib/modules
  127. echo /nix/store/d94yl3ab8nh76q9px1c66i4spg42d60b-extra-utils/bin/modprobe > /proc/sys/kernel/modprobe
  128. for i in btrfs crc32c dm_mod; do
  129.     echo "loading module $(basename $i)..."
  130.     modprobe $i || true
  131. done
  132.  
  133.  
  134. # Create device nodes in /dev.
  135. echo "running udev..."
  136. mkdir -p /etc/udev
  137. ln -sfn /nix/store/bndvjw25b57m7jx5bbg7g1yam5dk4cv8-udev-rules /etc/udev/rules.d
  138. mkdir -p /dev/.mdadm
  139. systemd-udevd --daemon
  140. udevadm trigger --action=add
  141. udevadm settle || true
  142.  
  143.  
  144. # Load boot-time keymap before any LVM/LUKS initialization
  145. /nix/store/d94yl3ab8nh76q9px1c66i4spg42d60b-extra-utils/bin/busybox loadkmap < "/nix/store/13rkissmw7kc3wmjlsc89q23x6n4hi93-boottime-keymap"
  146.  
  147.  
  148. # XXX: Use case usb->lvm will still fail, usb->luks->lvm is covered
  149. # Wait for luksRoot to appear, e.g. if on a usb drive.
  150. # XXX: copied and adapted from stage-1-init.sh - should be
  151. # available as a function.
  152. if ! test -e /dev/sda7; then
  153.     echo -n "waiting 10 seconds for device /dev/sda7 to appear..."
  154.     for try in $(seq 10); do
  155.         sleep 1
  156.         if test -e /dev/sda7; then break; fi
  157.         echo -n .
  158.     done
  159.     echo "ok"
  160. fi
  161.  
  162.  
  163.  
  164. open_normally() {
  165.     cryptsetup luksOpen /dev/sda7 root  \
  166.      
  167. }
  168.  
  169.  
  170.  
  171. # open luksRoot and scan for logical volumes
  172. open_normally
  173.  
  174.  
  175.  
  176.  
  177. echo "starting device mapper and LVM..."
  178. lvm vgchange -ay
  179.  
  180. if test -n "$debug1devices"; then fail; fi
  181.  
  182.  
  183. btrfs device scan
  184.  
  185.  
  186.  
  187.  
  188. # Try to resume - all modules are loaded now, and devices exist
  189. if test -e /sys/power/tuxonice/resume; then
  190.     if test -n "$(cat /sys/power/tuxonice/resume)"; then
  191.         echo 0 > /sys/power/tuxonice/user_interface/enabled
  192.         echo 1 > /sys/power/tuxonice/do_resume || echo "failed to resume..."
  193.     fi
  194. fi
  195.  
  196. if test -n "" -a -e /sys/power/resume -a -e /sys/power/disk; then
  197.     echo "" > /sys/power/resume 2> /dev/null || echo "failed to resume..."
  198.     echo shutdown > /sys/power/disk
  199. fi
  200.  
  201.  
  202. # Return true if the machine is on AC power, or if we can't determine
  203. # whether it's on AC power.
  204. onACPower() {
  205.     ! test -d "/proc/acpi/battery" ||
  206.     ! ls /proc/acpi/battery/BAT[0-9]* > /dev/null 2>&1 ||
  207.     ! cat /proc/acpi/battery/BAT*/state | grep "^charging state" | grep -q "discharg"
  208. }
  209.  
  210.  
  211. # Check the specified file system, if appropriate.
  212. checkFS() {
  213. #    local device="$1"
  214. #    local fsType="$2"
  215.  
  216.     # Only check block devices.
  217. #    if [ ! -b "$device" ]; then return 0; fi
  218.  
  219.     # Don't check ROM filesystems.
  220. #    if [ "$fsType" = iso9660 -o "$fsType" = udf ]; then return 0; fi
  221.  
  222.     # If we couldn't figure out the FS type, then skip fsck.
  223. #    if [ "$fsType" = auto ]; then
  224. #        echo 'cannot check filesystem with type "auto"!'
  225. #        return 0
  226. #    fi
  227.  
  228.     # Optionally, skip fsck on journaling filesystems.  This option is
  229.     # a hack - it's mostly because e2fsck on ext3 takes much longer to
  230.     # recover the journal than the ext3 implementation in the kernel
  231.     # does (minutes versus seconds).
  232. #    if test -z "" -a \
  233. #        \( "$fsType" = ext3 -o "$fsType" = ext4 -o "$fsType" = reiserfs \
  234. #        -o "$fsType" = xfs -o "$fsType" = jfs \)
  235. #    then
  236. #        return 0
  237. #    fi
  238.  
  239.     # Don't run `fsck' if the machine is on battery power.  !!! Is
  240.     # this a good idea?
  241. #    if ! onACPower; then
  242. #        echo "on battery power, so no \`fsck' will be performed on \`$device'"
  243. #        return 0
  244. #    fi
  245. #
  246. #    echo "checking $device..."
  247. #
  248. #    fsckFlags=
  249. #    if test "$fsType" != "btrfs"; then
  250. #        fsckFlags="-V -a"
  251. #    fi
  252. #    fsck $fsckFlags "$device"
  253. #    fsckResult=$?
  254. #
  255. #    if test $(($fsckResult | 2)) = $fsckResult; then
  256. #        echo "fsck finished, rebooting..."
  257. #        sleep 3
  258. #        reboot -f
  259. #    fi
  260.  
  261. #    if test $(($fsckResult | 4)) = $fsckResult; then
  262. #        echo "$device has unrepaired errors, please fix them manually."
  263. #        fail
  264. #    fi
  265. #
  266. #    if test $fsckResult -ge 8; then
  267. #        echo "fsck on $device failed."
  268. #        fail
  269. #    fi
  270. #
  271. #    return 0
  272. }
  273.  
  274.  
  275. # Function for mounting a file system.
  276. mountFS() {
  277.     local device="$1"
  278.     local mountPoint="$2"
  279.     local options="$3"
  280.     local fsType="$4"
  281.  
  282.     if [ "$fsType" = auto ]; then
  283.         fsType=$(blkid -o value -s TYPE "$device")
  284.         if [ -z "$fsType" ]; then fsType=auto; fi
  285.     fi
  286.  
  287.     echo "$device /mnt-root$mountPoint $fsType $options" >> /etc/fstab
  288.  
  289.     checkFS "$device" "$fsType"
  290.  
  291.     echo "mounting $device on $mountPoint..."
  292.  
  293.     mkdir -p "/mnt-root$mountPoint" || true
  294.  
  295.     # For CIFS mounts, retry a few times before giving up.
  296.     local n=0
  297.     while true; do
  298.         mount "/mnt-root$mountPoint" && break
  299.         if [ "$fsType" != cifs -o "$n" -ge 10 ]; then fail; break; fi
  300.         echo "retrying..."
  301.         n=$((n + 1))
  302.     done
  303. }
  304.  
  305.  
  306. # Try to find and mount the root device.
  307. mkdir /mnt-root
  308.  
  309. exec 3< /nix/store/nksi36abc4x178dzpg9k87v9lqk7gsbc-initrd-fsinfo
  310.  
  311. while read -u 3 mountPoint; do
  312.     read -u 3 device
  313.     read -u 3 fsType
  314.     read -u 3 options
  315.  
  316.     # !!! Really quick hack to support bind mounts, i.e., where the
  317.     # "device" should be taken relative to /mnt-root, not /.  Assume
  318.     # that every device that starts with / but doesn't start with /dev
  319.     # is a bind mount.
  320.     pseudoDevice=
  321.     case $device in
  322.         /dev/*)
  323.             ;;
  324.         //*)
  325.             # Don't touch SMB/CIFS paths.
  326.             pseudoDevice=1
  327.             ;;
  328.         /*)
  329.             device=/mnt-root$device
  330.             ;;
  331.         *)
  332.             # Not an absolute path; assume that it's a pseudo-device
  333.             # like an NFS path (e.g. "server:/path").
  334.             pseudoDevice=1
  335.             ;;
  336.     esac
  337.  
  338.     # USB storage devices tend to appear with some delay.  It would be
  339.     # great if we had a way to synchronously wait for them, but
  340.     # alas...  So just wait for a few seconds for the device to
  341.     # appear.  If it doesn't appear, try to mount it anyway (and
  342.     # probably fail).  This is a fallback for non-device "devices"
  343.     # that we don't properly recognise.
  344.     if test -z "$pseudoDevice" -a ! -e $device; then
  345.         echo -n "waiting for device $device to appear..."
  346.         for try in $(seq 1 20); do
  347.             sleep 1
  348.             # also re-try lvm activation now that new block devices might have appeared
  349.             lvm vgchange -ay
  350.             # and tell udev to create nodes for the new LVs
  351.             udevadm trigger --action=add
  352.             if test -e $device; then break; fi
  353.             echo -n "."
  354.         done
  355.         echo
  356.     fi
  357.  
  358.     # Wait once more for the udev queue to empty, just in case it's
  359.     # doing something with $device right now.
  360.     udevadm settle || true
  361.  
  362.     mountFS "$device" "$mountPoint" "$options" "$fsType"
  363. done
  364.  
  365. exec 3>&-
  366.  
  367.  
  368.  
  369.  
  370.  
  371. # Stop udevd.
  372. udevadm control --exit || true
  373.  
  374. # Kill any remaining processes, just to be sure we're not taking any
  375. # with us into stage 2. unionfs-fuse mounts require the unionfs process.
  376. pkill -9 -v '(1|unionfs)'
  377.  
  378.  
  379. if test -n "$debug1mounts"; then fail; fi
  380.  
  381.  
  382. # Restore /proc/sys/kernel/modprobe to its original value.
  383. echo /sbin/modprobe > /proc/sys/kernel/modprobe
  384.  
  385.  
  386. # Start stage 2.  `switch_root' deletes all files in the ramfs on the
  387. # current root.  Note that $stage2Init might be an absolute symlink,
  388. # in which case "-e" won't work because we're not in the chroot yet.
  389. if ! test -e "$targetRoot/$stage2Init" -o -L "$targetRoot/$stage2Init"; then
  390.     echo "stage 2 init script ($targetRoot/$stage2Init) not found"
  391.     fail
  392. fi
  393.  
  394. mkdir -m 0755 -p $targetRoot/proc $targetRoot/sys $targetRoot/dev $targetRoot/run
  395.  
  396. mount --move /proc $targetRoot/proc
  397. mount --move /sys $targetRoot/sys
  398. mount --move /dev $targetRoot/dev
  399. mount --move /run $targetRoot/run
  400.  
  401. exec env -i $(type -P switch_root) "$targetRoot" "$stage2Init"
  402.  
  403. fail # should never be reached
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement