source: src-sh/warden/scripts/backend/createtemplate.sh

Last change on this file was 698a249, checked in by Kris Moore <kris@…>, 5 weeks ago

When using warden to create jails, do all ZFS ops relative to the
JAILDIR dataset, this will allow moving JAILDIR around, and fix
issues with odd dataset creation.

This fix implies that your JAILDIR is indeed a mounted ZFS dataset,
but we have assumed that for a while now anyway.

  • Property mode set to 100755
File size: 5.1 KB
RevLine 
[1f8e4d7]1#!/bin/sh
2# Script to create a new jail template
3#####################################################################
4
5# Source our functions
6PROGDIR="/usr/local/share/warden"
7
8# Source our variables
9. ${PROGDIR}/scripts/backend/functions.sh
10
11### Download the template files
12download_template_files() {
13
14  # Create the download directory
15  if [ -d "${JDIR}/.download" ] ; then rm -rf ${JDIR}/.download; fi
16  mkdir ${JDIR}/.download
17
18  if [ ! -d "${JDIR}" ] ; then mkdir -p "${JDIR}" ; fi
19  cd ${JDIR}
20
21  echo "Fetching jail environment. This may take a while..."
22  if [ -n "$TRUEOSVER" ] ; then
23     for f in $DFILES
24     do
[9788f38]25       get_file_from_mirrors "/${TRUEOSVER}/${FBSDARCH}/dist/$f" "${JDIR}/.download/$f" "iso"
[1f8e4d7]26       if [ $? -ne 0 ] ; then
27         exit_err "Failed downloading: /${TRUEOS}/${FBSDARCH}/dist/${f}"
28       fi
29     done
30  else
[5093cde]31     
[d43d5be]32     # Start looking for current versions of FreeBSD
33     found=0
[1f8e4d7]34     for f in $DFILES
35     do
[228303d]36       fetch -o "${JDIR}/.download/$f" "ftp://ftp.freebsd.org/pub/FreeBSD/releases/${FBSDARCH}/${FBSDARCH}/${FBSDVER}/$f"
[1f8e4d7]37       if [ $? -ne 0 ] ; then
[aa37de2]38         echo "Trying ftp-archive..."
[228303d]39         fetch -o "${JDIR}/.download/$f" "http://ftp-archive.freebsd.org/pub/FreeBSD-Archive/old-releases/${FBSDARCH}/${FBSDARCH}/${FBSDVER}/$f"
[d43d5be]40         if [ $? -ne 0 ] ; then found=1 ; break; fi
[1f8e4d7]41       fi
42     done
[d43d5be]43
44     if [ $found -eq 0 ] ; then return ; fi
45
46     found=0
47     # Check if we are on old versions of FreeBSD
48     # Get the .inf list file
49     echo "Looking for old versions of FreeBSD"
50     fetch -o "${JDIR}/.download/${oldStr}.inf" "ftp://ftp.freebsd.org/pub/FreeBSD/releases/${FBSDARCH}/${FBSDVER}/$oldStr/${oldStr}.inf"
51     if [ $? -ne 0 ] ; then
52       # Check the ftp-archive
53       echo "Looking for older versions of FreeBSD"
54       fetch -o "${JDIR}/.download/${oldStr}.inf" "http://ftp-archive.freebsd.org/pub/FreeBSD-Archive/old-releases/${FBSDARCH}/${FBSDVER}/${oldStr}/${oldStr}.inf"
55       if [ $? -ne 0 ] ; then
56         exit_err "Failed downloading: FreeBSD ${FBSDVER} - ${oldStr}.inf"
57       fi
58       fDir="http://ftp-archive.freebsd.org/pub/FreeBSD-Archive/old-releases/${FBSDARCH}/${FBSDVER}/${oldStr}"
59     else
60       fDir="ftp://ftp.freebsd.org/pub/FreeBSD/releases/${FBSDARCH}/${FBSDVER}/$oldStr"
61     fi
62
63     # Now read in the list of files to fetch
64     while read line
65     do
66        echo "$line" | grep -q '^cksum'
67        if [ $? -ne 0 ] ; then continue ; fi
68        fName=`echo $line | cut -d " " -f 1 | sed "s|cksum|$oldStr|g"`
69        fetch -o "${JDIR}/.download/$fName" "${fDir}/${fName}"
70        if [ $? -ne 0 ] ; then
71          exit_err "Failed downloading: FreeBSD ${FBSDVER}"
72        fi
73     done < ${JDIR}/.download/${oldStr}.inf
[1f8e4d7]74  fi
75
76}
77
78create_template()
79{
[698a249]80  # Get the dataset of the jails mountpoint
81  rDataSet=`mount | grep "on ${JDIR} " | awk '{print $1}'`
82  tSubDir=`basename $TDIR`
83  nDataSet="${rDataSet}/${tSubDir}"
84
85  echo "Creating ZFS ${nDataSet} dataset..."
86  zfs create -p ${nDataSet}
87  if [ $? -ne 0 ] ; then exit_err "Failed creating ZFS base dataset"; fi
[4905ba9]88
89  # Using a supplied tar file?
90  if [ -n "$FBSDTAR" ] ; then
91    tar xvpf $FBSDTAR -C ${TDIR} 2>/dev/null
92    if [ $? -ne 0 ] ; then exit_err "Failed extracting: $FBSDTAR"; fi
93  elif [ "$oldFBSD" = "YES" ] ; then
94    cd ${JDIR}/.download/
95    cat ${oldStr}.?? | tar --unlink -xpzf - -C ${TDIR} 2>/dev/null
96    cd ${JDIR}
[1f8e4d7]97  else
[4905ba9]98    # Extract the dist files
99    for f in $DFILES
100    do
101      tar xvpf ${JDIR}/.download/$f -C ${TDIR} 2>/dev/null
102      if [ $? -ne 0 ] ; then exit_err "Failed extracting ZFS template environment"; fi
103      rm ${JDIR}/.download/${f}
104    done
105  fi
106
107  # Creating a plugin jail?
108  if [ "$TPLUGJAIL" = "YES" ] ; then
109    cp /etc/resolv.conf ${TDIR}/etc/resolv.conf
110    bootstrap_pkgng "${TDIR}" "pluginjail"
111  fi
112
[698a249]113  zfs snapshot ${nDataSet}@clean
[4905ba9]114  if [ $? -ne 0 ] ; then exit_err "Failed creating clean ZFS base snapshot"; fi
[1f8e4d7]115
[cee08ee]116  rm -rf ${JDIR}/.download
[1f8e4d7]117  echo "Created jail template: $TNICK"
118  exit 0
119};
120
121
122if [ -z "$TNICK" ] ; then exit_err "No nickname specified, use -nick <nickname>"; fi
123if [ -z "$FBSDTAR" ] ; then
124  if [ -z "$FBSDVER" -a -z "${TRUEOSVER}" ] ; then exit_err "Need either -fbsd or -trueos specified!"; fi
125  case $FBSDARCH in
126   amd64) if [ "`uname -m`" != "amd64" ] ; then exit_err "Can only use amd64 on amd64 systems"; fi ;;
127    i386) ;;
128       *) exit_err "Arch needs to be amd64 or i386" ;;
129  esac
130fi
131
132# Set the template directory
133TDIR="${JDIR}/.warden-template-$TNICK"
134
135# Make sure this template is available
136if [ -e "${TDIR}" ] ; then
137  exit_err "A template with this nickname already exists!"
138fi
139
140# Set the files we will be downloading
141DFILES="base.txz doc.txz games.txz"
142if [ "$FBSDARCH" = "amd64" ] ; then
143  DFILES="$DFILES lib32.txz"
144fi
145
[5093cde]146# Check if we are on REAL old versions of FreeBSD
147if [ -n "$FBSDVER" ] ; then
[228303d]148  mV=`echo $FBSDVER | cut -d '-' -f 1 | cut -d '.' -f 1`
[cee08ee]149  if [ $mV -lt 9 ] ; then
150     oldFBSD="YES"
151     oldStr="base"
152  fi
153  if [ $mV -lt 5 ] ; then 
154     # VERY VERY old!
155     oldFBSD="YES"
156     oldStr="bin"
157  fi
[5093cde]158fi
159
160
[1f8e4d7]161# If not using a tarball, lets download our files
162if [ -z "$FBSDTAR" ] ; then
163  download_template_files
164fi
165
166# Create the template now
167create_template
168
[95952b3]169exit 0
Note: See TracBrowser for help on using the repository browser.