source: src-sh/warden/scripts/backend/createtemplate.sh @ 411e12f

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

Hide tar output

  • Property mode set to 100755
File size: 6.6 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    zfs snapshot ${tank}${zfsp}@clean
103    if [ $? -ne 0 ] ; then exit_err "Failed creating clean ZFS base snapshot"; fi
104  else
105    # Sigh, still on UFS??
106    if [ -d "${JDIR}/.templatedir" ]; then
107       rm -rf ${JDIR}/.templatedir
108    fi
109
110    if [ -n "$FBSDTAR" ] ; then
111      # User-supplied tar file
112      cp $FBSDTAR ${TDIR}
113    elif [ "$oldFBSD" = "YES" ] ; then
114      mkdir ${JDIR}/.templatedir
115      cd ${JDIR}/.download/
116      echo "Extrating FreeBSD..."
117      cat ${oldStr}.?? | tar --unlink -xpzf - -C ${JDIR}/.templatedir 2>/dev/null
118      cd ${JDIR}
119      echo "Creating template archive..."
120      tar cvjf ${TDIR} -C ${JDIR}/.templatedir 2>/dev/null
121      rm -rf ${JDIR}/.templatedir
122    else
123      # Extract the dist files
124      mkdir ${JDIR}/.templatedir
125      for f in $DFILES
126      do
127        tar xvpf ${JDIR}/.download/$f -C ${JDIR}/.templatedir 2>/dev/null
128        if [ $? -ne 0 ] ; then
129           rm -rf ${JDIR}/.templatedir
130           exit_err "Failed extracting ZFS template environment"
131        fi
132        rm ${JDIR}/.download/${f}
133      done
134
135      echo "Creating template archive..."
136      tar cvjf ${TDIR} -C ${JDIR}/.templatedir 2>/dev/null
137      rm -rf ${JDIR}/.templatedir
138    fi
139  fi
140
141  rm -rf ${JDIR}/.download
142  echo "Created jail template: $TNICK"
143  exit 0
144};
145
146
147# Read our flags
148while [ $# -gt 0 ]; do
149   case $1 in
150    -fbsd) shift
151           if [ -z "$1" ] ; then exit_err "No FreeBSD version specified"; fi
152           FBSDVER="${1}"
153           ;;
154  -trueos) shift
155           if [ -z "$1" ] ; then exit_err "No TrueOS version specified"; fi
156           TRUEOSVER="${1}"
157           ;;
158    -arch) shift
159           if [ -z "$1" ] ; then exit_err "No FreeBSD architecture specified"; fi
160           FBSDARCH="${1}"
161           ;;
162    -tar) shift
163           if [ -z "$1" ] ; then exit_err "No tar file specified"; fi
164           if [ ! -e "$1" ] ; then exit_err "Could not find tar file: $1"; fi
165           FBSDTAR="${1}"
166           ;;
167    -nick) shift
168           if [ -z "$1" ] ; then exit_err "No nickname specified"; fi
169           TNICK="${1}"
170           ;;
171        *) exit_err "Invalid option: $1" ;;
172   esac
173   shift
174done
175
176if [ -z "$TNICK" ] ; then exit_err "No nickname specified, use -nick <nickname>"; fi
177if [ -z "$FBSDTAR" ] ; then
178  if [ -z "$FBSDVER" -a -z "${TRUEOSVER}" ] ; then exit_err "Need either -fbsd or -trueos specified!"; fi
179  case $FBSDARCH in
180   amd64) if [ "`uname -m`" != "amd64" ] ; then exit_err "Can only use amd64 on amd64 systems"; fi ;;
181    i386) ;;
182       *) exit_err "Arch needs to be amd64 or i386" ;;
183  esac
184fi
185
186# Set the template directory
187TDIR="${JDIR}/.warden-template-$TNICK"
188
189# Set the name based upon if using ZFS or UFS
190isDirZFS "${JDIR}"
191if [ $? -eq 0 ] ; then
192  TDIR="${TDIR}"
193else
194  TDIR="${TDIR}.tbz"
195fi
196
197# Make sure this template is available
198if [ -e "${TDIR}" ] ; then
199  exit_err "A template with this nickname already exists!"
200fi
201
202# Set the files we will be downloading
203DFILES="base.txz doc.txz games.txz"
204if [ "$FBSDARCH" = "amd64" ] ; then
205  DFILES="$DFILES lib32.txz"
206fi
207
208# Check if we are on REAL old versions of FreeBSD
209if [ -n "$FBSDVER" ] ; then
210  mV=`echo $FBSDVER | cut -d '.' -f 1`
211  if [ $mV -lt 9 ] ; then
212     oldFBSD="YES"
213     oldStr="base"
214  fi
215  if [ $mV -lt 5 ] ; then 
216     # VERY VERY old!
217     oldFBSD="YES"
218     oldStr="bin"
219  fi
220fi
221
222
223# If not using a tarball, lets download our files
224if [ -z "$FBSDTAR" ] ; then
225  download_template_files
226fi
227
228# Create the template now
229create_template
230
Note: See TracBrowser for help on using the repository browser.