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

Last change on this file was 82d9990, checked in by Kris Moore <kris@…>, 5 weeks ago

Update installer and warden to automatically create the default jail
"template" upon first boot. This will save the user the time downloading
the 100MB+ of dist-files the first time they create a jail, especially
since those dist files are on the ISO already.

Now when creating jails, the only downloads to do are packages, which is much
smaller / faster.

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