mkjail.sh

Check-in [e309fe668f]
Login

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

Overview
Comment:Remove old scripts
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | master | trunk
Files: files | file ages | folders
SHA3-256:e309fe668f9633853606d15a62241db6be93c07f460fd5ae6395314fad860936
User & Date: feld 2018-10-02 21:21:38
Context
2018-10-02
21:24
Merge properly (I think) check-in: 2439df6e88 user: feld tags: master, trunk
21:21
Remove old scripts check-in: e309fe668f user: feld tags: master, trunk
2018-08-29
18:03
Change preferred FreeBSD update mirror check-in: b1020a38f6 user: feld tags: master, trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Deleted getrelease.sh.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/bin/sh

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

ARCH=$(uname -m)

dists="base.txz doc.txz lib32.txz"

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

for i in $dists; do 
   fetch https://download.freebsd.org/ftp/releases/${ARCH}/${1}/${i}
done

<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<




































Deleted mkjail.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/bin/sh
# Lazy, dirty tool for creating fat jails.
#-
# Copyright (c) 2015 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.

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

ARCH=$(uname -m)

# Set your zpool name
zpool="zroot"

# Set jailroot
jailroot=/jails

# Sets you want extracted into new jail
# options include: base, doc, games (deprecated), kernel, lib32, ports, src
SETS="base doc lib32"

# The "version" names are names of dirs you have downloaded the release
# tarballs into. They are found in e.g., /var/db/mkjail/releases/amd64/10.1-RELEASE.
#
# Note, it is important to name the dir the full name of the release
# (10.1-RELEASE) as this information is added to a ZFS property used to
# track which version the jail is. It is then used by the updatejail.sh
# script to correctly update the jail with freebsd-update.
#
# The flavours are directories with config files you want copied into
# the new jail upon creation. If you have a flavour named "foo", you
# would create /var/db/mkjail/flavours/foo/etc/something.conf to have that
# copied to jail's /etc/something.conf upon creation.
#
#
# Nothing below here is configurable but I remind you that I did not
# build in any seatbelts :-)
#

ip4int=$(route -4 get default | awk '/interface: / {print $2}')
ip6int=$(route -6 get default | awk '/interface: / {print $2}')
ip4guess=$(ifconfig ${ip4int} | awk '/inet / && !/127.0/ {print $2}' | head -n 1)
ip6guess=$(ifconfig ${ip6int} | awk '/inet6 / && !/(fe80| ::1)/ {print $2}' | head -n 1)

show_help() {
cat <<HELP
usage: mkjail [-j JAILNAME] [-v VERSION] [-f FLAVOUR] [-s "SETS"]

	-f Flavour, copy in files after creation
	-h View this help
	-j Jail name
	-s Sets: "base doc games lib32"
	-v Version of jail (9.3-RELEASE, 10.1-RELEASE, etc)

mkjail.sh: 2015, feld@FreeBSD.org

HELP
}

exit_opts_req() {
    echo "Both -j and -v must be specified." >&2
    echo ""
    show_help
    exit 1
}


# option parsing has to happen below the show_help
while getopts "f:h:j:v:s:" opt; do
    case ${opt} in
        f)  fflag=1; FLAVOUR=${OPTARG}
            ;;
        h)
            show_help
            exit 0
            ;;
        j)  jflag=1; JAILNAME=${OPTARG}
            ;;
        s)  sflag=1; SETS=${OPTARG}
            ;;
        v)  vflag=1; VERSION=${OPTARG}
            ;;
    esac
done

shift $(($OPTIND - 1))

if [ -z $jflag ]
then
    exit_opts_req
fi

if [ -z $vflag ]
then
    exit_opts_req
fi

_build() {
# Make sure the release exists
if [ ! -d /var/db/mkjail/releases/${ARCH}/${VERSION} ]; then
    echo "Release ${VERSION} does not exist. Exiting."
    exit 1
fi

# Make sure target flavor exists
if [ x"${fflag}" = x1 ] && [ ! -d /var/db/mkjail/flavours/${FLAVOUR} ]; then
    echo "Error: flavour ${FLAVOUR} does not exist. Please create it first."
    exit 1
fi

# Create the ZFS filesystem
echo "Creating ${zpool}/jails/${JAILNAME}..."
zfs create -p -o mountpoint=/jails ${zpool}/jails
zfs create -p ${zpool}/jails/${JAILNAME}
zfs set mkjail:version=${VERSION} ${zpool}/jails/${JAILNAME}

# Extract the files
for set in $(echo ${SETS}); do
    echo "Extracting ${set} into ${jailroot}/${JAILNAME}..."
    tar -xf /var/db/mkjail/releases/${ARCH}/${VERSION}/$set.txz -C ${jailroot}/${JAILNAME} ;
done

if [ x"${fflag}" = x1 ] ; then
    # put in default configs:
    echo "Copying in our configs..."
    cp -a /var/db/mkjail/flavours/${FLAVOUR}/ ${jailroot}/${JAILNAME}
fi
}

_docs() {
# Give instructions
cat <<DOCS

Now put something like the following in /etc/jail.conf:

exec.start = "/bin/sh /etc/rc";
exec.stop = "/bin/sh /etc/rc.shutdown";
exec.clean;
mount.devfs;
path = ${jailroot}/\$name;
securelevel = 2;

${JAILNAME} {
    host.hostname = "${JAILNAME}";
    ip4.addr = ${ip4guess};
    ip6.addr = ${ip6guess};
    persist;
}

and then you can start the jail like so:

# service jail start ${JAILNAME}

DOCS
}

_build
_docs
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<










































































































































































































































































































































































Deleted updatejail.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
#!/bin/sh

export JAILROOT=/jails
export PAGER=cat
export JAILNAME=${@}
export MIRROR=update5.freebsd.org

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

_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 -s ${MIRROR} -b ${JAILROOT}/${i} -f ${JAILROOT}/${i}/etc/freebsd-update.conf fetch install
done
}

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

if [ "x${@}" = "x" ] ; then
	_alljails
else
	_onejail
fi
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<










































































Deleted upgradejail.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
#!/bin/sh
set -e
trap _cleanup HUP INT QUIT KILL TERM ABRT

export ZPOOL=zroot
export JAILROOT=/jails
export PAGER=cat
export JAILNAME=${1}
export VERSION=${2}
export MIRROR=update6.freebsd.org
export SNAPNAME="mkjail-$(date '+%Y%m%d%H%M')"

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

_upgradejail()
{
    _validate
    _snapshot
    echo "Updating ${JAILNAME} jail..."
    echo ""
    export UNAME_r=${MKJAILVER}
    freebsd-update -s ${MIRROR} -b ${JAILROOT}/${JAILNAME} -f ${JAILROOT}/${JAILNAME}/etc/freebsd-update.conf upgrade -r ${VERSION} || _cleanup
    freebsd-update -s ${MIRROR} -b ${JAILROOT}/${JAILNAME} -f ${JAILROOT}/${JAILNAME}/etc/freebsd-update.conf install || _cleanup
    freebsd-update -s ${MIRROR} -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
      _usage
    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
}

_usage()
{
    echo "usage: $0 [JAILNAME] [VERSION]"
    echo ""
    echo "ex: $0 mysql 10.3-RELEASE"
    echo ""
    exit 1
}

case "${JAILNAME}" in
    alljails) _alljails
      ;;
    "")  _usage
      ;;
     *)  _upgradejail
      ;;
esac
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<