Fixing persistence-media option when no devices are attached.
[live-boot-grml.git] / scripts / boot / 9990-overlay.sh
index 55254c7..96c3331 100755 (executable)
@@ -36,7 +36,7 @@ setup_unionfs ()
        croot="/"
 
        # Let's just mount the read-only file systems first
-       rofslist=""
+       rootfslist=""
 
        if [ -z "${PLAIN_ROOT}" ]
        then
@@ -101,7 +101,7 @@ setup_unionfs ()
                        if [ -d "${image}" ]
                        then
                                # it is a plain directory: do nothing
-                               rofslist="${image} ${rofslist}"
+                               rootfslist="${image} ${rootfslist}"
                        elif [ -f "${image}" ]
                        then
                                if losetup --help 2>&1 | grep -q -- "-r\b"
@@ -126,12 +126,12 @@ setup_unionfs ()
                                case "${UNIONTYPE}" in
                                        unionmount)
                                                mpoint="${rootmnt}"
-                                               rofslist="${rootmnt} ${rofslist}"
+                                               rootfslist="${rootmnt} ${rootfslist}"
                                                ;;
 
                                        *)
                                                mpoint="${croot}/${imagename}"
-                                               rofslist="${mpoint} ${rofslist}"
+                                               rootfslist="${mpoint} ${rootfslist}"
                                                ;;
                                esac
 
@@ -147,7 +147,7 @@ setup_unionfs ()
                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}"
+                       rootfslist="${croot}/filesystem ${rootfslist}"
                # probably broken:
                mount -o bind ${croot}/filesystem $mountpoint
                log_end_msg
@@ -189,19 +189,23 @@ setup_unionfs ()
                        done
                fi
 
-               case "${PERSISTENCE_MEDIA}" in
-                       removable)
-                               whitelistdev="$(removable_dev)"
-                               ;;
-
-                       removable-usb)
-                               whitelistdev="$(removable_usb_dev)"
-                               ;;
+               local whitelistdev=""
+               if [ -n "${PERSISTENCE_MEDIA}" ]
+               then
+                       case "${PERSISTENCE_MEDIA}" in
+                               removable)
+                                       whitelistdev="$(removable_dev)"
+                                       ;;
 
-                       *)
-                               whitelistdev=""
-                               ;;
-               esac
+                               removable-usb)
+                                       whitelistdev="$(removable_usb_dev)"
+                                       ;;
+                       esac
+                       if [ -z "${whitelistdev}" ]
+                       then
+                               whitelistdev="ignore_all_devices"
+                       fi
+               fi
 
                if is_in_comma_sep_list overlay ${PERSISTENCE_METHOD}
                then
@@ -209,29 +213,32 @@ setup_unionfs ()
                fi
 
                local overlay_devices=""
-               for media in $(find_persistence_media "${overlays}" "${whitelistdev}")
-               do
-                       media="$(echo ${media} | tr ":" " ")"
+               if [ "${whitelistdev}" != "ignore_all_devices" ]
+               then
+                       for media in $(find_persistence_media "${overlays}" "${whitelistdev}")
+                       do
+                               media="$(echo ${media} | tr ":" " ")"
 
-                       case ${media} in
-                               ${old_root_overlay_label}=*)
-                                       device="${media#*=}"
-                                       fix_backwards_compatibility ${device} / union
-                                       overlay_devices="${overlay_devices} ${device}"
-                                       ;;
+                               case ${media} in
+                                       ${old_root_overlay_label}=*)
+                                               device="${media#*=}"
+                                               fix_backwards_compatibility ${device} / union
+                                               overlay_devices="${overlay_devices} ${device}"
+                                               ;;
 
-                               ${old_home_overlay_label}=*)
-                                       device="${media#*=}"
-                                       fix_backwards_compatibility ${device} /home bind
-                                       overlay_devices="${overlay_devices} ${device}"
-                                       ;;
+                                       ${old_home_overlay_label}=*)
+                                               device="${media#*=}"
+                                               fix_backwards_compatibility ${device} /home bind
+                                               overlay_devices="${overlay_devices} ${device}"
+                                               ;;
 
-                               ${custom_overlay_label}=*)
-                                       device="${media#*=}"
-                                       overlay_devices="${overlay_devices} ${device}"
-                                       ;;
-                        esac
-               done
+                                       ${custom_overlay_label}=*)
+                                               device="${media#*=}"
+                                               overlay_devices="${overlay_devices} ${device}"
+                                               ;;
+                                esac
+                       done
+               fi
        elif [ -n "${NFS_COW}" ] && [ -z "${NOPERSISTENCE}" ]
        then
                # check if there are any nfs options
@@ -289,19 +296,19 @@ setup_unionfs ()
                fi
        fi
 
-       rofscount=$(echo ${rofslist} |wc -w)
+       rootfscount=$(echo ${rootfslist} |wc -w)
 
-       rofs=${rofslist%% }
+       rootfs=${rootfslist%% }
 
        if [ -n "${EXPOSED_ROOT}" ]
        then
-               if [ ${rofscount} -ne 1 ]
+               if [ ${rootfscount} -ne 1 ]
                then
-                       panic "only one RO file system supported with exposedroot: ${rofslist}"
+                       panic "only one RO file system supported with exposedroot: ${rootfslist}"
                fi
 
-               mount --bind ${rofs} ${rootmnt} || \
-                       panic "bind mount of ${rofs} failed"
+               mount --bind ${rootfs} ${rootmnt} || \
+                       panic "bind mount of ${rootfs} failed"
 
                if [ -z "${SKIP_UNION_MOUNTS}" ]
                then
@@ -329,13 +336,13 @@ setup_unionfs ()
                        mount_full $unionmountopts "${unionmountpoint}"
                else
                        cow_dir="/live/overlay${dir}"
-                       rofs_dir="${rofs}${dir}"
+                       rootfs_dir="${rootfs}${dir}"
                        mkdir -p ${cow_dir}
                        if [ -n "${PERSISTENCE_READONLY}" ] && [ "${cowdevice}" != "tmpfs" ]
                        then
-                               do_union ${unionmountpoint} ${cow_dir} ${root_backing} ${rofs_dir}
+                               do_union ${unionmountpoint} ${cow_dir} ${root_backing} ${rootfs_dir}
                        else
-                               do_union ${unionmountpoint} ${cow_dir} ${rofs_dir}
+                               do_union ${unionmountpoint} ${cow_dir} ${rootfs_dir}
                        fi
                fi || panic "mount ${UNIONTYPE} on ${unionmountpoint} failed with option ${unionmountopts}"
        done
@@ -349,30 +356,53 @@ setup_unionfs ()
                chmod 1777 "${rootmnt}"/tmp
        fi
 
-       live_rofs_list=""
-       for d in ${rofslist}
+       live_rootfs_list=""
+       for d in ${rootfslist}
        do
-               live_rofs="/live/rofs/${d##*/}"
-               live_rofs_list="${live_rofs_list} ${live_rofs}"
-               mkdir -p "${live_rofs}"
+               live_rootfs="/live/rootfs/${d##*/}"
+               live_rootfs_list="${live_rootfs_list} ${live_rootfs}"
+               mkdir -p "${live_rootfs}"
                case d in
                        *.dir)
-                               # do nothing # mount -o bind "${d}" "${live_rofs}"
+                               # do nothing # mount -o bind "${d}" "${live_rootfs}"
                                ;;
                        *)
                                case "${UNIONTYPE}" in
                                        unionfs-fuse)
-                                               mount -o bind "${d}" "${live_rofs}"
+                                               mount -o bind "${d}" "${live_rootfs}"
                                                ;;
 
                                        *)
-                                               mount -o move "${d}" "${live_rofs}"
+                                               mount -o move "${d}" "${live_rootfs}"
                                                ;;
                                esac
                                ;;
                esac
        done
 
+       # move all /live mountpoints that the custom persistence
+       # system depends on into /lib/live on the root filesystem
+       for _DIRECTORY in rootfs
+       do
+               if [ -d "/live/${_DIRECTORY}" ]
+               then
+                       mkdir -p "${rootmnt}/lib/live/mount/${_DIRECTORY}"
+
+                       for _MOUNT in $(ls /live/${_DIRECTORY})
+                       do
+                               mkdir -p "${rootmnt}/lib/live/mount/${_DIRECTORY}/${_MOUNT}"
+                               mount -o move "/live/${_DIRECTORY}/${_MOUNT}" "${rootmnt}/lib/live/mount/${_DIRECTORY}/${_MOUNT}" > /dev/null 2>&1 || \
+                                       mount -o bind "/live/${_DIRECTORY}/${_MOUNT}" "${rootmnt}/lib/live/mount/${_DIRECTORY}/${_MOUNT}" || \
+                                       log_warning_msg "W: failed to mount /live/${_DIRECTORY}/${_MOUNT} to ${rootmnt}/lib/live/mount/${_DIRECTORY}/${_MOUNT}"
+                       done
+               fi
+       done
+
+       mkdir -p "${rootmnt}/lib/live/mount/overlay"
+       mount -o move /live/overlay "${rootmnt}/lib/live/mount/overlay" > /dev/null 2>&1 || \
+               mount -o bind /live/overlay "${rootmnt}/lib/live/mount/overlay" || \
+               log_warning_msg "W: failed to mount /live/overlay to ${rootmnt}/lib/live/mount/overlay"
+
        # Adding custom persistence
        if [ -n "${PERSISTENCE}" ] && [ -z "${NOPERSISTENCE}" ]
        then
@@ -382,7 +412,7 @@ setup_unionfs ()
                # Gather information about custom mounts from devies detected as overlays
                get_custom_mounts ${custom_mounts} ${overlay_devices}
 
-               [ -n "${DEBUG}" ] && cp ${custom_mounts} "/live/persistence"
+               [ -n "${DEBUG}" ] && cp ${custom_mounts} "/lib/live/mount/persistence"
 
                # Now we do the actual mounting (and symlinking)
                local used_overlays=""
@@ -399,32 +429,6 @@ setup_unionfs ()
                done
        fi
 
-       # make /root/lib/live writable for moving filesystems
-       mkdir -p "${rootmnt}/lib/live"
-       mount -t tmpfs tmpfs "${rootmnt}/lib/live"
-
-       # move all mountpoints to root filesystem
-       for _DIRECTORY in rofs persistence
-       do
-               if [ -d "/live/${_DIRECTORY}" ]
-               then
-                       mkdir -p "${rootmnt}/lib/live/${_DIRECTORY}"
-
-                       for _MOUNT in $(ls /live/${_DIRECTORY})
-                       do
-                               mkdir -p "${rootmnt}/lib/live/${_DIRECTORY}/${_MOUNT}"
-                               mount -o move "/live/${_DIRECTORY}/${_MOUNT}" "${rootmnt}/lib/live/${_DIRECTORY}/${_MOUNT}" > /dev/null 2>&1 || \
-                                       mount -o bind "/live/${_DIRECTORY}/${_MOUNT}" "${rootmnt}/lib/live/${_DIRECTORY}/${_MOUNT}" || \
-                                       log_warning_msg "W: failed to mount /live/${_DIRECTORY}/${_MOUNT} to ${rootmnt}/lib/live/${_DIRECTORY}/${_MOUNT}"
-                       done
-               fi
-       done
-
-       mkdir -p "${rootmnt}/lib/live/overlay"
-       mount -o move /live/overlay "${rootmnt}/lib/live/overlay" > /dev/null 2>&1 || \
-               mount -o bind /live/overlay "${rootmnt}/lib/live/overlay" || \
-               log_warning_msg "W: failed to mount /live/overlay to ${rootmnt}/lib/live/overlay"
-
         # ensure that a potentially stray tmpfs gets removed
         # otherways, initramfs-tools is unable to remove /live
         # and fails to boot