Adding upstream version 1.79+debian.
[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     # move the first mount; no head in busybox-initramfs
239     for d in $(mount -t squashfs | cut -d\  -f 3); do
240         mkdir -p "${rootmnt}/rofs"
241         mount -o move "${d}" "${rootmnt}/rofs"
242         break
243     done
244
245     if grep -q showmounts /proc/cmdline; then
246         for d in $(mount -t squashfs | cut -d\  -f 3); do
247             mkdir -p "${rootmnt}/casper/${d##*/}"
248             mount -o move "${d}" "${rootmnt}/casper/${d##*/}"
249         done
250
251         mkdir -p "$rootmnt/cow"
252         mount -o bind /cow "$rootmnt/cow"
253     fi
254
255 }
256
257 is_usb_device() {
258     sysfs_path="${1#/sys}"
259     if /lib/udev/path_id "${sysfs_path}" | grep -E -q "ID_PATH=(usb|pci-[^-]*-usb)"; then
260         return 0
261     fi
262     return 1
263 }
264
265 find_livefs() {
266         mounted=
267         for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram); do
268             devname=$(sys2dev "${sysblock}")
269             fstype=$(get_fstype "${devname}")
270             if /lib/udev/cdrom_id ${devname} > /dev/null; then
271                 mount -t ${fstype} -o ro "$devname" $mountpoint || continue
272                 if is_casper_path $mountpoint; then
273                     echo $mountpoint
274                     return
275                 else
276                     umount $mountpoint
277                 fi
278             elif is_usb_device "$sysblock"; then
279                 for dev in $(subdevices "${sysblock}"); do
280                     devname=$(sys2dev "${dev}")
281                     fstype=$(get_fstype "${devname}")
282                     case ${fstype} in
283                         vfat|iso9660|udf)
284                             mount -t ${fstype} -o ro "${devname}" $mountpoint || continue
285                             if is_casper_path $mountpoint; then
286                                 echo $mountpoint
287                                 return
288                             else
289                                 umount $mountpoint
290                             fi
291                             ;;
292                     esac
293                 done
294             elif [ "${fstype}" = "squashfs" ]; then
295
296                 # This is an ugly hack situation, the block device has
297                 # a squashfs image directly on it.  It's hopefully
298                 # casper, so take it and run with it.
299
300                 ln -s "${devname}" "${devname}.${fstype}"
301                 echo "${devname}.${fstype}"
302                 return
303             fi
304         done
305 }
306
307 pulsate() {
308     if [ -x /sbin/usplash_write ]; then
309         /sbin/usplash_write "PULSATE"
310     fi
311 }
312
313 set_usplash_timeout() {
314     if [ -x /sbin/usplash_write ]; then
315         /sbin/usplash_write "TIMEOUT 120"
316     fi
317 }
318
319 mountroot() {
320     exec 6>&1
321     exec 7>&2
322     exec > casper.log
323     exec 2>&1
324     
325     set_usplash_timeout
326     [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/casper-premount"
327     pulsate
328     run_scripts /scripts/casper-premount
329     [ "$quiet" != "y" ] && log_end_msg
330
331     # Needed here too because some things (*cough* udev *cough*)
332     # changes the timeout
333
334     set_usplash_timeout
335
336     for i in 0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13; do
337         livefs_root=$(find_livefs)
338         if [ "${livefs_root}" ]; then
339             break
340         fi
341         sleep 1
342     done
343     if [ "$?" -gt 0 ]; then
344         panic "Unable to find a medium containing a live file system"
345     fi
346     
347     mount_images_in_directory "$livefs_root" "$rootmnt"
348     log_end_msg
349
350     maybe_break casper-bottom
351     [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/casper-bottom"
352
353     pulsate
354     run_scripts /scripts/casper-bottom
355     [ "$quiet" != "y" ] && log_end_msg
356
357     exec 1>&6 6>&-
358     exec 2>&7 7>&-
359     cp casper.log "${rootmnt}/var/log/"
360 }