CRUX-ARM : Home

Home :: Documentation :: Download :: Development :: Community :: Ports :: Packages :: Bugs :: Links :: About :: Donors
Changes and improvements in order to adopt the new upstream ideas:
[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 #ln -s $DEV /dev/media
55 break
56 else
57 echo ", but it's not the CRUX media."
58 umount /.tmpfs/.media
59 fi
60 else
61 echo -e " ${BOLD}${YELLOW}*${NORM} No media found on $DEV."
62 fi
63 done
64
65 # check if the media was mounted properly. if not, spawn a shell
66 if [ ! -e /.tmpfs/.media/crux-media ]
67 then
68 echo
69 echo -e " ${BOLD}${RED}*${NORM} The CRUX media was not properly mounted!"
70 echo " Spawning a shell for you to attempt to fix this problem. If"
71 echo " you are able to find the correct device, mount it at"
72 echo " /.tmpfs/.media and then log out of this shell to continue."
73 echo " If you are NOT able to fix the problem, installation will"
74 echo " not be possible."
75 echo
76 /bin/sh
77 fi
78
79 # check again and stop if it's still not there
80 if [ ! -e /.tmpfs/.media/crux-media ]
81 then
82 echo
83 echo -e " ${BOLD}${RED}*${NORM} The CRUX media still appears not to be"
84 echo " found and installation will not continue."
85 echo
86 sleep 99999
87 exit 1
88 fi
89
90 echo -e " ${BOLD}${BLUE}*${NORM} Populating root filesystem..."
91 mkdir -p /newroot
92 echo -e -n " ${BOLD}${BLUE}*${NORM} Mounting new root filesystem... "
93 mount -t tmpfs tmpfs /newroot
94 checkReturn
95 echo -e -n " ${BOLD}${BLUE}*${NORM} Copying files from rootfs.tar.xz... "
96 tar -xpf /.tmpfs/.media/rootfs.tar.xz -C /newroot
97 checkReturn
98 echo -e -n " ${BOLD}${BLUE}*${NORM} Copying devices from rootfs... "
99 cp -af /dev/* /newroot/dev
100 checkReturn
101 mkdir -p /newroot/dev/pts /newroot/media
102
103 # the media gets unmounted here and remounted after switch_root
104 umount /.tmpfs/.media
105 rmdir /.tmpfs/.media
106 sed -i -e "s,#CRUXMEDIA#,${CRUXMEDIA}," /newroot/etc/fstab
107
108 echo -e -n " ${BOLD}${BLUE}*${NORM} Unmounting tmpfs... "
109 umount /.tmpfs
110 checkReturn
111 rmdir /.tmpfs
112 }
113
114 /bin/busybox --install -s /bin
115
116 #
117 # main script
118 #
119
120 echo ""
121 echo -e "${BOLD}CRUX-ARM 2.7.1 - ${BLUE}http://crux-arm.nu/${NORM}"
122 echo ""
123
124 exec >/dev/console </dev/console 2>&1
125
126 echo -e -n " ${BOLD}${BLUE}*${NORM} Mounting "
127 echo -e -n "${BOLD}${GREEN}/proc${NORM}"
128 mount -t proc proc /proc
129
130 PRINTK="`cat /proc/sys/kernel/printk`"
131 echo "0" > /proc/sys/kernel/printk
132
133 echo -e ", ${BOLD}${GREEN}/sys${NORM}."
134 mount -t sysfs sysfs /sys
135
136 echo -e -n " ${BOLD}${BLUE}*${NORM} Populating /dev via mdev... "
137 mdev -s
138 checkReturn
139
140 echo -e -n " ${BOLD}${BLUE}*${NORM} Registering mdev as hotplug agent... "
141 echo "/bin/mdev" > /proc/sys/kernel/hotplug
142 checkReturn
143
144 if [ -d /lib/modules ]
145 then
146 echo -e -n " ${BOLD}${BLUE}*${NORM} Loading modules... "
147 # TODO
148 else
149 echo -e " ${BOLD}${YELLOW}*${NORM} No modules were found in the initial RAM filesystem."
150 fi
151
152 grep -q "devicetimeout=*" /proc/cmdline
153 if [ $? -eq 0 ]
154 then
155 for opt in `cat /proc/cmdline`
156 do
157 echo "$opt" | grep -q "devicetimeout="
158 if [ $? -eq 0 ]
159 then
160 DEVTIMEOUT=`echo $opt | cut -d'=' -f2`
161 fi
162 done
163 else
164 DEVTIMEOUT=10
165 fi
166 echo -e " ${BOLD}${BLUE}*${NORM} Waiting $DEVTIMEOUT seconds for devices to settle..."
167 sleep $DEVTIMEOUT
168
169 # if root=/dev/XXX was specified on the command line, use that as the new root
170 # instead of searching for the media and using it. if it fails, fall back to the media
171 grep -q "root=/dev/*" /proc/cmdline
172 if [ $? -eq 0 ]
173 then
174 for opt in `cat /proc/cmdline`
175 do
176 echo "$opt" | grep -q "root="
177 if [ $? -eq 0 ]
178 then
179 ROOTDEV=`echo $opt | cut -d'=' -f2`
180 fi
181 done
182 mkdir -p /newroot
183 # check the specified root device to see if it has an init
184 mount $ROOTDEV /newroot
185 if [ $? -ne 0 ]
186 then
187 echo -e " ${BOLD}${RED}*${NORM} Unable to mount the specified root device! Falling back to the live media."
188 find_and_mount_media
189 else
190 if [ -x /newroot/sbin/init ]
191 then
192 echo -e " ${BOLD}${BLUE}*${NORM} Mounted root device $ROOTDEV."
193 else
194 echo -e " ${BOLD}${RED}*${NORM} The specified root device ($ROOTDEV) does not appear to be usable! Falling back to the live media."
195 umount /newroot
196 find_and_mount_media
197 fi
198 fi
199 else
200 find_and_mount_media
201 fi
202
203 echo -e "${BOLD}${BLUE}*${NORM} Switching root.\n"
204 echo "$PRINTK" > /proc/sys/kernel/printk
205 echo > /proc/sys/kernel/hotplug
206 umount /sys
207 umount /proc
208 exec /bin/switch_root /newroot /sbin/init
209
210 echo "Something's broken, here's a shell."
211 exec /bin/sh
212
213 # End of file