slitaz-modular view initramfs/linuxrc @ rev 13

Add rootuuid support into init. Also started copying initramfs to /mnt/memory/initramfs to save space. Updated linuxrc to support /memory/initramfs in union.
author Christopher Rogers <slaxemulator@gmail.com>
date Thu Dec 16 08:53:50 2010 +0000 (2010-12-16)
parents 6d654d523ac8
children 5dd7947d1eeb
line source
1 #!/bin/ash
2 # Initial script for Linux Live operating system
3 # Author: Tomas M <http://www.linux-live.org/>
5 export PATH=.:/:/usr/sbin:/usr/bin:/sbin:/bin
7 mount -n -t proc proc /proc
8 mount -n -t sysfs sysfs /sys
9 #mount -n -o remount,rw / # for the case we forgot rw boot option
10 ln -sf /proc/mounts /etc/mtab # this allows us to use umount -a
12 . /liblinuxlive # it requires proc to be mounted
14 # Don't print kernel messages to konsole now.
15 # Syslog will reset printk settings, no need to remember it here anymore.
16 echo "0" >/proc/sys/kernel/printk
18 # Load essential drivers, like CDROM drivers, aufs/squashfs etc,
19 # use mdev to create /dev/ devices and setup it as a hotplug-handler
20 modprobe_essential_modules
22 # /usr and some drivers are compressed in initrd
23 # so it must be mounted from .lzm files
24 #mount_initrd_loops
26 # start hotplugging before hw drivers load
27 #mdev_start_hotplug
29 # Then load drivers for data storage and input devices
30 modprobe_usb_modules
31 modprobe_pcmcia_modules
33 debug_shell
35 # make sure ext3 partitions are not mounted using ext2 driver,
36 # and vfat partitions are not mounted using msdos driver
37 echo -e "ext4\next3\next2\nvfat\n*" >/etc/filesystems
39 mkdir -p $UNION
40 mkdir -p $MEMORY
42 debug_shell
44 # Find livecd data directory by searching for livecd.sgn file
45 SGN=$(cmdline_value sgnfile)
46 if [ "$SGN" = "" ]; then SGN=livecd.sgn; fi
47 echolog "looking for '$LIVECDNAME' data directory (searching for $SGN file)"
49 # First, try from= boot argument, if given
50 DATAFROM=$(cmdline_value from)
52 # Boot directly from iso
53 FINDISOVAR=$(cmdline_value findiso)
55 # If ip= parameter is present, assume we're booted over PXE
56 # In that case, we have to initialize network very soon (now)
57 # ip=<client-ip>:<boot-server-ip>:<gw-ip>:<netmask>
58 # Nevertheless, from= parameter won't be overwritten by this
59 IP=$(cmdline_value ip)
60 if [ "$IP" != "" -a "$DATAFROM" = "" ]; then
61 DATAFROM="http://"$(echo $IP | cut -d : -f 2)
62 fi
64 if [ "$FINDISOVAR" != "" -a "$DATAFROM" = "" ]; then
65 FINDISO=$(find_in_computer $FINDISOVAR)
66 DATAFROM="$FINDISO"
67 echolog "$FINDISO found."
68 fi
70 if [ "$DATAFROM" ]; then
71 if [ "$(echo $DATAFROM | cut -b 1-7 | tr "[:upper:]" "[:lower:]")" = "http://" ]; then
72 init_dhcp $(modprobe_network_modules)
73 mount_httpfs $DATAFROM $MOUNTDIR/httpfs
74 # if mountdir/httpfs/livecdname is found, set DATA=, else umount httpfs
75 # - currently this part works even without the above mentioned, but livecd.sgn is required now untill it's uncommented
76 elif [ "$FINDISO" ]; then
77 mount_device $DATAFROM $FINDISOMNT
78 echolog "$FINDISO mounted."
79 else
80 DATAFROM=$(find_in_computer $DATAFROM)
81 if [ "$DATAFROM" ]; then
82 mount_device $DATAFROM $LOOPMOUNT # mount again, it may be loop device
83 if [ $? -eq 0 -a "$(find_modules $LOOPMOUNT/$LIVECDNAME)" != "" ]; then
84 echolog "found in $DATAFROM"
85 DATA=$LOOPMOUNT/$LIVECDNAME
86 else
87 fumount $LOOPMOUNT
88 fumount $MOUNTDIR/*
89 fi
90 fi
91 fi
92 fi
94 if [ "$DATA" = "" ]; then
95 # from= is not used or it didn't contain valid data
96 DATA=$(find_in_computer $LIVECDNAME/$SGN)
97 DATA=$(dirname $DATA 2>/dev/null)
98 fi
100 if [ "$DATA" = "" ]; then fatal \
101 "$LIVECDNAME data not found.
102 You are maybe using an unsupported boot device (eg. SCSI or old PCMCIA).
103 Workaround: Copy the directory $LIVECDNAME from your boot device to an IDE/SATA
104 disk, eg. to /mnt/hda1/$LIVECDNAME or C:\\$LIVECDNAME. Then try to boot again."
105 fi
107 echolog "Using $LIVECDNAME data from $DATA"
109 debug_shell
111 echolog "Setting up directory for changes ..."
112 CHANGESVAL=$(cmdline_value changes)
114 if [ "$CHANGESVAL" ]; then
115 CHANGESMNT=$(find_in_computer $CHANGESVAL)
116 echolog $CHANGESMNT
117 fi
119 debug_shell
121 mount_device "$CHANGESMNT" $MEMORY # removes $MEMORY if CHANGESMNT is wrong
123 # test if the filesystem is writable so changes can be stored to it
124 touch $MEMORY/empty 2>/dev/null && \
125 rm -f $MEMORY/empty 2>/dev/null
127 # if changes can't be mounted or the filesystem is not writable,
128 # fallback to the default: tmpfs
129 if [ $? -ne 0 ]; then
130 echolog "Changes not used or not writable, using memory only"
131 fumount $MEMORY
132 mkdir -p $MEMORY # mount_device might removed it
134 RAMSIZE=$(cmdline_value ramsize)
135 if [ "$RAMSIZE" = "" ]; then RAMSIZE="60%"; fi
136 mount -t tmpfs -o "size=$RAMSIZE" tmpfs $MEMORY
137 XINO=$MEMORY
138 else
139 # So it is writable, we will keep the filesystem mounted.
140 # Check if it supports links and chmod.
141 # If not, overmount CHANGES using posixovl
142 echolog "Testing the filesystem for posix compatibility ..."
143 touch $MEMORY/.empty1 && \
144 ln -sf $MEMORY/.empty1 $MEMORY/.empty2 2>/dev/null && \
145 chmod +x $MEMORY/.empty1 2>/dev/null && \
146 test -x $MEMORY/.empty1 && \
147 chmod -x $MEMORY/.empty1 2>/dev/null && \
148 test ! -x $MEMORY/.empty1 && \
149 rm $MEMORY/.empty1 $MEMORY/.empty2 2>/dev/null
151 if [ $? -ne 0 ]; then
152 echolog "Not compatible - starting posixovl"
153 rm $MEMORY/.empty1 $MEMORY/.empty2 2>/dev/null
154 mkdir -p $CHANGES
155 posixovl -F $CHANGES -- -o attr_timeout=300,entry_timeout=300,negative_timeout=300,kernel_cache,allow_other
156 find $CHANGES >/dev/null 2>&1 # cache everything now
157 fi
158 fi
160 # $UNION will be used as a root directory, livecd modules will be added soon
161 echolog "Setup union directory (using aufs)"
163 mkdir -p $CHANGES
164 mkdir -p $IMAGES
165 mount -n -t tmpfs tmpfs $IMAGES
167 debug_shell
169 # store the xino file in memory in all cases, it's faster and safer
170 if [ "$XINO" != "$MEMORY" ]; then
171 mkdir -p $XINO
172 mount -n -t tmpfs tmpfs $XINO
173 fi
175 # mount aufs using the writable branch as the first one (leftmost/topmost)
176 mount -t aufs -o nowarn_perm,xino=$XINO/.aufs.xino,br:$CHANGES=rw aufs $UNION
177 if [ $? -ne 0 ]; then dmesg | tail -n 1; fatal "can't setup union (aufs)"; fi
179 debug_shell
181 # setup initramfs if it exist
182 if [ -d /memory/initramfs ]; then
183 echolog "Setup initramfs for first module..."
184 mount -n -o remount,add:1:/memory/initramfs=ro aufs $UNION
185 fi
187 debug_shell
189 # If toram or copy2ram boot parameter is present, copy all fs modules to RAM.
190 # (skip modules from /optional/ which are not listed in load= boot option)
191 # Finaly modify DATA variable so it will point to correct directory
192 if [ "$(cmdline_parameter toram)" != "" -o "$(cmdline_parameter copy2ram)" != "" ]; then
193 echolog "copying $LIVECDNAME data to RAM, this may take some time..."
194 mkdir -p $COPY2RAM
196 # make sure it's in RAM even with changes= parameter
197 if [ "$CHANGESMNT" ]; then mount -t tmpfs -o "size=$RAMSIZE" tmpfs $COPY2RAM; fi
198 copy_to_ram $DATA $COPY2RAM
200 cd_autoeject 1
201 fumount $DATA
202 fumount $MOUNTDIR/*
203 rmdir $MOUNTDIR/* 2>/dev/null # mounted device names are empty, remove them
204 DATA=$COPY2RAM
205 cd_autoeject 0
206 fi
208 debug_shell
210 MODPATH=$(cmdline_value modpath)
211 if [ "$MODPATH" != "" ]; then
212 MODPATH=$(find_in_computer $MODPATH)
213 if [ $FINDISOVAR ]; then
214 mount_device $MODPATH $FINDISOMNT/$LIVECDNAME/tmp rbind
215 else
216 mount_device $MODPATH $DATA/tmp rbind
217 fi
218 fi
220 debug_shell
222 #if [ $(cmdline_parameter copy) ]; then
223 # echolog "Copying initramfs to union. Please wait..."
224 # # copy busybox to union
225 # for i in $(ls -a /); do
226 # case "$i" in
227 # .|..) ;;
228 # memory|union) ;;
229 # mnt) mkdir -p $UNION/mnt ;;
230 # sys) mkdir -p $UNION/sys ;;
231 # proc) mkdir -p $UNION/proc ;;
232 # dev) ;;
233 # *) if ! cp -af /$i $UNION 2> /dev/null; then
234 # debug_shell
235 # fi ;;
236 # esac
237 # done
238 #fi
240 #debug_shell
242 # DATA contains path to the base directory of all fs modules which need
243 # to be mounted and inserted into live filesystem. Do it now.
244 echolog "Loading modules:"
245 union_insert_modules $UNION $DATA $IMAGES
247 # the $MEMORY directory can contain $MEMORY/modules too
248 # in the case if changes= boot argument is used. If not, it doesn't hurt
249 union_insert_modules $UNION $MEMORY $IMAGES
251 debug_shell
253 ROOTCOPY=$(cmdline_value rootcopy)
254 if [ $(cmdline_parameter norootcopy) ]; then
255 echolog "Not using rootcopy"
256 elif [ "$ROOTCOPY" ]; then
257 echolog "Mounting content of rootcopy directory"
258 ROOTCOPY=$(find_in_computer $ROOTCOPY)
259 mount_device $ROOTCOPY $DATA/rootcopy rbind
260 echolog "Copying content of rootcopy directory"
261 cp -af $DATA/rootcopy/* $UNION 2>/dev/null # may be empty
262 else
263 echolog "Copying content of rootcopy directory"
264 cp -af $DATA/rootcopy/* $UNION 2>/dev/null # may be empty
265 fi
267 # TODO: if copy2ram is used, there is no need to preserve the original in memory anymore
268 #if [ "$DATA" = "$COPY2RAM" ]; then
269 # rm from memory once ??
270 #fi
272 echolog "Copying liblinuxlive library to union"
273 cp -af /liblinuxlive $UNION/usr/lib/
275 debug_shell
277 echolog "Recreating /etc/fstab and /mnt directories"
278 touch $UNION/etc/fstab
279 rmdir $UNION/mnt/* 2>/dev/null
280 fstab_update $UNION
281 #fstab_clean $UNION
283 # everything is ready now, so we may unload unused kernel modules
284 # and do some cleanup, unmount few things which are no longer needed.
285 #rmmod_unused_modules
286 #fumount /usr
287 #fumount /sys
289 # More likely these directories aren't there.
290 # Even if they are, this won't hurt.
291 mkdir -p $UNION/boot
292 mkdir -p $UNION/proc
293 mkdir -p $UNION/sys
294 mkdir -p $UNION/dev
295 mkdir -p $UNION/tmp
296 chmod 1777 $UNION/tmp
298 # Boot will contain whatever was in ./boot directory in the bootable media
299 # Error output goes to null, as nothing is mounted with copy2ram
300 #mount -n -o rbind $(dirname $DATA)/boot $UNION/boot 2>/dev/null
302 # Union contains all the files and directories unioned from all modules.
303 # Change root directory to it, and move initrd's root to /mnt/live/initramdisk
304 # Finaly execute /sbin/init to start the distribution.
305 echolog "changing root directory..."
307 cd $UNION
308 mkdir -p $INITRAMDISK
310 # Copy all dev files (found by mdev) to unioned dev directory
311 # so at least disk devices exist (your Linux may need them).
312 # Two exceptions, do not copy pty* and tty* devs.
313 if [ "$(cmdline_parameter nohotplug)" ]; then
314 cp -fdR /dev . 2>/dev/null
315 #else
316 #mount -n -o move /dev dev
317 fi
319 #mount -n -o move /proc proc
320 #mount -n -o move /sys sys
321 fumount /sys
323 if [ ! -e /dev/console ]; then mknod /dev/console c 5 1; fi
325 # find chroot and init
326 if [ -x bin/chroot ]; then CHROOT=bin/chroot; fi
327 if [ -x sbin/chroot ]; then CHROOT=sbin/chroot; fi
328 if [ -x usr/bin/chroot ]; then CHROOT=usr/bin/chroot; fi
329 if [ -x usr/sbin/chroot ]; then CHROOT=usr/sbin/chroot; fi
330 if [ "$CHROOT" = "" ]; then fatal "Can't find executable chroot command"; fi
332 if [ -x bin/init ]; then INIT=bin/init; fi
333 if [ -x sbin/init ]; then INIT=sbin/init; fi
334 if [ "$INIT" = "" ]; then fatal "Can't find executable init command"; fi
336 # time to end Linux Live scripts and start the distribution itself,
337 # using /sbin/init or whatever was found.
339 debug_shell
341 fumount /proc
343 mount -n -o remount,ro aufs .
345 # We will copy init from the distro to initrd (there should be 2MB free)
346 # This allows us to use the cleanup script during reboot, as init will be
347 # started from memory and not from the union and /union will not be busy.
349 cp -af $INIT /bin >/dev/null 2>&1
350 if [ $? -eq 0 ]; then
351 pivot_root . $INITRAMDISK
352 exec $CHROOT . $INITRAMDISK/bin/init <dev/console >dev/console 2>&1
353 else # If copying fails, start init directly.
354 pivot_root . $INITRAMDISK
355 exec $CHROOT . $INIT <dev/console >dev/console 2>&1
356 fi
358 header "!!ERROR!!"
359 fatal "You are not supposed to be here, something went wrong!"