mkjail.sh

Check-in [bb7ddbd8cd]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Commit missing files. I don't understand why my merge from the other branch did't do this...
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | master | trunk
Files: files | file ages | folders
SHA3-256:bb7ddbd8cd79fb7023ee71c555ac3522bef8367e5d7c069d41d427fb37c9070e
User & Date: feld 2018-10-03 16:57:13
Context
2018-10-03
17:03
Force fetching always check-in: 73b796ff96 user: feld tags: master, trunk
16:57
Commit missing files. I don't understand why my merge from the other branch did't do this... check-in: bb7ddbd8cd user: feld tags: master, trunk
2018-10-02
21:25
Cannot reliable use set -u in this script check-in: 32a9c7f9ae user: feld tags: master, trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added src/bin/mkjail.

































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/sh
# Lazy, dirty tool for creating fat jails.
#-
# Copyright (c) 2018 Mark Felder
# All rights reserved
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted providing that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. 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.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.

set -e
set -u

export PATH=/bin:/sbin:/usr/bin:/usr/sbin

if [ $(id -u) -ne 0 ]; then
    echo "Error: must run as root or with sudo."
    exit 1
fi

show_help() {
cat <<HELP
usage: mkjail command [options]

Commands:
    create      -- Create jail
    update      -- Update jail with latest FreeBSD security/errata patches
    upgrade     -- Upgrade jail to a newer RELEASE

mkjail.sh: 2018, feld@FreeBSD.org

HELP
exit 0
}

[ $# -lt 1 ] && show_help

CMD=$1

MKJAILPATH=`realpath $0`
if [ "${MKJAILPATH%src/bin/mkjail}" != "${MKJAILPATH}" ]; then
        # It is running from src/bin/mkjail in checkout
        MKJAILPREFIX=${MKJAILPATH%/bin/*}
        set -a
        . ${MKJAILPREFIX}/etc/mkjail.conf
        set +a
elif [ "${MKJAILPATH%/bin/*}" = "${MKJAILPATH}" ]; then
        # It is running in a build directory or the source checkout as
        # ./mkjail.  Lookup VPATH to resolve to source checkout if in
        # build directory.
        [ -f Makefile ] && VPATH="$(make -V VPATH)"
        MKJAILPREFIX="${MKJAILPATH%/mkjail}${VPATH:+/${VPATH}}/src"
        [ -n "${VPATH}" ] && MKJAILPREFIX="$(realpath "${MKJAILPREFIX}")"
        set -a
        . ${MKJAILPREFIX}/etc/mkjail.conf
        set +a
else
        # Running from PREFIX/bin/mkjail
        MKJAILPREFIX=${MKJAILPATH%/bin/*}
        set -a
        . ${MKJAILPREFIX}/etc/mkjail.conf
        set +a
fi

export SCRIPTPREFIX=${MKJAILPREFIX}/share/mkjail
SCRIPTPATH="${SCRIPTPREFIX}/${CMD}.sh"

case "${CMD}" in
create|update|upgrade)
    ;;
*)
    echo "Unknown command '${CMD}'"
    show_help
    ;;
esac

: ${SH:=sh}

exec ${SH} "${SCRIPTPATH}" "$@"

Added src/share/mkjail/getrelease.sh.













































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/sh
set -u
trap _cleanup HUP INT QUIT KILL TERM ABRT

: ${ARCH=$(uname -m)}
RELEASE=${1}
: ${SETS="base doc lib32"}

_cleanup()
{
    echo ""
    echo "Error: could not fetch dists. Cleaning up."
    rm -rf /var/db/mkjail/releases/${ARCH}/${RELEASE}
    exit 1
}

mkdir -p /var/db/mkjail/releases/${ARCH}/${RELEASE}
cd /var/db/mkjail/releases/${ARCH}/${RELEASE}

for i in ${SETS}; do 
   fetch https://download.freebsd.org/ftp/releases/${ARCH}/${RELEASE}/${i}.txz || _cleanup
done

Added src/share/mkjail/update.sh.





























































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/sh
set -e
set -u

PAGER=cat

_alljails()
{
    echo "Updating all jails..."
    echo ""
    for i in $(jls -q name); do
      echo "Updating ${i} jail..."
      echo ""
      export UNAME_r=$(zfs get -H mkjail:version ${JAILROOT}/${i} | awk '{print $3}')
      freebsd-update -b ${JAILROOT}/${i} -f ${JAILROOT}/${i}/etc/freebsd-update.conf fetch install
    done
    exit 0
}

_onejail()
{
    echo "Updating ${JAILNAME} jail..."
    echo ""
    export UNAME_r=$(zfs get -H mkjail:version ${JAILROOT}/${JAILNAME} | awk '{print $3}')
    freebsd-update -b ${JAILROOT}/${JAILNAME} -f ${JAILROOT}/${JAILNAME}/etc/freebsd-update.conf fetch install
    exit 0
}

show_help() {
cat <<HELP
usage: mkjail update [-a] | [-j JAILNAME]

        -a Update all running jails
        -h Show help
        -j Jail name

mkjail.sh: 2018, feld@FreeBSD.org

HELP
exit 0
}

# option parsing has to happen below the show_help
# shift to skip the first argument or getopts loses its mind
shift
while getopts "ahj:" opt; do
    case "${opt}" in
        a)  _alljails
            ;;
        h)  show_help
            ;;
        j)  JAILNAME=${OPTARG}
            _onejail
            ;;
        *)  show_help
            ;;
    esac
done

shift $((OPTIND - 1))

show_help

Added src/share/mkjail/upgrade.sh.





















































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/bin/sh
set -e
set -u
trap _cleanup HUP INT QUIT KILL TERM ABRT

aflag=0
jflag=0
PAGER=cat
SNAPNAME="mkjail-$(date '+%Y%m%d%H%M')"

_upgradejail()
{
    _validate
    _snapshot
    echo "Updating ${JAILNAME} jail..."
    echo ""
    export UNAME_r=${MKJAILVER}
    freebsd-update -b ${JAILROOT}/${JAILNAME} -f ${JAILROOT}/${JAILNAME}/etc/freebsd-update.conf upgrade -r ${VERSION} || _cleanup
    freebsd-update -b ${JAILROOT}/${JAILNAME} -f ${JAILROOT}/${JAILNAME}/etc/freebsd-update.conf install || _cleanup
    freebsd-update -b ${JAILROOT}/${JAILNAME} -f ${JAILROOT}/${JAILNAME}/etc/freebsd-update.conf install || _cleanup
    jexec ${JAILNAME} pkg-static install -fy pkg || _cleanup
    jexec ${JAILNAME} pkg-static upgrade -fy || _cleanup
    zfs set mkjail:version=${VERSION} ${ZPOOL}${JAILROOT}/${JAILNAME}
}

_alljails()
{
    for i in $(jls -q name); do
      JAILNAME=${i}
      _upgradejail
    done
}

_validate()
{
    # Check for valid parameters
    if [ "${VERSION}x" = "x" ]; then
      show_help
    fi

    # Ensure jail is actually running
    jls -j ${JAILNAME} 2> /dev/null 1> /dev/null
    if [ ${?} -ne "0" ]; then
        echo "Error: jail ${JAILNAME} not running."
        exit 1
    fi

    # Capture mkjail:version zfs property for rollback
    export MKJAILVER="$(zfs get -H mkjail:version ${JAILROOT}/${JAILNAME} | awk '{print $3}')"
}

_snapshot()
{
    zfs snapshot ${ZPOOL}${JAILROOT}/${JAILNAME}@${SNAPNAME}
}

_rollback()
{
    zfs rollback ${ZPOOL}${JAILROOT}/${JAILNAME}@${SNAPNAME}
}

_rmsnap()
{
    zfs destroy ${ZPOOL}${JAILROOT}/${JAILNAME}@${SNAPNAME}
}

_cleanup()
{
    echo ""
    echo "Upgrade cancelled: reverting changes and cleaning up."
    _rollback
    _rmsnap
    zfs set mkjail:version=${MKJAILVER} ${ZPOOL}${JAILROOT}/${JAILNAME}
    exit 1
}

show_help() {
cat <<HELP
usage: mkjail upgrade [-a] [-v VERSION] | [-j JAILNAME] [-v VERSION]

        -a Upgrade all running jails
        -h Show help
        -j Jail name
        -v FreeBSD version (e.g., 11.1-RELEASE)

mkjail.sh: 2018, feld@FreeBSD.org

HELP
exit 0
}

# option parsing has to happen below the show_help
# shift to skip the first argument or getopts loses its mind
shift
while getopts "ahj:v:" opt; do
    case "${opt}" in
        a)  aflag=1
            ;;
        h)  show_help
            ;;
        j)  jflag=1; JAILNAME=${OPTARG}
            ;;
        v)  VERSION=${OPTARG}
            ;;
        *)  show_help
            ;;
    esac
done

if [ ${aflag} = 1 ] && [ ${jflag} = 1 ]; then
    show_help
fi

if [ ${aflag} = 1 ]; then
    _alljails
fi

if [ ${jflag} = 1 ]; then
    _upgradejail
fi

show_help