diff options
author | P. J. McDermott <pj@pehjota.net> | 2014-08-21 09:37:44 (EDT) |
---|---|---|
committer | P. J. McDermott <pj@pehjota.net> | 2014-08-21 09:37:44 (EDT) |
commit | 8634aa8bb70cc86fc3e5b5e07ff72acd0decc2d0 (patch) | |
tree | c93fa4e34c17ac93239ebb057e481d978488adda /lib | |
parent | e5a0731690b77f1b2863b04095f69ee6d0913fe7 (diff) |
lib/chroot.sh: New file
Diffstat (limited to 'lib')
-rw-r--r-- | lib/chroot.sh | 79 | ||||
-rw-r--r-- | lib/local.mk | 1 |
2 files changed, 80 insertions, 0 deletions
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 <http://www.gnu.org/licenses/>. + +[ "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}" +} diff --git a/lib/local.mk b/lib/local.mk index 2fc6dfd..1ed0c31 100644 --- a/lib/local.mk +++ b/lib/local.mk @@ -6,5 +6,6 @@ pkgdata_sources = \ lib/control.sh \ lib/feed.sh \ lib/pkg.sh \ + lib/chroot.sh \ lib/cmd.sh \ lib/profile.sh |