5 export PATH=/root/usr/bin:/root/usr/sbin:/root/bin:/root/sbin:/usr/bin:/usr/sbin:/bin:/sbin
9 # Will be mounted if found as copy on write instead of tmpfs
10 root_persistence="casper-rw"
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"
18 #overlay_method=unionfs
19 #if [ "${DPKG_ARCH}" = "ia64" ] || [ "${DPKG_ARCH}" = "hppa" ] || [ "${DPKG_ARCH}" = "sparc" ]; then
20 # overlay_method=devmapper
24 USERFULLNAME="Live session user"
29 [ -f /etc/casper.conf ] && . /etc/casper.conf
31 export USERNAME USERFULLNAME HOST
33 # looking for casper specifics options as kernel parameters
34 for x in $(cat /proc/cmdline); do
37 export NETBOOT=${x#netboot=} ;;
43 export PERSISTENT=1 ;;
45 export CASPERGETTY=1 ;;
49 # sort of compatibility with netboot.h from linux docs
50 if [ -z "${NETBOOT}" ]; then
51 if [ "${ROOT}" == "/dev/nfs" ]; then
54 elif [ "${ROOT}" == "/dev/cifs" ]; then
62 if [ -d "$path/casper" ]; then
63 if [ "$(echo $path/casper/*.cloop)" != "$path/casper/*.cloop" ] ||
64 [ "$(echo $path/casper/*.squashfs)" != "$path/casper/*.squashfs" ] ||
65 [ "$(echo $path/casper/*.ext2)" != "$path/casper/*.ext2" ] ||
66 [ "$(echo $path/casper/*.dir)" != "$path/casper/*.dir" ]; then
76 for dev in "${sysblock}" "${sysblock}"/*; do
77 if [ -e "${dev}/dev" ]; then
84 get_backing_device() {
87 echo $(setup_loop "$1" "cloop" "/sys/block/cloop*")
90 echo $(setup_loop "$1" "loop" "/sys/block/loop*")
96 panic "Unrecognized casper filesystem: $1"
101 match_files_in_dir() {
102 # Does any files match pattern $1 ?
105 if [ "$(echo $pattern)" != "$pattern" ]; then
111 mount_images_in_directory() {
114 if match_files_in_dir "$directory/casper/*.cloop"; then
115 # Let's hope there's just one matching *.cloop... FIXME
116 setup_devmapper $(get_backing_device "$directory/casper/*.cloop") "$rootmnt"
117 elif match_files_in_dir "$directory/casper/*.squashfs" ||
118 match_files_in_dir "$directory/casper/*.ext2" ||
119 match_files_in_dir "$directory/casper/*.dir"; then
120 setup_unionfs "$directory/casper" "$rootmnt"
128 echo "/dev/$(udevinfo -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
136 modprobe -qb "$module"
139 for loopdev in $pattern; do
140 if [ "$(cat $loopdev/size)" -eq 0 ]; then
141 dev=$(sys2dev "${loopdev}")
142 losetup "$dev" "$fspath"
147 panic "No loop devices available"
151 #FIXME# one use of this function expects "unknown" another does not!
156 if [ "$FSTYPE" != "unknown" ]; then
160 /lib/udev/vol_id -t $1 2>/dev/null
169 COW_NAME="casper-cow"
171 BACKING_FILE_SIZE=$(blockdev --getsize "$backdev")
172 MAX_COW_SIZE=$(blockdev --getsize "$COW_DEVICE")
173 CHUNK_SIZE=8 # sectors
175 if [ -z "$COW_SIZE" -o "$COW_SIZE" -gt "$MAX_COW_SIZE" ]; then
176 COW_SIZE=$MAX_COW_SIZE
179 echo "0 $COW_SIZE linear $COW_DEVICE 0" | dmsetup create $COW_NAME
181 echo "0 $BACKING_FILE_SIZE snapshot $backdev /dev/mapper/$COW_NAME p $CHUNK_SIZE" | \
182 dmsetup create casper-snapshot
183 if [ "$(get_fstype $backdev)" = "unknown" ]; then
184 panic "Unknown file system type on $backdev"
186 mount -t $(get_fstype "$backdev") /dev/mapper/casper-snapshot $rootmnt || panic "Can not mount /dev/mapper/casper/snapshot on $rootmnt"
188 mkdir -p "$rootmnt/rofs"
189 echo "0 $BACKING_FILE_SIZE linear $backdev 0" | dmsetup create casper-backing
190 mount -t $(get_fstype "$backdev") /dev/mapper/casper-backing "$rootmnt/rofs"
195 if grep -q "^$device " /proc/mounts; then
196 grep "^$device " /proc/mounts | read d mountpoint rest
206 if [ ! -z "${2}" ] ; then
207 # This will enable future rampersistence, todo yet
211 copyto="${copyfrom}_swap"
215 size=$(du -ks ${copyfrom} | cut -f1)
216 size=$(expr ${size} + ${size}/20 ) # Fixme: 5% more to be sure
217 needed_space=$(expr ${size} * 1024)
218 freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( cat /proc/meminfo | grep Cached | head -n 1 | awk '/Cached/{print $2}' - ))
220 if [ ! ${freespace} -lt ${needed_space} ] ; then
221 [ "$quiet" != "y" ] && log_begin_msg "Not enough free memory to copy to ram"
222 [ "$quiet" != "y" ] && log_end_msg
225 [ "$quiet" != "y" ] && log_begin_msg "Copying live media to ram..."
227 mount -t tmpfs -o size=${size}k /dev/shm ${copyto}
228 cp -a ${copyfrom}/* ${copyto} # "cp -a" from busybox also copies hidden files
230 if [ "${moveit}" == "True" ]; then
231 mount -r -o move ${copyto} ${copyfrom}
234 [ "$quiet" != "y" ] && log_end_msg
240 cow_backing="/${pers_label}-backing"
241 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop); do
242 for dev in $(subdevices "${sysblock}"); do
243 devname=$(sys2dev "${dev}")
244 if [ "$(/lib/udev/vol_id -l $devname 2>/dev/null)" = "${pers_label}" ]; then
247 elif [ "$(get_fstype ${devname})" = "vfat" ]; then
248 mkdir -p "${cow_backing}"
249 if where_is_mounted ${devname} > /dev/null; then
250 mount -o remount,rw ${devname} $(where_is_mounted ${devname}) || panic "Remounting failed"
251 mount -o bind $(where_is_mounted ${devname}) ${cow_backing} || panic "Cannot bind-mount"
253 mount -t $(get_fstype "${devname}") -o rw "${devname}" ${cow_backing} || panic "Cannot mount $devname on /cow-backing"
256 if [ -e "${cow_backing}/${pers_label}" ]; then
257 echo $(setup_loop "${cow_backing}/${pers_label}" "loop" "/sys/block/loop*")
260 umount ${cow_backing}
270 modprobe -q af_packet # For DHCP
272 ipconfig ${DEVICE} /tmp/net-${DEVICE}.conf
274 if [ "${NFSROOT}" = "auto" ]; then
275 NFSROOT=${ROOTSERVER}:${ROOTPATH}
278 [ "$quiet" != "y" ] && log_begin_msg "Trying netboot from ${NFSROOT}"
280 if [ "${NETBOOT}" != "nfs" ] && do_cifsmount ; then
282 elif do_nfsmount ; then
288 [ "$quiet" != "y" ] && log_end_msg
295 if [ -z "${NFSOPTS}" ]; then
299 [ "$quiet" != "y" ] && log_begin_msg "Trying nfsmount -o nolock -o ro ${NFSOPTS} ${NFSROOT} ${mountpoint}"
300 # FIXME: This for loop is an ugly HACK round an nfs bug
301 for i in 0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13; do
302 nfsmount -o nolock -o ro ${NFSOPTS} "${NFSROOT}" "${mountpoint}" && rc=0 && break
310 if [ -x "/sbin/mount.cifs" ]; then
311 if [ -z "${NFSOPTS}" ]; then
312 CIFSOPTS="-ouser=root,password="
314 CIFSOPTS="${NFSOPTS}"
317 [ "$quiet" != "y" ] && log_begin_msg "Trying mount.cifs ${NFSROOT} ${mountpoint} ${CIFSOPTS}"
320 if mount.cifs "${NFSROOT}" "${mountpoint}" "${CIFSOPTS}" ; then
333 # run-init can't deal with this, but we're going to move all of these
334 # away before it runs anyway.
337 # Let's just mount the read-only file systems first
340 if [ "${NETBOOT}" == "nfs" ] ; then
341 roopt="nfsro" # go aroung a bug in nfs-unionfs locking
347 for image_type in "ext2" "squashfs" "dir" ; do
348 for image in "${image_directory}"/*."${image_type}"; do
349 imagename=$(basename "${image}")
350 if [ -d "${image}" ]; then
351 # it is a plain directory: do nothing
352 rofsstring="${image}=${roopt}:${rofsstring}"
353 rofslist="${image} ${rofslist}"
354 elif [ -f "${image}" ]; then
355 backdev=$(get_backing_device "$image")
356 fstype=$(get_fstype "${backdev}")
357 if [ "${fstype}" = "unknown" ]; then
358 panic "Unknown file system type on ${backdev} (${image})"
360 mkdir -p "${croot}/${imagename}"
361 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}"
365 rofsstring=${rofsstring%:}
371 # Looking for "${root_persistence}" device or file
372 if [ ! -z "${PERSISTENT}" ]; then
373 cowprobe=$(find_cow_device "${root_persistence}")
374 if [ -b "${cowprobe}" ]; then
375 cowdevice=${cowprobe}
376 cow_fstype=$(get_fstype "${cowprobe}")
378 [ "$quiet" != "y" ] && log_begin_msg "Unable to find the persistent medium"
382 mount ${cowdevice} -t ${cow_fstype} -o rw /cow || panic "Can not mount $cowdevice on /cow"
384 mount -t unionfs -o dirs=/cow=rw:$rofsstring unionfs "$rootmnt" || panic "Unionfs mount failed"
386 for d in ${rofslist}; do
387 mkdir -p "${rootmnt}/casper/${d##*/}"
389 *.dir) mount -o bind "${d}" "${rootmnt}/casper/${d##*/}" ;;
390 *) mount -o move "${d}" "${rootmnt}/casper/${d##*/}" ;;
394 # Adding other custom mounts
395 if [ ! -z "${PERSISTENT}" ]; then
396 homecow=$(find_cow_device "${home_persistence}" )
397 if [ -b "${homecow}" ]; then
398 mount ${homecow} -t $(get_fstype "${homecow}") -o rw "${rootmnt}/home"
400 [ "$quiet" != "y" ] && log_begin_msg "Unable to find the persistent home medium"
404 if [ ! -z "${SHOWCOW}" ]; then
405 mkdir -p "$rootmnt/cow"
406 mount -o bind /cow "$rootmnt/cow"
411 sysfs_path="${1#/sys}"
412 if /lib/udev/path_id "${sysfs_path}" | grep -Eq "ID_PATH=(usb|pci-[^-]*-usb)"; then
420 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram); do
421 devname=$(sys2dev "${sysblock}")
422 fstype=$(get_fstype "${devname}")
423 if /lib/udev/cdrom_id ${devname} > /dev/null; then
424 mount -t ${fstype} -o ro "$devname" $mountpoint || continue
425 if is_casper_path $mountpoint; then
431 elif is_usb_device "$sysblock"; then
432 for dev in $(subdevices "${sysblock}"); do
433 devname=$(sys2dev "${dev}")
434 fstype=$(get_fstype "${devname}")
437 mount -t ${fstype} -o ro "${devname}" $mountpoint || continue
438 if is_casper_path $mountpoint; then
447 elif [ "${fstype}" = "squashfs" || \
448 "${fstype}" = "ext2" ]; then
450 # This is an ugly hack situation, the block device has
451 # an image directly on it. It's hopefully
452 # casper, so take it and run with it.
454 ln -s "${devname}" "${devname}.${fstype}"
455 echo "${devname}.${fstype}"
461 set_usplash_timeout() {
462 if [ -x /sbin/usplash_write ]; then
463 /sbin/usplash_write "TIMEOUT 120"
474 [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/casper-premount"
475 run_scripts /scripts/casper-premount
476 [ "$quiet" != "y" ] && log_end_msg
478 # Needed here too because some things (*cough* udev *cough*)
479 # changes the timeout
483 if [ ! -z "${NETBOOT}" ]; then
484 if do_netmount ; then
485 livefs_root="${mountpoint}"
487 panic "Unable to find a the network rootfs live file system"
490 # Scan local devices for the image
491 for i in 0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13; do
492 livefs_root=$(find_livefs)
493 if [ "${livefs_root}" ]; then
498 if [ "$?" -gt 0 ]; then
499 panic "Unable to find a medium containing a live file system"
502 if [ ! -z "${TORAM}" ]; then
503 copy_to_ram "${livefs_root}"
508 mount_images_in_directory "$livefs_root" "$rootmnt"
512 maybe_break casper-bottom
513 [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/casper-bottom"
515 run_scripts /scripts/casper-bottom
516 [ "$quiet" != "y" ] && log_end_msg
520 cp casper.log "${rootmnt}/var/log/"