#!/bin/bash
#  chroot - chroot support

set -e

lib_load 'sys/tool/mount'


######
# Settings

sys_tool_chroot_config_init() {
	lib_setting_vars chroot_mntdir
	lib_setting_arrays -ro chroot_mounts

	chroot_mntdir="$script_tempdir/mnt"
	chroot_mounts=( /dev /dev/pts /proc /sys /run )
}


######
# Public interface

#  $1 - chroot name
#  $2 - full path of file to create inside the chroot
chroot_filename() {
	has_args 2 "$@"
	echo "$chroot_mntdir/$1$2"
}

#  $1 - chroot name
#  $2 - full path of file to create inside the chroot
chroot_write_file() {
	has_args 2 "$@"
	local name=$1
	local path=$2
	local file
	file=$(chroot_filename "$name" "$path")
	dir=$(dirname "$file")
	run_sudo mkdir -pv "$dir"
	run_sudo tee "$file" >/dev/null
}

#  $1 - chroot name
run_chroot() {
	min_args 1 "$@"
	local name=$1
	shift
	run_sudo chroot "$(chroot_filename "$name" "/")" "$@"
}


######
# Mounting support

#  $1 - chroot name
chroot_mount() {
	has_args 1 "$@"
	for_each "chroot_mount_partition $1" "${chroot_mounts[@]}"
}

#  $1 - chroot name
#  $2 - mount point
chroot_mount_partition() {
	has_args 2 "$@"
	run_mount --bind "$2" "$(chroot_filename "$1" "$2")"
}

#  $1 - chroot name
chroot_umount() {
	has_args 1 "$@"

	local -a mounts
	mounts=( "${chroot_mounts[@]}" )
	list_reverse mounts

	for_each "chroot_umount_partition $1" "${mounts[@]}"
}

#  $1 - chroot name
#  $2 - mount point
chroot_umount_partition() {
	has_args 2 "$@"
	run_umount "$(chroot_filename "$1" "$2")"
}

View the Developer Guide Index

View the Reference Manual Index


Generated on Fri Jul 28 14:36:06 PDT 2017 by mcsh d14 v0.23.0.