source: src-sh/warden/scripts/backend/createtemplate.sh @ 5093cde

9.2-releasereleng/10.0releng/10.0.1releng/10.0.2releng/10.0.3releng/10.1
Last change on this file since 5093cde was 5093cde, checked in by Kris Moore <kris@…>, 20 months ago

Add ability to create templates of very OLD versions of FreeBSD, tested back to FreeBSD 5.1-RELEASE

  • 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/base.inf" "http://ftp-archive.freebsd.org/pub/FreeBSD-Archive/old-releases/${FBSDARCH}/${FBSDVER}/base/base.inf"
36         if [ $? -ne 0 ] ; then
37           exit_err "Failed downloading: FreeBSD ${FBSDVER} - base.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|base|g'`
45            fetch -o "${JDIR}/.download/$fName" "http://ftp-archive.freebsd.org/pub/FreeBSD-Archive/old-releases/${FBSDARCH}/${FBSDVER}/base/$fName"
46            if [ $? -ne 0 ] ; then
47              exit_err "Failed downloading: FreeBSD ${FBSDVER} - $fName"
48            fi
49         done < ${JDIR}/.download/base.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 base.?? | 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 base.?? | 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  echo "Created jail template: $TNICK"
142  exit 0
143};
144
145
146# Read our flags
147while [ $# -gt 0 ]; do
148   case $1 in
149    -fbsd) shift
150           if [ -z "$1" ] ; then exit_err "No FreeBSD version specified"; fi
151           FBSDVER="${1}"
152           ;;
153  -trueos) shift
154           if [ -z "$1" ] ; then exit_err "No TrueOS version specified"; fi
155           TRUEOSVER="${1}"
156           ;;
157    -arch) shift
158           if [ -z "$1" ] ; then exit_err "No FreeBSD architecture specified"; fi
159           FBSDARCH="${1}"
160           ;;
161    -tar) shift
162           if [ -z "$1" ] ; then exit_err "No tar file specified"; fi
163           if [ ! -e "$1" ] ; then exit_err "Could not find tar file: $1"; fi
164           FBSDTAR="${1}"
165           ;;
166    -nick) shift
167           if [ -z "$1" ] ; then exit_err "No nickname specified"; fi
168           TNICK="${1}"
169           ;;
170        *) exit_err "Invalid option: $1" ;;
171   esac
172   shift
173done
174
175if [ -z "$TNICK" ] ; then exit_err "No nickname specified, use -nick <nickname>"; fi
176if [ -z "$FBSDTAR" ] ; then
177  if [ -z "$FBSDVER" -a -z "${TRUEOSVER}" ] ; then exit_err "Need either -fbsd or -trueos specified!"; fi
178  case $FBSDARCH in
179   amd64) if [ "`uname -m`" != "amd64" ] ; then exit_err "Can only use amd64 on amd64 systems"; fi ;;
180    i386) ;;
181       *) exit_err "Arch needs to be amd64 or i386" ;;
182  esac
183fi
184
185# Set the template directory
186TDIR="${JDIR}/.warden-template-$TNICK"
187
188# Set the name based upon if using ZFS or UFS
189isDirZFS "${JDIR}"
190if [ $? -eq 0 ] ; then
191  TDIR="${TDIR}"
192else
193  TDIR="${TDIR}.tbz"
194fi
195
196# Make sure this template is available
197if [ -e "${TDIR}" ] ; then
198  exit_err "A template with this nickname already exists!"
199fi
200
201# Set the files we will be downloading
202DFILES="base.txz doc.txz games.txz"
203if [ "$FBSDARCH" = "amd64" ] ; then
204  DFILES="$DFILES lib32.txz"
205fi
206
207# Check if we are on REAL old versions of FreeBSD
208if [ -n "$FBSDVER" ] ; then
209  mV=`echo $FBSDVER | cut -d '.' -f 1`
210  if [ $mV -lt 9 ] ; then oldFBSD="YES"; fi
211fi
212
213
214# If not using a tarball, lets download our files
215if [ -z "$FBSDTAR" ] ; then
216  download_template_files
217fi
218
219# Create the template now
220create_template
221
Note: See TracBrowser for help on using the repository browser.