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"
13 USERFULLNAME="Live session user"
19 [ -f /etc/casper.conf ] && . /etc/casper.conf
21 export USERNAME USERFULLNAME HOST BUILD_SYSTEM
23 if [ "${BUILD_SYSTEM}" == "Ubuntu" ]; then
29 # looking for casper specifics options as kernel parameters
30 for x in $(cat /proc/cmdline); do
33 export USERFULLNAME=${x#userfullname=}
34 export CASPERCONF="changed"
37 export HOST=${x#host=}
38 export CASPERCONF="changed"
41 export USERNAME=${x#username=}
42 export CASPERCONF="changed"
45 export NETBOOT=${x#netboot=} ;;
51 export PERSISTENT=1 ;;
53 STATICIP=${x#staticip=}
54 if [ "${STATICIP}" == "" ]; then
59 export CASPERGETTY=1 ;;
63 # sort of compatibility with netboot.h from linux docs
64 if [ -z "${NETBOOT}" ]; then
65 if [ "${ROOT}" == "/dev/nfs" ]; then
68 elif [ "${ROOT}" == "/dev/cifs" ]; then
76 if [ -d "$path/casper" ]; then
77 if [ "$(echo $path/casper/*.cloop)" != "$path/casper/*.cloop" ] ||
78 [ "$(echo $path/casper/*.squashfs)" != "$path/casper/*.squashfs" ] ||
79 [ "$(echo $path/casper/*.ext2)" != "$path/casper/*.ext2" ] ||
80 [ "$(echo $path/casper/*.dir)" != "$path/casper/*.dir" ]; then
90 for dev in "${sysblock}" "${sysblock}"/*; do
91 if [ -e "${dev}/dev" ]; then
98 get_backing_device() {
101 echo $(setup_loop "$1" "cloop" "/sys/block/cloop*")
104 echo $(setup_loop "$1" "loop" "/sys/block/loop*")
110 panic "Unrecognized casper filesystem: $1"
115 match_files_in_dir() {
116 # Does any files match pattern $1 ?
119 if [ "$(echo $pattern)" != "$pattern" ]; then
125 mount_images_in_directory() {
128 if match_files_in_dir "$directory/casper/*.cloop"; then
129 # Let's hope there's just one matching *.cloop... FIXME
130 setup_devmapper $(get_backing_device "$directory/casper/*.cloop") "$rootmnt"
131 elif match_files_in_dir "$directory/casper/*.squashfs" ||
132 match_files_in_dir "$directory/casper/*.ext2" ||
133 match_files_in_dir "$directory/casper/*.dir"; then
134 setup_unionfs "$directory/casper" "$rootmnt"
142 echo "/dev/$(udevinfo -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
150 modprobe "${MP_QUIET}" -b "$module"
153 for loopdev in $pattern; do
154 if [ "$(cat $loopdev/size)" -eq 0 ]; then
155 dev=$(sys2dev "${loopdev}")
156 losetup "$dev" "$fspath"
161 panic "No loop devices available"
168 if [ "$FSTYPE" != "unknown" ]; then
172 /lib/udev/vol_id -t $1 2>/dev/null
179 modprobe "${MP_QUIET}" -b dm-mod
181 COW_NAME="casper-cow"
183 BACKING_FILE_SIZE=$(blockdev --getsize "$backdev")
184 MAX_COW_SIZE=$(blockdev --getsize "$COW_DEVICE")
185 CHUNK_SIZE=8 # sectors
187 if [ -z "$COW_SIZE" -o "$COW_SIZE" -gt "$MAX_COW_SIZE" ]; then
188 COW_SIZE=$MAX_COW_SIZE
191 echo "0 $COW_SIZE linear $COW_DEVICE 0" | dmsetup create $COW_NAME
193 echo "0 $BACKING_FILE_SIZE snapshot $backdev /dev/mapper/$COW_NAME p $CHUNK_SIZE" | \
194 dmsetup create casper-snapshot
195 if [ "$(get_fstype $backdev)" = "unknown" ]; then
196 panic "Unknown file system type on $backdev"
198 mount -t $(get_fstype "$backdev") /dev/mapper/casper-snapshot $rootmnt || panic "Can not mount /dev/mapper/casper/snapshot on $rootmnt"
200 mkdir -p "$rootmnt/rofs"
201 echo "0 $BACKING_FILE_SIZE linear $backdev 0" | dmsetup create casper-backing
202 mount -t $(get_fstype "$backdev") /dev/mapper/casper-backing "$rootmnt/rofs"
207 if grep -q "^$device " /proc/mounts; then
208 grep "^$device " /proc/mounts | read d mountpoint rest
218 if [ ! -z "${2}" ] ; then
219 # This will enable future rampersistence, todo yet
223 copyto="${copyfrom}_swap"
227 size=$(du -ks ${copyfrom} | cut -f1)
228 size=$(expr ${size} + ${size}/20 ) # Fixme: 5% more to be sure
229 needed_space=$(expr ${size} * 1024)
230 freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( cat /proc/meminfo | grep Cached | head -n 1 | awk '/Cached/{print $2}' - ))
232 if [ ! ${freespace} -lt ${needed_space} ] ; then
233 [ "$quiet" != "y" ] && log_begin_msg "Not enough free memory to copy to ram"
234 [ "$quiet" != "y" ] && log_end_msg
237 [ "$quiet" != "y" ] && log_begin_msg "Copying live media to ram..."
239 mount -t tmpfs -o size=${size}k /dev/shm ${copyto}
240 cp -a ${copyfrom}/* ${copyto} # "cp -a" from busybox also copies hidden files
242 if [ "${moveit}" == "True" ]; then
243 mount -r -o move ${copyto} ${copyfrom}
246 [ "$quiet" != "y" ] && log_end_msg
252 cow_backing="/${pers_label}-backing"
253 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop); do
254 for dev in $(subdevices "${sysblock}"); do
255 devname=$(sys2dev "${dev}")
256 if [ "$(/lib/udev/vol_id -l $devname 2>/dev/null)" = "${pers_label}" ]; then
259 elif [ "$(get_fstype ${devname})" = "vfat" ]; then
260 mkdir -p "${cow_backing}"
261 if where_is_mounted ${devname} > /dev/null; then
262 mount -o remount,rw ${devname} $(where_is_mounted ${devname}) || panic "Remounting failed"
263 mount -o bind $(where_is_mounted ${devname}) ${cow_backing} || panic "Cannot bind-mount"
265 mount -t $(get_fstype "${devname}") -o rw "${devname}" ${cow_backing} || panic "Cannot mount $devname on /cow-backing"
268 if [ -e "${cow_backing}/${pers_label}" ]; then
269 echo $(setup_loop "${cow_backing}/${pers_label}" "loop" "/sys/block/loop*")
272 umount ${cow_backing}
282 modprobe "${MP_QUIET}" af_packet # For DHCP
284 ipconfig ${DEVICE} /tmp/net-${DEVICE}.conf
286 if [ "${NFSROOT}" = "auto" ]; then
287 NFSROOT=${ROOTSERVER}:${ROOTPATH}
290 [ "$quiet" != "y" ] && log_begin_msg "Trying netboot from ${NFSROOT}"
292 if [ "${NETBOOT}" != "nfs" ] && do_cifsmount ; then
294 elif do_nfsmount ; then
300 [ "$quiet" != "y" ] && log_end_msg
306 modprobe "${MP_QUIET}" nfs
307 if [ -z "${NFSOPTS}" ]; then
311 [ "$quiet" != "y" ] && log_begin_msg "Trying nfsmount -o nolock -o ro ${NFSOPTS} ${NFSROOT} ${mountpoint}"
312 # FIXME: This for loop is an ugly HACK round an nfs bug
313 for i in 0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13; do
314 nfsmount -o nolock -o ro ${NFSOPTS} "${NFSROOT}" "${mountpoint}" && rc=0 && break
322 if [ -x "/sbin/mount.cifs" ]; then
323 if [ -z "${NFSOPTS}" ]; then
324 CIFSOPTS="-ouser=root,password="
326 CIFSOPTS="${NFSOPTS}"
329 [ "$quiet" != "y" ] && log_begin_msg "Trying mount.cifs ${NFSROOT} ${mountpoint} ${CIFSOPTS}"
330 modprobe "${MP_QUIET}" cifs
332 if mount.cifs "${NFSROOT}" "${mountpoint}" "${CIFSOPTS}" ; then
343 modprobe "${MP_QUIET}" -b unionfs
345 # run-init can't deal with this, but we're going to move all of these
346 # away before it runs anyway.
349 # Let's just mount the read-only file systems first
352 if [ "${NETBOOT}" == "nfs" ] ; then
353 roopt="nfsro" # go aroung a bug in nfs-unionfs locking
359 for image_type in "ext2" "squashfs" "dir" ; do
360 for image in "${image_directory}"/*."${image_type}"; do
361 imagename=$(basename "${image}")
362 if [ -d "${image}" ]; then
363 # it is a plain directory: do nothing
364 rofsstring="${image}=${roopt}:${rofsstring}"
365 rofslist="${image} ${rofslist}"
366 elif [ -f "${image}" ]; then
367 backdev=$(get_backing_device "$image")
368 fstype=$(get_fstype "${backdev}")
369 if [ "${fstype}" = "unknown" ]; then
370 panic "Unknown file system type on ${backdev} (${image})"
372 mkdir -p "${croot}/${imagename}"
373 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}"
377 rofsstring=${rofsstring%:}
383 # Looking for "${root_persistence}" device or file
384 if [ ! -z "${PERSISTENT}" ]; then
385 cowprobe=$(find_cow_device "${root_persistence}")
386 if [ -b "${cowprobe}" ]; then
387 cowdevice=${cowprobe}
388 cow_fstype=$(get_fstype "${cowprobe}")
390 [ "$quiet" != "y" ] && log_begin_msg "Unable to find the persistent medium"
394 mount ${cowdevice} -t ${cow_fstype} -o rw /cow || panic "Can not mount $cowdevice on /cow"
396 # Look for a snapshot to copy
399 mount -t unionfs -o dirs=/cow=rw:$rofsstring unionfs "$rootmnt" || panic "Unionfs mount failed"
401 for d in ${rofslist}; do
402 mkdir -p "${rootmnt}/casper/${d##*/}"
404 *.dir) # do nothing # mount -o bind "${d}" "${rootmnt}/casper/${d##*/}"
406 *) mount -o move "${d}" "${rootmnt}/casper/${d##*/}"
411 # Adding other custom mounts
412 if [ ! -z "${PERSISTENT}" ]; then
413 homecow=$(find_cow_device "${home_persistence}" )
414 if [ -b "${homecow}" ]; then
415 mount ${homecow} -t $(get_fstype "${homecow}") -o rw "${rootmnt}/home"
417 [ "$quiet" != "y" ] && log_begin_msg "Unable to find the persistent home medium"
421 if [ -z "${HIDECOW}" ]; then
422 mkdir -p "$rootmnt/cow"
423 mount -o bind /cow "$rootmnt/cow"
428 sysfs_path="${1#/sys}"
429 if /lib/udev/path_id "${sysfs_path}" | grep -E -q "ID_PATH=(usb|pci-[^-]*-usb)"; then
437 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram); do
438 devname=$(sys2dev "${sysblock}")
439 fstype=$(get_fstype "${devname}")
440 if /lib/udev/cdrom_id ${devname} > /dev/null; then
441 mount -t ${fstype} -o ro "$devname" $mountpoint || continue
442 if is_casper_path $mountpoint; then
448 elif is_usb_device "$sysblock"; then
449 for dev in $(subdevices "${sysblock}"); do
450 devname=$(sys2dev "${dev}")
451 fstype=$(get_fstype "${devname}")
454 mount -t ${fstype} -o ro "${devname}" $mountpoint || continue
455 if is_casper_path $mountpoint; then
464 elif [ "${fstype}" = "squashfs" || \
465 "${fstype}" = "ext2" ]; then
467 # This is an ugly hack situation, the block device has
468 # an image directly on it. It's hopefully
469 # casper, so take it and run with it.
471 ln -s "${devname}" "${devname}.${fstype}"
472 echo "${devname}.${fstype}"
478 set_usplash_timeout() {
479 if [ -x /sbin/usplash_write ]; then
480 /sbin/usplash_write "TIMEOUT 120"
491 [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/casper-premount"
492 run_scripts /scripts/casper-premount
493 [ "$quiet" != "y" ] && log_end_msg
495 # Needed here too because some things (*cough* udev *cough*)
496 # changes the timeout
500 if [ ! -z "${NETBOOT}" ]; then
501 if do_netmount ; then
502 livefs_root="${mountpoint}"
504 panic "Unable to find a the network rootfs live file system"
507 # Scan local devices for the image
508 for i in 0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13; do
509 livefs_root=$(find_livefs)
510 if [ "${livefs_root}" ]; then
515 if [ "$?" -gt 0 ]; then
516 panic "Unable to find a medium containing a live file system"
519 if [ ! -z "${TORAM}" ]; then
520 copy_to_ram "${livefs_root}"
525 mount_images_in_directory "$livefs_root" "$rootmnt"
529 maybe_break casper-bottom
530 [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/casper-bottom"
532 run_scripts /scripts/casper-bottom
533 [ "$quiet" != "y" ] && log_end_msg
537 cp casper.log "${rootmnt}/var/log/"