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