slitaz-modular view initramfs/linuxrc @ rev 177

mkiso.sh: more filenames support
author Pascal Bellard <pascal.bellard@slitaz.org>
date Wed Mar 30 09:46:37 2016 +0200 (2016-03-30)
parents a260a9380e9d
children
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 -t proc proc /proc -o nosuid,noexec,nodev
8 mount -t sysfs sys /sys -o nosuid,noexec,nodev
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 depmod -a
16 # Don't print kernel messages to konsole now.
17 # Syslog will reset printk settings, no need to remember it here anymore.
18 echo "0" >/proc/sys/kernel/printk
20 # Load essential drivers, like CDROM drivers, aufs/squashfs etc,
21 # use mdev to create /dev/ devices and setup it as a hotplug-handler
22 modprobe_essential_modules
24 # /usr and some drivers are compressed in initrd
25 # so it must be mounted from .lzm files
26 #mount_initrd_loops
28 # start hotplugging before hw drivers load
29 #mdev_start_hotplug
31 # Then load drivers for data storage and input devices
32 modprobe_usb_modules
33 modprobe_pcmcia_modules
35 debug_shell
37 # make sure ext3 partitions are not mounted using ext2 driver,
38 # and vfat partitions are not mounted using msdos driver
39 echo -e "ext4\next3\next2\nvfat\n*" >/etc/filesystems
41 mkdir -p $UNION
42 mkdir -p $MEMORY
44 debug_shell
46 # Find livecd data directory by searching for livecd.sgn file
47 SGN=$(cmdline_value sgnfile)
48 if [ "$SGN" = "" ]; then SGN=livecd.sgn; fi
49 echolog "looking for '$LIVECDNAME' data directory (searching for $SGN file)"
51 # First, try from= boot argument, if given
52 DATAFROM=$(cmdline_value from)
54 # Boot directly from iso
55 FINDISOVAR=$(cmdline_value findiso)
57 # If ip= parameter is present, assume we're booted over PXE
58 # In that case, we have to initialize network very soon (now)
59 # ip=<client-ip>:<boot-server-ip>:<gw-ip>:<netmask>
60 # Nevertheless, from= parameter won't be overwritten by this
61 IP=$(cmdline_value ip)
62 if [ "$IP" != "" -a "$DATAFROM" = "" ]; then
63 DATAFROM="http://"$(echo $IP | cut -d : -f 2)
64 fi
66 if [ "$FINDISOVAR" != "" -a "$DATAFROM" = "" ]; then
67 FINDISO=$(find_in_computer $FINDISOVAR)
68 DATAFROM="$FINDISO"
69 echolog "$FINDISO found."
70 fi
72 if [ "$DATAFROM" ]; then
73 if [ "$(echo $DATAFROM | cut -b 1-7 | tr "[:upper:]" "[:lower:]")" = "http://" ]; then
74 init_dhcp $(modprobe_network_modules)
75 mount_httpfs $DATAFROM $MOUNTDIR/httpfs
76 # if mountdir/httpfs/livecdname is found, set DATA=, else umount httpfs
77 # - currently this part works even without the above mentioned, but livecd.sgn is required now untill it's uncommented
78 elif [ "$FINDISO" ]; then
79 mount_device $DATAFROM $FINDISOMNT
80 echolog "$FINDISO mounted."
81 else
82 DATAFROM=$(find_in_computer $DATAFROM)
83 if [ "$DATAFROM" ]; then
84 mount_device $DATAFROM $LOOPMOUNT # mount again, it may be loop device
85 if [ $? -eq 0 -a "$(find_modules $LOOPMOUNT/$LIVECDNAME)" != "" ]; then
86 echolog "found in $DATAFROM"
87 DATA=$LOOPMOUNT/$LIVECDNAME
88 else
89 fumount $LOOPMOUNT
90 fumount $MOUNTDIR/*
91 fi
92 fi
93 fi
94 fi
96 if [ "$DATA" = "" ]; then
97 # from= is not used or it didn't contain valid data
98 DATA=$(find_in_computer $LIVECDNAME/$SGN)
99 DATA=$(dirname $DATA 2>/dev/null)
100 fi
102 if [ "$DATA" = "" ]; then fatal \
103 "$LIVECDNAME data not found.
104 You are maybe using an unsupported boot device (eg. SCSI or old PCMCIA).
105 Workaround: Copy the directory $LIVECDNAME from your boot device to an IDE/SATA
106 disk, eg. to /mnt/hda1/$LIVECDNAME or C:\\$LIVECDNAME. Then try to boot again."
107 fi
109 echolog "Using $LIVECDNAME data from $DATA"
111 debug_shell
113 echolog "Setting up directory for changes ..."
114 CHANGESVAL=$(cmdline_value changes)
116 if [ "$CHANGESVAL" ]; then
117 CHANGESMNT=$(find_in_computer $CHANGESVAL)
118 echolog $CHANGESMNT
119 fi
121 debug_shell
123 mount_device "$CHANGESMNT" $MEMORY # removes $MEMORY if CHANGESMNT is wrong
125 # test if the filesystem is writable so changes can be stored to it
126 touch $MEMORY/empty 2>/dev/null && \
127 rm -f $MEMORY/empty 2>/dev/null
129 # if changes can't be mounted or the filesystem is not writable,
130 # fallback to the default: tmpfs
131 if [ $? -ne 0 ]; then
132 echolog "Changes not used or not writable, using memory only"
133 fumount $MEMORY
134 mkdir -p $MEMORY # mount_device might removed it
136 RAMSIZE=$(cmdline_value ramsize)
137 if [ "$RAMSIZE" = "" ]; then RAMSIZE="60%"; fi
138 mount -t tmpfs -o "size=$RAMSIZE" tmpfs $MEMORY
139 XINO=$MEMORY
140 else
141 # So it is writable, we will keep the filesystem mounted.
142 # Check if it supports links and chmod.
143 # If not, overmount CHANGES using posixovl
144 echolog "Testing the filesystem for posix compatibility ..."
145 touch $MEMORY/.empty1 && \
146 ln -sf $MEMORY/.empty1 $MEMORY/.empty2 2>/dev/null && \
147 chmod +x $MEMORY/.empty1 2>/dev/null && \
148 test -x $MEMORY/.empty1 && \
149 chmod -x $MEMORY/.empty1 2>/dev/null && \
150 test ! -x $MEMORY/.empty1 && \
151 rm $MEMORY/.empty1 $MEMORY/.empty2 2>/dev/null
153 if [ $? -ne 0 ]; then
154 echolog "Not compatible - starting posixovl"
155 rm $MEMORY/.empty1 $MEMORY/.empty2 2>/dev/null
156 mkdir -p $CHANGES
157 posixovl -F $CHANGES -- -o attr_timeout=300,entry_timeout=300,negative_timeout=300,kernel_cache,allow_other
158 find $CHANGES >/dev/null 2>&1 # cache everything now
159 fi
160 fi
162 # $UNION will be used as a root directory, livecd modules will be added soon
163 echolog "Setup union directory (using aufs)"
165 mkdir -p $CHANGES
166 mkdir -p $IMAGES
167 mount -n -t tmpfs tmpfs $IMAGES
169 debug_shell
171 # store the xino file in memory in all cases, it's faster and safer
172 if [ "$XINO" != "$MEMORY" ]; then
173 mkdir -p $XINO
174 mount -n -t tmpfs tmpfs $XINO
175 fi
177 # mount aufs using the writable branch as the first one (leftmost/topmost)
178 mount -t aufs -o nowarn_perm,xino=$XINO/.aufs.xino,br:$CHANGES=rw aufs $UNION
179 if [ $? -ne 0 ]; then dmesg | tail -n 1; fatal "can't setup union (aufs)"; fi
181 debug_shell
183 # setup initramfs if it exist
184 if [ -d /initramfs ]; then
185 echolog "Setup initramfs for first module..."
186 mount -n -o remount,add:1:/initramfs=ro aufs $UNION
187 fi
189 debug_shell
191 # If toram or copy2ram boot parameter is present, copy all fs modules to RAM.
192 # (skip modules from /optional/ which are not listed in load= boot option)
193 # Finaly modify DATA variable so it will point to correct directory
194 if [ "$(cmdline_parameter toram)" != "" -o "$(cmdline_parameter copy2ram)" != "" ]; then
195 echolog "copying $LIVECDNAME data to RAM, this may take some time..."
196 mkdir -p $COPY2RAM
198 # make sure it's in RAM even with changes= parameter
199 if [ "$CHANGESMNT" ]; then mount -t tmpfs -o "size=$RAMSIZE" tmpfs $COPY2RAM; fi
200 copy_to_ram $DATA $COPY2RAM
202 cd_autoeject 1
203 fumount $DATA
204 fumount $MOUNTDIR/*
205 rmdir $MOUNTDIR/* 2>/dev/null # mounted device names are empty, remove them
206 DATA=$COPY2RAM
207 cd_autoeject 0
208 fi
210 debug_shell
212 MODPATH=$(cmdline_value modpath)
213 if [ "$MODPATH" != "" ]; then
214 MODPATH=$(find_in_computer $MODPATH)
215 if [ $FINDISOVAR ]; then
216 mount_device $MODPATH $FINDISOMNT/$LIVECDNAME/tmp rbind
217 else
218 mount_device $MODPATH $DATA/tmp rbind
219 fi
220 fi
222 debug_shell
224 # DATA contains path to the base directory of all fs modules which need
225 # to be mounted and inserted into live filesystem. Do it now.
226 echolog "Loading modules:"
227 union_insert_modules $UNION $DATA $IMAGES
229 # the $MEMORY directory can contain $MEMORY/modules too
230 # in the case if changes= boot argument is used. If not, it doesn't hurt
231 union_insert_modules $UNION $MEMORY $IMAGES
233 debug_shell
235 ROOTCOPY=$(cmdline_value rootcopy)
236 if [ $(cmdline_parameter norootcopy) ]; then
237 echolog "Not using rootcopy"
238 elif [ "$ROOTCOPY" ]; then
239 echolog "Mounting content of rootcopy directory"
240 ROOTCOPY=$(find_in_computer $ROOTCOPY)
241 mount_device $ROOTCOPY $DATA/rootcopy rbind
242 echolog "Copying content of rootcopy directory"
243 cp -af $DATA/rootcopy/* $UNION 2>/dev/null # may be empty
244 else
245 echolog "Copying content of rootcopy directory"
246 cp -af $DATA/rootcopy/* $UNION 2>/dev/null # may be empty
247 fi
249 # TODO: if copy2ram is used, there is no need to preserve the original in memory anymore
250 #if [ "$DATA" = "$COPY2RAM" ]; then
251 # rm from memory once ??
252 #fi
254 echolog "Copying liblinuxlive library to union"
255 cp -af /liblinuxlive $UNION/usr/lib/
257 debug_shell
259 echolog "Recreating /etc/fstab and /mnt directories"
260 touch $UNION/etc/fstab
261 rmdir $UNION/mnt/* 2>/dev/null
262 #fstab_update $UNION
263 fstab_clean $UNION
265 # everything is ready now, so we may unload unused kernel modules
266 # and do some cleanup, unmount few things which are no longer needed.
267 #rmmod_unused_modules
268 #fumount /usr
269 #fumount /sys
271 # More likely these directories aren't there.
272 # Even if they are, this won't hurt.
273 mkdir -p $UNION/boot
274 mkdir -p $UNION/proc
275 mkdir -p $UNION/sys
276 mkdir -p $UNION/dev
277 mkdir -p $UNION/tmp
278 chmod 1777 $UNION/tmp
280 # Boot will contain whatever was in ./boot directory in the bootable media
281 # Error output goes to null, as nothing is mounted with copy2ram
282 #mount -n -o rbind $(dirname $DATA)/boot $UNION/boot 2>/dev/null
283 MIRRORCMD=$(cmdline_value mirror)
284 if [ "$MIRRORCMD" != "" ]; then
285 MIRRORCMD=$(find_in_computer $MIRRORCMD)
286 if [ -d $MIRRORCMD ]; then
287 mkdir -p $MEMORY/$MIRROR
288 mount_device $MIRRORCMD $MEMORY/$MIRROR #rbind
289 fi
290 fi
292 if [ -d $MEMORY/$MIRROR ]; then
293 echolog "Setup repo for mirror iso..."
294 mount -n -o remount,add:1:/$MEMORY/$MIRROR=ro aufs $UNION
295 elif [ -d $FINDISOMNT/$MIRROR ]; then
296 echolog "Setup repo for mirror iso..."
297 mount -n -o remount,add:1:/$FINDISOMNT/$MIRROR=ro aufs $UNION
298 elif [ -d $(dirname $DATA)/$MIRROR ]; then
299 echolog "Setup repo for mirror iso..."
300 mount -n -o remount,add:1:/$(dirname $DATA)/$MIRROR=ro aufs $UNION
301 fi
303 # Union contains all the files and directories unioned from all modules.
304 # Change root directory to it, and move initrd's root to /mnt/live/initramdisk
305 # Finaly execute /sbin/init to start the distribution.
306 echolog "changing root directory..."
308 cd $UNION
309 mkdir -p $INITRAMDISK
311 # Copy all dev files (found by mdev) to unioned dev directory
312 # so at least disk devices exist (your Linux may need them).
313 # Two exceptions, do not copy pty* and tty* devs.
314 if [ "$(cmdline_parameter nohotplug)" ]; then
315 cp -fdR /dev . 2>/dev/null
316 #else
317 #mount -n -o move /dev dev
318 fi
320 #mount -n -o move /proc proc
321 #mount -n -o move /sys sys
322 fumount /sys
323 fumount /run
324 fumount /dev
326 if [ ! -e /dev/console ]; then mknod /dev/console c 5 1; fi
327 if [ ! -d dev/shm ]; then mkdir -p dev/shm; fi
328 if [ ! -d dev/pts ]; then mkdir -p dev/pts; fi
330 # find chroot and init
331 if [ -x bin/chroot ]; then CHROOT=bin/chroot; fi
332 if [ -x sbin/chroot ]; then CHROOT=sbin/chroot; fi
333 if [ -x usr/bin/chroot ]; then CHROOT=usr/bin/chroot; fi
334 if [ -x usr/sbin/chroot ]; then CHROOT=usr/sbin/chroot; fi
335 if [ "$CHROOT" = "" ]; then fatal "Can't find executable chroot command"; fi
337 if [ -x bin/init ]; then INIT=bin/init; fi
338 if [ -x sbin/init ]; then INIT=sbin/init; fi
339 if [ "$INIT" = "" ]; then fatal "Can't find executable init command"; fi
341 # time to end Linux Live scripts and start the distribution itself,
342 # using /sbin/init or whatever was found.
344 debug_shell
346 fumount /proc
348 mount -n -o remount,ro aufs .
350 # We will copy init from the distro to initrd (there should be 2MB free)
351 # This allows us to use the cleanup script during reboot, as init will be
352 # started from memory and not from the union and /union will not be busy.
354 cp -af $INIT /bin >/dev/null 2>&1
355 if [ $? -eq 0 ]; then
356 pivot_root . $INITRAMDISK
357 exec $CHROOT . $INITRAMDISK/bin/init <dev/console >dev/console 2>&1
358 else # If copying fails, start init directly.
359 pivot_root . $INITRAMDISK
360 exec $CHROOT . $INIT <dev/console >dev/console 2>&1
361 fi
363 header "!!ERROR!!"
364 fatal "You are not supposed to be here, something went wrong!"