Script chroot.sh

From campisano.org
Jump to navigation Jump to search
#!/bin/sh
#
# NAME      /srv/config/chroot.sh
# VERSION   1.4
# REQUIRED  mount, chroot
#



### BEGIN INIT INFO
# Provides:          chroot.sh
# Required-Start:    $remote_fs $local_fs $syslog
# Required-Stop:     $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Chroot Environment
# Description:       A chroot environment used to host
#                    a separate copy of the software system.
### END INIT INFO



PATH=/sbin:/bin:/usr/sbin:/usr/bin;

CHROOT=/srv/CHROOT;
RUNPATH=/srv/config;
SCRIPT="${RUNPATH}/linuxrc.sh";



case "$1" in
    start)
        mount -t proc none "${CHROOT}/proc";
        mount -t sysfs none "${CHROOT}/sys";
        mount -t devpts none "${CHROOT}/dev/pts";
        mkdir -p "${CHROOT}/lib/modules";
        mount -o bind /lib/modules "${CHROOT}/lib/modules";
        #mount -o rbind / "${CHROOT}/HOST";

        /usr/sbin/chroot "${CHROOT}" env -i TERM=linux HOME=/root bash --login --posix -c "${SCRIPT} ${RUNPATH} start";
        RET_CODE=$?;
        ;;
    stop)
        /usr/sbin/chroot "${CHROOT}" env -i TERM=linux HOME=/root bash --login --posix -c "${SCRIPT} ${RUNPATH} stop";
        RET_CODE=$?;

        #umount "${CHROOT}/HOST";
        umount "${CHROOT}/lib/modules";
        umount "${CHROOT}/dev/pts";
        umount "${CHROOT}/sys";
        umount "${CHROOT}/proc";
        ;;
    restart)
        /usr/sbin/chroot "${CHROOT}" env -i TERM=linux HOME=/root bash --login --posix -c "${SCRIPT} ${RUNPATH} stop";
        sleep 3s;
        /usr/sbin/chroot "${CHROOT}" env -i TERM=linux HOME=/root bash --login --posix -c "${SCRIPT} ${RUNPATH} start";
        RET_CODE=$?;
        ;;
    status|reload|force-reload)
        echo "Error: '$1' not supported" >&2;
        RET_CODE=1;
        ;;
    *)
        echo "Usage: $0 {start|stop|restart}";
        RET_CODE=1;
        ;;
esac;



exit "${RET_CODE}";



# End