новости

Простенький скрипт для создания chroot под FreeBSD

Простенький и незатейливый скрипт позволяет быстро создать и уничтожить chroot окружение.


#!/bin/sh

# VERSION 0.0.1
# Script creted by Ryabinin Sergey Vladimirovich
# [dev@brigar.ru](mailto:dev@brigar.ru)
#


set -e

help(){
printf "Usage:

Create chroot
$0 -c your_name_chroot

Mount chroot
$0 -m your_name_chroot

Umount chroot
$0 -u your_name_chroot

Destroy chroot
$0 -r your_name_chroot
"
}

chroot_create(){

CHROOT_DIR="/chroot/$1"

mkdir -p ${CHROOT_DIR}
mkdir -p ${CHROOT_DIR}/dev
mkdir -p ${CHROOT_DIR}/bin
mkdir -p ${CHROOT_DIR}/libexec
mkdir -p ${CHROOT_DIR}/lib
mkdir -p ${CHROOT_DIR}/usr/lib
mkdir -p ${CHROOT_DIR}/usr/ports
mkdir -p ${CHROOT_DIR}/usr/bin
mkdir -p ${CHROOT_DIR}/usr/sbin
mkdir -p ${CHROOT_DIR}/usr/share

mount -t nullfs -o ro,nosuid,noexec /usr/ports/ ${CHROOT_DIR}/usr/ports
mount -t nullfs -o ro,nosuid /usr/lib/ ${CHROOT_DIR}/usr/lib
mount -t nullfs -o ro,nosuid /libexec/ ${CHROOT_DIR}/libexec
mount -t nullfs -o ro,nosuid /lib/ ${CHROOT_DIR}/lib
mount -t nullfs -o ro,nosuid /usr/sbin/ ${CHROOT_DIR}/usr/sbin
mount -t nullfs -o ro,nosuid /usr/bin/ ${CHROOT_DIR}/usr/bin
mount -t nullfs -o ro,nosuid /bin/ ${CHROOT_DIR}/bin
mount -t nullfs -o ro,nosuid /usr/share/ ${CHROOT_DIR}/usr/share
mount -t devfs devfs ${CHROOT_DIR}/dev

printf "Chroot created: ${CHROOT_DIR} and entered
"

chroot ${CHROOT_DIR}
}


chroot_destroy(){
CHROOT_DIR="/chroot/$1"
umount -f ${CHROOT_DIR}/usr/lib
umount -f ${CHROOT_DIR}/libexec
umount -f ${CHROOT_DIR}/lib
umount -f ${CHROOT_DIR}/usr/ports
umount -f ${CHROOT_DIR}/usr/sbin
umount -f ${CHROOT_DIR}/bin
umount -f ${CHROOT_DIR}/usr/bin
umount -f ${CHROOT_DIR}/dev
umount -f ${CHROOT_DIR}/usr/share

rm -rf ${CHROOT_DIR}
echo "Chroot destroy: ${CHROOT_DIR}"
}

chroot_umount(){
CHROOT_DIR="/chroot/$1"
umount -f ${CHROOT_DIR}/usr/lib
umount -f ${CHROOT_DIR}/libexec
umount -f ${CHROOT_DIR}/lib
umount -f ${CHROOT_DIR}/usr/ports
umount -f ${CHROOT_DIR}/usr/sbin
umount -f ${CHROOT_DIR}/usr/bin
umount -f ${CHROOT_DIR}/bin
umount -f ${CHROOT_DIR}/dev
umount -f ${CHROOT_DIR}/usr/share

echo "Chroot umount: ${CHROOT_DIR}"
}

chroot_mount(){
CHROOT_DIR="/chroot/$1"
mount -t nullfs -o ro,nosuid,noexec /usr/lib/ ${CHROOT_DIR}/usr/lib
mount -t nullfs -o ro,nosuid,noexec /libexec/ ${CHROOT_DIR}/libexec
mount -t nullfs -o ro,nosuid,noexec /lib/ ${CHROOT_DIR}/lib
mount -t nullfs -o ro,nosuid,noexec /usr/ports/ ${CHROOT_DIR}/usr/ports
mount -t nullfs -o ro,nosuid /usr/sbin/ ${CHROOT_DIR}/usr/sbin
mount -t nullfs -o ro,nosuid /usr/bin/ ${CHROOT_DIR}/usr/bin
mount -t nullfs -o ro,nosuid /bin/ ${CHROOT_DIR}/bin
mount -t nullfs -o ro,nosuid /usr/share/ ${CHROOT_DIR}/usr/share
mount -t devfs devfs ${CHROOT_DIR}/dev

echo "Chroot mount: ${CHROOT_DIR}"
}


if [ "${#1}" -eq 0 ] || [ "${#2}" -eq 0 ]; then
help
fi

if [ "${1}" = "-c" ] && [ "${#2}" -gt 0 ]; then
chroot_create $2
fi

if [ "${1}" = "-u" ] && [ "${#2}" -gt 0 ]; then
chroot_umount $2
fi

if [ "${1}" = "-r" ] && [ "${#2}" -gt 0 ]; then
chroot_destroy $2
fi

if [ "${1}" = "-m" ] && [ "${#2}" -gt 0 ]; then
chroot_mount $2
fi

Последнею версию всегда можно будет найти по ссылке chroot_cr