2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2008, 2009 Michael Tremer & Christian Schmidt #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
22 .
/usr
/lib
/mkinitramfs
/functions
24 # Modules needed by the live system
25 MODULES
="$MODULES aufs squashfs loop vfat ehci-hcd ohci-hcd uhci-hcd usb-storage"
28 MODULES
="$MODULES nfs"
29 for module
in /lib
/modules
/$KERNEL/kernel
/drivers
/net
/{,*/}*; do
30 MODULES
="$MODULES $(basename ${module/.ko})"
33 # Add all storage modules
34 for module
in /lib
/modules
/$KERNEL/kernel
/drivers
/{ata
,message
/fusion
,pcmcia
,scsi
{,/*}}/*; do
35 MODULES
="$MODULES $(basename ${module/.ko})"
38 install arping dhclient dhclient-script
ping
41 mkdir
-p mnt
/{source,tmpfs
,overlayfs
,squashfs
}
43 cat >> sbin
/real-init
<<'EOF'
47 # Users can override rootfs target on the kernel commandline
48 for o in $(cat /proc/cmdline); do
54 rootflags=${o#rootflags=}
57 rootfstype=${o#rootfstype=}
63 netdevice=${o#netdevice=}
74 # generate udev rules to generate /dev/root symlink
75 if [ -z $root ] ; then
80 LABEL=${root#/dev/disk/by-label/}
81 echo "SUBSYSTEM==\"block\", PROGRAM=\"/lib/udev/vol_id -l %N\", RESULT==\"$LABEL\", SYMLINK+=\"root\"" > /etc/udev/rules.d/00-label.rules
83 echo "Added udev rule 00-label.rules:"
84 cat /etc/udev/rules.d/00-label.rules
86 thingtomount=/dev/root
89 CDLABEL=${root#CDLABEL=}
90 echo "KERNEL==\"hd[a-z]\", BUS==\"ide\", SYSFS{removable}==\"1\", ATTRS{media}==\"cdrom\", PROGRAM=\"/lib/udev/vol_id -l %N\", RESULT==\"$CDLABEL\", SYMLINK+=\"root\"" > /etc/udev/rules.d/00-cdlabel.rules
91 echo "KERNEL==\"sr[0-9]\", PROGRAM=\"/lib/udev/vol_id -l %N\", RESULT==\"$CDLABEL\", SYMLINK+=\"root\"" >> /etc/udev/rules.d/00-cdlabel.rules
92 echo "KERNEL==\"scd[0-9]\", PROGRAM=\"/lib/udev/vol_id -l %N\", RESULT==\"$CDLABEL\", SYMLINK+=\"root\"" >> /etc/udev/rules.d/00-cdlabel.rules
93 echo "KERNEL==\"pcd[0-9]\", PROGRAM=\"/lib/udev/vol_id -l %N\", RESULT==\"$CDLABEL\", SYMLINK+=\"root\"" >> /etc/udev/rules.d/00-cdlabel.rules
95 echo "Added udev rule 00-cdlabel.rules:"
96 cat /etc/udev/rules.d/00-cdlabel.rules
98 thingtomount=/dev/root
102 echo "SUBSYSTEM==\"block\", PROGRAM=\"/lib/udev/vol_id -l %N\", RESULT==\"$LABEL\", SYMLINK+=\"root\"" > /etc/udev/rules.d/00-label.rules
104 echo "Added udev rule 00-label.rules:"
105 cat /etc/udev/rules.d/00-label.rules
107 thingtomount=/dev/root
110 UUID=${root#/dev/disk/by-id/}
111 echo "SUBSYSTEM==\"block\", PROGRAM=\"/lib/udev/vol_id -u %N\", RESULT==\"$UUID\", SYMLINK+=\"root\"" > /etc/udev/rules.d/01-uuid.rules
113 echo "Added udev rule 01-uuid.rules:"
114 cat /etc/udev/rules.d/01-uuid.rules
116 thingtomount=/dev/root
120 echo "SUBSYSTEM==\"block\", PROGRAM=\"/lib/udev/vol_id -u %N\", RESULT==\"$UUID\", SYMLINK+=\"root\"" > /etc/udev/rules.d/01-uuid.rules
122 echo "Added udev rule 01-uuid.rules:"
123 cat /etc/udev/rules.d/01-uuid.rules
125 thingtomount=/dev/root
129 vecho "Going on to mount $NFS"
130 #mount -t nfs $NFS /mnt/source
132 #thingtomount=$(ls -1 /mnt/source/*.iso | head -n 1)
135 ln -s $root /dev/root
144 echo "udev_log=\"error\"" >> /etc/udev/udev.conf
146 # rules for loading modules
147 echo -n "ACTION==\"add\", SUBSYSTEM==\"?*\", ENV{MODALIAS}==\"?*\", RUN+=\"/sbin/modprobe $" >> /etc/udev/rules.d/10-modprobe.rules
148 echo "env{MODALIAS}\"" >> /etc/udev/rules.d/10-modprobe.rules
149 echo "ACTION==\"add\", SUBSYSTEM==\"scsi_device\" RUN+=\"/sbin/modprobe sg\"" >> /etc/udev/rules.d/10-modprobe.rules
150 echo "ACTION==\"add\", SUBSYSTEM==\"scsi_device\", SYSFS{type}==\"0|7|14\", RUN+=\"/sbin/modprobe sd_mod\"" >> /etc/udev/rules.d/10-modprobe.rules
151 echo "ACTION==\"add\", SUBSYSTEM==\"scsi_device\", SYSFS{type}==\"[45]\", RUN+=\"/sbin/modprobe sr_mod\"" >> /etc/udev/rules.d/10-modprobe.rules
152 echo "SUBSYSTEM==\"mmc\", RUN+=\"/sbin/modprobe mmc_block\"" >> /etc/udev/rules.d/10-modprobe.rules
154 # FIXME: hack since sr_mod seems to fail to get loaded sometimes (#239657)
157 modprobe loop max_loop=16
159 vecho "Starting udevd..."
162 vecho "Creating devices..."
165 if [ -n "$net" ]; then
166 # Wait 30 seconds for network to appear.
168 while [ "x$COUNTDOWN" != "x0" ] ; do
169 is_verbose && echo -n "."
171 if ip link show $netdevice &>/dev/null; then
176 COUNTDOWN=$(($COUNTDOWN - 1))
181 ip link set $netdevice up
182 if [ "$net" = "dhcp" ]; then
183 vecho "Getting an IP address by DHCP..."
184 dhclient $(get_verbose) $netdevice
186 vecho "Setting IP address $net..."
187 ip addr add $net dev $netdevice
189 if [ -n "$gateway" ]; then
190 vecho "Setting default gateway $gateway..."
191 ip route add default via $gateway
193 if ! ping -c3 -w10 ping.ipfire.org 2>/dev/null; then
194 echo "This box does not seem to have an internet connection."
195 echo "You may fix this now and continue then:"
200 if [ "$SHELL" == "1" ] ; then
201 echo "Shell requested on kernel commandline. Exit to continue booting."
206 # Wait 90 seconds for $thingtomount to appear.
208 while [ "x$COUNTDOWN" != "x0" ] ; do
209 is_verbose && echo -n "."
211 if [ -e $thingtomount ]; then
215 # this is kind of lame, but we could have had a situation
216 # where we were unable to read the volume id. so trigger
217 # another run through the block devs
218 if [ "x$COUNTDOWN" = "x30" ]; then
219 udevadm trigger --subsystem-match=block
222 COUNTDOWN=$(($COUNTDOWN - 1))
227 if [ ! -e $thingtomount ] ; then
229 echo "--------------------------------------"
230 echo "WARNING: Cannot find root file system!"
231 echo "--------------------------------------"
233 echo "Create symlink $thingtomount and then exit this shell to continue"
234 echo "the boot sequence."
240 vecho "Mounting $thingtomount..."
244 if [ "x$READONLY" == "x1" ] ; then
245 rootflags="$rootflags,ro"
247 rootflags="$rootflags,rw"
250 if [ -n $rootflags ]; then
251 mountoptions=" -o$rootflags"
254 mount -n -t $rootfstype $mountoptions $thingtomount /mnt/source
257 if [ "$RES" != "0" ]; then
258 echo "---------------------------------"
259 echo "WARNING: Cannot mount rootfs!"
260 echo "---------------------------------"
262 echo "Dropping to a shell. "
263 echo "Mount /mnt/source and exit shell to continue. Good luck!"
268 mount -n -t tmpfs none /mnt/tmpfs
269 aufsmountoptions="br:/mnt/tmpfs=rw"
271 OVERLAY=$(ls /mnt/source/*.overlay 2>/dev/null || true)
272 if [ -n "$OVERLAY" ]; then
273 vecho "Setting up overlay for squashfs..."
274 mount -t squashfs -o loop,ro $OVERLAY /mnt/overlayfs
275 aufsmountoptions="$aufsmountoptions:/mnt/overlayfs=rr"
278 SQUASHED=$(ls /mnt/source/*.img 2>/dev/null || true)
279 if [ -n "$SQUASHED" ] ; then
280 vecho "Setting up embedded squashfs..."
281 mount -t squashfs -o loop,ro $SQUASHED /mnt/squashfs
282 aufsmountoptions="$aufsmountoptions:/mnt/squashfs=rr"
285 mount -t aufs -o $aufsmountoptions none /sysroot
287 for i in $(cd /mnt; ls); do
288 mountpoint /mnt/$i >/dev/null || continue
289 mkdir -p /sysroot/mnt/$i || :
290 mount --move /mnt/$i /sysroot/mnt/$i
293 if [ "$ESHELL" == "1" ]; then
294 echo "Shell requested on kernel commandline."
295 echo "Rootfs is mounted ro on /sysroot. Exit to continue booting."
300 if [ -x /sysroot$init ]; then
301 # Leave initramfs and transition to rootfs
303 vecho "Transfering control to $init"
305 exec switchroot $(get_verbose) /sysroot
306 echo "---------------------------------"
307 echo "WARNING: Error switching to real rootfs!"
308 echo "---------------------------------"
310 echo "Dropping to a shell. Good luck!"
314 echo "---------------------------------------------------------"
315 echo "WARNING: Requested $init binary does not exist on rootfs."
316 echo "---------------------------------------------------------"
318 echo "Dropping to a shell. Good luck!"