346a6caf223a11209488699558ff9ba5e8fba831
[live-boot-grml.git] / scripts / casper
1 #!/bin/sh
2
3 # set -e
4
5 export PATH=/root/usr/bin:/root/usr/sbin:/root/bin:/root/sbin:/usr/bin:/usr/sbin:/bin:/sbin
6
7 mountpoint=/cdrom
8
9 mkdir -p $mountpoint
10
11 #overlay_method=unionfs
12 #if [ "${DPKG_ARCH}" = "ia64" ] || [ "${DPKG_ARCH}" = "hppa" ] || [ "${DPKG_ARCH}" = "sparc" ]; then
13 #    overlay_method=devmapper
14 #fi
15
16 USERNAME=casper
17 USERFULLNAME="Live session user"
18 HOST=live
19
20 [ -f /etc/casper.conf ] && . /etc/casper.conf
21
22 export USERNAME USERFULLNAME HOST
23
24 is_casper_path() {
25     path=$1
26     if [ -d "$path/casper" ]; then
27         if [ "$(echo $path/casper/*.cloop)" != "$path/casper/*.cloop" ] || 
28             [ "$(echo $path/casper/*.squashfs)" != "$path/casper/*.squashfs" ]; then
29             return 0
30         fi
31     fi
32     return 1
33 }
34
35 subdevices() {
36     sysblock=$1
37     r=""
38     for dev in "${sysblock}" "${sysblock}"/*; do
39         if [ -e "${dev}/dev" ]; then
40             r="${r} ${dev}"
41         fi
42     done
43     echo ${r}
44 }
45
46 get_backing_device() {
47         case "$1" in
48             *.cloop)
49                 echo $(setup_loop "$1" "cloop" "/sys/block/cloop*")
50                         ;;
51             *.squashfs)
52                 echo $(setup_loop "$1" "loop" "/sys/block/loop*")
53                 ;;
54             *)
55                 panic "Unrecognized casper filesystem: $1"
56                 ;;
57         esac
58 }
59
60 match_files_in_dir() {
61     # Does any files match pattern $1 ?
62
63     local pattern="$1"
64     if [ "$(echo $pattern)" != "$pattern" ]; then
65         return 0
66     fi
67     return 1
68 }
69
70 mount_images_in_directory() {
71     directory="$1"
72     rootmnt="$2"
73     if match_files_in_dir "$directory/casper/*.cloop"; then
74         # Let's hope there's just one matching *.cloop... FIXME
75         setup_devmapper $(get_backing_device "$directory/casper/*.cloop") "$rootmnt"
76     elif match_files_in_dir "$directory/casper/*.squashfs"; then
77         setup_unionfs "$directory/casper" "$rootmnt"
78     else 
79         :
80     fi
81 }
82
83 sys2dev() {
84     sysdev=${1#/sys}
85     echo "/dev/$(udevinfo -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
86 }
87
88 setup_loop() {
89     local fspath=$1
90     local module=$2
91     local pattern=$3
92
93     modprobe -Qb "$module"
94     udevsettle
95  
96     for loopdev in $pattern; do
97         if [ "$(cat $loopdev/size)" -eq 0 ]; then
98             dev=$(sys2dev "${loopdev}")
99             losetup "$dev" "$fspath"
100             echo "$dev"
101             return 0
102         fi
103     done
104     panic "No loop devices available"
105 }
106
107 get_fstype() {
108     local FSTYPE
109     local FSSIZE
110     eval $(fstype < $1)
111     if [ "$FSTYPE" != "unknown" ]; then
112         echo $FSTYPE
113         return 0
114     fi
115     /lib/udev/vol_id -t $1 2>/dev/null
116 }
117
118 setup_devmapper() {
119     backdev="$1"
120     rootmnt="$2"
121
122     modprobe -Qb dm-mod
123     COW_DEVICE=/dev/ram1
124     COW_NAME="casper-cow"
125
126     BACKING_FILE_SIZE=$(blockdev --getsize "$backdev")
127     MAX_COW_SIZE=$(blockdev --getsize "$COW_DEVICE")
128     CHUNK_SIZE=8 # sectors
129
130     if [ -z "$COW_SIZE" -o "$COW_SIZE" -gt "$MAX_COW_SIZE" ]; then
131         COW_SIZE=$MAX_COW_SIZE
132     fi
133
134     echo "0 $COW_SIZE linear $COW_DEVICE 0" | dmsetup create $COW_NAME
135
136     echo "0 $BACKING_FILE_SIZE snapshot $backdev /dev/mapper/$COW_NAME p $CHUNK_SIZE" | \
137         dmsetup create casper-snapshot
138     if [ "$(get_fstype $backdev)" = "unknown" ]; then
139         panic "Unknown file system type on $backdev"
140     fi
141     mount -t $(get_fstype "$backdev") /dev/mapper/casper-snapshot $rootmnt || panic "Can not mount /dev/mapper/casper/snapshot on $rootmnt"
142
143     mkdir -p "$rootmnt/rofs"
144     echo "0 $BACKING_FILE_SIZE linear $backdev 0" | dmsetup create casper-backing
145     mount -t $(get_fstype "$backdev") /dev/mapper/casper-backing "$rootmnt/rofs"
146 }
147
148 where_is_mounted() {
149     device=$1
150     if grep -q "^$device " /proc/mounts; then
151         grep "^$device " /proc/mounts | read d mountpoint rest
152         echo $mountpoint
153         return 0
154     fi
155     return 1
156 }
157
158 find_cow_device() {
159     for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop); do
160         for dev in $(subdevices "${sysblock}"); do
161             devname=$(sys2dev "${dev}")
162             if [ "$(/lib/udev/vol_id -l $devname 2>/dev/null)" = "casper-rw" ]; then
163                 echo "$devname"
164                 return
165             elif [ "$(get_fstype ${devname})" = "vfat" ]; then
166                 mkdir -p /cow-backing
167                 if where_is_mounted ${devname} > /dev/null; then
168                     mount -o remount,rw ${devname} $(where_is_mounted ${devname}) || panic "Remounting failed"
169                     mount -o bind $(where_is_mounted ${devname}) /cow-backing || panic "Cannot bind-mount"
170                 else
171                     mount -t $(get_fstype "${devname}") -o rw "${devname}" /cow-backing || panic "Cannot mount $devname on /cow-backing"
172                 fi
173
174                 if [ -e "/cow-backing/casper-rw" ]; then
175                     echo $(setup_loop "/cow-backing/casper-rw" "loop" "/sys/block/loop*")
176                     return 0
177                 else
178                     umount /cow-backing
179                 fi
180             fi
181             
182         done
183     done
184     return 1    
185 }
186
187 setup_unionfs() {
188     image_directory="$1"
189     rootmnt="$2"
190     modprobe -Qb unionfs
191
192     # run-init can't deal with images in a subdir, but we're going to
193     # move all of these away before it runs anyway.  No, we're not,
194     # put them in / since move-mounting them into / breaks mono and
195     # some other apps.
196
197     croot="/"
198
199     # Let's just mount the read-only file systems first
200     mkdir -p "${croot}"
201     for image in "${image_directory}"/*.squashfs; do
202         imagename=$(basename "${image}")
203         backdev=$(get_backing_device "$image")
204         fstype=$(get_fstype "${backdev}")
205         if [ "${fstype}" = "unknown" ]; then
206             panic "Unknown file system type on ${backdev} (${image})"
207         fi
208         mkdir -p "${croot}/${imagename}"
209         mount -t "${fstype}" -o ro "${backdev}" "${croot}/${imagename}" || panic "Can not mount $backdev ($image) on ${croot}/${imagename}"
210     done
211
212     rofsstring=""
213     for dir in $(mount -t squashfs | cut -d\  -f 3); do
214         rofsstring="$dir=ro:$rofsstring"
215     done
216     rofsstring=${rofsstring%:}
217
218     mkdir -p /cow
219     
220     if grep -q persistent /proc/cmdline; then
221         i=0
222         # We love udev and the kernel!
223         while [ "$i" -lt 300 ]; do
224             cowdevice=$(find_cow_device) 
225             if [ -b "$cowdevice" ]; then
226                 mount -t $(get_fstype "$cowdevice") -o rw "$cowdevice" /cow || panic "Can not mount $cowdevice on /cow"
227                 break
228             fi
229             sleep 0.1
230             i=$(( $i + 1 ))
231         done
232     else
233         mount -t tmpfs tmpfs /cow
234     fi
235     
236     mount -t unionfs -o dirs=/cow=rw:$rofsstring unionfs "$rootmnt"
237
238     if grep -q showmounts /proc/cmdline; then
239         for d in $(mount -t squashfs | cut -d\  -f 3); do
240             mkdir -p "${rootmnt}/casper/${d##*/}"
241             mount -o move "${d}" "${rootmnt}/casper/${d##*/}"
242         done
243
244         mkdir -p "$rootmnt/cow"
245         mount -o bind /cow "$rootmnt/cow"
246     fi
247
248 }
249
250 is_usb_device() {
251     sysfs_path="${1#/sys}"
252     if /lib/udev/path_id "${sysfs_path}" | grep -E -q "ID_PATH=(usb|pci-[^-]*-usb)"; then
253         return 0
254     fi
255     return 1
256 }
257
258 find_livefs() {
259         mounted=
260         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram); do
261             devname=$(sys2dev "${sysblock}")
262             fstype=$(get_fstype "${devname}")
263             if /lib/udev/cdrom_id ${devname} > /dev/null; then
264                 mount -t ${fstype} -o ro "$devname" $mountpoint || continue
265                 if is_casper_path $mountpoint; then
266                     echo $mountpoint
267                     return
268                 else
269                     umount $mountpoint
270                 fi
271             elif is_usb_device "$sysblock"; then
272                 for dev in $(subdevices "${sysblock}"); do
273                     devname=$(sys2dev "${dev}")
274                     fstype=$(get_fstype "${devname}")
275                     case ${fstype} in
276                         vfat|iso9660|udf)
277                             mount -t ${fstype} -o ro "${devname}" $mountpoint || continue
278                             if is_casper_path $mountpoint; then
279                                 echo $mountpoint
280                                 return
281                             else
282                                 umount $mountpoint
283                             fi
284                             ;;
285                     esac
286                 done
287             elif [ "${fstype}" = "squashfs" ]; then
288
289                 # This is an ugly hack situation, the block device has
290                 # a squashfs image directly on it.  It's hopefully
291                 # casper, so take it and run with it.
292
293                 ln -s "${devname}" "${devname}.${fstype}"
294                 echo "${devname}.${fstype}"
295                 return
296             fi
297         done
298 }
299
300 pulsate() {
301     if [ -x /sbin/usplash_write ]; then
302         /sbin/usplash_write "PULSATE"
303     fi
304 }
305
306 set_usplash_timeout() {
307     if [ -x /sbin/usplash_write ]; then
308         /sbin/usplash_write "TIMEOUT 120"
309     fi
310 }
311
312 mountroot() {
313     exec 6>&1
314     exec 7>&2
315     exec > casper.log
316     exec 2>&1
317     
318     set_usplash_timeout
319     [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/casper-premount"
320     pulsate
321     run_scripts /scripts/casper-premount
322     [ "$quiet" != "y" ] && log_end_msg
323
324     # Needed here too because some things (*cough* udev *cough*)
325     # changes the timeout
326
327     set_usplash_timeout
328
329     for i in 0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13; do
330         livefs_root=$(find_livefs)
331         if [ "${livefs_root}" ]; then
332             break
333         fi
334         sleep 1
335     done
336     if [ "$?" -gt 0 ]; then
337         panic "Unable to find a medium containing a live file system"
338     fi
339     
340     mount_images_in_directory "$livefs_root" "$rootmnt"
341     log_end_msg
342
343     maybe_break casper-bottom
344     [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/casper-bottom"
345
346     pulsate
347     run_scripts /scripts/casper-bottom
348     [ "$quiet" != "y" ] && log_end_msg
349
350     exec 1>&6 6>&-
351     exec 2>&7 7>&-
352     cp casper.log "${rootmnt}/var/log/"
353 }