Fix for file injection is broken on centos computes with Cirros images LP #1587960
[packages/trusty/cirros-testvm.git] / cirros-testvm / src-cirros / src / init
1 #!/bin/sh
2 # vi: ts=4 noexpandtab
3 #
4 # This is the initramfs init script
5 ROOTFS_LABEL="cirros-rootfs"
6
7 . /lib/cirros/shlib
8
9 mkdir -p /proc /newroot /dev /tmp
10
11 mount -t devtmpfs /dev /dev
12 mount -t proc /proc /proc
13
14 echo "6 4 1 7" >/proc/sys/kernel/printk
15
16 parse_cmdline
17
18 for x in $KC_CONSOLES; do
19         [ "$x" = "$KC_CONSOLE" ] ||
20                 echo "further output written to $KC_CONSOLE" > "$x";
21 done
22 if [ -n "$KC_PREF_CONSOLE" ]; then
23         exec >"$KC_PREF_CONSOLE" 2>&1 <"$KC_PREF_CONSOLE"
24 fi
25
26 read uptime cputime < /proc/uptime
27 iinfo "initramfs: up at $uptime"
28
29 idebug "loading modules"
30 /etc/init.d/load-modules start
31
32 ROOT=""
33 MOUNTED=""
34
35 if [ "$KC_RAMDISK_ROOT" != "1" ]; then
36         while read maj min blk name other; do
37                 [ -n "${maj}" -a "${maj}" != "major" ] || continue
38                 umount=0
39                 mount -o ro "/dev/${name}" "/newroot" >/dev/null 2>&1 &&
40                         MOUNTED="$MOUNTED ${name}" && umount=1 &&
41                         [ -x /newroot/sbin/init ] && ROOT="/dev/$name" && break
42                 [ ${umount} -eq 0 ] || umount /newroot
43         done < /proc/partitions
44 fi
45
46 if [ "$KC_DEBUG" = "1" ]; then
47         echo "dropping into initramfs debug shell"
48         /bin/sh
49 fi
50
51 if [ -z "$ROOT" -a "$KC_RAMDISK_ROOT" != "1" ] &&
52    blank=$(blkid -l -o device -t LABEL=$ROOTFS_LABEL) &&
53    [ -e "$blank" ]; then
54         iinfo "copying initramfs to $blank"
55         items=""
56         set +f
57         for x in /*; do
58                 case "$x" in
59                         /dev|/proc|/newroot) : ;;
60                         *) items="$items $x";;
61                 esac
62         done
63         set -f
64         mount "$blank" /newroot
65         mkdir -p /newroot/dev /newroot/proc
66         cp -a $items /newroot/
67         cp -a /dev/console /newroot/dev
68         mount -o remount,ro /newroot
69         ROOT="$blank"
70 fi
71
72 if [ -n "$ROOT" ]; then
73         cd /newroot
74         mount -o move /dev ./dev
75         umount /proc
76         iinfo "initramfs loading root from $ROOT"
77         exec switch_root /newroot "$KC_INIT" "$@"
78 else
79         while read maj min blk name other; do
80                 [ -n "${maj}" -a "${maj}" != "major" ] || continue
81                 umount=0
82                 mount -o ro "/dev/${name}" "/newroot" >/dev/null 2>&1 &&
83                         umount=1 &&
84                         [ -x /newroot/sbin/init ] && ROOT="/dev/$name" && break
85                 [ ${umount} -eq 0 ] || umount /newroot
86         done < /proc/partitions
87         umount /proc
88         [ "$KC_RAMDISK_ROOT" = "0" ] &&
89                 iinfo "initramfs did not find root, running as ramdisk" ||
90                 iinfo "running in ramdisk mode as requested"
91         exec "$KC_INIT" "$@"
92 fi
93 failure "badness occurred in ramdisk"