5 export PATH=/root/usr/bin:/root/usr/sbin:/root/bin:/root/sbin:/usr/bin:/usr/sbin:/bin:/sbin
9 root_persistence="casper-rw"
10 home_persistence="home-rw"
11 root_snapshot_label="casper-sn"
12 home_snapshot_label="home-sn"
15 USERFULLNAME="Live session user"
21 [ -f /etc/casper.conf ] && . /etc/casper.conf
22 export USERNAME USERFULLNAME HOSTNAME BUILD_SYSTEM
24 . /scripts/casper-helpers
28 # looking for casper specifics options as kernel parameters
29 for x in $(cat /proc/cmdline); do
32 export USERFULLNAME=${x#userfullname=}
33 export CASPERCONF="changed"
36 export HOSTNAME=${x#hostname=}
37 export CASPERCONF="changed"
40 export USERNAME=${x#username=}
41 export CASPERCONF="changed"
44 export NETBOOT=${x#netboot=} ;;
48 export TODISK=${x#todisk=} ;;
50 export SHOWMOUNTS=1 ;;
52 export PERSISTENT=1 ;;
55 if [ "${STATICIP}" == "" ]; then
60 export CASPERGETTY=1 ;;
62 export DEFCONSOLE=$(sed -e 's%.*console=%console=%' /proc/cmdline) ;;
64 export LIVEMEDIA=${x#bootfrom=} ;;
66 export LIVEMEDIA=${x#live-media=} ;;
67 debian-installer/locale=*)
68 export LOCALE=${x#debian-installer/locale=} ;;
70 export LOCALE=${x#locale=} ;;
72 export KBD=${x#kbd-chooser/method=} ;;
74 export KBD=${x#keyb=} ;;
75 console-setup/layoutcode=*)
76 export CSLAYOUT=${x#console-setup/layoutcode=} ;;
77 console-setup/variantcode=*)
78 export CSVARIANT=${x#console-setup/variantcode=} ;;
79 console-setup/modelcode=*)
80 export CSMODEL=${x#console-setup/modelcode=} ;;
84 # sort of compatibility with netboot.h from linux docs
85 if [ -z "${NETBOOT}" ]; then
86 if [ "${ROOT}" == "/dev/nfs" ]; then
89 elif [ "${ROOT}" == "/dev/cifs" ]; then
98 if [ -d "$path/casper" ]; then
99 if [ "$(echo $path/casper/*.squashfs)" != "$path/casper/*.squashfs" ] ||
100 [ "$(echo $path/casper/*.ext2)" != "$path/casper/*.ext2" ] ||
101 [ "$(echo $path/casper/*.dir)" != "$path/casper/*.dir" ]; then
108 get_backing_device() {
111 echo $(setup_loop "$1" "loop" "/sys/block/loop*")
117 panic "Unrecognized casper filesystem: $1"
122 match_files_in_dir() {
123 # Does any files match pattern $1 ?
126 if [ "$(echo $pattern)" != "$pattern" ]; then
132 mount_images_in_directory() {
135 if match_files_in_dir "$directory/casper/*.squashfs" ||
136 match_files_in_dir "$directory/casper/*.ext2" ||
137 match_files_in_dir "$directory/casper/*.dir"; then
138 setup_unionfs "$directory/casper" "$rootmnt"
148 if [ "$FSTYPE" != "unknown" ]; then
152 /lib/udev/vol_id -t $1 2>/dev/null
156 sysfs_path="${1#/sys}"
157 if /lib/udev/path_id "${sysfs_path}" | grep -E -q "ID_PATH=(usb|pci-[^-]*-[ide|scsi|usb])"; then
165 # FIXME: do something better like the scan of supported filesystems
168 vfat|iso9660|udf|ext2|ext3|ntfs)
178 copyto="${copyfrom}_swap"
180 size=$(used_fs_size "null" "${copyfrom}")
182 if [ "${copytodev}" = "ram" ]; then
184 freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( cat /proc/meminfo | grep Cached | head -n 1 | awk '/Cached/{print $2}' - ) )
185 mount_options="-o size=${size}k"
190 # it should be a writable block device
191 if [ -b "${copytodev}" ]; then
193 freespace="$(df -k ${copytodev} | grep -s ${copytodev} | awk '{print $4}')"
194 fstype="$(get_fstype ${devname})"
200 if [ ! ${freespace} -lt ${size} ] ; then
201 [ "$quiet" != "y" ] && log_begin_msg "Not enough free ${free_string} to copy live media in ${copytodev}."
202 [ "$quiet" != "y" ] && log_end_msg
207 [ "$quiet" != "y" ] && log_begin_msg "Copying live media to ${copytodev}..."
209 mount -t "${fstype}" ${mount_options} "${dev}" "${copyto}"
210 cp -a ${copyfrom}/* ${copyto} # "cp -a" from busybox also copies hidden files
212 mount -r -o move ${copyto} ${copyfrom}
214 [ "$quiet" != "y" ] && log_end_msg
221 modprobe "${MP_QUIET}" af_packet # For DHCP
223 ipconfig ${DEVICE} /tmp/net-${DEVICE}.conf
225 if [ "${NFSROOT}" = "auto" ]; then
226 NFSROOT=${ROOTSERVER}:${ROOTPATH}
229 [ "$quiet" != "y" ] && log_begin_msg "Trying netboot from ${NFSROOT}"
231 if [ "${NETBOOT}" != "nfs" ] && do_cifsmount ; then
233 elif do_nfsmount ; then
239 [ "$quiet" != "y" ] && log_end_msg
245 modprobe "${MP_QUIET}" nfs
246 if [ -z "${NFSOPTS}" ]; then
250 [ "$quiet" != "y" ] && log_begin_msg "Trying nfsmount -o nolock -o ro ${NFSOPTS} ${NFSROOT} ${mountpoint}"
251 # FIXME: This for loop is an ugly HACK round an nfs bug
252 for i in 0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13; do
253 nfsmount -o nolock -o ro ${NFSOPTS} "${NFSROOT}" "${mountpoint}" && rc=0 && break
261 if [ -x "/sbin/mount.cifs" ]; then
262 if [ -z "${NFSOPTS}" ]; then
263 CIFSOPTS="-ouser=root,password="
265 CIFSOPTS="${NFSOPTS}"
268 [ "$quiet" != "y" ] && log_begin_msg "Trying mount.cifs ${NFSROOT} ${mountpoint} ${CIFSOPTS}"
269 modprobe "${MP_QUIET}" cifs
271 if mount.cifs "${NFSROOT}" "${mountpoint}" "${CIFSOPTS}" ; then
284 #size=$(used_fs_size "${fromdev}")
286 if [ -b "${fromdev}" ]; then
288 if [ -n "${HOMEMOUNTED}" -a "${snap_type}" = "HOME" ]; then
289 todev=$(cat /proc/mounts | grep -s " $(base_path ${todir}) " | awk '{print $1}' )
290 freespace=$(df -k | grep -s ${todev} | awk '{print $4}')
292 freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( cat /proc/meminfo | grep Cached | head -n 1 | awk '/Cached/{print $2}' - ))
295 tomount="/mnt/tmpsnap"
296 if [ ! -d "${tomount}" ] ; then
297 mkdir -p "${tomount}"
300 fstype=$(get_fstype "${fromdev}")
301 if [ -n "${fstype}" ]; then
303 mount -t "${fstype}" -o ro "${fromdev}" "${tomount}"
304 cp -a "${tomount}"/* ${todir}
307 log_warning_msg "Unrecognized fstype: ${fstype} on ${fromdev}:${snap_type}"
311 if echo ${fromdev} | grep -qs loop; then
312 losetup -d "${fromdev}"
317 [ "$quiet" != "y" ] && log_warning_msg "Unable to find the snapshot ${snap_type} medium"
323 # Look for $snap_label.* in block devices and copy the contents to $snap_mount
324 # and remember the device and filename for resync on exit in casper.init
330 snapdata=$(find_files "${snap_label}.squashfs ${snap_label}.cpio.gz ${snap_label}.ext2")
331 if [ ! -z "${snapdata}" ]; then
332 snapdev="$(echo ${snapdata} | cut -f1 -d ' ')"
333 snapback="$(echo ${snapdata} | cut -f2 -d ' ')"
334 snapfile="$(echo ${snapdata} | cut -f3 -d ' ')"
335 if echo "${snapfile}" | grep -qs '\(squashfs\|ext2\)'; then
336 # squashfs or ext2 snapshot
337 dev=$(get_backing_device "${snapback}/${snapfile}")
338 if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"; then
339 log_warning_msg "Impossible to include the ${snapfile} Snapshot"
344 if ! (cd "${snap_mount}" && zcat "${snapback}/${snapfile}" | cpio -i -u -d 2>/dev/null) ; then
345 log_warning_msg "Impossible to include the ${snapfile} Snapshot"
351 dev=$(find_cow_device "${snap_label}")
352 if [ -b ${dev} ]; then
353 if echo "${dev}" | grep -qs loop; then
354 # strange things happens, user confused?
355 snaploop=$( losetup ${dev} | awk '{print $3}' | tr -d '()' )
356 snapfile=$(basename ${snaploop})
357 snapdev=$(cat /proc/mounts | awk '{print $2,$1}' | grep -es "^$( dirname ${snaploop} )" | cut -f2 -d ' ')
361 if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}" ; then
362 log_warning_msg "Impossible to include the ${snap_label} Snapshot"
365 if [ -n "${snapfile}" ]; then
366 # it was a loop device, user confused
371 log_warning_msg "Impossible to include the ${snap_label} Snapshot"
375 echo "export ${snap_type}SNAP="${snap_mount}":${snapdev}:${snapfile}" >> /etc/casper.conf # for resync on reboot/halt
383 modprobe "${MP_QUIET}" -b unionfs
385 # run-init can't deal with images in a subdir, but we're going to
386 # move all of these away before it runs anyway. No, we're not,
387 # put them in / since move-mounting them into / breaks mono and
392 # Let's just mount the read-only file systems first
395 if [ "${NETBOOT}" == "nfs" ] ; then
396 roopt="nfsro" # go aroung a bug in nfs-unionfs locking
402 for image_type in "ext2" "squashfs" "dir" ; do
403 for image in "${image_directory}"/*."${image_type}"; do
404 imagename=$(basename "${image}")
405 if [ -d "${image}" ]; then
406 # it is a plain directory: do nothing
407 rofsstring="${image}=${roopt}:${rofsstring}"
408 rofslist="${image} ${rofslist}"
409 elif [ -f "${image}" ]; then
410 backdev=$(get_backing_device "$image")
411 fstype=$(get_fstype "${backdev}")
412 if [ "${fstype}" = "unknown" ]; then
413 panic "Unknown file system type on ${backdev} (${image})"
415 mkdir -p "${croot}/${imagename}"
416 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}"
420 rofsstring=${rofsstring%:}
426 # Looking for "${root_persistence}" device or file
427 if [ -n "${PERSISTENT}" ]; then
428 cowprobe=$(find_cow_device "${root_persistence}")
429 if [ -b "${cowprobe}" ]; then
430 cowdevice=${cowprobe}
431 cow_fstype=$(get_fstype "${cowprobe}")
433 [ "$quiet" != "y" ] && log_warning_msg "Unable to find the persistent medium"
437 mount ${cowdevice} -t ${cow_fstype} -o rw /cow || panic "Can not mount $cowdevice on /cow"
439 mount -t unionfs -o dirs=/cow=rw:$rofsstring unionfs "$rootmnt" || panic "Unionfs mount failed"
441 # Adding other custom mounts
442 if [ -n "${PERSISTENT}" ]; then
443 # directly mount /home
444 # FIXME: add a custom mounts configurable system
445 homecow=$(find_cow_device "${home_persistence}" )
446 if [ -b "${homecow}" ]; then
447 mount -t $(get_fstype "${homecow}") -o rw "${homecow}" "${rootmnt}/home"
448 export HOMEMOUNTED=1 # used to proper calculate free space in do_snap_copy()
450 [ "$quiet" != "y" ] && log_warning_msg "Unable to find the persistent home medium"
452 # Look for other snapshots to copy in
453 try_snap "${root_snapshot_label}" "${rootmnt}" "ROOT"
454 try_snap "${home_snapshot_label}" "${rootmnt}/home" "HOME"
457 if [ -n "${SHOWMOUNTS}" ]; then
458 for d in ${rofslist}; do
459 mkdir -p "${rootmnt}/casper/${d##*/}"
461 *.dir) # do nothing # mount -o bind "${d}" "${rootmnt}/casper/${d##*/}"
463 *) mount -o move "${d}" "${rootmnt}/casper/${d##*/}"
469 # shows cow fs on /cow for use by casper-snapshot
470 mkdir -p "${rootmnt}/cow"
471 mount -o bind /cow "${rootmnt}/cow"
478 if [ -z "${devname}" ]; then
479 devname=$(sys2dev "${sysdev}")
481 fstype=$(get_fstype "${devname}")
482 if is_supported_fs ${fstype}; then
483 mount -t ${fstype} -o ro "${devname}" $mountpoint || continue
484 if is_casper_path $mountpoint; then
495 # first look at the one specified in the command line
496 if [ ! -z "${LIVEMEDIA}" ]; then
497 if check_dev "null" "${LIVEMEDIA}"; then
501 # or do the scan of block devices
502 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram); do
503 devname=$(sys2dev "${sysblock}")
504 fstype=$(get_fstype "${devname}")
505 if /lib/udev/cdrom_id ${devname} > /dev/null; then
506 if check_dev "null" "${devname}" ; then
509 elif is_nice_device "${sysblock}" ; then
510 for dev in $(subdevices "${sysblock}"); do
511 if check_dev "${dev}" ; then
515 elif [ "${fstype}" = "squashfs" -o \
516 "${fstype}" = "ext3" ] -o \
517 "${fstype}" = "ext2" ]; then
518 # This is an ugly hack situation, the block device has
519 # an image directly on it. It's hopefully
520 # casper, so take it and run with it.
521 ln -s "${devname}" "${devname}.${fstype}"
522 echo "${devname}.${fstype}"
530 if [ -x /sbin/usplash_write ]; then
531 /sbin/usplash_write "PULSATE"
535 set_usplash_timeout() {
536 if [ -x /sbin/usplash_write ]; then
537 /sbin/usplash_write "TIMEOUT 120"
550 [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/casper-premount"
552 run_scripts /scripts/casper-premount
553 [ "$quiet" != "y" ] && log_end_msg
555 # Needed here too because some things (*cough* udev *cough*)
556 # changes the timeout
560 if [ ! -z "${NETBOOT}" ]; then
561 if do_netmount ; then
562 livefs_root="${mountpoint}"
564 panic "Unable to find a the network rootfs live file system"
567 # Scan local devices for the image
568 for i in 0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13; do
569 livefs_root=$(find_livefs)
570 if [ ! -z "${livefs_root}" ]; then
577 if [ -z "${livefs_root}" ]; then
578 panic "Unable to find a medium containing a live file system"
581 if [ ! -z "${TORAM}" ]; then
582 copy_live_to "${livefs_root}" "ram"
583 elif [ ! -z "${TODISK}" ]; then
584 copy_live_to "${livefs_root}" "${TODISK}"
587 mount_images_in_directory "${livefs_root}" "${rootmnt}"
591 maybe_break casper-bottom
592 [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/casper-bottom"
595 run_scripts /scripts/casper-bottom
596 [ "$quiet" != "y" ] && log_end_msg
600 cp casper.log "${rootmnt}/var/log/"