source: src-sh/warden/scripts/backend/createtemplate.sh @ 95952b3

9.2-releasereleng/10.0releng/10.0.1releng/10.0.2
Last change on this file since 95952b3 was 95952b3, checked in by Kris Moore <kris@…>, 16 months ago

Welcome the new template system to creating jails!

Now you can create jail templates, and then select which template to use
when you create a new jail. Tested it all the way back to FreeBSD 4.1.1 ;)

  • Property mode set to 100755
File size: 7.3 KB
Line 
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
25       get_file_from_mirrors "/${TRUEOSVER}/${FBSDARCH}/dist/$f" "${JDIR}/.download/$f"
26       if [ $? -ne 0 ] ; then
27         exit_err "Failed downloading: /${TRUEOS}/${FBSDARCH}/dist/${f}"
28       fi
29     done
30  else
31     
32     # Check if we are on REAL old versions of FreeBSD
33     if [ "$oldFBSD" = "YES" ] ; then
34         # Get the .inf list file
35         fetch -o "${JDIR}/.download/${oldStr}.inf" "http://ftp-archive.freebsd.org/pub/FreeBSD-Archive/old-releases/${FBSDARCH}/${FBSDVER}/${oldStr}/${oldStr}.inf"
36         if [ $? -ne 0 ] ; then
37           exit_err "Failed downloading: FreeBSD ${FBSDVER} - ${oldStr}.inf"
38         fi
39         # Now read in the list of files to fetch
40         while read line
41         do
42            echo "$line" | grep -q '^cksum'
43            if [ $? -ne 0 ] ; then continue ; fi
44            fName=`echo $line | cut -d " " -f 1 | sed "s|cksum|$oldStr|g"`
45            fetch -o "${JDIR}/.download/$fName" "http://ftp-archive.freebsd.org/pub/FreeBSD-Archive/old-releases/${FBSDARCH}/${FBSDVER}/${oldStr}/$fName"
46            if [ $? -ne 0 ] ; then
47              exit_err "Failed downloading: FreeBSD ${FBSDVER} - $fName"
48            fi
49         done < ${JDIR}/.download/${oldStr}.inf
50         return
51     fi
52
53     for f in $DFILES
54     do
55       fetch -o "${JDIR}/.download/$f" "ftp://ftp.freebsd.org/pub/FreeBSD/releases/${FBSDARCH}/${FBSDVER}/$f"
56       if [ $? -ne 0 ] ; then
57         echo "Trying ftp-archive..."
58         fetch -o "${JDIR}/.download/$f" "http://ftp-archive.freebsd.org/pub/FreeBSD-Archive/old-releases/${FBSDARCH}/${FBSDVER}/$f"
59         if [ $? -ne 0 ] ; then
60           exit_err "Failed downloading: FreeBSD ${FBSDVER}"
61         fi
62       fi
63     done
64  fi
65
66}
67
68create_template()
69{
70  # Creating ZFS dataset?
71  isDirZFS "${JDIR}"
72  if [ $? -eq 0 ] ; then
73    local zfsp=`getZFSRelativePath "${TDIR}"`
74
75    # Use ZFS base for cloning
76    tank=`getZFSTank "${JDIR}"`
77    isDirZFS "${TDIR}" "1"
78    if [ $? -ne 0 ] ; then
79       echo "Creating ZFS ${TDIR} dataset..."
80       zfs create -o mountpoint=/${zfsp} -p ${tank}${zfsp}
81       if [ $? -ne 0 ] ; then exit_err "Failed creating ZFS base dataset"; fi
82    fi
83
84    # Using a supplied tar file?
85    if [ -n "$FBSDTAR" ] ; then
86      tar xvpf $FBSDTAR -C ${TDIR} 2>/dev/null
87      if [ $? -ne 0 ] ; then exit_err "Failed extracting: $FBSDTAR"; fi
88    elif [ "$oldFBSD" = "YES" ] ; then
89      cd ${JDIR}/.download/
90      cat ${oldStr}.?? | tar --unlink -xpzf - -C ${TDIR} 2>/dev/null
91      cd ${JDIR}
92    else
93      # Extract the dist files
94      for f in $DFILES
95      do
96        tar xvpf ${JDIR}/.download/$f -C ${TDIR} 2>/dev/null
97        if [ $? -ne 0 ] ; then exit_err "Failed extracting ZFS template environment"; fi
98        rm ${JDIR}/.download/${f}
99      done
100    fi
101
102    # Creating a plugin jail?
103    if [ "$TPLUGJAIL" = "YES" ] ; then
104      cp /etc/resolv.conf ${TDIR}/etc/resolv.conf
105      bootstrap_pkgng "${TDIR}" "pluginjail"
106    fi
107
108    zfs snapshot ${tank}${zfsp}@clean
109    if [ $? -ne 0 ] ; then exit_err "Failed creating clean ZFS base snapshot"; fi
110  else
111    # Sigh, still on UFS??
112    if [ -d "${JDIR}/.templatedir" ]; then
113       rm -rf ${JDIR}/.templatedir
114    fi
115
116    if [ -n "$FBSDTAR" ] ; then
117      # User-supplied tar file
118      cp $FBSDTAR ${TDIR}
119    elif [ "$oldFBSD" = "YES" ] ; then
120      mkdir ${JDIR}/.templatedir
121      cd ${JDIR}/.download/
122      echo "Extrating FreeBSD..."
123      cat ${oldStr}.?? | tar --unlink -xpzf - -C ${JDIR}/.templatedir 2>/dev/null
124      cd ${JDIR}
125
126      # Creating a plugin jail?
127      if [ "$TPLUGJAIL" = "YES" ] ; then
128        cp /etc/resolv.conf ${JDIR}/.templatedir/etc/resolv.conf
129        bootstrap_pkgng "${JDIR}/.templatedir/" "pluginjail"
130      fi
131
132      echo "Creating template archive..."
133      tar cvjf ${TDIR} -C ${JDIR}/.templatedir 2>/dev/null
134      rm -rf ${JDIR}/.templatedir
135    else
136      # Extract the dist files
137      mkdir ${JDIR}/.templatedir
138      for f in $DFILES
139      do
140        tar xvpf ${JDIR}/.download/$f -C ${JDIR}/.templatedir 2>/dev/null
141        if [ $? -ne 0 ] ; then
142           rm -rf ${JDIR}/.templatedir
143           exit_err "Failed extracting ZFS template environment"
144        fi
145        rm ${JDIR}/.download/${f}
146      done
147
148      # Creating a plugin jail?
149      if [ "$TPLUGJAIL" = "YES" ] ; then
150        cp /etc/resolv.conf ${JDIR}/.templatedir/etc/resolv.conf
151        bootstrap_pkgng "${JDIR}/.templatedir/" "pluginjail"
152      fi
153
154      echo "Creating template archive..."
155      tar cvjf ${TDIR} -C ${JDIR}/.templatedir 2>/dev/null
156      rm -rf ${JDIR}/.templatedir
157    fi
158  fi
159
160  rm -rf ${JDIR}/.download
161  echo "Created jail template: $TNICK"
162  exit 0
163};
164
165
166# Read our flags
167while [ $# -gt 0 ]; do
168   case $1 in
169    -fbsd) shift
170           if [ -z "$1" ] ; then exit_err "No FreeBSD version specified"; fi
171           FBSDVER="${1}"
172           ;;
173  -trueos) shift
174           if [ -z "$1" ] ; then exit_err "No TrueOS version specified"; fi
175           TRUEOSVER="${1}"
176           ;;
177    -arch) shift
178           if [ -z "$1" ] ; then exit_err "No FreeBSD architecture specified"; fi
179           FBSDARCH="${1}"
180           ;;
181    -tar) shift
182           if [ -z "$1" ] ; then exit_err "No tar file specified"; fi
183           if [ ! -e "$1" ] ; then exit_err "Could not find tar file: $1"; fi
184           FBSDTAR="${1}"
185           ;;
186    -nick) shift
187           if [ -z "$1" ] ; then exit_err "No nickname specified"; fi
188           TNICK="${1}"
189           ;;
190 -pluginjail) shift
191           TPLUGJAIL="YES"
192           ;;
193        *) exit_err "Invalid option: $1" ;;
194   esac
195   shift
196done
197
198if [ -z "$TNICK" ] ; then exit_err "No nickname specified, use -nick <nickname>"; fi
199if [ -z "$FBSDTAR" ] ; then
200  if [ -z "$FBSDVER" -a -z "${TRUEOSVER}" ] ; then exit_err "Need either -fbsd or -trueos specified!"; fi
201  case $FBSDARCH in
202   amd64) if [ "`uname -m`" != "amd64" ] ; then exit_err "Can only use amd64 on amd64 systems"; fi ;;
203    i386) ;;
204       *) exit_err "Arch needs to be amd64 or i386" ;;
205  esac
206fi
207
208# Set the template directory
209TDIR="${JDIR}/.warden-template-$TNICK"
210
211# Set the name based upon if using ZFS or UFS
212isDirZFS "${JDIR}"
213if [ $? -eq 0 ] ; then
214  TDIR="${TDIR}"
215else
216  TDIR="${TDIR}.tbz"
217fi
218
219# Make sure this template is available
220if [ -e "${TDIR}" ] ; then
221  exit_err "A template with this nickname already exists!"
222fi
223
224# Set the files we will be downloading
225DFILES="base.txz doc.txz games.txz"
226if [ "$FBSDARCH" = "amd64" ] ; then
227  DFILES="$DFILES lib32.txz"
228fi
229
230# Check if we are on REAL old versions of FreeBSD
231if [ -n "$FBSDVER" ] ; then
232  mV=`echo $FBSDVER | cut -d '.' -f 1`
233  if [ $mV -lt 9 ] ; then
234     oldFBSD="YES"
235     oldStr="base"
236  fi
237  if [ $mV -lt 5 ] ; then 
238     # VERY VERY old!
239     oldFBSD="YES"
240     oldStr="bin"
241  fi
242fi
243
244
245# If not using a tarball, lets download our files
246if [ -z "$FBSDTAR" ] ; then
247  download_template_files
248fi
249
250# Create the template now
251create_template
252
253exit 0
Note: See TracBrowser for help on using the repository browser.