source: src-sh/warden/scripts/backend/createtemplate.sh @ 2c8cdfc

9.2-releasereleng/10.0releng/10.0.1releng/10.0.2
Last change on this file since 2c8cdfc was 9cc2f9c, checked in by Kris Moore <kris@…>, 13 months ago

If user gives jail template with spaces in the name, correct it

  • Property mode set to 100755
File size: 6.4 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
166if [ -z "$TNICK" ] ; then exit_err "No nickname specified, use -nick <nickname>"; fi
167if [ -z "$FBSDTAR" ] ; then
168  if [ -z "$FBSDVER" -a -z "${TRUEOSVER}" ] ; then exit_err "Need either -fbsd or -trueos specified!"; fi
169  case $FBSDARCH in
170   amd64) if [ "`uname -m`" != "amd64" ] ; then exit_err "Can only use amd64 on amd64 systems"; fi ;;
171    i386) ;;
172       *) exit_err "Arch needs to be amd64 or i386" ;;
173  esac
174fi
175
176# Set the template directory
177TDIR="${JDIR}/.warden-template-$TNICK"
178
179# Set the name based upon if using ZFS or UFS
180isDirZFS "${JDIR}"
181if [ $? -eq 0 ] ; then
182  TDIR="${TDIR}"
183else
184  TDIR="${TDIR}.tbz"
185fi
186
187# Make sure this template is available
188if [ -e "${TDIR}" ] ; then
189  exit_err "A template with this nickname already exists!"
190fi
191
192# Set the files we will be downloading
193DFILES="base.txz doc.txz games.txz"
194if [ "$FBSDARCH" = "amd64" ] ; then
195  DFILES="$DFILES lib32.txz"
196fi
197
198# Check if we are on REAL old versions of FreeBSD
199if [ -n "$FBSDVER" ] ; then
200  mV=`echo $FBSDVER | cut -d '.' -f 1`
201  if [ $mV -lt 9 ] ; then
202     oldFBSD="YES"
203     oldStr="base"
204  fi
205  if [ $mV -lt 5 ] ; then 
206     # VERY VERY old!
207     oldFBSD="YES"
208     oldStr="bin"
209  fi
210fi
211
212
213# If not using a tarball, lets download our files
214if [ -z "$FBSDTAR" ] ; then
215  download_template_files
216fi
217
218# Create the template now
219create_template
220
221exit 0
Note: See TracBrowser for help on using the repository browser.