2018-11-07 10:36:54 -07:00
#!/bin/sh
#
2019-02-04 21:08:00 -07:00
# Copyright (c) 2018-2019, Christer Edwards <christer.edwards@gmail.com>
2018-11-07 10:36:54 -07:00
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
. /usr/local/share/bastille/colors.pre.sh
. /usr/local/etc/bastille/bastille.conf
usage( ) {
echo -e " ${ COLOR_RED } Usage: bastille destroy name. ${ COLOR_RESET } "
exit 1
}
destroy_jail( ) {
2018-11-24 09:55:16 -07:00
bastille_jail_base = " ${ bastille_jailsdir } / ${ NAME } " ## dir
2018-11-07 10:36:54 -07:00
bastille_jail_log = " ${ bastille_logsdir } / ${ NAME } _console.log " ## file
2018-11-24 09:55:16 -07:00
if [ $( jls name | grep ${ NAME } ) ] ; then
2018-11-07 10:36:54 -07:00
echo -e " ${ COLOR_RED } Jail running. ${ COLOR_RESET } "
echo -e " ${ COLOR_RED } See 'bastille stop ${ NAME } '. ${ COLOR_RESET } "
exit 1
fi
if [ ! -d " ${ bastille_jail_base } " ] ; then
echo -e " ${ COLOR_RED } Jail not found. ${ COLOR_RESET } "
exit 1
fi
if [ -d " ${ bastille_jail_base } " ] ; then
echo -e " ${ COLOR_GREEN } Deleting Jail: ${ NAME } . ${ COLOR_RESET } "
2019-06-22 09:28:42 -06:00
if [ " ${ bastille_zfs_enable } " = "YES" ] ; then
if [ ! -z " ${ bastille_zfs_zpool } " ] ; then
2019-11-03 01:58:29 -04:00
## remove zfs datasets individually
zfs destroy ${ bastille_zfs_zpool } /${ bastille_zfs_prefix } /jails/${ NAME } /root
2019-06-22 09:28:42 -06:00
zfs destroy ${ bastille_zfs_zpool } /${ bastille_zfs_prefix } /jails/${ NAME }
fi
fi
2019-10-24 17:02:50 -04:00
if [ -d " ${ bastille_jail_base } " ] ; then
## removing all flags
chflags -R noschg ${ bastille_jail_base }
2019-06-22 09:28:42 -06:00
2019-10-24 17:02:50 -04:00
## remove jail base
rm -rf ${ bastille_jail_base }
fi
2019-06-22 09:28:42 -06:00
## archive jail log
if [ -f " ${ bastille_jail_log } " ] ; then
mv ${ bastille_jail_log } ${ bastille_jail_log } -$( date +%F)
echo -e " ${ COLOR_GREEN } Note: jail console logs archived. ${ COLOR_RESET } "
echo -e " ${ COLOR_GREEN } ${ bastille_jail_log } - $( date +%F) ${ COLOR_RESET } "
fi
2018-11-07 10:36:54 -07:00
echo
fi
}
2019-10-24 17:02:50 -04:00
destroy_rel( ) {
bastille_rel_base = " ${ bastille_releasesdir } / ${ NAME } " ## dir
2019-11-03 00:15:02 -04:00
2019-11-02 16:05:49 -04:00
## check if this release have containers child
2019-11-03 00:15:02 -04:00
BASE_HASCHILD = "0"
if [ -d " ${ bastille_jailsdir } " ] ; then
JAIL_LIST = $( ls " ${ bastille_jailsdir } " | sed "s/\n//g" )
for _jail in ${ JAIL_LIST } ; do
if grep -qwo " ${ NAME } " ${ bastille_jailsdir } /${ _jail } /fstab 2>/dev/null; then
echo -e " ${ COLOR_RED } Notice: ( ${ _jail } ) depends on ${ NAME } base. ${ COLOR_RESET } "
BASE_HASCHILD = "1"
fi
done
2019-11-02 16:05:49 -04:00
fi
2019-10-24 17:02:50 -04:00
if [ ! -d " ${ bastille_rel_base } " ] ; then
echo -e " ${ COLOR_RED } Release base not found. ${ COLOR_RESET } "
exit 1
2019-11-03 00:15:02 -04:00
else
if [ " ${ BASE_HASCHILD } " -eq "0" ] ; then
echo -e " ${ COLOR_GREEN } Deleting base: ${ NAME } . ${ COLOR_RESET } "
if [ " ${ bastille_zfs_enable } " = "YES" ] ; then
if [ ! -z " ${ bastille_zfs_zpool } " ] ; then
zfs destroy ${ bastille_zfs_zpool } /${ bastille_zfs_prefix } /releases/${ NAME }
fi
2019-10-24 17:02:50 -04:00
fi
2019-11-03 00:15:02 -04:00
if [ -d " ${ bastille_rel_base } " ] ; then
## removing all flags
chflags -R noschg ${ bastille_rel_base }
2019-10-24 17:02:50 -04:00
2019-11-03 00:15:02 -04:00
## remove jail base
rm -rf ${ bastille_rel_base }
fi
echo
else
echo -e " ${ COLOR_RED } Cannot destroy base with containers child. ${ COLOR_RESET } "
2019-10-24 17:02:50 -04:00
fi
fi
}
2018-11-07 10:36:54 -07:00
# Handle special-case commands first.
case " $1 " in
help| -h| --help)
usage
; ;
esac
if [ $# -gt 1 ] || [ $# -lt 1 ] ; then
usage
fi
NAME = " $1 "
2019-10-24 17:02:50 -04:00
## check what should we clean
2019-11-08 02:37:45 -04:00
case " ${ NAME } " in
*-RELEASE| *-release| *-RC1| *-rc1| *-RC2| *-rc2)
## check for FreeBSD releases name
NAME_VERIFY = $( echo " ${ NAME } " | grep -iwE '^([1-9]{2,2})\.[0-9](-RELEASE|-RC[1-2])$' | tr '[:lower:]' '[:upper:]' )
if [ -n " ${ NAME_VERIFY } " ] ; then
NAME = " ${ NAME_VERIFY } "
2019-10-24 17:02:50 -04:00
destroy_rel
else
2019-11-08 02:37:45 -04:00
usage
2019-10-24 17:02:50 -04:00
fi
2019-11-08 02:37:45 -04:00
; ;
*-stable-LAST| *-STABLE-last| *-stable-last| *-STABLE-LAST)
## check for HardenedBSD releases name
NAME_VERIFY = $( echo " ${ NAME } " | grep -iwE '^([1-9]{2,2})(-stable-LAST|-STABLE-last|-stable-last|-STABLE-LAST)$' | sed 's/STABLE/stable/g' | sed 's/last/LAST/g' )
if [ -n " ${ NAME_VERIFY } " ] ; then
NAME = " ${ NAME_VERIFY } "
destroy_rel
else
usage
fi
; ;
*)
## just destroy a jail
destroy_jail
; ;
esac