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"
30 [ -f /etc/casper.conf ] && . /etc/casper.conf
32 export USERNAME USERFULLNAME HOST BUILD_SYSTEM
34 if [ "${BUILD_SYSTEM}" == "Ubuntu" ]; then
40 # looking for casper specifics options as kernel parameters
41 for x in $(cat /proc/cmdline); do
44 export NETBOOT=${x#netboot=} ;;
50 export PERSISTENT=1 ;;
52 export CASPERGETTY=1 ;;
56 # sort of compatibility with netboot.h from linux docs
57 if [ -z "${NETBOOT}" ]; then
58 if [ "${ROOT}" == "/dev/nfs" ]; then
61 elif [ "${ROOT}" == "/dev/cifs" ]; then
69 if [ -d "$path/casper" ]; then
70 if [ "$(echo $path/casper/*.cloop)" != "$path/casper/*.cloop" ] ||
71 [ "$(echo $path/casper/*.squashfs)" != "$path/casper/*.squashfs" ] ||
72 [ "$(echo $path/casper/*.ext2)" != "$path/casper/*.ext2" ] ||
73 [ "$(echo $path/casper/*.dir)" != "$path/casper/*.dir" ]; then
83 for dev in "${sysblock}" "${sysblock}"/*; do
84 if [ -e "${dev}/dev" ]; then
91 get_backing_device() {
94 echo $(setup_loop "$1" "cloop" "/sys/block/cloop*")
97 echo $(setup_loop "$1" "loop" "/sys/block/loop*")
103 panic "Unrecognized casper filesystem: $1"
108 match_files_in_dir() {
109 # Does any files match pattern $1 ?
112 if [ "$(echo $pattern)" != "$pattern" ]; then
118 mount_images_in_directory() {
121 if match_files_in_dir "$directory/casper/*.cloop"; then
122 # Let's hope there's just one matching *.cloop... FIXME
123 setup_devmapper $(get_backing_device "$directory/casper/*.cloop") "$rootmnt"
124 elif match_files_in_dir "$directory/casper/*.squashfs" ||
125 match_files_in_dir "$directory/casper/*.ext2" ||
126 match_files_in_dir "$directory/casper/*.dir"; then
127 setup_unionfs "$directory/casper" "$rootmnt"
135 echo "/dev/$(udevinfo -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
143 modprobe "${MP_QUIET}" -b "$module"
146 for loopdev in $pattern; do
147 if [ "$(cat $loopdev/size)" -eq 0 ]; then
148 dev=$(sys2dev "${loopdev}")
149 losetup "$dev" "$fspath"
154 panic "No loop devices available"
158 #FIXME# one use of this function expects "unknown" another does not!
163 if [ "$FSTYPE" != "unknown" ]; then
167 /lib/udev/vol_id -t $1 2>/dev/null
174 modprobe "${MP_QUIET}" -b dm-mod
176 COW_NAME="casper-cow"
178 BACKING_FILE_SIZE=$(blockdev --getsize "$backdev")
179 MAX_COW_SIZE=$(blockdev --getsize "$COW_DEVICE")
180 CHUNK_SIZE=8 # sectors
182 if [ -z "$COW_SIZE" -o "$COW_SIZE" -gt "$MAX_COW_SIZE" ]; then
183 COW_SIZE=$MAX_COW_SIZE
186 echo "0 $COW_SIZE linear $COW_DEVICE 0" | dmsetup create $COW_NAME
188 echo "0 $BACKING_FILE_SIZE snapshot $backdev /dev/mapper/$COW_NAME p $CHUNK_SIZE" | \
189 dmsetup create casper-snapshot
190 if [ "$(get_fstype $backdev)" = "unknown" ]; then
191 panic "Unknown file system type on $backdev"
193 mount -t $(get_fstype "$backdev") /dev/mapper/casper-snapshot $rootmnt || panic "Can not mount /dev/mapper/casper/snapshot on $rootmnt"
195 mkdir -p "$rootmnt/rofs"
196 echo "0 $BACKING_FILE_SIZE linear $backdev 0" | dmsetup create casper-backing
197 mount -t $(get_fstype "$backdev") /dev/mapper/casper-backing "$rootmnt/rofs"
202 if grep -q "^$device " /proc/mounts; then
203 grep "^$device " /proc/mounts | read d mountpoint rest
213 if [ ! -z "${2}" ] ; then
214 # This will enable future rampersistence, todo yet
218 copyto="${copyfrom}_swap"
222 size=$(du -ks ${copyfrom} | cut -f1)
223 size=$(expr ${size} + ${size}/20 ) # Fixme: 5% more to be sure
224 needed_space=$(expr ${size} * 1024)
225 freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( cat /proc/meminfo | grep Cached | head -n 1 | awk '/Cached/{print $2}' - ))
227 if [ ! ${freespace} -lt ${needed_space} ] ; then
228 [ "$quiet" != "y" ] && log_begin_msg "Not enough free memory to copy to ram"
229 [ "$quiet" != "y" ] && log_end_msg
232 [ "$quiet" != "y" ] && log_begin_msg "Copying live media to ram..."
234 mount -t tmpfs -o size=${size}k /dev/shm ${copyto}
235 cp -a ${copyfrom}/* ${copyto} # "cp -a" from busybox also copies hidden files
237 if [ "${moveit}" == "True" ]; then
238 mount -r -o move ${copyto} ${copyfrom}
241 [ "$quiet" != "y" ] && log_end_msg
247 cow_backing="/${pers_label}-backing"
248 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop); do
249 for dev in $(subdevices "${sysblock}"); do
250 devname=$(sys2dev "${dev}")
251 if [ "$(/lib/udev/vol_id -l $devname 2>/dev/null)" = "${pers_label}" ]; then
254 elif [ "$(get_fstype ${devname})" = "vfat" ]; then
255 mkdir -p "${cow_backing}"
256 if where_is_mounted ${devname} > /dev/null; then
257 mount -o remount,rw ${devname} $(where_is_mounted ${devname}) || panic "Remounting failed"
258 mount -o bind $(where_is_mounted ${devname}) ${cow_backing} || panic "Cannot bind-mount"
260 mount -t $(get_fstype "${devname}") -o rw "${devname}" ${cow_backing} || panic "Cannot mount $devname on /cow-backing"
263 if [ -e "${cow_backing}/${pers_label}" ]; then
264 echo $(setup_loop "${cow_backing}/${pers_label}" "loop" "/sys/block/loop*")
267 umount ${cow_backing}
277 modprobe "${MP_QUIET}" af_packet # For DHCP
279 ipconfig ${DEVICE} /tmp/net-${DEVICE}.conf
281 if [ "${NFSROOT}" = "auto" ]; then
282 NFSROOT=${ROOTSERVER}:${ROOTPATH}
285 [ "$quiet" != "y" ] && log_begin_msg "Trying netboot from ${NFSROOT}"
287 if [ "${NETBOOT}" != "nfs" ] && do_cifsmount ; then
289 elif do_nfsmount ; then
295 [ "$quiet" != "y" ] && log_end_msg
301 modprobe "${MP_QUIET}" nfs
302 if [ -z "${NFSOPTS}" ]; then
306 [ "$quiet" != "y" ] && log_begin_msg "Trying nfsmount -o nolock -o ro ${NFSOPTS} ${NFSROOT} ${mountpoint}"
307 # FIXME: This for loop is an ugly HACK round an nfs bug
308 for i in 0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13; do
309 nfsmount -o nolock -o ro ${NFSOPTS} "${NFSROOT}" "${mountpoint}" && rc=0 && break
317 if [ -x "/sbin/mount.cifs" ]; then
318 if [ -z "${NFSOPTS}" ]; then
319 CIFSOPTS="-ouser=root,password="
321 CIFSOPTS="${NFSOPTS}"
324 [ "$quiet" != "y" ] && log_begin_msg "Trying mount.cifs ${NFSROOT} ${mountpoint} ${CIFSOPTS}"
325 modprobe "${MP_QUIET}" cifs
327 if mount.cifs "${NFSROOT}" "${mountpoint}" "${CIFSOPTS}" ; then
338 modprobe "${MP_QUIET}" -b unionfs
340 # run-init can't deal with this, but we're going to move all of these
341 # away before it runs anyway.
344 # Let's just mount the read-only file systems first
347 if [ "${NETBOOT}" == "nfs" ] ; then
348 roopt="nfsro" # go aroung a bug in nfs-unionfs locking
354 for image_type in "ext2" "squashfs" "dir" ; do
355 for image in "${image_directory}"/*."${image_type}"; do
356 imagename=$(basename "${image}")
357 if [ -d "${image}" ]; then
358 # it is a plain directory: do nothing
359 rofsstring="${image}=${roopt}:${rofsstring}"
360 rofslist="${image} ${rofslist}"
361 elif [ -f "${image}" ]; then
362 backdev=$(get_backing_device "$image")
363 fstype=$(get_fstype "${backdev}")
364 if [ "${fstype}" = "unknown" ]; then
365 panic "Unknown file system type on ${backdev} (${image})"
367 mkdir -p "${croot}/${imagename}"
368 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}"
372 rofsstring=${rofsstring%:}
378 # Looking for "${root_persistence}" device or file
379 if [ ! -z "${PERSISTENT}" ]; then
380 cowprobe=$(find_cow_device "${root_persistence}")
381 if [ -b "${cowprobe}" ]; then
382 cowdevice=${cowprobe}
383 cow_fstype=$(get_fstype "${cowprobe}")
385 [ "$quiet" != "y" ] && log_begin_msg "Unable to find the persistent medium"
389 mount ${cowdevice} -t ${cow_fstype} -o rw /cow || panic "Can not mount $cowdevice on /cow"
391 mount -t unionfs -o dirs=/cow=rw:$rofsstring unionfs "$rootmnt" || panic "Unionfs mount failed"
393 for d in ${rofslist}; do
394 mkdir -p "${rootmnt}/casper/${d##*/}"
396 *.dir) mount -o bind "${d}" "${rootmnt}/casper/${d##*/}" ;;
397 *) mount -o move "${d}" "${rootmnt}/casper/${d##*/}" ;;
401 # Adding other custom mounts
402 if [ ! -z "${PERSISTENT}" ]; then
403 homecow=$(find_cow_device "${home_persistence}" )
404 if [ -b "${homecow}" ]; then
405 mount ${homecow} -t $(get_fstype "${homecow}") -o rw "${rootmnt}/home"
407 [ "$quiet" != "y" ] && log_begin_msg "Unable to find the persistent home medium"
411 if [ ! -z "${SHOWCOW}" ]; then
412 mkdir -p "$rootmnt/cow"
413 mount -o bind /cow "$rootmnt/cow"
418 sysfs_path="${1#/sys}"
419 if /lib/udev/path_id "${sysfs_path}" | grep -Eq "ID_PATH=(usb|pci-[^-]*-usb)"; then
427 for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram); do
428 devname=$(sys2dev "${sysblock}")
429 fstype=$(get_fstype "${devname}")
430 if /lib/udev/cdrom_id ${devname} > /dev/null; then
431 mount -t ${fstype} -o ro "$devname" $mountpoint || continue
432 if is_casper_path $mountpoint; then
438 elif is_usb_device "$sysblock"; then
439 for dev in $(subdevices "${sysblock}"); do
440 devname=$(sys2dev "${dev}")
441 fstype=$(get_fstype "${devname}")
444 mount -t ${fstype} -o ro "${devname}" $mountpoint || continue
445 if is_casper_path $mountpoint; then
454 elif [ "${fstype}" = "squashfs" || \
455 "${fstype}" = "ext2" ]; then
457 # This is an ugly hack situation, the block device has
458 # an image directly on it. It's hopefully
459 # casper, so take it and run with it.
461 ln -s "${devname}" "${devname}.${fstype}"
462 echo "${devname}.${fstype}"
468 set_usplash_timeout() {
469 if [ -x /sbin/usplash_write ]; then
470 /sbin/usplash_write "TIMEOUT 120"
481 [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/casper-premount"
482 run_scripts /scripts/casper-premount
483 [ "$quiet" != "y" ] && log_end_msg
485 # Needed here too because some things (*cough* udev *cough*)
486 # changes the timeout
490 if [ ! -z "${NETBOOT}" ]; then
491 if do_netmount ; then
492 livefs_root="${mountpoint}"
494 panic "Unable to find a the network rootfs live file system"
497 # Scan local devices for the image
498 for i in 0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13; do
499 livefs_root=$(find_livefs)
500 if [ "${livefs_root}" ]; then
505 if [ "$?" -gt 0 ]; then
506 panic "Unable to find a medium containing a live file system"
509 if [ ! -z "${TORAM}" ]; then
510 copy_to_ram "${livefs_root}"
515 mount_images_in_directory "$livefs_root" "$rootmnt"
519 maybe_break casper-bottom
520 [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/casper-bottom"
522 run_scripts /scripts/casper-bottom
523 [ "$quiet" != "y" ] && log_end_msg
527 cp casper.log "${rootmnt}/var/log/"