Adding casper 1.66+debian-1.
[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=/live_media
8
9 # Will be mounted if found as copy on write instead of tmpfs
10 root_persistence="casper-rw"
11
12 # TODO
13 ## Each file found with this pattern will be mounted directly in the 
14 ## mountpoint extracted from file name "${other_mounts_pattern}<mountpoint>"
15 #other_mounts_pattern="casper-mount-"
16 home_persistence="home-rw"
17
18 #overlay_method=unionfs
19 #if [ "${DPKG_ARCH}" = "ia64" ] || [ "${DPKG_ARCH}" = "hppa" ] || [ "${DPKG_ARCH}" = "sparc" ]; then
20 #    overlay_method=devmapper
21 #fi
22
23 USERNAME="casper"
24 USERFULLNAME="Live session user"
25 HOST="live"
26 BUILD_SYSTEM="Debian"
27
28 mkdir -p $mountpoint
29
30 [ -f /etc/casper.conf ] && . /etc/casper.conf
31
32 export USERNAME USERFULLNAME HOST BUILD_SYSTEM
33
34 if [ "${BUILD_SYSTEM}" == "Ubuntu" ]; then
35     MP_QUIET="-Q"
36 else
37     MP_QUIET="-q"
38 fi
39
40 # looking for casper specifics options as kernel parameters
41 for x in $(cat /proc/cmdline); do
42     case $x in
43         netboot*)
44             export NETBOOT=${x#netboot=} ;;
45         toram)
46             export TORAM=1 ;;
47         show-cow)
48             export SHOWCOW=1 ;;
49         persistent)
50             export PERSISTENT=1 ;;
51         casper-getty)
52             export CASPERGETTY=1 ;;
53     esac
54 done
55
56 # sort of compatibility with netboot.h from linux docs
57 if [ -z "${NETBOOT}" ]; then
58     if [ "${ROOT}" == "/dev/nfs" ]; then
59         NETBOOT="nfs"
60         export NETBOOT
61     elif [ "${ROOT}" == "/dev/cifs" ]; then
62         NETBOOT="cifs"
63         export NETBOOT
64     fi
65 fi
66
67 is_casper_path() {
68     path=$1
69     if [ -d "$path/casper" ]; then
70         if [ "$(echo $path/casper/*.cloop)" != "$path/casper/*.cloop" ] ||
71             [ "$(echo $path/casper/*.squashfs)" != "$path/casper/*.squashfs" ] ||
72             [ "$(echo $path/casper/*.ext2)" != "$path/casper/*.ext2" ] ||
73             [ "$(echo $path/casper/*.dir)" != "$path/casper/*.dir" ]; then
74             return 0
75         fi
76     fi
77     return 1
78 }
79
80 subdevices() {
81     sysblock=$1
82     r=""
83     for dev in "${sysblock}" "${sysblock}"/*; do
84         if [ -e "${dev}/dev" ]; then
85             r="${r} ${dev}"
86         fi
87     done
88     echo ${r}
89 }
90
91 get_backing_device() {
92     case "$1" in
93         *.cloop)
94             echo $(setup_loop "$1" "cloop" "/sys/block/cloop*")
95             ;;
96         *.squashfs|*.ext2)
97             echo $(setup_loop "$1" "loop" "/sys/block/loop*")
98             ;;
99         *.dir)
100             echo "directory"
101             ;;
102         *)
103             panic "Unrecognized casper filesystem: $1"
104             ;;
105     esac
106 }
107
108 match_files_in_dir() {
109     # Does any files match pattern $1 ?
110
111     local pattern="$1"
112     if [ "$(echo $pattern)" != "$pattern" ]; then
113         return 0
114     fi
115     return 1
116 }
117
118 mount_images_in_directory() {
119     directory="$1"
120     rootmnt="$2"
121     if match_files_in_dir "$directory/casper/*.cloop"; then
122         # Let's hope there's just one matching *.cloop... FIXME
123         setup_devmapper $(get_backing_device "$directory/casper/*.cloop") "$rootmnt"
124     elif match_files_in_dir "$directory/casper/*.squashfs" || 
125         match_files_in_dir "$directory/casper/*.ext2" ||
126         match_files_in_dir "$directory/casper/*.dir"; then
127         setup_unionfs "$directory/casper" "$rootmnt"
128     else
129         :
130     fi
131 }
132
133 sys2dev() {
134     sysdev=${1#/sys}
135     echo "/dev/$(udevinfo -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
136 }
137
138 setup_loop() {
139     local fspath=$1
140     local module=$2
141     local pattern=$3
142
143     modprobe "${MP_QUIET}" -b "$module"
144     udevsettle
145
146     for loopdev in $pattern; do
147         if [ "$(cat $loopdev/size)" -eq 0 ]; then
148             dev=$(sys2dev "${loopdev}")
149             losetup "$dev" "$fspath"
150             echo "$dev"
151             return 0
152         fi
153     done
154     panic "No loop devices available"
155 }
156
157 get_fstype() {
158     #FIXME# one use of this function expects "unknown" another does not!
159     # which is it???
160     local FSTYPE
161     local FSSIZE
162     eval $(fstype < $1)
163     if [ "$FSTYPE" != "unknown" ]; then
164         echo $FSTYPE
165         return 0
166     fi
167     /lib/udev/vol_id -t $1 2>/dev/null
168 }
169
170 setup_devmapper() {
171     backdev="$1"
172     rootmnt="$2"
173
174     modprobe "${MP_QUIET}" -b dm-mod
175     COW_DEVICE=/dev/ram1
176     COW_NAME="casper-cow"
177
178     BACKING_FILE_SIZE=$(blockdev --getsize "$backdev")
179     MAX_COW_SIZE=$(blockdev --getsize "$COW_DEVICE")
180     CHUNK_SIZE=8 # sectors
181
182     if [ -z "$COW_SIZE" -o "$COW_SIZE" -gt "$MAX_COW_SIZE" ]; then
183         COW_SIZE=$MAX_COW_SIZE
184     fi
185
186     echo "0 $COW_SIZE linear $COW_DEVICE 0" | dmsetup create $COW_NAME
187
188     echo "0 $BACKING_FILE_SIZE snapshot $backdev /dev/mapper/$COW_NAME p $CHUNK_SIZE" | \
189         dmsetup create casper-snapshot
190     if [ "$(get_fstype $backdev)" = "unknown" ]; then
191         panic "Unknown file system type on $backdev"
192     fi
193     mount -t $(get_fstype "$backdev") /dev/mapper/casper-snapshot $rootmnt || panic "Can not mount /dev/mapper/casper/snapshot on $rootmnt"
194
195     mkdir -p "$rootmnt/rofs"
196     echo "0 $BACKING_FILE_SIZE linear $backdev 0" | dmsetup create casper-backing
197     mount -t $(get_fstype "$backdev") /dev/mapper/casper-backing "$rootmnt/rofs"
198 }
199
200 where_is_mounted() {
201     device=$1
202     if grep -q "^$device " /proc/mounts; then
203         grep "^$device " /proc/mounts | read d mountpoint rest
204         echo $mountpoint
205         return 0
206     fi
207     return 1
208 }
209
210 copy_to_ram() {
211     copyfrom="$1"
212
213     if [ ! -z "${2}" ] ; then
214         # This will enable future rampersistence, todo yet
215         copyto="${2}"
216         moveit="False"
217     else
218         copyto="${copyfrom}_swap"
219         moveit="True"
220     fi
221
222     size=$(du -ks ${copyfrom} | cut -f1)
223     size=$(expr ${size} + ${size}/20 ) # Fixme: 5% more to be sure
224     needed_space=$(expr ${size} * 1024)
225     freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( cat /proc/meminfo | grep Cached | head -n 1 | awk '/Cached/{print $2}' - ))
226     
227     if [ ! ${freespace} -lt ${needed_space}  ] ; then
228         [ "$quiet" != "y" ] && log_begin_msg "Not enough free memory to copy to ram"
229         [ "$quiet" != "y" ] && log_end_msg
230         return
231     else
232         [ "$quiet" != "y" ] && log_begin_msg "Copying live media to ram..."
233         mkdir "${copyto}"
234         mount -t tmpfs -o size=${size}k /dev/shm ${copyto}
235         cp -a ${copyfrom}/* ${copyto} # "cp -a" from busybox also copies hidden files
236         umount ${copyfrom}
237         if [ "${moveit}" == "True" ]; then
238             mount -r -o move ${copyto} ${copyfrom}
239             rmdir ${copyto}
240         fi
241         [ "$quiet" != "y" ] && log_end_msg
242     fi
243 }
244
245 find_cow_device() {
246     pers_label="${1}"
247     cow_backing="/${pers_label}-backing"
248     for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop); do
249         for dev in $(subdevices "${sysblock}"); do
250             devname=$(sys2dev "${dev}")
251             if [ "$(/lib/udev/vol_id -l $devname 2>/dev/null)" = "${pers_label}" ]; then
252                 echo "$devname"
253                 return
254             elif [ "$(get_fstype ${devname})" = "vfat" ]; then
255                 mkdir -p "${cow_backing}"
256                 if where_is_mounted ${devname} > /dev/null; then
257                     mount -o remount,rw ${devname} $(where_is_mounted ${devname}) || panic "Remounting failed"
258                     mount -o bind $(where_is_mounted ${devname}) ${cow_backing} || panic "Cannot bind-mount"
259                 else
260                     mount -t $(get_fstype "${devname}") -o rw "${devname}" ${cow_backing} || panic "Cannot mount $devname on /cow-backing"
261                 fi
262
263                 if [ -e "${cow_backing}/${pers_label}" ]; then
264                     echo $(setup_loop "${cow_backing}/${pers_label}" "loop" "/sys/block/loop*")
265                     return 0
266                 else
267                     umount ${cow_backing}
268                 fi
269             fi
270         done
271     done
272 }
273
274 do_netmount() {
275     rc=1
276
277     modprobe "${MP_QUIET}" af_packet # For DHCP
278
279     ipconfig ${DEVICE} /tmp/net-${DEVICE}.conf
280
281     if [ "${NFSROOT}" = "auto" ]; then
282         NFSROOT=${ROOTSERVER}:${ROOTPATH}
283     fi
284
285     [ "$quiet" != "y" ] && log_begin_msg "Trying netboot from ${NFSROOT}"
286     
287     if [ "${NETBOOT}" != "nfs" ] && do_cifsmount ; then
288         rc=0
289     elif do_nfsmount ; then
290         NETBOOT="nfs"
291         export NETBOOT
292         rc=0
293     fi
294
295     [ "$quiet" != "y" ] && log_end_msg
296     return ${rc}
297 }
298
299 do_nfsmount() {
300     rc=1
301     modprobe "${MP_QUIET}" nfs
302     if [ -z "${NFSOPTS}" ]; then
303         NFSOPTS=""
304     fi
305
306     [ "$quiet" != "y" ] && log_begin_msg "Trying nfsmount -o nolock -o ro ${NFSOPTS} ${NFSROOT} ${mountpoint}"
307     # FIXME: This for loop is an ugly HACK round an nfs bug
308     for i in 0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13; do
309         nfsmount -o nolock -o ro ${NFSOPTS} "${NFSROOT}" "${mountpoint}" && rc=0 && break
310         sleep 1
311     done
312     return ${rc}
313 }
314
315 do_cifsmount() {
316     rc=1
317     if [ -x "/sbin/mount.cifs" ]; then
318         if [ -z "${NFSOPTS}" ]; then
319             CIFSOPTS="-ouser=root,password="
320         else
321             CIFSOPTS="${NFSOPTS}"
322         fi
323
324         [ "$quiet" != "y" ] && log_begin_msg "Trying mount.cifs ${NFSROOT} ${mountpoint} ${CIFSOPTS}"
325         modprobe "${MP_QUIET}" cifs
326
327         if mount.cifs "${NFSROOT}" "${mountpoint}" "${CIFSOPTS}" ; then
328             rc=0
329         fi
330     fi
331     return ${rc}
332 }
333
334 setup_unionfs() {
335     image_directory="$1"
336     rootmnt="$2"
337
338     modprobe "${MP_QUIET}" -b unionfs
339
340     # run-init can't deal with this, but we're going to move all of these
341     # away before it runs anyway.
342     croot="/casper"
343
344     # Let's just mount the read-only file systems first
345     rofsstring=""
346     rofslist=""
347     if [ "${NETBOOT}" == "nfs" ] ; then
348         roopt="nfsro" # go aroung a bug in nfs-unionfs locking
349     else
350         roopt="ro"
351     fi
352
353     mkdir -p "${croot}"
354     for image_type in "ext2" "squashfs" "dir" ; do
355         for image in "${image_directory}"/*."${image_type}"; do
356             imagename=$(basename "${image}")
357             if [ -d "${image}" ]; then
358                 # it is a plain directory: do nothing
359                 rofsstring="${image}=${roopt}:${rofsstring}"
360                 rofslist="${image} ${rofslist}"
361             elif [ -f "${image}" ]; then
362                 backdev=$(get_backing_device "$image")
363                 fstype=$(get_fstype "${backdev}")
364                 if [ "${fstype}" = "unknown" ]; then
365                     panic "Unknown file system type on ${backdev} (${image})"
366                 fi
367                 mkdir -p "${croot}/${imagename}"
368                 mount -t "${fstype}" -o ro "${backdev}" "${croot}/${imagename}" || panic "Can not mount $backdev ($image) on ${croot}/${imagename}" && rofsstring="${croot}/${imagename}=${roopt}:${rofsstring}" && rofslist="${croot}/${imagename} ${rofslist}"
369             fi
370         done
371     done
372     rofsstring=${rofsstring%:}
373
374     mkdir -p /cow
375     cowdevice="tmpfs"
376     cow_fstype="tmpfs"
377     
378     # Looking for "${root_persistence}" device or file
379     if [ ! -z "${PERSISTENT}" ]; then
380         cowprobe=$(find_cow_device "${root_persistence}")
381         if [ -b "${cowprobe}" ]; then
382             cowdevice=${cowprobe}
383             cow_fstype=$(get_fstype "${cowprobe}")
384         else
385             [ "$quiet" != "y" ] &&    log_begin_msg "Unable to find the persistent medium"
386         fi
387     fi
388
389     mount ${cowdevice} -t ${cow_fstype} -o rw /cow || panic "Can not mount $cowdevice on /cow"
390     
391     mount -t unionfs -o dirs=/cow=rw:$rofsstring unionfs "$rootmnt" || panic "Unionfs mount failed"
392     
393     for d in ${rofslist}; do
394         mkdir -p "${rootmnt}/casper/${d##*/}"
395         case d in
396             *.dir) mount -o bind "${d}" "${rootmnt}/casper/${d##*/}" ;;
397             *) mount -o move "${d}" "${rootmnt}/casper/${d##*/}" ;;
398         esac
399     done
400
401     # Adding other custom mounts
402     if [ ! -z "${PERSISTENT}" ]; then
403         homecow=$(find_cow_device "${home_persistence}" )
404         if [ -b "${homecow}" ]; then
405             mount ${homecow} -t $(get_fstype "${homecow}") -o rw "${rootmnt}/home"
406         else 
407             [ "$quiet" != "y" ] &&  log_begin_msg "Unable to find the persistent home medium"
408         fi
409     fi
410
411     if [ ! -z "${SHOWCOW}" ]; then
412         mkdir -p "$rootmnt/cow"
413         mount -o bind /cow "$rootmnt/cow"
414     fi
415 }
416
417 is_usb_device() {
418     sysfs_path="${1#/sys}"
419     if /lib/udev/path_id "${sysfs_path}" | grep -Eq "ID_PATH=(usb|pci-[^-]*-usb)"; then
420         return 0
421     fi
422     return 1
423 }
424
425 find_livefs() {
426     mounted=
427     for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram); do
428         devname=$(sys2dev "${sysblock}")
429         fstype=$(get_fstype "${devname}")
430         if /lib/udev/cdrom_id ${devname} > /dev/null; then
431             mount -t ${fstype} -o ro "$devname" $mountpoint || continue
432             if is_casper_path $mountpoint; then
433                 echo $mountpoint
434                 return
435             else
436                 umount $mountpoint
437             fi
438         elif is_usb_device "$sysblock"; then
439             for dev in $(subdevices "${sysblock}"); do
440                 devname=$(sys2dev "${dev}")
441                 fstype=$(get_fstype "${devname}")
442                 case ${fstype} in
443                     vfat|iso9660|udf)
444                         mount -t ${fstype} -o ro "${devname}" $mountpoint || continue
445                         if is_casper_path $mountpoint; then
446                             echo $mountpoint
447                             return
448                         else
449                             umount $mountpoint
450                         fi
451                         ;;
452                 esac
453             done
454         elif [ "${fstype}" = "squashfs" ||  \
455                 "${fstype}" = "ext2" ]; then
456         
457             # This is an ugly hack situation, the block device has
458             # an image directly on it.  It's hopefully
459             # casper, so take it and run with it.
460         
461             ln -s "${devname}" "${devname}.${fstype}"
462             echo "${devname}.${fstype}"
463             return
464         fi
465     done
466 }
467
468 set_usplash_timeout() {
469     if [ -x /sbin/usplash_write ]; then
470         /sbin/usplash_write "TIMEOUT 120"
471     fi
472 }
473
474 mountroot() {
475     exec 6>&1
476     exec 7>&2
477     exec > casper.log
478     exec 2>&1
479     
480     set_usplash_timeout
481     [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/casper-premount"
482     run_scripts /scripts/casper-premount
483     [ "$quiet" != "y" ] && log_end_msg
484
485     # Needed here too because some things (*cough* udev *cough*)
486     # changes the timeout
487
488     set_usplash_timeout
489
490     if [ ! -z "${NETBOOT}" ]; then
491         if do_netmount ; then
492             livefs_root="${mountpoint}"
493         else
494             panic "Unable to find a the network rootfs live file system"
495         fi
496     else
497         # Scan local devices for the image
498         for i in 0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13; do
499             livefs_root=$(find_livefs)
500             if [ "${livefs_root}" ]; then
501                 break
502             fi
503         done
504
505         if [ "$?" -gt 0 ]; then
506             panic "Unable to find a medium containing a live file system"
507         fi
508             
509         if [ ! -z "${TORAM}" ]; then
510             copy_to_ram "${livefs_root}"
511         fi
512     fi
513     sleep 1
514
515     mount_images_in_directory "$livefs_root" "$rootmnt"
516
517     log_end_msg
518
519     maybe_break casper-bottom
520     [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/casper-bottom"
521
522     run_scripts /scripts/casper-bottom
523     [ "$quiet" != "y" ] && log_end_msg
524
525     exec 1>&6 6>&-
526     exec 2>&7 7>&-
527     cp casper.log "${rootmnt}/var/log/"
528 }