]> git.ipfire.org Git - ipfire-3.x.git/blob - src/mkinitramfs/mkliveramfs
6bdc82d4bff388c38b2f15327524221fd89104b9
[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"
26
27 # Add all storage modules
28 for module in /lib/modules/$KERNEL/kernel/drivers/{ata,message/fusion,pcmcia,scsi{,/*}}/*; do
29 MODULES="$MODULES $(basename ${module/.ko})"
30 done
31 ## XXX need to add support for usb cdroms
32
33 # Creating folders
34 mkdir -p mnt/{source,tmpfs,overlayfs,squashfs}
35
36 cat >> sbin/real-init <<'EOF'
37 # Users can override rootfs target on the kernel commandline
38 for o in $(cat /proc/cmdline); do
39 case $o in
40 root=*)
41 root=${o#root=}
42 ;;
43 rootflags=*)
44 rootflags=${o#rootflags=}
45 ;;
46 rootfstype=*)
47 rootfstype=${o#rootfstype=}
48 ;;
49 esac
50 done
51
52 # generate udev rules to generate /dev/root symlink
53 if [ -z $root ] ; then
54 root=/dev/something
55 else
56 case $root in
57 /dev/disk/by-label/*)
58 LABEL=${root#/dev/disk/by-label/}
59 echo "SUBSYSTEM==\"block\", PROGRAM=\"/lib/udev/vol_id -l %N\", RESULT==\"$LABEL\", SYMLINK+=\"root\"" > /etc/udev/rules.d/00-label.rules
60 if is_verbose; then
61 echo "Added udev rule 00-label.rules:"
62 cat /etc/udev/rules.d/00-label.rules
63 fi
64 thingtomount=/dev/root
65 ;;
66 CDLABEL=*)
67 CDLABEL=${root#CDLABEL=}
68 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
69 echo "KERNEL==\"sr[0-9]\", PROGRAM=\"/lib/udev/vol_id -l %N\", RESULT==\"$CDLABEL\", SYMLINK+=\"root\"" >> /etc/udev/rules.d/00-cdlabel.rules
70 echo "KERNEL==\"scd[0-9]\", PROGRAM=\"/lib/udev/vol_id -l %N\", RESULT==\"$CDLABEL\", SYMLINK+=\"root\"" >> /etc/udev/rules.d/00-cdlabel.rules
71 echo "KERNEL==\"pcd[0-9]\", PROGRAM=\"/lib/udev/vol_id -l %N\", RESULT==\"$CDLABEL\", SYMLINK+=\"root\"" >> /etc/udev/rules.d/00-cdlabel.rules
72 if is_verbose; then
73 echo "Added udev rule 00-cdlabel.rules:"
74 cat /etc/udev/rules.d/00-cdlabel.rules
75 fi
76 thingtomount=/dev/root
77 ;;
78 LABEL=*)
79 LABEL=${root#LABEL=}
80 echo "SUBSYSTEM==\"block\", PROGRAM=\"/lib/udev/vol_id -l %N\", RESULT==\"$LABEL\", SYMLINK+=\"root\"" > /etc/udev/rules.d/00-label.rules
81 if is_verbose; then
82 echo "Added udev rule 00-label.rules:"
83 cat /etc/udev/rules.d/00-label.rules
84 fi
85 thingtomount=/dev/root
86 ;;
87 /dev/disk/by-id/*)
88 UUID=${root#/dev/disk/by-id/}
89 echo "SUBSYSTEM==\"block\", PROGRAM=\"/lib/udev/vol_id -u %N\", RESULT==\"$UUID\", SYMLINK+=\"root\"" > /etc/udev/rules.d/01-uuid.rules
90 if is_verbose; then
91 echo "Added udev rule 01-uuid.rules:"
92 cat /etc/udev/rules.d/01-uuid.rules
93 fi
94 thingtomount=/dev/root
95 ;;
96 UUID=*)
97 UUID=${root#UUID=}
98 echo "SUBSYSTEM==\"block\", PROGRAM=\"/lib/udev/vol_id -u %N\", RESULT==\"$UUID\", SYMLINK+=\"root\"" > /etc/udev/rules.d/01-uuid.rules
99 if is_verbose; then
100 echo "Added udev rule 01-uuid.rules:"
101 cat /etc/udev/rules.d/01-uuid.rules
102 fi
103 thingtomount=/dev/root
104 ;;
105 /dev/*)
106 ln -s $root /dev/root
107 thingtomount=$root
108 ;;
109 *)
110 thingtomount=$root
111 ;;
112 esac
113 fi
114
115 echo "udev_log=\"error\"" >> /etc/udev/udev.conf
116
117 # rules for loading modules
118 echo -n "ACTION==\"add\", SUBSYSTEM==\"?*\", ENV{MODALIAS}==\"?*\", RUN+=\"/sbin/modprobe $" >> /etc/udev/rules.d/10-modprobe.rules
119 echo "env{MODALIAS}\"" >> /etc/udev/rules.d/10-modprobe.rules
120 echo "ACTION==\"add\", SUBSYSTEM==\"scsi_device\" RUN+=\"/sbin/modprobe sg\"" >> /etc/udev/rules.d/10-modprobe.rules
121 echo "ACTION==\"add\", SUBSYSTEM==\"scsi_device\", SYSFS{type}==\"0|7|14\", RUN+=\"/sbin/modprobe sd_mod\"" >> /etc/udev/rules.d/10-modprobe.rules
122 echo "ACTION==\"add\", SUBSYSTEM==\"scsi_device\", SYSFS{type}==\"[45]\", RUN+=\"/sbin/modprobe sr_mod\"" >> /etc/udev/rules.d/10-modprobe.rules
123 echo "SUBSYSTEM==\"mmc\", RUN+=\"/sbin/modprobe mmc_block\"" >> /etc/udev/rules.d/10-modprobe.rules
124
125 # FIXME: hack since sr_mod seems to fail to get loaded sometimes (#239657)
126 modprobe sr_mod
127
128 modprobe loop max_loop=16
129
130 vecho "Starting udevd..."
131 udevd --daemon
132
133 vecho "Creating devices..."
134 udevadm trigger
135
136 vecho "Waiting for system to settle..."
137 udevadm settle --timeout=30 || :
138
139 if [ "$SHELL" == "1" ] ; then
140 echo "Shell requested on kernel commandline. Exit to continue booting."
141 echo
142 bash
143 fi
144
145 # Wait 60 seconds for /dev/root to appear.
146 COUNTDOWN=60
147 while [ "x$COUNTDOWN" != "x0" ] ; do
148 is_verbose && echo -n "."
149
150 if [ -e /dev/root ]; then
151 COUNTDOWN=0
152 continue
153 fi
154 # this is kind of lame, but we could have had a situation
155 # where we were unable to read the volume id. so trigger
156 # another run through the block devs
157 if [ "x$COUNTDOWN" = "x30" ]; then
158 udevadm trigger --subsystem-match=block
159 fi
160
161 COUNTDOWN=$(($COUNTDOWN - 1))
162 sleep 1
163 done
164 vecho # Blank line
165
166 if [ ! -b /dev/root ] ; then
167 echo
168 echo "--------------------------------------"
169 echo "WARNING: Cannot find root file system!"
170 echo "--------------------------------------"
171 echo
172 echo "Create symlink /dev/root and then exit this shell to continue"
173 echo "the boot sequence."
174 echo
175 bash
176 fi
177
178 if is_verbose; then
179 vecho "Mounting /dev/root..."
180 ls -l /dev/root
181 fi
182
183 if [ "x$READONLY" == "x1" ] ; then
184 rootflags="$rootflags,ro"
185 else
186 rootflags="$rootflags,rw"
187 fi
188
189 if [ -n $rootflags ]; then
190 mountoptions=" -o$rootflags"
191 fi
192
193 mount -n -t $rootfstype $mountoptions $thingtomount /mnt/source
194 RES=$?
195
196 if [ "$RES" != "0" ]; then
197 echo "---------------------------------"
198 echo "WARNING: Cannot mount rootfs!"
199 echo "---------------------------------"
200 echo
201 echo "Dropping to a shell. "
202 echo "Mount /mnt/source and exit shell to continue. Good luck!"
203 echo
204 bash
205 fi
206
207 mount -n -t tmpfs none /mnt/tmpfs
208 aufsmountoptions="br:/mnt/tmpfs=rw"
209
210 OVERLAY=$(ls /mnt/source/*.overlay 2>/dev/null || true)
211 if [ -n "$OVERLAY" ]; then
212 vecho "Setting up overlay for squashfs..."
213 mount -t squashfs -o loop,ro $OVERLAY /mnt/overlayfs
214 aufsmountoptions="$aufsmountoptions:/mnt/overlayfs=rr"
215 fi
216
217 SQUASHED=$(ls /mnt/source/*.img 2>/dev/null || true)
218 if [ -n "$SQUASHED" ] ; then
219 vecho "Setting up embedded squashfs..."
220 mount -t squashfs -o loop,ro $SQUASHED /mnt/squashfs
221 aufsmountoptions="$aufsmountoptions:/mnt/squashfs=rr"
222 fi
223
224 mount -t aufs -o $aufsmountoptions none /sysroot
225
226 for i in $(cd /mnt; ls); do
227 mountpoint /mnt/$i >/dev/null || continue
228 mkdir -p /sysroot/mnt/$i || :
229 mount --move /mnt/$i /sysroot/mnt/$i
230 done
231
232 if [ "$ESHELL" == "1" ]; then
233 echo "Shell requested on kernel commandline."
234 echo "Rootfs is mounted ro on /sysroot. Exit to continue booting."
235 echo
236 bash
237 fi
238
239 if [ -x /sysroot$init ]; then
240 # Leave initramfs and transition to rootfs
241 kill $(pidof udevd)
242 vecho "Transfering control to $init"
243
244 exec switchroot $(get_verbose) /sysroot
245 echo "---------------------------------"
246 echo "WARNING: Error switching to real rootfs!"
247 echo "---------------------------------"
248 echo
249 echo "Dropping to a shell. Good luck!"
250 echo
251 bash
252 else
253 echo "---------------------------------------------------------"
254 echo "WARNING: Requested $init binary does not exist on rootfs."
255 echo "---------------------------------------------------------"
256 echo
257 echo "Dropping to a shell. Good luck!"
258 echo
259 bash
260 fi
261
262 EOF
263
264 finalize