-setup_unionfs() {
- image_directory="$1"
- rootmnt="$2"
-
- modprobe "${MP_QUIET}" -b unionfs
-
- # run-init can't deal with images in a subdir, but we're going to
- # move all of these away before it runs anyway. No, we're not,
- # put them in / since move-mounting them into / breaks mono and
- # some other apps.
-
- croot="/"
-
- # Let's just mount the read-only file systems first
- rofsstring=""
- rofslist=""
- if [ "${NETBOOT}" = "nfs" ] ; then
- roopt="nfsro" # go aroung a bug in nfs-unionfs locking
- else
- roopt="ro"
- fi
-
- # Read image names from ${MODULE}.lst if it exists
- if [ -e "${image_directory}/${MODULE}.lst" ]; then
- for image in $(cat "${image_directory}/${MODULE}.lst"); do
- image_string="${image_string} ${image_directory}/${image}";
- done
- else
- # If ${MODULE}.lst does not exist, create a list of images
- for image_type in "ext2" "ext3" "squashfs" "dir"; do
- for image in "${image_directory}"/*."${image_type}"; do
- if [ -e "${image}" ]; then
- image_string="${image_string} ${image}";
- fi
- done
- done
- # Now sort the list
- image_string=$(echo ${image_string} | sed -e 's/ /\n/g' | sort )
- fi
-
- mkdir -p "${croot}"
- for image in ${image_string}; do
- imagename=$(basename "${image}")
- if [ -d "${image}" ]; then
- # it is a plain directory: do nothing
- rofsstring="${image}=${roopt}:${rofsstring}"
- rofslist="${image} ${rofslist}"
- elif [ -f "${image}" ]; then
- backdev=$(get_backing_device "$image")
- fstype=$(get_fstype "${backdev}")
- if [ "${fstype}" = "unknown" ]; then
- panic "Unknown file system type on ${backdev} (${image})"
- fi
- mkdir -p "${croot}/${imagename}"
- 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}"
- fi
- done
- rofsstring=${rofsstring%:}
-
- mkdir -p /cow
- cowdevice="tmpfs"
- cow_fstype="tmpfs"
-
- # Looking for "${root_persistence}" device or file
- if [ -n "${PERSISTENT}" ]; then
- cowprobe=$(find_cow_device "${root_persistence}")
- if [ -b "${cowprobe}" ]; then
- cowdevice=${cowprobe}
- cow_fstype=$(get_fstype "${cowprobe}")
- else
- [ "$quiet" != "y" ] && log_warning_msg "Unable to find the persistent medium"
- fi
- fi
-
- mount ${cowdevice} -t ${cow_fstype} -o rw /cow || panic "Can not mount $cowdevice on /cow"
-
- mount -t unionfs -o dirs=/cow=rw:$rofsstring unionfs "$rootmnt" || panic "Unionfs mount failed"
-
- # Adding other custom mounts
- if [ -n "${PERSISTENT}" ]; then
- # directly mount /home
- # FIXME: add a custom mounts configurable system
- homecow=$(find_cow_device "${home_persistence}" )
- if [ -b "${homecow}" ]; then
- mount -t $(get_fstype "${homecow}") -o rw "${homecow}" "${rootmnt}/home"
- export HOMEMOUNTED=1 # used to proper calculate free space in do_snap_copy()
- else
- [ "$quiet" != "y" ] && log_warning_msg "Unable to find the persistent home medium"
- fi
- # Look for other snapshots to copy in
- try_snap "${root_snapshot_label}" "${rootmnt}" "ROOT"
- try_snap "${home_snapshot_label}" "${rootmnt}/home" "HOME"
- fi
-
- if [ -n "${SHOWMOUNTS}" ]; then
- for d in ${rofslist}; do
- mkdir -p "${rootmnt}/live/${d##*/}"
- case d in
- *.dir) # do nothing # mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
- ;;
- *) mount --move "${d}" "${rootmnt}/live/${d##*/}"
- ;;
- esac
- done
- fi
-
- # shows cow fs on /cow for use by live-snapshot
- mkdir -p "${rootmnt}/cow"
- mount -o bind /cow "${rootmnt}/cow"
+setup_unionfs ()
+{
+ image_directory="${1}"
+ rootmnt="${2}"
+ addimage_directory="${3}"
+
+ case ${UNIONTYPE} in
+ aufs|unionfs|overlayfs)
+ modprobe -q -b ${UNIONTYPE}
+
+ if ! cut -f2 /proc/filesystems | grep -q "^${UNIONTYPE}\$" && [ -x /bin/unionfs-fuse ]
+ then
+ echo "${UNIONTYPE} not available, falling back to unionfs-fuse."
+ echo "This might be really slow."
+
+ UNIONTYPE="unionfs-fuse"
+ fi
+ ;;
+ esac
+
+ if [ "${UNIONTYPE}" = unionfs-fuse ]
+ then
+ modprobe fuse
+ fi
+
+ # run-init can't deal with images in a subdir, but we're going to
+ # move all of these away before it runs anyway. No, we're not,
+ # put them in / since move-mounting them into / breaks mono and
+ # some other apps.
+
+ croot="/"
+
+ # Let's just mount the read-only file systems first
+ rofslist=""
+
+ if [ "${UNIONTYPE}" = "aufs" ]
+ then
+ roopt="rr+wh"
+ noxino_opt="noxino,"
+ elif [ "${UNIONTYPE}" = "unionfs-fuse" ]
+ then
+ roopt="RO"
+ else
+ roopt="ro"
+ fi
+
+ if [ -z "${PLAIN_ROOT}" ]
+ then
+ # Read image names from ${MODULE}.module if it exists
+ if [ -e "${image_directory}/filesystem.${MODULE}.module" ]
+ then
+ for IMAGE in $(cat ${image_directory}/filesystem.${MODULE}.module)
+ do
+ image_string="${image_string} ${image_directory}/${IMAGE}"
+ done
+ elif [ -e "${image_directory}/${MODULE}.module" ]
+ then
+ for IMAGE in $(cat ${image_directory}/${MODULE}.module)
+ do
+ image_string="${image_string} ${image_directory}/${IMAGE}"
+ done
+ else
+ # ${MODULE}.module does not exist, create a list of images
+ for FILESYSTEM in squashfs ext2 ext3 ext4 xfs jffs2 dir
+ do
+ for IMAGE in "${image_directory}"/*."${FILESYSTEM}"
+ do
+ if [ -e "${IMAGE}" ]
+ then
+ image_string="${image_string} ${IMAGE}"
+ fi
+ done
+ done
+
+ if [ -n "${addimage_directory}" ] && [ -d "${addimage_directory}" ]
+ then
+ for FILESYSTEM in squashfs ext2 ext3 ext4 xfs jffs2 dir
+ do
+ for IMAGE in "${addimage_directory}"/*."${FILESYSTEM}"
+ do
+ if [ -e "${IMAGE}" ]
+ then
+ image_string="${image_string} ${IMAGE}"
+ fi
+ done
+ done
+ fi
+
+ # Now sort the list
+ image_string="$(echo ${image_string} | sed -e 's/ /\n/g' | sort )"
+ fi
+
+ [ -n "${MODULETORAMFILE}" ] && image_string="${image_directory}/$(basename ${MODULETORAMFILE})"
+
+ mkdir -p "${croot}"
+
+ for image in ${image_string}
+ do
+ imagename=$(basename "${image}")
+
+ export image devname
+ maybe_break live-realpremount
+ log_begin_msg "Running /scripts/live-realpremount"
+ run_scripts /scripts/live-realpremount
+ log_end_msg
+
+ if [ -d "${image}" ]
+ then
+ # it is a plain directory: do nothing
+ rofslist="${image} ${rofslist}"
+ elif [ -f "${image}" ]
+ then
+ if losetup --help 2>&1 | grep -q -- "-r\b"
+ then
+ backdev=$(get_backing_device "${image}" "-r")
+ else
+ backdev=$(get_backing_device "${image}")
+ fi
+ fstype=$(get_fstype "${backdev}")
+
+ if [ "${fstype}" = "unknown" ]
+ then
+ panic "Unknown file system type on ${backdev} (${image})"
+ fi
+
+ if [ -z "${fstype}" ]
+ then
+ fstype="${imagename##*.}"
+ log_warning_msg "Unknown file system type on ${backdev} (${image}), assuming ${fstype}."
+ fi
+
+ if [ "${UNIONTYPE}" != "unionmount" ]
+ then
+ mpoint="${croot}/${imagename}"
+ rofslist="${mpoint} ${rofslist}"
+ else
+ mpoint="${rootmnt}"
+ rofslist="${rootmnt} ${rofslist}"
+ fi
+ mkdir -p "${mpoint}"
+ log_begin_msg "Mounting \"${image}\" on \"${mpoint}\" via \"${backdev}\""
+ mount -t "${fstype}" -o ro,noatime "${backdev}" "${mpoint}" || panic "Can not mount ${backdev} (${image}) on ${mpoint}"
+ log_end_msg
+ fi
+ done
+ else
+ # we have a plain root system
+ mkdir -p "${croot}/filesystem"
+ log_begin_msg "Mounting \"${image_directory}\" on \"${croot}/filesystem\""
+ mount -t $(get_fstype "${image_directory}") -o ro,noatime "${image_directory}" "${croot}/filesystem" || \
+ panic "Can not mount ${image_directory} on ${croot}/filesystem" && \
+ rofslist="${croot}/filesystem ${rofslist}"
+ # probably broken:
+ mount -o bind ${croot}/filesystem $mountpoint
+ log_end_msg
+ fi
+
+ mkdir -p /cow
+
+ # Looking for "${root_persistence}" device or file
+ if [ -n "${PERSISTENT}" ] && [ -z "${NOPERSISTENT}" ]
+ then
+
+ if [ -z "${QUICKUSBMODULES}" ]
+ then
+ # Load USB modules
+ num_block=$(ls -l /sys/block | wc -l)
+ for module in sd_mod uhci-hcd ehci-hcd ohci-hcd usb-storage
+ do
+ modprobe -q -b ${module}
+ done
+
+ udevadm trigger
+ udevadm settle
+
+ # For some reason, udevsettle does not block in this scenario,
+ # so we sleep for a little while.
+ #
+ # See https://bugs.launchpad.net/ubuntu/+source/casper/+bug/84591
+ for timeout in 5 4 3 2 1
+ do
+ sleep 1
+
+ if [ $(ls -l /sys/block | wc -l) -gt ${num_block} ]
+ then
+ break
+ fi
+ done
+ fi
+
+ case "${PERSISTENT_MEDIA}" in
+ removable)
+ whitelistdev="$(removable_dev)"
+ ;;
+ removable-usb)
+ whitelistdev="$(removable_usb_dev)"
+ ;;
+ *)
+ whitelistdev=""
+ ;;
+ esac
+
+ if echo ${PERSISTENT_METHOD} | grep -qe "\<overlay\>"
+ then
+ overlays="${root_persistence}"
+ fi
+
+ if echo ${PERSISTENT_METHOD} | grep -qe "\<snapshot\>"
+ then
+ snapshots="${root_snapshot_label} ${home_snapshot_label}"
+ fi
+
+ overlay_devices=""
+ for media in $(find_persistent_media "${overlays}" "${snapshots}" "${blacklistdev}" "${whitelistdev}")
+ do
+ media="$(echo ${media} | tr ":" " ")"
+ case ${media} in
+ ${root_snapshot_label}=*)
+ root_snapdata="${media#*=}"
+ ;;
+ ${home_snapshot_label}=*)
+ # This second type should be removed when snapshot will get smarter,
+ # hence when "/etc/live-snapshot*list" will be supported also by
+ # ext2|ext3|ext4|jffs2 snapshot types.
+ home_snapdata="${media#*=}"
+ ;;
+ *)
+ device="${media#*=}"
+ overlay_devices="${overlay_devices} ${device}"
+ ;;
+ esac
+ done
+
+ if [ -b "${cowprobe}" ] || [ -b "${homecow}" ]
+ then
+ PERSISTENCE_IS_ON="1"
+ export PERSISTENCE_IS_ON
+ fi
+
+ if [ -b "${cowprobe}" ]
+ then
+ cowdevice=${cowprobe}
+ cow_fstype=$(get_fstype "${cowprobe}")
+ if [ -z "${PERSISTENT_READONLY}" ]
+ then
+ cow_mountopt="rw,noatime"
+ else
+ cow_mountopt="ro,noatime"
+ fi
+
+ if [ "${FORCEPERSISTENTFSCK}" = "Yes" ]
+ then
+ fsck -y ${cowdevice}
+ fi
+ fi
+ elif [ -n "${NFS_COW}" ] && [ -z "${NOPERSISTENT}" ]
+ then
+ # check if there are any nfs options
+ if echo ${NFS_COW}|grep -q ','
+ then
+ nfs_cow_opts="-o nolock,$(echo ${NFS_COW}|cut -d, -f2-)"
+ nfs_cow=$(echo ${NFS_COW}|cut -d, -f1)
+ else
+ nfs_cow_opts="-o nolock"
+ nfs_cow=${NFS_COW}
+ fi
+
+ if [ -n "${PERSISTENT_READONLY}" ]
+ then
+ nfs_cow_opts="${nfs_cow_opts},nocto,ro"
+ fi
+
+ mac="$(get_mac)"
+ if [ -n "${mac}" ]
+ then
+ cowdevice=$(echo ${nfs_cow}|sed "s/client_mac_address/${mac}/")
+ cow_fstype="nfs"
+ else
+ panic "unable to determine mac address"
+ fi
+ fi
+
+ if [ -z "${cowdevice}" ]
+ then
+ cowdevice="tmpfs"
+ cow_fstype="tmpfs"
+ cow_mountopt="rw,noatime,mode=755"
+ fi
+
+ if [ "${UNIONTYPE}" != "unionmount" ]
+ then
+ if [ -n "${PERSISTENT_READONLY}" ]
+ then
+ persistent_root="/$(basename ${cowdevice})-backing"
+ mkdir -p ${persistent_root}
+ else
+ persistent_root="/cow"
+ fi
+
+ if [ "${cow_fstype}" = "nfs" ]
+ then
+ log_begin_msg \
+ "Trying nfsmount ${nfs_cow_opts} ${cowdevice} ${persistent_root}"
+ nfsmount ${nfs_cow_opts} ${cowdevice} ${persistent_root} || \
+ panic "Can not mount ${cowdevice} (n: ${cow_fstype}) on ${persistent_root}"
+ else
+ mount -t ${cow_fstype} -o ${cow_mountopt} ${cowdevice} ${persistent_root} || \
+ panic "Can not mount ${cowdevice} (o: ${cow_fstype}) on ${persistent_root}"
+ fi
+ fi
+
+ rofscount=$(echo ${rofslist} |wc -w)
+
+ if [ ${rofscount} -ne 1 ]
+ then
+ panic "only one RO file system supported with exposedroot: ${rofslist}"
+ fi
+ rofs=${rofslist%% }
+
+ if [ -n "${EXPOSED_ROOT}" ]
+ then
+ mount --bind ${rofs} ${rootmnt} || \
+ panic "bind mount of ${rofs} failed"
+
+ if [ -z "${SKIP_UNION_MOUNTS}" ]
+ then
+ cow_dirs='/var/tmp /var/lock /var/run /var/log /var/spool /home /var/lib/live'
+ else
+ cow_dirs=''
+ fi
+ else
+ cow_dirs="/"
+ fi
+
+ if [ "${cow_fstype}" != "tmpfs" ] && [ "${cow_dirs}" != "/" ] && [ "${UNIONTYPE}" = "unionmount" ]
+ then
+ true # FIXME: Maybe it does, I don't really know.
+ #panic "unionmount does not support subunions (${cow_dirs})."
+ fi
+
+ unionmountopts=""
+ unionmountpoint=""
+
+ for dir in ${cow_dirs}; do
+ mkdir -p /cow${dir}
+
+ unionmountpoint="${rootmnt}${dir}"
+ unionrw="/cow${dir}"
+ unionro="${rofs}${dir}"
+ # We don't handle spaces and other junk gracefully here, hopefully not needed.
+ case "${UNIONTYPE}" in
+ unionfs-fuse)
+ unionmountopts="-o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid"
+ unionmountopts="${unionmountopts} ${unionrw}=RW:${unionro}=RO"
+ ( sysctl -w fs.file-max=391524 ; ulimit -HSn 16384
+ unionfs-fuse ${unionmountopts} "${unionmountpoint}" ) && \
+ ( mkdir -p /run/sendsigs.omit.d
+ pidof unionfs-fuse >> /run/sendsigs.omit.d/unionfs-fuse || true )
+ ;;
+
+ unionmount)
+ unionmountopts="-t ${cow_fstype} -o noatime,union,${cow_mountopt} ${cowdevice}"
+ mount_full $unionmountopts "${unionmountpoint}"
+ ;;
+
+ overlayfs)
+ unionmountopts="-o noatime,${noxino_opt},lowerdir=${unionro},upperdir=${unionrw}"
+ mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
+ ;;
+
+ *)
+ if [ -n "${PERSISTENT_READONLY}" ]
+ then
+ mount -t tmpfs -o rw,noatime,mode=755 tmpfs "${unionrw}"
+ unionmountopts="-o noatime,${noxino_opt}dirs=${unionrw}=rw:${persistent_root}=${roopt}:${unionro}=${roopt}"
+ else
+ unionmountopts="-o noatime,${noxino_opt}dirs=${unionrw}=rw:${unionro}=${roopt}"
+ fi
+ mount -t ${UNIONTYPE} ${unionmountopts} ${UNIONTYPE} "${unionmountpoint}"
+ ;;
+ esac || \
+ panic "mount ${UNIONTYPE} on ${unionmountpoint} failed with option ${unionmountopts}"
+ done
+
+ # Correct the permissions of /:
+ chmod 0755 "${rootmnt}"
+
+ # tmpfs file systems
+ touch /etc/fstab
+ mkdir -p "${rootmnt}/live"
+ mount -t tmpfs tmpfs ${rootmnt}/live
+
+ # Adding other custom mounts
+ if [ -n "${PERSISTENT}" ] && [ -z "${NOPERSISTENT}" ]
+ then
+ bindings="/${persistence_list}"
+ touch ${bindings}
+ for device in ${overlay_devices}
+ do
+ if [ ! -b "${device}" ]
+ then
+ continue
+ fi
+ backing="/$(basename ${device})-backing"
+ mkdir -p "${backing}"
+ device_fstype="$(get_fstype ${device})"
+ if [ -z "${PERSISTENT_READONLY}" ]
+ then
+ device_mount_opts="rw,noatime"
+ else
+ device_mount_opts="ro,noatime"
+ fi
+ device_used=""
+ mount -t "${device_fstype}" -o "${device_mount_opts}" "${device}" "${backing}"
+ include_list="${backing}/${persistence_list}"
+ if [ ! -r "${include_list}" ]
+ then
+ umount "${backing}"
+ rmdir "${backing}"
+ continue
+ fi
+
+ # FIXME: debug stuff, remove me?
+ [ "${DEBUG}" == "Yes" ] && cat ${include_list} >> ${rootmnt}/${bindings}-origs
+ while read source dest
+ do
+ if echo ${source} | grep -qe "^[[:space:]]*#"
+ then
+ # skipping commented line
+ continue
+ fi
+ if [ -z "${dest}" ]
+ then
+ dest="${source}"
+ fi
+ if echo ${dest} | grep -qe "^/\+$"
+ then
+ log_warning_msg "Skipping custom mount on /"
+ continue
+ fi
+
+ # FIXME: handle case: we already have /a/b in $bindings, but now we find /a -- /a should replace /a/b in $bindings.
+ # FIXME: handle case: we have /a in $bindings, now we find /a/b, so we skip /a/b
+
+ # ensure that no multiple-/ occur in paths
+ full_source="$(echo ${backing}/${source}/ | sed -e 's|/\+|/|g')"
+ full_dest="$(echo ${rootmnt}/${dest}/ | sed -e 's|/\+|/|g')"
+ device_used="yes"
+ echo "${full_source} ${full_dest}" >> ${bindings}
+ done < ${include_list}
+
+ if [ -z "${device_used}" ]
+ then
+ # this device was not used for / earlier, or custom mount point now, so it's useless
+ umount "${backing}"
+ rmdir "${backing}"
+ fi
+ done
+
+ # we sort the list according to destination so we're sure that we won't hide a previous mount. we also ignore duplicate destinations in a more or less arbitrary way
+ sort -k2 -sbu ${bindings} -o ${bindings}
+
+ # FIXME: debug stuff, remove me?
+ [ "${DEBUG}" == "Yes" ] && cp ${bindings} ${rootmnt}/${bindings}-results
+
+ while read source dest
+ do
+ if mountpoint -q "${dest}";
+ then
+ log_warning_msg "Skipping custom mount ${source} on ${dest}: destination is already a mount point"
+ continue
+ fi
+
+ # FIXME: we don't handle already existing non-directory files in the paths of both $source and $dest.
+
+ if [ ! -d "${dest}" ]
+ then
+ # if ${dest} is in /home/$user, try fixing proper ownership
+ # FIXME: this should really be handled by live-config since we don't know for sure which uid a certain user has until then
+ if echo ${dest} | grep -qe "^${rootmnt}/*home/\+[^/]\+"
+ then
+ path="/"
+ for dir in $(echo ${dest} | sed -e 's|/\+| |g')
+ do
+ path=${path}/${dir}
+ if [ ! -e ${path} ]
+ then
+ mkdir -p ${path}
+ # assume that the intended user is the first, which is usually the case
+ chown 1000:1000 ${path}
+ fi
+ done
+ else
+ mkdir -p ${dest}
+ fi
+ fi
+
+ # FIXME: could we instead only save the aufs-diff in the persistent media? implications? What about when there's changes in the live image?
+
+ # if ${source} doesn't exist on our persistent media we
+ # bootstrap it with $dest from the live filesystem.
+ # this both makes sense and is critical if we're
+ # dealing with /etc or other system dir.
+ if [ ! -d "${source}" ]
+ then
+ if [ -n "${PERSISTENT_READONLY}" ]
+ then
+ continue
+ fi
+ # ensure that $dest is not copied *into* $source
+ mkdir -p "$(dirname ${source})"
+ cp -a "${dest}" "${source}"
+ fi
+
+ if [ -z "${PERSISTENT_READONLY}" ]
+ then
+ mount --bind "${source}" "${dest}"
+ else
+ unionrw="$(echo ${dest} | sed -e "s|${rootmnt}|/cow/|")"
+ mkdir -p ${unionrw}
+ unionmountopts="noatime,${noxino_opt}dirs=${unionrw}=rw:${source}=${roopt}"
+ mount -t "${UNIONTYPE}" -o "${unionmountopts}" "${UNIONTYPE}" "${dest}"
+ fi
+ done < ${bindings}
+ rm ${bindings}
+
+ # Look for other snapshots to copy in
+ try_snap "${root_snapdata}" "${rootmnt}" "ROOT"
+ # This second type should be removed when snapshot grow smarter
+ try_snap "${home_snapdata}" "${rootmnt}" "HOME" "/home"
+ fi
+
+ if [ -n "${SHOWMOUNTS}" ]
+ then
+ for d in ${rofslist}
+ do
+ mkdir -p "${rootmnt}/live/${d##*/}"
+
+ case d in
+ *.dir)
+ # do nothing # mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
+ ;;
+
+ *)
+ case "${UNIONTYPE}" in
+ unionfs-fuse)
+ mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
+ ;;
+
+ *)
+ mount -o move "${d}" "${rootmnt}/live/${d##*/}"
+ ;;
+ esac
+ ;;
+ esac
+ done
+ fi
+
+ # shows cow fs on /cow for use by live-snapshot
+ mkdir -p "${rootmnt}/live/cow"
+ mount -o move /cow "${rootmnt}/live/cow" >/dev/null 2>&1 || mount -o bind /cow "${rootmnt}/live/cow" || log_warning_msg "Unable to move or bind /cow to ${rootmnt}/live/cow"