]> git.ipfire.org Git - ipfire-3.x.git/blob - src/mkinitramfs/mkliveramfs
Added some nice things on mkinitramfs.
[ipfire-3.x.git] / src / mkinitramfs / mkliveramfs
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2008, 2009 Michael Tremer & Christian Schmidt #
6 # #
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. #
11 # #
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. #
16 # #
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/>. #
19 # #
20 ###############################################################################
21
22 . /usr/lib/mkinitramfs/functions
23
24 # Modules needed by the live system
25 MODULES="$MODULES aufs squashfs loop vfat ehci-hcd ohci-hcd uhci-hcd usb-storage"
26
27 # NFS
28 MODULES="$MODULES nfs"
29 for module in /lib/modules/$KERNEL/kernel/drivers/net/{,*/}*; do
30 MODULES="$MODULES $(basename ${module/.ko})"
31 done
32
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})"
36 done
37
38 install arping dhclient dhclient-script ping
39
40 # Creating folders
41 mkdir -p mnt/{source,tmpfs,overlayfs,squashfs}
42
43 cat >> sbin/real-init <<'EOF'
44
45 netdevice=eth0
46
47 # Users can override rootfs target on the kernel commandline
48 for o in $(cat /proc/cmdline); do
49 case $o in
50 root=*)
51 root=${o#root=}
52 ;;
53 rootflags=*)
54 rootflags=${o#rootflags=}
55 ;;
56 rootfstype=*)
57 rootfstype=${o#rootfstype=}
58 ;;
59 net=*)
60 net=${o#net=}
61 ;;
62 netdevice=*)
63 netdevice=${o#netdevice=}
64 ;;
65 gateway=*)
66 gateway=${o#gateway=}
67 ;;
68 dns=*)
69 dns="$dns ${o#dns=}"
70 ;;
71 esac
72 done
73
74 # generate udev rules to generate /dev/root symlink
75 if [ -z $root ] ; then
76 root=/dev/something
77 else
78 case $root in
79 /dev/disk/by-label/*)
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
82 if is_verbose; then
83 echo "Added udev rule 00-label.rules:"
84 cat /etc/udev/rules.d/00-label.rules
85 fi
86 thingtomount=/dev/root
87 ;;
88 CDLABEL=*)
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
94 if is_verbose; then
95 echo "Added udev rule 00-cdlabel.rules:"
96 cat /etc/udev/rules.d/00-cdlabel.rules
97 fi
98 thingtomount=/dev/root
99 ;;
100 LABEL=*)
101 LABEL=${root#LABEL=}
102 echo "SUBSYSTEM==\"block\", PROGRAM=\"/lib/udev/vol_id -l %N\", RESULT==\"$LABEL\", SYMLINK+=\"root\"" > /etc/udev/rules.d/00-label.rules
103 if is_verbose; then
104 echo "Added udev rule 00-label.rules:"
105 cat /etc/udev/rules.d/00-label.rules
106 fi
107 thingtomount=/dev/root
108 ;;
109 /dev/disk/by-id/*)
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
112 if is_verbose; then
113 echo "Added udev rule 01-uuid.rules:"
114 cat /etc/udev/rules.d/01-uuid.rules
115 fi
116 thingtomount=/dev/root
117 ;;
118 UUID=*)
119 UUID=${root#UUID=}
120 echo "SUBSYSTEM==\"block\", PROGRAM=\"/lib/udev/vol_id -u %N\", RESULT==\"$UUID\", SYMLINK+=\"root\"" > /etc/udev/rules.d/01-uuid.rules
121 if is_verbose; then
122 echo "Added udev rule 01-uuid.rules:"
123 cat /etc/udev/rules.d/01-uuid.rules
124 fi
125 thingtomount=/dev/root
126 ;;
127 NFS=*)
128 NFS=${root#NFS=}
129 vecho "Going on to mount $NFS"
130 #mount -t nfs $NFS /mnt/source
131 #rootfstype=loop
132 #thingtomount=$(ls -1 /mnt/source/*.iso | head -n 1)
133 ;;
134 /dev/*)
135 ln -s $root /dev/root
136 thingtomount=$root
137 ;;
138 *)
139 thingtomount=$root
140 ;;
141 esac
142 fi
143
144 echo "udev_log=\"error\"" >> /etc/udev/udev.conf
145
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
153
154 # FIXME: hack since sr_mod seems to fail to get loaded sometimes (#239657)
155 modprobe sr_mod
156
157 modprobe loop max_loop=16
158
159 vecho "Starting udevd..."
160 udevd --daemon
161
162 vecho "Creating devices..."
163 udevadm trigger
164
165 if [ -n "$net" ]; then
166 # Wait 30 seconds for network to appear.
167 COUNTDOWN=30
168 while [ "x$COUNTDOWN" != "x0" ] ; do
169 is_verbose && echo -n "."
170
171 if ip link show $netdevice &>/dev/null; then
172 COUNTDOWN=0
173 continue
174 fi
175
176 COUNTDOWN=$(($COUNTDOWN - 1))
177 sleep 1
178 done
179 vecho # Blank line
180
181 ip link set $netdevice up
182 if [ "$net" = "dhcp" ]; then
183 vecho "Getting an IP address by DHCP..."
184 dhclient $(get_verbose) $netdevice
185 else
186 vecho "Setting IP address $net..."
187 ip addr add $net dev $netdevice
188 fi
189 if [ -n "$gateway" ]; then
190 vecho "Setting default gateway $gateway..."
191 ip route add default via $gateway
192 fi
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:"
196 bash
197 fi
198 fi
199
200 if [ "$SHELL" == "1" ] ; then
201 echo "Shell requested on kernel commandline. Exit to continue booting."
202 echo
203 bash
204 fi
205
206 # Wait 90 seconds for $thingtomount to appear.
207 COUNTDOWN=90
208 while [ "x$COUNTDOWN" != "x0" ] ; do
209 is_verbose && echo -n "."
210
211 if [ -e $thingtomount ]; then
212 COUNTDOWN=0
213 continue
214 fi
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
220 fi
221
222 COUNTDOWN=$(($COUNTDOWN - 1))
223 sleep 1
224 done
225 vecho # Blank line
226
227 if [ ! -e $thingtomount ] ; then
228 echo
229 echo "--------------------------------------"
230 echo "WARNING: Cannot find root file system!"
231 echo "--------------------------------------"
232 echo
233 echo "Create symlink $thingtomount and then exit this shell to continue"
234 echo "the boot sequence."
235 echo
236 bash
237 fi
238
239 if is_verbose; then
240 vecho "Mounting $thingtomount..."
241 ls -l $thingtomount
242 fi
243
244 if [ "x$READONLY" == "x1" ] ; then
245 rootflags="$rootflags,ro"
246 else
247 rootflags="$rootflags,rw"
248 fi
249
250 if [ -n $rootflags ]; then
251 mountoptions=" -o$rootflags"
252 fi
253
254 mount -n -t $rootfstype $mountoptions $thingtomount /mnt/source
255 RES=$?
256
257 if [ "$RES" != "0" ]; then
258 echo "---------------------------------"
259 echo "WARNING: Cannot mount rootfs!"
260 echo "---------------------------------"
261 echo
262 echo "Dropping to a shell. "
263 echo "Mount /mnt/source and exit shell to continue. Good luck!"
264 echo
265 bash
266 fi
267
268 mount -n -t tmpfs none /mnt/tmpfs
269 aufsmountoptions="br:/mnt/tmpfs=rw"
270
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"
276 fi
277
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"
283 fi
284
285 mount -t aufs -o $aufsmountoptions none /sysroot
286
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
291 done
292
293 if [ "$ESHELL" == "1" ]; then
294 echo "Shell requested on kernel commandline."
295 echo "Rootfs is mounted ro on /sysroot. Exit to continue booting."
296 echo
297 bash
298 fi
299
300 if [ -x /sysroot$init ]; then
301 # Leave initramfs and transition to rootfs
302 kill $(pidof udevd)
303 vecho "Transfering control to $init"
304
305 exec switchroot $(get_verbose) /sysroot
306 echo "---------------------------------"
307 echo "WARNING: Error switching to real rootfs!"
308 echo "---------------------------------"
309 echo
310 echo "Dropping to a shell. Good luck!"
311 echo
312 bash
313 else
314 echo "---------------------------------------------------------"
315 echo "WARNING: Requested $init binary does not exist on rootfs."
316 echo "---------------------------------------------------------"
317 echo
318 echo "Dropping to a shell. Good luck!"
319 echo
320 bash
321 fi
322
323 EOF
324
325 finalize