1 #!/bin/sh
2 #
3 # initramfs /init (busybox ash)
4 #
5
6 # color codes and some predefined texts
7 BOLD="\033[1m"
8 NORM="\033[0m"
9 RED="\033[31m"
10 GREEN="\033[32m"
11 YELLOW="\033[33m"
12 BLUE="\033[34m"
13 OK="${BOLD}${GREEN}OK${NORM}"
14 FAILED="${BOLD}${RED}FAILED${NORM}"
15 DONE="${BOLD}${GREEN}DONE${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 # print a colored done status
30 echoDone() {
31 echo -e $DONE
32 }
33
34 # search for and mount the cdrom device, populate a tmpfs from it,
35 # and prepare /newroot for switch_root at the end of the script
36 find_and_mount_cdrom() {
37 echo -e -n " ${BOLD}${BLUE}*${NORM} Creating and mounting tmpfs... "
38 mkdir /.tmpfs
39 mount -t tmpfs tmpfs /.tmpfs
40 checkReturn
41
42 echo -e " ${BOLD}${BLUE}*${NORM} Searching for the CRUX CD..."
43 mkdir /.tmpfs/.cdrom
44 CRUXCD=""
45 CDROM_DEVICES="`grep 'drive name:' /proc/sys/dev/cdrom/info | cut -d: -f2`"
46 for DEV in $CDROM_DEVICES
47 do
48 DEV="/dev/$DEV"
49 mount -r -t iso9660 $DEV /.tmpfs/.cdrom
50 if [ $? -eq 0 ]
51 then
52 echo -e -n " ${BOLD}${GREEN}*${NORM} Found media on $DEV"
53 if [ -e /.tmpfs/.cdrom/crux-cd ]
54 then
55 echo ", CRUX CD."
56 CRUXCD=$DEV
57 ln -s $DEV /dev/cdrom
58 break
59 else
60 echo ", but it's not the CRUX CD."
61 umount /.tmpfs/.cdrom
62 fi
63 else
64 echo -e " ${BOLD}${YELLOW}*${NORM} No media found on $DEV."
65 fi
66 done
67
68 # check if the cdrom was mounted properly. if not, spawn a shell
69 if [ ! -e /.tmpfs/.cdrom/crux-cd ]
70 then
71 echo -e " ${BOLD}${RED}*${NORM} The CRUX CD was not properly mounted!"
72 echo " Spawning a shell for you to attempt to fix this problem. If"
73 echo " you are able to find your cdrom device, mount it at"
74 echo " /.tmpfs/.cdrom and then log out of this shell to continue."
75 echo " If you are NOT able to fix the problem, installation will"
76 echo " not be possible."
77 echo
78 /bin/sh
79 fi
80
81 # check again and stop if it's still not there
82 if [ ! -e /.tmpfs/.cdrom/crux-cd ]
83 then
84 echo -e " ${BOLD}${RED}*${NORM} The CRUX CD still appears not to be"
85 echo " found and installation will not continue."
86 echo
87 sleep 99999
88 exit 1
89 fi
90
91 echo -e -n " ${BOLD}${BLUE}*${NORM} Mounting squashfs filesystem... "
92 mkdir /.tmpfs/.squashfs
93 mount -r -t squashfs -o loop /.tmpfs/.cdrom/crux.squashfs /.tmpfs/.squashfs
94 checkReturn
95
96 echo -e " ${BOLD}${BLUE}*${NORM} Populating root filesystem..."
97 mkdir -p /newroot
98 echo -e -n " ${BOLD}${BLUE}*${NORM} Mounting new root filesystem... "
99 mount -t tmpfs tmpfs /newroot
100 checkReturn
101 echo -e -n " ${BOLD}${BLUE}*${NORM} Copying files from squashfs... "
102 cp -af /.tmpfs/.squashfs/* /newroot/
103 checkReturn
104 echo -e -n " ${BOLD}${BLUE}*${NORM} Copying devices from rootfs... "
105 cp -af /dev/* /newroot/dev/
106 checkReturn
107 mkdir -p /newroot/dev/pts /newroot/cdrom
108
109 echo -e -n " ${BOLD}${BLUE}*${NORM} Unmounting squashfs filesystem... "
110 umount /.tmpfs/.squashfs
111 checkReturn
112 rmdir /.tmpfs/.squashfs
113
114 # even though the CDROM gets unmounted here, it gets remounted after
115 # the new init starts, so the message might be confusing...
116 #echo -e -n " ${BOLD}${BLUE}*${NORM} Unmounting CDROM... "
117 umount -l /.tmpfs/.cdrom
118 #checkReturn
119 rmdir /.tmpfs/.cdrom
120
121 echo -e -n " ${BOLD}${BLUE}*${NORM} Unmounting tmpfs... "
122 umount /.tmpfs
123 checkReturn
124 rmdir /.tmpfs
125 }
126
127 # main script
128 echo ""
129 echo -e "${BOLD}CRUX 2.5 - ${BLUE}http://crux.nu/${NORM}"
130 echo ""
131
132 exec >/dev/console </dev/console 2>&1
133
134 echo -e -n " ${BOLD}${BLUE}*${NORM} Mounting "
135 echo -e -n "${BOLD}${GREEN}/proc${NORM}"
136 mount -t proc proc /proc
137
138 PRINTK="`cat /proc/sys/kernel/printk`"
139 echo "0" > /proc/sys/kernel/printk
140
141 echo -e ", ${BOLD}${GREEN}/sys${NORM}."
142 mount -t sysfs sysfs /sys
143
144 echo -e -n " ${BOLD}${BLUE}*${NORM} Populating /dev via mdev... "
145 mdev -s
146 checkReturn
147 echo -e -n " ${BOLD}${BLUE}*${NORM} Registering mdev as hotplug agent... "
148 echo "/bin/mdev" > /proc/sys/kernel/hotplug
149 checkReturn
150
151 if [ -d /lib/modules ]
152 then
153 echo -e -n " ${BOLD}${BLUE}*${NORM} Loading modules... "
154 # IDE support
155 modprobe ide-core
156
157 # general cdrom/disk support
158 modprobe cdrom
159 modprobe ide-cd_mod
160 modprobe ide-gd_mod
161 modprobe sr_mod
162 modprobe sd_mod
163
164 # IDE
165 load_ide_modules() {
166 echo -e -n "${BOLD}${BLUE}IDE${NORM}"
167 for mod in aec62xx alim15x3 amd74xx atiixp \
168 cmd64x cs5520 cs5530 cs5535 ide-pci-generic hpt366 it8213 \
169 it821x jmicron ns87415 opti621 pdc202xx_new piix rz1000 sc1200 \
170 serverworks siimage sis5513 slc90e66 tc86c001 triflex trm290 \
171 via82cxxx
172 do
173 modprobe $mod
174 done
175 echo -n ", "
176 }
177
178 # SCSI
179 load_scsi_modules() {
180 echo -e -n "${BOLD}${BLUE}SCSI${NORM}"
181 for mod in scsi_transport_fc scsi_transport_iscsi scsi_transport_sas \
182 scsi_transport_spi 3w-9xxx 3w-xxxx 53c700 BusLogic NCR53c406a a100u2w \
183 aacraid advansys aha152x aha1542 aha1740 aic79xx aic7xxx aic94xx arcmsr \
184 atp870u dc395x dmx3191d dpt_i2o dtc eata fdomain gdth hptiop in2000 \
185 initio ipr ips lpfc megaraid megaraid_mbox megaraid_sas nsp32 pas16 \
186 psi240i qla1280 qla2xxx qla4xxx qlogicfas qlogicfas408 seagate \
187 sim710 stex sym53c416 sym53c8xx t128 tmscsim u14-34f ultrastor \
188 wd7000 mptsas mptspi
189 do
190 modprobe $mod > /dev/null 2>&1
191 done
192 echo -n ", "
193 }
194
195 # USB
196 load_usb_modules() {
197 echo -e -n "${BOLD}${BLUE}USB${NORM}"
198 for mod in ehci-hcd ohci-hcd uhci-hcd sl811-hcd usb-storage usbhid
199 do
200 modprobe $mod
201 done
202 echo -n ", "
203 }
204
205 # SATA
206 load_sata_modules() {
207 echo -e -n "${BOLD}${BLUE}SATA${NORM}"
208 for mod in libata ata_piix sata_promise sata_sil sata_svw sata_via sata_nv \
209 sata_sx4 sata_sis sata_uli sata_qstor ahci
210 do
211 modprobe $mod
212 done
213 echo -n ", "
214 }
215
216 # FireWire
217 load_firewire_modules() {
218 echo -e -n "${BOLD}${BLUE}FireWire${NORM}"
219 for mod in core ohci sbp2
220 do
221 modprobe firewire-$mod
222 done
223 echo -n ", "
224 }
225
226 grep -q "libata" /proc/cmdline
227 C1=$?
228 grep -q "sata" /proc/cmdline
229 C2=$?
230 if [ $C1 -eq 0 -o $C2 -eq 0 ]
231 then
232 MODORDER="sata ide scsi usb firewire"
233 else
234 MODORDER="ide sata scsi usb firewire"
235 fi
236
237 for MODS in $MODORDER
238 do
239 grep -q "no$MODS" /proc/cmdline
240 [ $? -ne 0 ] && load_${MODS}_modules
241 done
242
243 echoDone
244 else
245 echo -e " ${BOLD}${YELLOW}*${NORM} No modules were found in the initial RAM filesystem."
246 fi
247
248 grep -q "devicetimeout=*" /proc/cmdline
249 if [ $? -eq 0 ]
250 then
251 for opt in `cat /proc/cmdline`
252 do
253 echo $opt | grep -q "devicetimeout"
254 if [ $? -eq 0 ]
255 then
256 DEVTIMEOUT=`echo $opt | cut -d= -f2`
257 fi
258 done
259 else
260 DEVTIMEOUT=10
261 fi
262 echo -e " ${BOLD}${BLUE}*${NORM} Waiting $DEVTIMEOUT seconds for devices to settle..."
263 sleep $DEVTIMEOUT
264
265 # if root=/dev/XXX was specified on the command line, use that as the new root
266 # instead of searching for the cdrom and using it. if it fails, fall back to
267 # the cdrom
268 grep -q "root=/dev/*" /proc/cmdline
269 if [ $? -eq 0 ]
270 then
271 #echo "root= was specified"
272 for opt in `cat /proc/cmdline`
273 do
274 echo "$opt" | grep -q root=
275 if [ $? -eq 0 ]
276 then
277 ROOTDEV=`echo $opt | cut -d= -f2`
278 fi
279 done
280 #/bin/sh
281 mkdir -p /newroot
282 # check the specified root device to see if it has an init
283 mount $ROOTDEV /newroot
284 if [ $? -ne 0 ]
285 then
286 echo -e " ${BOLD}${RED}*${NORM} Unable to mount the specified root device! Falling back to the live CD."
287 find_and_mount_cdrom
288 else
289 if [ -x /newroot/sbin/init ]
290 then
291 echo -e " ${BOLD}${BLUE}*${NORM} Mounted root device $ROOTDEV."
292 else
293 echo -e " ${BOLD}${RED}*${NORM} The specified root device ($ROOTDEV) does not appear to be usable! Falling back to the live CD."
294 umount /newroot
295 find_and_mount_cdrom
296 fi
297 fi
298 else
299 find_and_mount_cdrom
300 fi
301
302 echo -e " ${BOLD}${BLUE}*${NORM} Switching root.\n"
303 echo "$PRINTK" > /proc/sys/kernel/printk
304 echo > /proc/sys/kernel/hotplug
305 umount /sys
306 umount /proc
307 exec /bin/switch_root /newroot /sbin/init
308
309 echo "Something's broken, here's a shell."
310 exec /bin/sh
|