rwx/sh/alias/overlay.sh

141 lines
3.3 KiB
Bash
Raw Permalink Normal View History

2024-11-25 11:50:43 +00:00
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
2024-11-29 18:45:58 +00:00
rwx_log_error "Unable to bind mount directory: ${directory}"
2024-11-25 11:50:43 +00:00
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
2024-11-29 18:45:58 +00:00
rwx_log_error "Unable to bind unmount directory: ${directory}"
2024-11-25 11:50:43 +00:00
return 1
fi
done
}
2024-11-25 11:51:29 +00:00
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" "${@}"
}
2024-11-25 11:52:21 +00:00
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"
}
2024-11-25 11:49:17 +00:00
orm() { a__overlay_root_mount "${@}"; }
a__overlay_root_mount() {
2024-11-16 02:34:01 +00:00
local root="${1}"
2024-11-16 03:04:44 +00:00
if [ -z "${root}" ]; then
2024-11-29 18:45:58 +00:00
rwx_log_error "No root target directory"
2024-11-16 02:34:01 +00:00
return 1
2024-11-12 09:11:35 +00:00
fi
2024-11-16 03:04:44 +00:00
root="$(realpath "${root}")"
if ! mkdir "overlay"; then
2024-11-29 18:45:58 +00:00
rwx_log_error "Unable to make overlay directory"
2024-11-16 03:04:44 +00:00
return 2
fi
2024-11-16 03:23:54 +00:00
(
2024-11-16 03:04:44 +00:00
if ! cd "overlay"; then
2024-11-29 18:45:58 +00:00
rwx_log_error "Unable to move into overlay directory"
2024-11-16 03:04:44 +00:00
return 3
fi
local directory
for directory in "lower" "upper" "work" "mount"; do
if ! mkdir --parents "${directory}"; then
2024-11-29 18:45:58 +00:00
rwx_log_error "Unable to make directory: ${directory}"
2024-11-16 03:04:44 +00:00
return 4
fi
done
local file="${root}/filesystem.squashfs"
if ! mount "${file}" "lower"; then
2024-11-29 18:45:58 +00:00
rwx_log_error "Unable to lower mount: ${file}"
2024-11-16 03:04:44 +00:00
return 5
fi
if ! mount \
-o "lowerdir=lower,upperdir=upper,workdir=work" \
-t "overlay" \
"overlay" "mount"; then
2024-11-29 18:45:58 +00:00
rwx_log_error "Unable to overlay mount"
2024-11-16 03:04:44 +00:00
return 6
fi
2024-11-16 03:23:54 +00:00
)
2023-05-09 20:02:34 +00:00
}
2024-11-25 11:53:01 +00:00
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
}
2024-11-25 11:49:17 +00:00
oru() { a__overlay_root_unmount "${@}"; }
a__overlay_root_unmount() {
2024-11-16 03:23:54 +00:00
(
if ! cd "overlay"; then
2024-11-29 18:45:58 +00:00
rwx_log_error "Unable to move into overlay directory"
2024-11-16 03:23:54 +00:00
return 1
fi
if ! umount "mount"; then
2024-11-29 18:45:58 +00:00
rwx_log_error "Unable to unmount mount directory"
2024-11-16 03:23:54 +00:00
return 2
fi
if ! rmdir "mount"; then
2024-11-29 18:45:58 +00:00
rwx_log_error "Unable to remove mount directory"
2024-11-16 03:23:54 +00:00
return 3
fi
local directory
for directory in "upper" "work"; do
if ! rm --force --recursive "${directory}"; then
2024-11-29 18:45:58 +00:00
rwx_log_error "Unable to remove directory: ${directory}"
2024-11-16 03:23:54 +00:00
return 4
fi
done
if ! umount "lower"; then
2024-11-29 18:45:58 +00:00
rwx_log_error "Unable to unmount lower directory"
2024-11-16 03:23:54 +00:00
return 5
fi
if ! rmdir "lower"; then
2024-11-29 18:45:58 +00:00
rwx_log_error "Unable to remove lower directory"
2024-11-16 03:23:54 +00:00
return 6
2024-11-12 09:11:35 +00:00
fi
2024-11-16 03:23:54 +00:00
)
if ! rmdir "overlay"; then
2024-11-29 18:45:58 +00:00
rwx_log_error "Unable to remove overlay directory"
2024-11-16 03:23:54 +00:00
return 7
2024-11-12 09:11:35 +00:00
fi
2023-05-09 20:02:34 +00:00
}