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

Last change on this file was b56639b, checked in by Kris Moore <kris@…>, 4 weeks ago

Looks like the games distfile is going away upstream, lets nuke it here
right now

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