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

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

Fix downloading files from the FreeBSD archives, next have to do old style install

  • Property mode set to 100755
File size: 4.9 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     for f in $DFILES
32     do
33       fetch -o "${JDIR}/.download/$f" "ftp://ftp.freebsd.org/pub/FreeBSD/releases/${FBSDARCH}/${FBSDVER}/$f"
34       if [ $? -ne 0 ] ; then
35         echo "Trying ftp-archive..."
36         fetch -o "${JDIR}/.download/$f" "http://ftp-archive.freebsd.org/pub/FreeBSD-Archive/old-releases/${FBSDARCH}/${FBSDVER}/$f"
37         if [ $? -ne 0 ] ; then
38           exit_err "Failed downloading: FreeBSD ${FBSDVER}"
39         fi
40       fi
41     done
42  fi
43
44}
45
46create_template()
47{
48  # Creating ZFS dataset?
49  isDirZFS "${JDIR}"
50  if [ $? -eq 0 ] ; then
51    local zfsp=`getZFSRelativePath "${TDIR}"`
52
53    # Use ZFS base for cloning
54    tank=`getZFSTank "${JDIR}"`
55    isDirZFS "${TDIR}" "1"
56    if [ $? -ne 0 ] ; then
57       echo "Creating ZFS ${TDIR} dataset..."
58       zfs create -o mountpoint=/${zfsp} -p ${tank}${zfsp}
59       if [ $? -ne 0 ] ; then exit_err "Failed creating ZFS base dataset"; fi
60    fi
61
62    # Using a supplied tar file?
63    if [ -n "$FBSDTAR" ] ; then
64        tar xvpf $FBSDTAR -C ${TDIR} 2>/dev/null
65        if [ $? -ne 0 ] ; then exit_err "Failed extracting: $FBSDTAR"; fi
66    else
67      # Extract the dist files
68      for f in $DFILES
69      do
70        tar xvpf ${JDIR}/.download/$f -C ${TDIR} 2>/dev/null
71        if [ $? -ne 0 ] ; then exit_err "Failed extracting ZFS template environment"; fi
72        rm ${JDIR}/.download/${f}
73      done
74    fi
75
76    zfs snapshot ${tank}${zfsp}@clean
77    if [ $? -ne 0 ] ; then exit_err "Failed creating clean ZFS base snapshot"; fi
78  else
79    # Sigh, still on UFS??
80    if [ -d "${JDIR}/.templatedir" ]; then
81       rm -rf ${JDIR}/.templatedir
82    fi
83    mkdir ${JDIR}/.templatedir
84
85    if [ -n "$FBSDTAR" ] ; then
86      # User-supplied tar file
87      cp $FBSDTAR ${TDIR}
88    else
89      # Extract the dist files
90      for f in $DFILES
91      do
92        tar xvpf ${JDIR}/.download/$f -C ${JDIR}/.templatedir 2>/dev/null
93        if [ $? -ne 0 ] ; then
94           rm -rf ${JDIR}/.templatedir
95           exit_err "Failed extracting ZFS template environment"
96        fi
97        rm ${JDIR}/.download/${f}
98      done
99
100      echo "Creating template archive..."
101      tar cvjf ${TDIR} -C ${JDIR}/.templatedir 2>/dev/null
102      rm -rf ${JDIR}/.templatedir
103    fi
104  fi
105
106  echo "Created jail template: $TNICK"
107  exit 0
108};
109
110
111# Read our flags
112while [ $# -gt 0 ]; do
113   case $1 in
114    -fbsd) shift
115           if [ -z "$1" ] ; then exit_err "No FreeBSD version specified"; fi
116           FBSDVER="${1}"
117           ;;
118  -trueos) shift
119           if [ -z "$1" ] ; then exit_err "No TrueOS version specified"; fi
120           TRUEOSVER="${1}"
121           ;;
122    -arch) shift
123           if [ -z "$1" ] ; then exit_err "No FreeBSD architecture specified"; fi
124           FBSDARCH="${1}"
125           ;;
126    -tar) shift
127           if [ -z "$1" ] ; then exit_err "No tar file specified"; fi
128           if [ ! -e "$1" ] ; then exit_err "Could not find tar file: $1"; fi
129           FBSDTAR="${1}"
130           ;;
131    -nick) shift
132           if [ -z "$1" ] ; then exit_err "No nickname specified"; fi
133           TNICK="${1}"
134           ;;
135        *) exit_err "Invalid option: $1" ;;
136   esac
137   shift
138done
139
140if [ -z "$TNICK" ] ; then exit_err "No nickname specified, use -nick <nickname>"; fi
141if [ -z "$FBSDTAR" ] ; then
142  if [ -z "$FBSDVER" -a -z "${TRUEOSVER}" ] ; then exit_err "Need either -fbsd or -trueos specified!"; fi
143  case $FBSDARCH in
144   amd64) if [ "`uname -m`" != "amd64" ] ; then exit_err "Can only use amd64 on amd64 systems"; fi ;;
145    i386) ;;
146       *) exit_err "Arch needs to be amd64 or i386" ;;
147  esac
148fi
149
150# Set the template directory
151TDIR="${JDIR}/.warden-template-$TNICK"
152
153# Set the name based upon if using ZFS or UFS
154isDirZFS "${JDIR}"
155if [ $? -eq 0 ] ; then
156  TDIR="${TDIR}"
157else
158  TDIR="${TDIR}.tbz"
159fi
160
161# Make sure this template is available
162if [ -e "${TDIR}" ] ; then
163  exit_err "A template with this nickname already exists!"
164fi
165
166# Set the files we will be downloading
167DFILES="base.txz doc.txz games.txz"
168if [ "$FBSDARCH" = "amd64" ] ; then
169  DFILES="$DFILES lib32.txz"
170fi
171
172# If not using a tarball, lets download our files
173if [ -z "$FBSDTAR" ] ; then
174  download_template_files
175fi
176
177# Create the template now
178create_template
179
Note: See TracBrowser for help on using the repository browser.