1c01195f7245b29b03a0131aa20085d0aaa38850
[libguestfs.git] / appliance / init
1 #!/bin/sh
2
3 echo Starting /init script ...
4
5 PATH=/sbin:/usr/sbin:/bin:/usr/bin
6 export PATH
7
8 mkdir -p /sysroot
9
10 mount -t proc /proc /proc
11 mount -t sysfs /sys /sys
12
13 if [ ! -L /etc/init.d/udev -a -x /etc/init.d/udev ]; then
14   if type service >/dev/null 2>&1; then
15      service udev start
16   else
17      /etc/init.d/udev start
18   fi
19 elif [ -x /sbin/start_udev ] && /sbin/start_udev; then
20   :
21 elif [ -x /sbin/udevd ]; then
22   echo -e '\000\000\000\000' > /proc/sys/kernel/hotplug
23   /sbin/udevd --daemon
24   /sbin/udevadm trigger
25   /sbin/udevadm settle --timeout=10
26 else
27   echo No udev, creating /dev manually
28   mount -t tmpfs none /dev
29   mkdir /dev/pts /dev/shm /dev/mapper
30   mount -t devpts -o gid=5,mode=620 /dev/pts /dev/pts
31   # Must do each MAKEDEV individually, because if one device fails,
32   # MAKEDEV will quit without creating the rest (RHBZ#507374).
33   for dev in mem null port zero core full ram tty console fd \
34     hda hdb hdc hdd sda sdb sdc sdd loop sd; do
35     MAKEDEV $dev ||:
36   done
37   mknod /dev/ptmx c 5 2;    chmod 0666 /dev/ptmx
38   mknod /dev/random c 1 8;  chmod 0666 /dev/random
39   mknod /dev/urandom c 1 9; chmod 0444 /dev/urandom
40   ln -sf /proc/self/fd/0 /dev/stdin
41   ln -sf /proc/self/fd/1 /dev/stdout
42   ln -sf /proc/self/fd/2 /dev/stderr
43
44   modprobe virtio_pci
45   modprobe virtio_net
46 fi
47
48 if grep -sq selinux=1 /proc/cmdline; then
49   mount -t selinuxfs none /selinux
50 fi
51
52 # Update the system clock.
53 hwclock -u -s
54
55 # Set up the network.
56 ifconfig lo 127.0.0.1
57 ifconfig eth0 169.254.2.10
58 route add default gw 169.254.2.2
59
60 # Scan for LVM.
61 modprobe dm_mod ||:
62
63 lvm vgscan --ignorelockingfailure
64 lvm vgchange -ay --ignorelockingfailure
65
66 # Improve virtio-blk performance (RHBZ#509383).
67 for f in /sys/block/vd*/queue/rotational; do echo 1 > $f; done
68
69 # http://kbase.redhat.com/faq/docs/DOC-5428
70 for f in /sys/block/[hsv]d*/queue/scheduler; do echo noop > $f; done
71
72 # These are useful when debugging.
73 if grep -sq guestfs_verbose=1 /proc/cmdline; then
74     ls -l /dev
75     cat /proc/mounts
76     lvm pvs
77     lvm vgs
78     lvm lvs
79     ifconfig
80     netstat -rn
81     lsmod
82     #hwclock -r
83     date
84     #ping -n -v -c 5 10.0.2.2
85     #ping -n -v -c 5 10.0.2.4
86
87     echo -n "uptime: "; cat /proc/uptime
88 fi
89
90 if ! grep -sq guestfs_rescue=1 /proc/cmdline; then
91   # The host will kill qemu abruptly if guestfsd shuts down normally
92   guestfsd -f
93
94   # Otherwise we try to clean up gracefully. For example, this ensures that a
95   # core dump generated by the guest daemon will be written to disk.
96 else
97   # Use appliance in rescue mode, also used by the virt-rescue command.
98   eval $(grep -Eo 'TERM=[^[:space:]]+' /proc/cmdline)
99   PS1='><rescue> '
100   export TERM PS1
101   echo
102   echo "------------------------------------------------------------"
103   echo
104   echo "Welcome to virt-rescue, the libguestfs rescue shell."
105   echo
106   echo "Note: The contents of / are the rescue appliance."
107   echo "You have to mount the guest's partitions under /sysroot"
108   echo "before you can examine them."
109   echo
110   bash -i
111   echo
112   echo "virt-rescue: Syncing the disk now before exiting ..."
113   echo
114 fi
115
116 sync
117 /sbin/reboot -f