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

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

Add initial support for creating new jail templates via the "warden template create"
command-line option

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