slitaz-modular view initramfs/linuxrc @ rev 0

first release
author Christopher Rogers <slaxemulator@gmail.com>
date Thu Nov 25 03:30:16 2010 +0000 (2010-11-25)
parents
children d52e861c9970
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 # If toram or copy2ram boot parameter is present, copy all fs modules to RAM.
182 # (skip modules from /optional/ which are not listed in load= boot option)
183 # Finaly modify DATA variable so it will point to correct directory
184 if [ "$(cmdline_parameter toram)" != "" -o "$(cmdline_parameter copy2ram)" != "" ]; then
185 echolog "copying $LIVECDNAME data to RAM, this may take some time..."
186 mkdir -p $COPY2RAM
188 # make sure it's in RAM even with changes= parameter
189 if [ "$CHANGESMNT" ]; then mount -t tmpfs -o "size=$RAMSIZE" tmpfs $COPY2RAM; fi
190 copy_to_ram $DATA $COPY2RAM
192 cd_autoeject 1
193 fumount $DATA
194 fumount $MOUNTDIR/*
195 rmdir $MOUNTDIR/* 2>/dev/null # mounted device names are empty, remove them
196 DATA=$COPY2RAM
197 cd_autoeject 0
198 fi
200 debug_shell
202 MODPATH=$(cmdline_value modpath)
203 if [ "$MODPATH" != "" ]; then
204 MODPATH=$(find_in_computer $MODPATH)
205 if [ $FINDISOVAR ]; then
206 mount_device $MODPATH $FINDISOMNT/$LIVECDNAME/tmp rbind
207 else
208 mount_device $MODPATH $DATA/tmp rbind
209 fi
210 fi
212 debug_shell
214 #if [ $(cmdline_parameter copy) ]; then
215 # echolog "Copying initramfs to union. Please wait..."
216 # # copy busybox to union
217 # for i in $(ls -a /); do
218 # case "$i" in
219 # .|..) ;;
220 # memory|union) ;;
221 # mnt) mkdir -p $UNION/mnt ;;
222 # sys) mkdir -p $UNION/sys ;;
223 # proc) mkdir -p $UNION/proc ;;
224 # dev) ;;
225 # *) if ! cp -af /$i $UNION 2> /dev/null; then
226 # debug_shell
227 # fi ;;
228 # esac
229 # done
230 #fi
232 #debug_shell
234 # DATA contains path to the base directory of all fs modules which need
235 # to be mounted and inserted into live filesystem. Do it now.
236 echolog "Loading modules:"
237 union_insert_modules $UNION $DATA $IMAGES
239 # the $MEMORY directory can contain $MEMORY/modules too
240 # in the case if changes= boot argument is used. If not, it doesn't hurt
241 union_insert_modules $UNION $MEMORY $IMAGES
243 debug_shell
245 ROOTCOPY=$(cmdline_value rootcopy)
246 if [ $(cmdline_parameter norootcopy) ]; then
247 echolog "Not using rootcopy"
248 elif [ "$ROOTCOPY" ]; then
249 echolog "Mounting content of rootcopy directory"
250 ROOTCOPY=$(find_in_computer $ROOTCOPY)
251 mount_device $ROOTCOPY $DATA/rootcopy rbind
252 echolog "Copying content of rootcopy directory"
253 cp -af $DATA/rootcopy/* $UNION 2>/dev/null # may be empty
254 else
255 echolog "Copying content of rootcopy directory"
256 cp -af $DATA/rootcopy/* $UNION 2>/dev/null # may be empty
257 fi
259 # TODO: if copy2ram is used, there is no need to preserve the original in memory anymore
260 #if [ "$DATA" = "$COPY2RAM" ]; then
261 # rm from memory once ??
262 #fi
264 echolog "Copying liblinuxlive library to union"
265 cp -af /liblinuxlive $UNION/usr/lib/
267 debug_shell
269 echolog "Recreating /etc/fstab and /mnt directories"
270 touch $UNION/etc/fstab
271 rmdir $UNION/mnt/* 2>/dev/null
272 fstab_update $UNION
273 #fstab_clean $UNION
275 # everything is ready now, so we may unload unused kernel modules
276 # and do some cleanup, unmount few things which are no longer needed.
277 #rmmod_unused_modules
278 #fumount /usr
279 #fumount /sys
281 # More likely these directories aren't there.
282 # Even if they are, this won't hurt.
283 mkdir -p $UNION/boot
284 mkdir -p $UNION/proc
285 mkdir -p $UNION/sys
286 mkdir -p $UNION/dev
287 mkdir -p $UNION/tmp
288 chmod 1777 $UNION/tmp
290 # Boot will contain whatever was in ./boot directory in the bootable media
291 # Error output goes to null, as nothing is mounted with copy2ram
292 #mount -n -o rbind $(dirname $DATA)/boot $UNION/boot 2>/dev/null
294 # Union contains all the files and directories unioned from all modules.
295 # Change root directory to it, and move initrd's root to /mnt/live/initramdisk
296 # Finaly execute /sbin/init to start the distribution.
297 echolog "changing root directory..."
299 cd $UNION
300 mkdir -p $INITRAMDISK
302 # Copy all dev files (found by mdev) to unioned dev directory
303 # so at least disk devices exist (your Linux may need them).
304 # Two exceptions, do not copy pty* and tty* devs.
305 if [ "$(cmdline_parameter nohotplug)" ]; then
306 cp -fdR /dev . 2>/dev/null
307 #else
308 #mount -n -o move /dev dev
309 fi
311 #mount -n -o move /proc proc
312 #mount -n -o move /sys sys
313 fumount /sys
315 if [ ! -e /dev/console ]; then mknod /dev/console c 5 1; fi
317 # find chroot and init
318 if [ -x bin/chroot ]; then CHROOT=bin/chroot; fi
319 if [ -x sbin/chroot ]; then CHROOT=sbin/chroot; fi
320 if [ -x usr/bin/chroot ]; then CHROOT=usr/bin/chroot; fi
321 if [ -x usr/sbin/chroot ]; then CHROOT=usr/sbin/chroot; fi
322 if [ "$CHROOT" = "" ]; then fatal "Can't find executable chroot command"; fi
324 if [ -x bin/init ]; then INIT=bin/init; fi
325 if [ -x sbin/init ]; then INIT=sbin/init; fi
326 if [ "$INIT" = "" ]; then fatal "Can't find executable init command"; fi
328 # time to end Linux Live scripts and start the distribution itself,
329 # using /sbin/init or whatever was found.
331 debug_shell
333 fumount /proc
335 mount -n -o remount,ro aufs .
337 # We will copy init from the distro to initrd (there should be 2MB free)
338 # This allows us to use the cleanup script during reboot, as init will be
339 # started from memory and not from the union and /union will not be busy.
341 cp -af $INIT /bin >/dev/null 2>&1
342 if [ $? -eq 0 ]; then
343 pivot_root . $INITRAMDISK
344 exec $CHROOT . $INITRAMDISK/bin/init <dev/console >dev/console 2>&1
345 else # If copying fails, start init directly.
346 pivot_root . $INITRAMDISK
347 exec $CHROOT . $INIT <dev/console >dev/console 2>&1
348 fi
350 header "!!ERROR!!"
351 fatal "You are not supposed to be here, something went wrong!"