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