rwx/sh/alias/overlay.sh
2024-11-29 19:45:58 +01:00

140 lines
3.3 KiB
Bash

obm() { a__overlay_bind_mount "${@}"; }
a__overlay_bind_mount() {
local directory
for directory in "dev" "dev/pts" "proc" "sys"; do
if ! mount --bind "/${directory}" "overlay/mount/${directory}"; then
rwx_log_error "Unable to bind mount directory: ${directory}"
return 1
fi
done
}
obu() { a__overlay_bind_unmount "${@}"; }
a__overlay_bind_unmount() {
local directory
for directory in "sys" "proc" "dev/pts" "dev"; do
if ! umount --lazy "overlay/mount/${directory}"; then
rwx_log_error "Unable to bind unmount directory: ${directory}"
return 1
fi
done
}
ocr() { a__overlay_command_root "${@}"; }
a__overlay_command_root() {
chroot \
"overlay/mount" "${@}"
}
ocu() { a__overlay_command_user "${@}"; }
a__overlay_command_user() {
chroot \
--userspec "1000:1000" \
"overlay/mount" "${@}"
}
omm() { a__overlay_mirror_mount "${@}"; }
a__overlay_mirror_mount() {
mount --make-rslave --rbind "/deb" "overlay/mount/deb"
}
omu() { a__overlay_mirror_unmount "${@}"; }
a__overlay_mirror_unmount() {
umount --recursive "overlay/mount/deb"
}
orm() { a__overlay_root_mount "${@}"; }
a__overlay_root_mount() {
local root="${1}"
if [ -z "${root}" ]; then
rwx_log_error "No root target directory"
return 1
fi
root="$(realpath "${root}")"
if ! mkdir "overlay"; then
rwx_log_error "Unable to make overlay directory"
return 2
fi
(
if ! cd "overlay"; then
rwx_log_error "Unable to move into overlay directory"
return 3
fi
local directory
for directory in "lower" "upper" "work" "mount"; do
if ! mkdir --parents "${directory}"; then
rwx_log_error "Unable to make directory: ${directory}"
return 4
fi
done
local file="${root}/filesystem.squashfs"
if ! mount "${file}" "lower"; then
rwx_log_error "Unable to lower mount: ${file}"
return 5
fi
if ! mount \
-o "lowerdir=lower,upperdir=upper,workdir=work" \
-t "overlay" \
"overlay" "mount"; then
rwx_log_error "Unable to overlay mount"
return 6
fi
)
}
ors() { a__overlay_root_squash "${@}"; }
a__overlay_root_squash() {
local directory="${1}"
local file
local level="${2}"
if [ -n "${directory}" ]; then
if mkdir "${directory}"; then
[ -n "${level}" ] || level="18"
for file in "vmlinuz" "initrd.img"; do
cp "overlay/mount/${file}" "${directory}"
done
mksquashfs \
"overlay/mount" "${directory}/filesystem.squashfs" \
-noappend \
-comp "zstd" -Xcompression-level "${level}"
chown --recursive 1000:1000 "${directory}"
fi
fi
}
oru() { a__overlay_root_unmount "${@}"; }
a__overlay_root_unmount() {
(
if ! cd "overlay"; then
rwx_log_error "Unable to move into overlay directory"
return 1
fi
if ! umount "mount"; then
rwx_log_error "Unable to unmount mount directory"
return 2
fi
if ! rmdir "mount"; then
rwx_log_error "Unable to remove mount directory"
return 3
fi
local directory
for directory in "upper" "work"; do
if ! rm --force --recursive "${directory}"; then
rwx_log_error "Unable to remove directory: ${directory}"
return 4
fi
done
if ! umount "lower"; then
rwx_log_error "Unable to unmount lower directory"
return 5
fi
if ! rmdir "lower"; then
rwx_log_error "Unable to remove lower directory"
return 6
fi
)
if ! rmdir "overlay"; then
rwx_log_error "Unable to remove overlay directory"
return 7
fi
}