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

Last change on this file was 4905ba9, checked in by Kris Moore <kris@…>, 2 weeks ago

Remove much of the legacy UFS cruft which was still present in Warden

It will *require* ZFS going forward, which has been the default on PC-BSD for
a while now

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