CRUX-ARM : Home

Home :: Documentation :: Download :: Development :: Community :: Ports :: Packages :: Bugs :: Links :: About :: Donors
Cleanup for 'init' script
[initramfs.git] / init
1 #!/bin/sh
2
3 #
4 # initramfs /init (busybox ash)
5 #
6
7 # color codes and some predefined texts
8 BOLD="\033[1m"
9 NORM="\033[0m"
10 RED="\033[31m"
11 GREEN="\033[32m"
12 YELLOW="\033[33m"
13 BLUE="\033[34m"
14 OK="${BOLD}${GREEN}OK${NORM}"
15 FAILED="${BOLD}${RED}FAILED${NORM}"
16
17 # helper functions
18
19 # check an exit value and print a colored status
20 checkReturn() {
21 if [ $? -ne 0 ]
22 then
23 echo -e $FAILED
24 else
25 echo -e $OK
26 fi
27 }
28
29 # search for and mount the crux media, populate a tmpfs from it,
30 # and prepare /newroot for switch_root at the end of the script
31 find_and_mount_media() {
32 echo -e -n " ${BOLD}${BLUE}*${NORM} Creating and mounting tmpfs... "
33 mkdir /.tmpfs
34 mount -t tmpfs tmpfs /.tmpfs
35 checkReturn
36
37 echo -e " ${BOLD}${BLUE}*${NORM} Searching for the CRUX media..."
38 mkdir -p /.tmpfs/.media
39 CRUXMEDIA=""
40 CDROM_DEVICES="`grep 'drive name:' /proc/sys/dev/cdrom/info | awk '{ print $3 }'`"
41 MMC_DEVICES="`grep -E 'mmcblk' /proc/partitions | awk '{ print $4 }'`"
42 BLOCK_DEVICES="`grep -E '[sh]d' /proc/partitions | awk '{ print $4 }'`"
43 for DEV in $CDROM_DEVICES $MMC_DEVICES $BLOCK_DEVICES
44 do
45 DEV="/dev/$DEV"
46 mount -r $DEV /.tmpfs/.media 2> /dev/null
47 if [ $? -eq 0 ]
48 then
49 echo -e -n " ${BOLD}${GREEN}*${NORM} Found media on $DEV"
50 if [ -e /.tmpfs/.media/crux-media ]
51 then
52 echo ", CRUX media."
53 CRUXMEDIA=$DEV
54 break
55 else
56 echo ", but it's not the CRUX media."
57 umount /.tmpfs/.media
58 fi
59 else
60 echo -e " ${BOLD}${YELLOW}*${NORM} No media found on $DEV."
61 fi
62 done
63
64 # check if the media was mounted properly. if not, spawn a shell
65 if [ ! -e /.tmpfs/.media/crux-media ]
66 then
67 echo -e " ${BOLD}${RED}*${NORM} The CRUX media was not properly mounted!"
68 echo " Spawning a shell for you to attempt to fix this problem. If"
69 echo " you are able to find the correct device, mount it at"
70 echo " /.tmpfs/.media and then log out of this shell to continue."
71 echo " If you are NOT able to fix the problem, installation will"
72 echo " not be possible."
73 echo
74 /bin/sh
75 fi
76
77 # check again and stop if it's still not there
78 if [ ! -e /.tmpfs/.media/crux-media ]
79 then
80 echo -e " ${BOLD}${RED}*${NORM} The CRUX media still appears not to be"
81 echo " found and installation will not continue."
82 echo
83 sleep 99999
84 exit 1
85 fi
86
87 echo -e " ${BOLD}${BLUE}*${NORM} Populating root filesystem..."
88 mkdir -p /newroot
89 echo -e -n " ${BOLD}${BLUE}*${NORM} Mounting new root filesystem... "
90 mount -t tmpfs tmpfs /newroot
91 checkReturn
92 echo -e -n " ${BOLD}${BLUE}*${NORM} Copying files from rootfs.tar.xz... "
93 tar -xpf /.tmpfs/.media/rootfs.tar.xz -C /newroot
94 checkReturn
95 echo -e -n " ${BOLD}${BLUE}*${NORM} Copying devices from rootfs... "
96 cp -af /dev/* /newroot/dev/
97 checkReturn
98 mkdir -p /newroot/dev/pts /newroot/media
99
100 # the media gets unmounted here and remounted after switch_root
101 umount /.tmpfs/.media
102 rmdir /.tmpfs/.media
103 sed -i -e "s,#CRUXMEDIA#,${CRUXMEDIA}," /newroot/etc/fstab
104
105 echo -e -n " ${BOLD}${BLUE}*${NORM} Unmounting tmpfs... "
106 umount /.tmpfs
107 checkReturn
108 rmdir /.tmpfs
109 }
110
111 /bin/busybox --install -s /bin
112
113 # main script
114
115 echo ""
116 echo -e "${BOLD}CRUX-ARM 2.7.1 - ${BLUE}http://crux-arm.nu/${NORM}"
117 echo ""
118
119 exec >/dev/console </dev/console 2>&1
120
121 echo -e -n " ${BOLD}${BLUE}*${NORM} Mounting "
122 echo -e -n "${BOLD}${GREEN}/proc${NORM}"
123 mount -t proc proc /proc
124
125 PRINTK="`cat /proc/sys/kernel/printk`"
126 echo "0" > /proc/sys/kernel/printk
127
128 echo -e ", ${BOLD}${GREEN}/sys${NORM}."
129 mount -t sysfs sysfs /sys
130
131 echo -e -n " ${BOLD}${BLUE}*${NORM} Populating /dev via mdev... "
132 mdev -s
133 checkReturn
134 echo -e -n " ${BOLD}${BLUE}*${NORM} Registering mdev as hotplug agent... "
135 echo "/bin/mdev" > /proc/sys/kernel/hotplug
136 checkReturn
137
138 if [ -d /lib/modules ]
139 then
140 echo -e -n " ${BOLD}${BLUE}*${NORM} Loading modules... "
141 # TODO
142 else
143 echo -e " ${BOLD}${YELLOW}*${NORM} No modules were found in the initial RAM filesystem."
144 fi
145
146 grep -q "devicetimeout=*" /proc/cmdline
147 if [ $? -eq 0 ]
148 then
149 for opt in `cat /proc/cmdline`
150 do
151 echo "$opt" | grep -q "devicetimeout="
152 if [ $? -eq 0 ]
153 then
154 DEVTIMEOUT=`echo $opt | cut -d'=' -f2`
155 fi
156 done
157 else
158 DEVTIMEOUT=10
159 fi
160 echo -e " ${BOLD}${BLUE}*${NORM} Waiting $DEVTIMEOUT seconds for devices to settle..."
161 sleep $DEVTIMEOUT
162
163 # if root=/dev/XXX was specified on the command line, use that as the new root
164 # instead of searching for the media and using it. if it fails, fall back to
165 # the media
166 grep -q "root=/dev/*" /proc/cmdline
167 if [ $? -eq 0 ]
168 then
169 for opt in `cat /proc/cmdline`
170 do
171 echo "$opt" | grep -q "root="
172 if [ $? -eq 0 ]
173 then
174 ROOTDEV=`echo $opt | cut -d'=' -f2`
175 fi
176 done
177 mkdir -p /newroot
178 # check the specified root device to see if it has an init
179 mount $ROOTDEV /newroot
180 if [ $? -ne 0 ]
181 then
182 echo -e " ${BOLD}${RED}*${NORM} Unable to mount the specified root device! Falling back to the live media."
183 find_and_mount_media
184 else
185 if [ -x /newroot/sbin/init ]
186 then
187 echo -e " ${BOLD}${BLUE}*${NORM} Mounted root device $ROOTDEV."
188 else
189 echo -e " ${BOLD}${RED}*${NORM} The specified root device ($ROOTDEV) does not appear to be usable! Falling back to the live media."
190 umount /newroot
191 find_and_mount_media
192 fi
193 fi
194 else
195 find_and_mount_media
196 fi
197
198 echo -e " ${BOLD}${BLUE}*${NORM} Switching root.\n"
199 echo "$PRINTK" > /proc/sys/kernel/printk
200 echo > /proc/sys/kernel/hotplug
201 umount /sys
202 umount /proc
203 exec /bin/switch_root /newroot /sbin/init
204
205 echo "Something's broken, here's a shell."
206 exec /bin/sh
207
208 # End of file