]> git.ipfire.org Git - ipfire-3.x.git/blame - src/mkinitramfs/mkliveramfs
Added some nice things on mkinitramfs.
[ipfire-3.x.git] / src / mkinitramfs / mkliveramfs
CommitLineData
17e7e3d5
MT
1#!/bin/bash
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
fa424239 5# Copyright (C) 2008, 2009 Michael Tremer & Christian Schmidt #
17e7e3d5
MT
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
9bf81f83 22. /usr/lib/mkinitramfs/functions
17e7e3d5 23
fa424239 24# Modules needed by the live system
dbadb38c 25MODULES="$MODULES aufs squashfs loop vfat ehci-hcd ohci-hcd uhci-hcd usb-storage"
17e7e3d5 26
d2d97950
MT
27# NFS
28MODULES="$MODULES nfs"
29for module in /lib/modules/$KERNEL/kernel/drivers/net/{,*/}*; do
30 MODULES="$MODULES $(basename ${module/.ko})"
31done
32
fa424239
MT
33# Add all storage modules
34for module in /lib/modules/$KERNEL/kernel/drivers/{ata,message/fusion,pcmcia,scsi{,/*}}/*; do
17e7e3d5
MT
35 MODULES="$MODULES $(basename ${module/.ko})"
36done
37
16d10a88 38install arping dhclient dhclient-script ping
d2d97950 39
fa424239
MT
40# Creating folders
41mkdir -p mnt/{source,tmpfs,overlayfs,squashfs}
17e7e3d5 42
fa424239 43cat >> sbin/real-init <<'EOF'
16d10a88
MT
44
45netdevice=eth0
46
17e7e3d5 47# Users can override rootfs target on the kernel commandline
fa424239
MT
48for 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 ;;
16d10a88
MT
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 ;;
fa424239 71 esac
17e7e3d5
MT
72done
73
17e7e3d5
MT
74# generate udev rules to generate /dev/root symlink
75if [ -z $root ] ; then
fa424239 76 root=/dev/something
17e7e3d5 77else
fa424239
MT
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 ;;
16d10a88
MT
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 ;;
fa424239
MT
134 /dev/*)
135 ln -s $root /dev/root
136 thingtomount=$root
137 ;;
138 *)
139 thingtomount=$root
140 ;;
141 esac
17e7e3d5
MT
142fi
143
144echo "udev_log=\"error\"" >> /etc/udev/udev.conf
145
146# rules for loading modules
17e7e3d5
MT
147echo -n "ACTION==\"add\", SUBSYSTEM==\"?*\", ENV{MODALIAS}==\"?*\", RUN+=\"/sbin/modprobe $" >> /etc/udev/rules.d/10-modprobe.rules
148echo "env{MODALIAS}\"" >> /etc/udev/rules.d/10-modprobe.rules
149echo "ACTION==\"add\", SUBSYSTEM==\"scsi_device\" RUN+=\"/sbin/modprobe sg\"" >> /etc/udev/rules.d/10-modprobe.rules
150echo "ACTION==\"add\", SUBSYSTEM==\"scsi_device\", SYSFS{type}==\"0|7|14\", RUN+=\"/sbin/modprobe sd_mod\"" >> /etc/udev/rules.d/10-modprobe.rules
151echo "ACTION==\"add\", SUBSYSTEM==\"scsi_device\", SYSFS{type}==\"[45]\", RUN+=\"/sbin/modprobe sr_mod\"" >> /etc/udev/rules.d/10-modprobe.rules
152echo "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)
fa424239 155modprobe sr_mod
17e7e3d5 156
fa424239 157modprobe loop max_loop=16
17e7e3d5 158
fa424239
MT
159vecho "Starting udevd..."
160udevd --daemon
17e7e3d5 161
fa424239
MT
162vecho "Creating devices..."
163udevadm trigger
17e7e3d5 164
16d10a88
MT
165if [ -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
198fi
199
fa424239
MT
200if [ "$SHELL" == "1" ] ; then
201 echo "Shell requested on kernel commandline. Exit to continue booting."
202 echo
203 bash
17e7e3d5
MT
204fi
205
16d10a88 206# Wait 90 seconds for $thingtomount to appear.
d2d97950 207COUNTDOWN=90
fa424239
MT
208while [ "x$COUNTDOWN" != "x0" ] ; do
209 is_verbose && echo -n "."
210
16d10a88 211 if [ -e $thingtomount ]; then
fa424239
MT
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
224done
225vecho # Blank line
226
16d10a88 227if [ ! -e $thingtomount ] ; then
fa424239
MT
228 echo
229 echo "--------------------------------------"
230 echo "WARNING: Cannot find root file system!"
231 echo "--------------------------------------"
232 echo
16d10a88 233 echo "Create symlink $thingtomount and then exit this shell to continue"
fa424239
MT
234 echo "the boot sequence."
235 echo
236 bash
17e7e3d5
MT
237fi
238
fa424239 239if is_verbose; then
16d10a88
MT
240 vecho "Mounting $thingtomount..."
241 ls -l $thingtomount
17e7e3d5
MT
242fi
243
fa424239
MT
244if [ "x$READONLY" == "x1" ] ; then
245 rootflags="$rootflags,ro"
246else
247 rootflags="$rootflags,rw"
17e7e3d5
MT
248fi
249
fa424239
MT
250if [ -n $rootflags ]; then
251 mountoptions=" -o$rootflags"
17e7e3d5
MT
252fi
253
e8916989 254mount -n -t $rootfstype $mountoptions $thingtomount /mnt/source
17e7e3d5
MT
255RES=$?
256
fa424239
MT
257if [ "$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
17e7e3d5
MT
266fi
267
e8916989
MT
268mount -n -t tmpfs none /mnt/tmpfs
269aufsmountoptions="br:/mnt/tmpfs=rw"
17e7e3d5 270
e8916989 271OVERLAY=$(ls /mnt/source/*.overlay 2>/dev/null || true)
17e7e3d5 272if [ -n "$OVERLAY" ]; then
fa424239
MT
273 vecho "Setting up overlay for squashfs..."
274 mount -t squashfs -o loop,ro $OVERLAY /mnt/overlayfs
dbf243d2 275 aufsmountoptions="$aufsmountoptions:/mnt/overlayfs=rr"
17e7e3d5
MT
276fi
277
e8916989 278SQUASHED=$(ls /mnt/source/*.img 2>/dev/null || true)
17e7e3d5 279if [ -n "$SQUASHED" ] ; then
fa424239
MT
280 vecho "Setting up embedded squashfs..."
281 mount -t squashfs -o loop,ro $SQUASHED /mnt/squashfs
dbf243d2 282 aufsmountoptions="$aufsmountoptions:/mnt/squashfs=rr"
17e7e3d5
MT
283fi
284
285mount -t aufs -o $aufsmountoptions none /sysroot
286
3ee4d887
MT
287for i in $(cd /mnt; ls); do
288 mountpoint /mnt/$i >/dev/null || continue
289 mkdir -p /sysroot/mnt/$i || :
e8916989
MT
290 mount --move /mnt/$i /sysroot/mnt/$i
291done
292
fa424239
MT
293if [ "$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
17e7e3d5
MT
298fi
299
fa424239
MT
300if [ -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
17e7e3d5 313else
fa424239
MT
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
17e7e3d5
MT
321fi
322
323EOF
324
fa424239 325finalize