5 export PATH=/usr/bin:/usr/sbin:/bin:/sbin
10 root_persistence="casper-rw"
11 home_persistence="home-rw"
12 root_snapshot_label="casper-sn"
13 home_snapshot_label="home-sn"
16 USERFULLNAME="Live session user"
23 [ -f /etc/casper.conf ] && . /etc/casper.conf
24 export USERNAME USERFULLNAME HOST BUILD_SYSTEM
26 . /scripts/casper-helpers
28 if [ ! -f /casper.vars ]; then
33 for x in $(cat /proc/cmdline); do
36 export SHOWMOUNTS='Yes' ;;
38 export PERSISTENT="Yes" ;;
40 export PERSISTENT="" ;;
42 export UNIONFS="${x#union=}";;
45 if [ "${STATICIP}" = "" ]; then
52 LIVE_MEDIA_PATH="${x#live-media-path=}"
53 export LIVE_MEDIA_PATH
54 echo "export LIVE_MEDIA_PATH=\"$LIVE_MEDIA_PATH\"" >> /etc/casper.conf ;;
57 if [ "${UNIONFS}" = "" ]; then
64 if [ -d "$path/$LIVE_MEDIA_PATH" ]; then
65 if [ "$(echo $path/$LIVE_MEDIA_PATH/*.squashfs)" != "$path/$LIVE_MEDIA_PATH/*.squashfs" ] ||
66 [ "$(echo $path/$LIVE_MEDIA_PATH/*.ext2)" != "$path/$LIVE_MEDIA_PATH/*.ext2" ] ||
67 [ "$(echo $path/$LIVE_MEDIA_PATH/*.dir)" != "$path/$LIVE_MEDIA_PATH/*.dir" ]; then
75 if [ "$IGNORE_UUID" ] || [ ! -e /conf/uuid.conf ]; then
79 uuid="$(cat /conf/uuid.conf)"
80 for try_uuid_file in "$path/.disk/casper-uuid"*; do
81 [ -e "$try_uuid_file" ] || continue
82 try_uuid="$(cat "$try_uuid_file")"
83 if [ "$uuid" = "$try_uuid" ]; then
90 get_backing_device() {
93 echo $(setup_loop "$1" "loop" "/sys/block/loop*")
99 panic "Unrecognized casper filesystem: $1"
104 match_files_in_dir() {
105 # Does any files match pattern $1 ?
108 if [ "$(echo $pattern)" != "$pattern" ]; then
114 mount_images_in_directory() {
117 if match_files_in_dir "$directory/$LIVE_MEDIA_PATH/*.squashfs" ||
118 match_files_in_dir "$directory/$LIVE_MEDIA_PATH/*.ext2" ||
119 match_files_in_dir "$directory/$LIVE_MEDIA_PATH/*.dir"; then
120 setup_unionfs "$directory/$LIVE_MEDIA_PATH" "$rootmnt"
127 sysfs_path="${1#/sys}"
128 if /lib/udev/path_id "${sysfs_path}" | grep -E -q "ID_PATH=(usb|pci-[^-]*-(ide|scsi|usb)|platform-orion-ehci|platform-mmc|platform-mxsdhci)"; then
137 copyto="${copyfrom}_swap"
139 size=$(fs_size "" ${copyfrom} "used")
141 if [ "${copytodev}" = "ram" ]; then
143 freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( cat /proc/meminfo | grep Cached | head -n 1 | awk '/Cached/{print $2}' - ) )
144 mount_options="-o size=${size}k"
149 # it should be a writable block device
150 if [ -b "${copytodev}" ]; then
153 fstype=$(get_fstype "${dev}")
154 freespace=$(fs_size "${dev}")
156 [ "$quiet" != "y" ] && log_warning_msg "${copytodev} is not a block device."
160 if [ "${freespace}" -lt "${size}" ] ; then
161 [ "$quiet" != "y" ] && log_warning_msg "Not enough free ${free_string} (${freespace}k > ${size}k) to copy live media in ${copytodev}."
167 echo "mount -t ${fstype} ${mount_options} ${dev} ${copyto}"
168 mount -t "${fstype}" ${mount_options} "${dev}" "${copyto}"
169 cp -a ${copyfrom}/* ${copyto} # "cp -a" from busybox also copies hidden files
171 mount -r -o move ${copyto} ${copyfrom}
179 modprobe "${MP_QUIET}" af_packet # For DHCP
181 /sbin/udevadm trigger
184 ipconfig ${DEVICE} /tmp/net-${DEVICE}.conf | tee /netboot.config
186 if [ "${NFSROOT}" = "auto" ]; then
187 NFSROOT=${ROOTSERVER}:${ROOTPATH}
190 [ "$quiet" != "y" ] && log_begin_msg "Trying netboot from ${NFSROOT}"
192 if [ "${NETBOOT}" != "nfs" ] && do_cifsmount ; then
194 elif do_nfsmount ; then
200 [ "$quiet" != "y" ] && log_end_msg
206 modprobe "${MP_QUIET}" nfs
207 if [ -z "${NFSOPTS}" ]; then
211 [ "$quiet" != "y" ] && log_begin_msg "Trying nfsmount -o nolock -o ro ${NFSOPTS} ${NFSROOT} ${mountpoint}"
212 # FIXME: This while loop is an ugly HACK round an nfs bug
214 while [ "$i" -lt 60 ]; do
215 nfsmount -o nolock -o ro ${NFSOPTS} "${NFSROOT}" "${mountpoint}" && rc=0 && break
224 if [ -x "/sbin/mount.cifs" ]; then
225 if [ -z "${NFSOPTS}" ]; then
226 CIFSOPTS="-ouser=root,password="
228 CIFSOPTS="${NFSOPTS}"
231 [ "$quiet" != "y" ] && log_begin_msg "Trying mount.cifs ${NFSROOT} ${mountpoint} ${CIFSOPTS}"
232 modprobe "${MP_QUIET}" cifs
234 if mount.cifs "${NFSROOT}" "${mountpoint}" "${CIFSOPTS}" ; then
247 size=$(fs_size "${fromdev}" "" "used")
249 if [ -b "${fromdev}" ]; then
251 if [ -n "${HOMEMOUNTED}" -a "${snap_type}" = "HOME" ]; then
252 todev=$(cat /proc/mounts | grep -s " $(base_path ${todir}) " | awk '{print $1}' )
253 freespace=$(df -k | grep -s ${todev} | awk '{print $4}')
255 freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( cat /proc/meminfo | grep Cached | head -n 1 | awk '/Cached/{print $2}' - ))
258 tomount="/mnt/tmpsnap"
259 if [ ! -d "${tomount}" ] ; then
260 mkdir -p "${tomount}"
263 fstype=$(get_fstype "${fromdev}")
264 if [ -n "${fstype}" ]; then
266 mount -t "${fstype}" -o ro,noatime "${fromdev}" "${tomount}"
267 cp -a "${tomount}"/* ${todir}
270 log_warning_msg "Unrecognized fstype: ${fstype} on ${fromdev}:${snap_type}"
274 if echo ${fromdev} | grep -qs loop; then
275 losetup -d "${fromdev}"
280 [ "$quiet" != "y" ] && log_warning_msg "Unable to find the snapshot ${snap_type} medium"
286 # Look for $snap_label.* in block devices and copy the contents to $snap_mount
287 # and remember the device and filename for resync on exit in casper.init
293 snapdata=$(find_files "${snap_label}.squashfs ${snap_label}.cpio.gz ${snap_label}.ext2")
294 if [ ! -z "${snapdata}" ]; then
295 snapdev="$(echo ${snapdata} | cut -f1 -d ' ')"
296 snapback="$(echo ${snapdata} | cut -f2 -d ' ')"
297 snapfile="$(echo ${snapdata} | cut -f3 -d ' ')"
298 if echo "${snapfile}" | grep -qs '\(squashfs\|ext2\)'; then
299 # squashfs or ext2 snapshot
300 dev=$(get_backing_device "${snapback}/${snapfile}")
301 if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"; then
302 log_warning_msg "Impossible to include the ${snapfile} Snapshot"
307 # Unfortunately klibc's cpio is incompatible with the rest of
308 # the world; everything else requires -u -d, while klibc doesn't
309 # implement them. Try to detect whether it's in use.
310 cpiopath="$(which cpio)" || true
311 if [ "$cpiopath" ] && grep -aq /lib/klibc "$cpiopath"; then
316 if ! (cd "${snap_mount}" && zcat "${snapback}/${snapfile}" | cpio -i $cpioargs 2>/dev/null) ; then
317 log_warning_msg "Impossible to include the ${snapfile} Snapshot"
323 dev=$(find_cow_device "${snap_label}")
324 if [ -b "${dev}" ]; then
325 if echo "${dev}" | grep -qs loop; then
326 # strange things happens, user confused?
327 snaploop=$( losetup ${dev} | awk '{print $3}' | tr -d '()' )
328 snapfile=$(basename ${snaploop})
329 snapdev=$(cat /proc/mounts | awk '{print $2,$1}' | grep -es "^$( dirname ${snaploop} )" | cut -f2 -d ' ')
333 if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}" ; then
334 log_warning_msg "Impossible to include the ${snap_label} Snapshot"
337 if [ -n "${snapfile}" ]; then
338 # it was a loop device, user confused
343 log_warning_msg "Impossible to include the ${snap_label} Snapshot"
347 echo "export ${snap_type}SNAP="/cow${snap_mount#$rootmnt}":${snapdev}:${snapfile}" >> /etc/casper.conf # for resync on reboot/halt
357 modprobe "${MP_QUIET}" -b ${UNIONFS} || true
358 if ! cut -f2 /proc/filesystems | grep -q "^${UNIONFS}\$" && \
359 [ -x /bin/unionfs-fuse ]; then
360 UNIONFS="unionfs-fuse"
365 # run-init can't deal with images in a subdir, but we're going to
366 # move all of these away before it runs anyway. No, we're not,
367 # put them in / since move-mounting them into / breaks mono and
372 # Let's just mount the read-only file systems first
375 if [ "${NETBOOT}" = "nfs" ] ; then
376 roopt="nfsro" # work around a bug in nfs-unionfs locking
377 elif [ "${UNIONFS}" = "aufs" ]; then
379 elif [ "${UNIONFS}" = "unionfs-fuse" ]; then
386 for image_type in "ext2" "squashfs" "dir" ; do
387 for image in "${image_directory}"/*."${image_type}"; do
388 imagename=$(basename "${image}")
389 if [ -d "${image}" ]; then
390 # it is a plain directory: do nothing
391 rofsstring="${image}=${roopt}:${rofsstring}"
392 rofslist="${image} ${rofslist}"
393 elif [ -f "${image}" ]; then
394 backdev=$(get_backing_device "$image")
395 fstype=$(get_fstype "${backdev}")
396 if [ "${fstype}" = "unknown" ]; then
397 panic "Unknown file system type on ${backdev} (${image})"
399 mkdir -p "${croot}/${imagename}"
400 mount -t "${fstype}" -o ro,noatime "${backdev}" "${croot}/${imagename}" || panic "Can not mount $backdev ($image) on ${croot}/${imagename}" && rofsstring="${croot}/${imagename}=${roopt}:${rofsstring}" && rofslist="${croot}/${imagename} ${rofslist}"
404 rofsstring=${rofsstring%:}
409 cow_mountopt="rw,noatime,mode=755"
411 # Looking for "${root_persistence}" device or file
412 if [ -n "${PERSISTENT}" ]; then
413 cowprobe=$(find_cow_device "${root_persistence}")
414 if [ -b "${cowprobe}" ]; then
415 cowdevice=${cowprobe}
416 cow_fstype=$(get_fstype "${cowprobe}")
417 cow_mountopt="rw,noatime"
419 [ "$quiet" != "y" ] && log_warning_msg "Unable to find the persistent medium"
423 mount -t ${cow_fstype} -o ${cow_mountopt} ${cowdevice} /cow || panic "Can not mount $cowdevice on /cow"
427 (ulimit -n 16384; unionfs-fuse -o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid /cow=RW:$rofsstring "$rootmnt" || panic "${UNIONFS} mount failed")
428 mkdir -p /dev/.initramfs/varrun
429 pidof unionfs-fuse >> /dev/.initramfs/varrun/sendsigs.omit || true
432 mount -t ${UNIONFS} -o noatime,dirs=/cow=rw:$rofsstring ${UNIONFS} "$rootmnt" || panic "${UNIONFS} mount failed"
436 # Adding other custom mounts
437 if [ -n "${PERSISTENT}" ]; then
438 # directly mount /home
439 # FIXME: add a custom mounts configurable system
440 homecow=$(find_cow_device "${home_persistence}" )
441 if [ -b "${homecow}" ]; then
442 mount -t $(get_fstype "${homecow}") -o rw,noatime "${homecow}" "${rootmnt}/home"
443 export HOMEMOUNTED=1 # used to proper calculate free space in do_snap_copy()
445 [ "$quiet" != "y" ] && log_warning_msg "Unable to find the persistent home medium"
447 # Look for other snapshots to copy in
448 try_snap "${root_snapshot_label}" "${rootmnt}" "ROOT"
449 try_snap "${home_snapshot_label}" "${rootmnt}/home" "HOME"
452 if [ -n "${SHOWMOUNTS}" ]; then
453 for d in ${rofslist}; do
454 mkdir -p "${rootmnt}/${LIVE_MEDIA_PATH}/${d##*/}"
456 *.dir) # do nothing # mount -o bind "${d}" "${rootmnt}/${LIVE_MEDIA_PATH}/${d##*/}"
459 if [ "${UNIONFS}" = unionfs-fuse ]; then
460 mount -o bind "${d}" "${rootmnt}/${LIVE_MEDIA_PATH}/${d##*/}"
462 mount -o move "${d}" "${rootmnt}/${LIVE_MEDIA_PATH}/${d##*/}"
467 # shows cow fs on /cow for use by casper-snapshot
468 mkdir -p "${rootmnt}/cow"
469 mount -o bind /cow "${rootmnt}/cow"
472 # move the first mount; no head in busybox-initramfs
473 for d in $(mount -t squashfs | cut -d\ -f 3); do
474 mkdir -p "${rootmnt}/rofs"
475 if [ "${UNIONFS}" = unionfs-fuse ]; then
476 mount -o bind "${d}" "${rootmnt}/rofs"
478 mount -o move "${d}" "${rootmnt}/rofs"
488 skip_uuid_check="${3}"
489 if [ -z "${devname}" ]; then
490 devname=$(sys2dev "${sysdev}")
493 if [ -d "${devname}" ]; then
494 mount -o bind "${devname}" $mountpoint || continue
495 if is_casper_path $mountpoint; then
502 [ -e "$devname" ] || continue
504 if [ -n "${LIVEMEDIA_OFFSET}" ]; then
505 loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVEMEDIA_OFFSET}")
506 devname="${loopdevname}"
509 fstype=$(get_fstype "${devname}")
510 if is_supported_fs ${fstype}; then
511 devuid=$(blkid -o value -s UUID "$devname")
512 [ -n "$devuid" ] && grep -qs "\<$devuid\>" $tried && continue
513 mount -t ${fstype} -o ro,noatime "${devname}" $mountpoint || continue
514 [ -n "$devuid" ] && echo "$devuid" >> $tried
515 if is_casper_path $mountpoint && \
516 ([ "$skip_uuid_check" ] || matches_uuid $mountpoint); then
524 if [ -n "${LIVEMEDIA_OFFSET}" ]; then
525 losetup -d "${loopdevname}"
532 # first look at the one specified in the command line
533 if [ ! -z "${LIVEMEDIA}" ]; then
534 if check_dev "null" "${LIVEMEDIA}" "skip_uuid_check"; then
538 # don't start autodetection before timeout has expired
539 if [ -n "${LIVEMEDIA_TIMEOUT}" ]; then
540 if [ "${timeout}" -lt "${LIVEMEDIA_TIMEOUT}" ]; then
544 # or do the scan of block devices
545 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|fd)"); do
546 devname=$(sys2dev "${sysblock}")
547 [ -e "$devname" ] || continue
548 fstype=$(get_fstype "${devname}")
549 if /lib/udev/cdrom_id ${devname} > /dev/null; then
550 if check_dev "null" "${devname}" ; then
553 elif is_nice_device "${sysblock}" ; then
554 for dev in $(subdevices "${sysblock}"); do
555 if check_dev "${dev}" ; then
559 elif [ "${fstype}" = "squashfs" -o \
560 "${fstype}" = "ext4" -o \
561 "${fstype}" = "ext3" -o \
562 "${fstype}" = "ext2" ]; then
563 # This is an ugly hack situation, the block device has
564 # an image directly on it. It's hopefully
565 # casper, so take it and run with it.
566 ln -s "${devname}" "${devname}.${fstype}"
567 echo "${devname}.${fstype}"
574 set_usplash_timeout() {
575 if [ -x /sbin/usplash_write ]; then
576 /sbin/usplash_write "TIMEOUT 120"
585 tail -f casper.log >&7 &
591 [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/casper-premount"
592 run_scripts /scripts/casper-premount
593 [ "$quiet" != "y" ] && log_end_msg
595 # Needed here too because some things (*cough* udev *cough*)
596 # changes the timeout
600 if [ ! -z "${NETBOOT}" ]; then
601 if do_netmount ; then
602 livefs_root="${mountpoint}"
604 panic "Unable to find a live file system on the network"
607 # Scan local devices for the image
609 while [ "$i" -lt 60 ]; do
610 livefs_root=$(find_livefs $i)
611 if [ "${livefs_root}" ]; then
619 if [ -z "${livefs_root}" ]; then
620 panic "Unable to find a medium containing a live file system"
623 if [ "${TORAM}" ]; then
625 elif [ "${TODISK}" ]; then
626 live_dest="${TODISK}"
628 if [ "${live_dest}" ]; then
629 log_begin_msg "Copying live_media to ${live_dest}"
630 copy_live_to "${livefs_root}" "${live_dest}"
634 mount_images_in_directory "${livefs_root}" "${rootmnt}"
638 # unionfs-fuse needs /dev to be bind-mounted for the duration of
639 # casper-bottom; udev's init script will take care of things after that
640 if [ "${UNIONFS}" = unionfs-fuse ]; then
641 mount -n -o bind /dev "${rootmnt}/dev"
644 maybe_break casper-bottom
645 [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/casper-bottom"
647 run_scripts /scripts/casper-bottom
648 [ "$quiet" != "y" ] && log_end_msg
650 if [ "${UNIONFS}" = unionfs-fuse ]; then
651 umount "${rootmnt}/dev"
657 cp casper.log "${rootmnt}/var/log/"
658 if [ -f /etc/casper.conf ]; then
659 cp /etc/casper.conf "${rootmnt}/etc/"