#!/bin/sh

echo Starting /init script ...

PATH=/sbin:/usr/sbin:/bin:/usr/bin
export PATH

# Debian bug 606622.
RUNLEVEL=S
PREVLEVEL=N
export RUNLEVEL PREVLEVEL

mkdir -p /sysroot

if [ ! -d /proc ]; then rm -f /proc; fi
mkdir -p /proc
mount -t proc /proc /proc
if [ ! -d /sys ]; then rm -f /sys; fi
mkdir -p /sys
mount -t sysfs /sys /sys
mkdir -p /run/lock

# devtmpfs is required since udev 176
mount -t devtmpfs /dev /dev

if [ ! -L /etc/init.d/udev -a -x /etc/init.d/udev ]; then
  if type service >/dev/null 2>&1; then
     service udev start
  else
     /etc/init.d/udev start
  fi
elif [ -x /sbin/start_udev ] && /sbin/start_udev; then
  :
else
  # Find udevd and run it directly.
  for f in /sbin/udevd /lib/udev/udevd /lib/systemd/systemd-udevd; do
    if [ -x "$f" ]; then UDEVD="$f"; fi
  done
  if [ -n "$UDEVD" ]; then
    echo -e '\000\000\000\000' > /proc/sys/kernel/hotplug
    $UDEVD --daemon
    udevadm trigger
    udevadm settle
  else
    echo No udevd, creating /dev manually.
    mount -t tmpfs none /dev
    mkdir /dev/pts /dev/shm /dev/mapper
    mount -t devpts -o gid=5,mode=620 /dev/pts /dev/pts
    # Must do each MAKEDEV individually, because if one device fails,
    # MAKEDEV will quit without creating the rest (RHBZ#507374).
    for dev in mem null port zero core full ram tty console fd \
      hda hdb hdc hdd sda sdb sdc sdd loop sd; do
      MAKEDEV $dev ||:
    done
    mknod /dev/ptmx c 5 2;    chmod 0666 /dev/ptmx
    mknod /dev/random c 1 8;  chmod 0666 /dev/random
    mknod /dev/urandom c 1 9; chmod 0444 /dev/urandom
    ln -sf /proc/self/fd/0 /dev/stdin
    ln -sf /proc/self/fd/1 /dev/stdout
    ln -sf /proc/self/fd/2 /dev/stderr

    modprobe virtio_pci
    modprobe virtio_net
  fi
fi

if grep -sq selinux=1 /proc/cmdline; then
  mount -t selinuxfs none /selinux
fi

# Update the system clock.
hwclock -u -s

# Set up the network.
ip addr add 127.0.0.1/8 brd + dev lo scope host
ip link set dev lo up

ip addr add 169.254.2.10/16 brd + dev eth0 scope global
ip link set dev eth0 up

ip route add default via 169.254.2.2

# Scan for MDs.
mdadm -As --auto=yes --run

# Scan for LVM.
modprobe dm_mod ||:

lvm vgscan --ignorelockingfailure
lvm vgchange -ay --ignorelockingfailure

# Improve virtio-blk performance (RHBZ#509383).
for f in /sys/block/vd*/queue/rotational; do echo 1 > $f; done

# http://kbase.redhat.com/faq/docs/DOC-5428
# Disabled this until https://bugzilla.redhat.com/show_bug.cgi?id=630583
# is fixed (broken in Linux 2.6.36).
#for f in /sys/block/[hsv]d*/queue/scheduler; do echo noop > $f; done

# These are useful when debugging.
if grep -sq guestfs_verbose=1 /proc/cmdline; then
    ls -lR /dev
    cat /proc/mounts
    lvm pvs
    lvm vgs
    lvm lvs
    ip a
    ip r
    lsmod
    #hwclock -r
    date
    #ping -n -v -c 5 10.0.2.2
    #ping -n -v -c 5 10.0.2.4

    echo -n "uptime: "; cat /proc/uptime
fi

if ! grep -sq guestfs_rescue=1 /proc/cmdline; then
  # Run the daemon under valgrind if ./configure --enable-valgrind-daemon
  vg_channel=/dev/virtio-ports/org.libguestfs.valgrind
  if [ -w $vg_channel ]; then
    if [ -r /etc/guestfsd.suppressions ]; then
      suppressions="--suppressions=/etc/guestfsd.suppressions"
    fi
    exec 3>$vg_channel
    vg="valgrind --leak-check=full --log-fd=3 --error-exitcode=119 --max-stackframe=8388608 --child-silent-after-fork=yes $suppressions"
    echo "enabling valgrind: $vg"
  fi

  # The host will kill qemu abruptly if guestfsd shuts down normally
  $vg guestfsd

  # Otherwise we try to clean up gracefully. For example, this ensures that a
  # core dump generated by the guest daemon will be written to disk.
else
  # Use appliance in rescue mode, also used by the virt-rescue command.
  eval $(grep -Eo 'TERM=[^[:space:]]+' /proc/cmdline)
  PS1='><rescue> '
  export TERM PS1
  echo
  echo "------------------------------------------------------------"
  echo
  echo "Welcome to virt-rescue, the libguestfs rescue shell."
  echo
  echo "Note: The contents of / are the rescue appliance."
  echo "You have to mount the guest's partitions under /sysroot"
  echo "before you can examine them."
  echo
  bash -i
  echo
  echo "virt-rescue: Syncing the disk now before exiting ..."
  echo
fi

sync
reboot -f
