slitaz-boot-scripts view init @ rev 383
Added tag 5.6.5 for changeset ae0038500c6d
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Tue Nov 18 14:24:33 2014 +0100 (2014-11-18) |
parents | d820c9b15fc1 |
children | d51fd6bbb28e |
line source
1 #!/bin/sh
3 ### cached kernel parameters :
4 #
5 # modprobe=
6 # list of modules to load, example modprobe=r8169,btrfs
7 #
8 ### Frugal mode or live CD : using a root ram disk.
9 #
10 # tmpram= [RAMDISK]
11 # minimum % of free RAM to build the root tmpfs, default 100
12 # a lower value may work, such as 50
13 # rootfssize= [RAMDISK]
14 # size of / regarding the RAM size, default 90%
15 #
16 ### Indirect mode ; an initramfs is used to mount root from the hard disk.
17 ### Any rootfs should fit, minimum required is slitaz-base.
18 #
19 # mount= [MOUNT]
20 # device with root loop file or sub directory
21 # posixovl [MOUNT]
22 # use posix attributes on a non posix filesystem (VFAT, NTFS...)
23 # loopfs= [MOUNT]
24 # loop file for root filesystem
25 # subroot= [MOUNT]
26 # sub directory for root filesystem
27 # bindfs= [MOUNT] (deprecated)
28 # bind out of root tree part
29 #
30 ### Special disk mode ; needs extra modules/softwares from preinit flavor.
31 #
32 # dmraid= [RAID]
33 # root semi hardware raid device name in /dev/mapper
34 # softraid= [RAID]
35 # root software device name in /dev/mapper
36 # lvmroot= [LVM]
37 # root logical volume name in /dev/mapper
38 # cryptoroot= [CRYPTO]
39 # encrypted root device using luks or loop-aes
40 #
41 ### Special mode used by loram flavor with aufs & squashfs
42 ### incremental/nested rootfs (russian dolls).
43 #
44 # rodev= [LORAM][MERGE]
45 # device (+ optional path) for the read only aufs branch
46 # rwdev= [LORAM][MERGE]
47 # (persistent) device for the r/w aufs branch instead of the ramfs
48 # isofs [LORAM]
49 # do not use squashfs filesystem images, but the cdrom filesystem only
51 fail()
52 {
53 echo -e '\033[70G[ \033[1;3'${1:-1mFailed}'\033[0;39m ]'
54 }
56 quit()
57 {
58 [ -d /mnt$4/etc ] || return
59 [ -n "$4" ] || busybox mount /mnt -o remount,ro
60 busybox mount --move /run /mnt/${1:-run}
61 cat>/run/init<<EOT
62 $2
63 x=/sbin/switch_root
64 [ -x \$x ] && exec \$x mnt $3 /sbin/init
65 EOT
66 fail 2mDone
67 exit
68 }
70 arg()
71 {
72 grep -q $1 /proc/cmdline
73 }
75 got()
76 {
77 arg $1= && root="$(sed "s/.*$1=\([^ ]*\).*/\1/" </proc/cmdline)"
78 }
80 msg()
81 {
82 echo "Switching / to $1..."
83 }
85 use()
86 {
87 got $1 && msg "$1 $root"
88 }
90 mnt()
91 {
92 r=$(blkid 2> /dev/null | sed "s|\"||g;|$root|!d;s|:.*||;q")
93 r=${r:-$root}
94 r=${r#/dev/}
95 r=/dev/${r%%/*}
96 d=${root#*$r}
97 busybox mount $r $1 && return
98 w=$(cat /sys/module/usb_storage/parameters/delay_use)
99 w=$((1+${w:-2}))
100 echo -n "Sleep $w seconds..."
101 sleep $w
102 busybox mount $r $1
103 }
105 mod()
106 {
107 for i in $@; do
108 echo "Loading module: $i"
109 modprobe $i 2>/dev/null || busybox insmod $(find /lib/modules|sed "/$i.ko/!dq")
110 done
111 }
113 try()
114 {
115 if [ ! -d /mnt/etc ] && got cryptoroot; then
116 mod dm-mod dm-crypt aes-256
117 d=${root#/dev/}
118 l=crypto-$d
119 if cryptsetup isLuks $root 2>/dev/null; then
120 cryptsetup luksOpen $root $l
121 else
122 read -st 60 -p "Pass phrase : " p
123 k=$(echo $p|hashalot -x -n 32 sha512)
124 echo 0 $(cat $(find /sys/block|grep /$d/size))\
125 crypt aes-plain $k 0 $root 0|dmsetup create $l
126 fi
127 busybox mount /dev/mapper/$l /mnt
128 fi
129 got subroot && return
130 got loopfs && return
131 if [ -d /mnt/etc ]; then
132 for i in $@; do
133 cp -a $i /mnt$(dirname $i)
134 done
135 quit
136 fi
137 fail
138 }
140 lvm()
141 {
142 use lvmroot || return
143 mod dm-mod
144 vgscan --ignorelockingfailure
145 vgchange -ay --ignorelockingfailure
146 busybox mount /dev/mapper/$root /mnt
147 try /dev/mapper $1
148 }
150 ldraid()
151 {
152 while read l
153 do case "$l" in
154 *raid10*) mod raid10;;
155 *raid0*) mod raid0;;
156 *raid1*) mod raid1;;
157 *raid*) mod raid456;;
158 *mirror*) mod dm-mirror
159 esac
160 done
161 }
163 if [ "$1" != "log" ]; then
164 busybox mount -t proc proc /proc
165 busybox mount -t sysfs sys /sys
166 busybox mount -t tmpfs tmpfs /run
167 x=/sbin/init; echo "[ -x $x ] && exec $x" >/run/init
168 $0 log 2>&1 | tee /run/boot.log
169 busybox umount /proc
170 busybox umount /sys
171 . /run/init
172 sh
173 fi
174 got modprobe && mod ${root//,/ }
175 [ /proc/sys/dev/cdrom ] &&
176 ln -s $(sed '/name/!d;s/.*:[^a-z]*//' /proc/sys/dev/cdrom/info) /dev/cdrom
177 if use dmraid
178 then dmraid -s|sed '/^type/!ds/.*: *//'|ldraid
179 [ ${root:0:4} = /dev ] ||
180 root=/dev/mapper/$(dmraid -s|sed '/^name/!ds/.*: *//')p${root#p}
181 dmraid -ay
182 fi
183 if use softraid
184 then mdadm -E -s -c partitions>/etc/mdadm.conf
185 grep -qs " $root " /etc/mdadm.conf ||
186 root=$(awk '/dev.md/{print $2;exit}'</etc/mdadm.conf)
187 grep level=raid /etc/mdadm.conf|ldraid
188 for i in 1 2 3 4 5 6 7 8 9
189 do sleep $i
190 mdadm -A -s
191 grep -qs ': act' /proc/mdstat && break
192 done
193 lvm /etc/mdadm.conf
194 fi
195 lvm
196 if got mount
197 then dev=$root
198 x=$(blkid|grep $dev|sed 's/:.*//;q')
199 root=${x:-$dev}
200 [ "$dev" = "$root" ] || dev="$root ($dev)"
201 echo "Mount $dev..."
202 mnt /mnt
203 arg posixovl && echo "And posixovl..." &&
204 mount.posixovl -F /mnt -- -oallow_other -odefault_permissions -osuid
205 fi
206 got loopfs && echo "Into file $root..." &&
207 losetup /dev/loop0 /mnt/$root && busybox mount /dev/loop0 /mnt
208 got bindfs && echo "Bind ${root/,/ to }..." &&
209 busybox mount --bind /mnt/${root%,*} /mnt/${root/,//}
210 arg cryptoroot= && try
211 if use subroot
212 then cp $(LD_TRACE_LOADED_OBJECTS=1 /lib/ld*.so /usr/sbin/chroot | sed\
213 's|.*=> \(/lib/l[^ ]*\).*|\1|;/^\//!d') /usr/sbin/chroot /run
214 r=$root/run
215 quit $r "export LD_LIBRARY_PATH=$r:/lib"\
216 "$root$(ls /run/ld-*so) $r/chroot $root" "/$root"
217 fi
218 quit
219 msg tmpfs
220 root=100
221 got tmpram
222 r=$root
223 root=90%
224 got rootfssize
225 [ $(busybox free|busybox awk '/Mem:/{print int(($4*100)/$3)}') -ge $r ] &&
226 busybox mount -t tmpfs -o size=$root tmpfs /mnt &&
227 for i in $(ls -ar /)
228 do case "$i" in
229 .*|cdrom) ;;
230 mnt|proc|sys) mkdir /mnt/$i;;
231 usr|var|rootfs*) mv /$i /mnt;;
232 *) cp -a /$i /mnt 2>/dev/null && continue
233 fail
234 busybox umount /mnt
235 exit
236 esac
237 done || fail 3mSkipped
238 quit
239 mod squashfs 2>/dev/null || exit
240 msg aufs
241 br=/mnt/.rw
242 mkdir $br
243 got rwdev && mnt $br && br=$br$d
244 o=
245 p=
246 c=/mnt/.cdrom
247 if [ -z "$(ls /mnt/rootfs* 2>/dev/null)" ]
248 then root=/dev/cdrom/fs
249 got rodev
250 mkdir -p $c /mnt$c /mnt/.rw$c
251 mnt $c
252 o="-o 124"
253 p=/.cdrom/boot
254 c=$c$d
255 fi
256 l=0
257 r=
258 got isofs || for i in /mnt$p/rootfs?*.gz
259 do fs=${i#*root}
260 r=$r:/mnt/.$fs
261 mkdir -p /mnt/.rw/mnt/.$fs /mnt/.$fs
262 losetup $o /dev/loop$l $i
263 busybox mount -o ro -t squashfs /dev/loop$((l++)) /mnt/.$fs
264 done
265 mod aufs
266 busybox mount -t aufs -o br=$br${r:-:$c} none /mnt
267 quit