From 8634aa8bb70cc86fc3e5b5e07ff72acd0decc2d0 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Thu, 21 Aug 2014 09:37:44 -0400 Subject: lib/chroot.sh: New file --- (limited to 'lib/chroot.sh') diff --git a/lib/chroot.sh b/lib/chroot.sh new file mode 100644 index 0000000..de3b2f5 --- /dev/null +++ b/lib/chroot.sh @@ -0,0 +1,79 @@ +# ProteanOS Development Kit +# lib/chroot.sh +# Functions for setting up isolated file system environments +# +# Copyright (C) 2014 Patrick "P. J." McDermott +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +[ "x${_CHROOT_SM+set}" = 'xset' ] && return 0 +_CHROOT_SM=1 + +use profile + +chroot_mount() +{ + local root="${1}" + local arch= + local plat= + local fs= + local dir= + local fstype= + local options= + + arch="$(cat "${root}/etc/proteanos_arch")" + plat="$(cat "${root}/etc/proteanos_plat")" + + while read fs dir fstype options; do + mount -t "${fstype}" -o "${options}" "${fs}" "${root}/${dir}" + done <<-EOF + $(profile_get_fstab "${arch}" "${plat}") + EOF +} + +chroot_umount() +{ + local root="${1}" + local arch= + local plat= + local fs= + local dir= + local fstype= + local options= + + arch="$(cat "${root}/etc/proteanos_arch")" + plat="$(cat "${root}/etc/proteanos_plat")" + + while read fs dir fstype options; do + # umount sometimes complains that the /dev file system is busy. + # Here's a kludge to try to handle that. We better make sure + # bind mounts get unmounted; otherwise, `rm -Rf ${root}` can be + # painful. + while ! umount "${root}/${dir}"; do + sleep 1 + done + done <<-EOF + $(profile_get_fstab "${arch}" "${plat}" | sed -n '1!G;h;$p') + EOF +} + +chroot_exec() +{ + local root="${1}" + shift 1 + + chroot_mount "${root}" + chroot "${root}" "${@}" + chroot_umount "${root}" +} -- cgit v0.9.1