- if where_is_mounted ${dev} > /dev/null; then
- mount -o remount,"${opts}" ${dev} $(where_is_mounted ${dev}) || panic "Remounting failed"
- mount -o bind $(where_is_mounted ${dev}) ${mountp} || panic "Cannot bind-mount"
- else
- mount -t $(get_fstype "${dev}") -o "${opts}" "${dev}" "${mountp}" || panic "Cannot mount ${dev} on ${mountp}"
- fi
+ old_mountp="$(where_is_mounted ${dev})"
+
+ if [ -n "${old_mountp}" ]
+ then
+ if [ "${opts}" != "ro" ]
+ then
+ mount -o remount,"${opts}" "${dev}" "${old_mountp}" || panic "Remounting ${dev} ${opts} on ${old_mountp} failed"
+ fi
+
+ mount -o bind "${old_mountp}" "${mountp}" || panic "Cannot bind-mount ${old_mountp} on ${mountp}"
+ else
+ if [ -z "${fstype}" ]
+ then
+ fstype=$(get_fstype "${dev}")
+ fi
+ mount -t "${fstype}" -o "${opts}" "${dev}" "${mountp}" || panic "Cannot mount ${dev} on ${mountp}, fstype=${fstype}, options=${opts}"
+ fi
+}
+
+find_cow_device ()
+{
+ # Returns a device containing a partition labeled "${pers_label}" or containing a file named the same way
+ # in the latter case the partition containing the file is left mounted
+ # if is not in black_listed_devices
+ pers_label="${1}"
+ cow_backing="/${pers_label}-backing"
+ black_listed_devices="${2}"
+
+ for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
+ do
+ for dev in $(subdevices "${sysblock}")
+ do
+ devname=$(sys2dev "${dev}")
+
+ if echo "${black_listed_devices}" | grep -q "${devname}"
+ then
+ # skip this device enterely
+ break
+ fi
+
+ if [ "$(/lib/udev/vol_id -l ${devname} 2>/dev/null)" = "${pers_label}" ]
+ then
+ echo "${devname}"
+ return
+ fi
+
+ if [ "${PERSISTENT}" = "nofiles" ]
+ then
+ # do not mount the device to find for image files
+ # just skip this
+ break
+ fi
+
+ case "$(get_fstype ${devname})" in
+ vfat|ext2|ext3|jffs2)
+ mkdir -p "${cow_backing}"
+ if ! try_mount "${devname}" "${cow_backing}" "rw"
+ then
+ break
+ fi
+
+ if [ -f "${cow_backing}/${pers_label}" ]
+ then
+ echo $(setup_loop "${cow_backing}/${pers_label}" "loop" "/sys/block/loop*")
+ return 0
+ else
+ umount ${cow_backing}
+ fi
+ ;;
+ *)
+ ;;
+ esac
+ done
+ done