source: src-sh/warden/scripts/backend/importjail.sh @ 0988d23

9.1-release9.2-releasereleng/10.0releng/10.0.1releng/10.0.2releng/10.0.3releng/10.1
Last change on this file since 0988d23 was 0988d23, checked in by Kris Moore <kris@…>, 19 months ago

Add my working directory for the new warden backend changes.

Includes patches from John Hixson (john@…) to change
how warden uses "nicknames" instead of IP addresses for jail
identification.

  • Property mode set to 100755
File size: 6.5 KB
Line 
1#!/bin/sh
2# Install a package set into a jail
3######################################################################
4
5# Source our functions
6PROGDIR="/usr/local/share/warden"
7
8# Source our variables
9. ${PROGDIR}/scripts/backend/functions.sh
10
11IFILE="$1"
12HOST="${2}"
13IP4="${3}"
14IP6="${4}"
15
16if [ "${IP4}" != "OFF" ] ; then
17  get_ip_and_netmask "${IP4}"
18  IP4="${JIP}"
19  MASK4="${JMASK}"
20fi
21
22if [ "${IP6}" != "OFF" ] ; then
23  get_ip_and_netmask "${IP6}"
24  IP6="${JIP}"
25  MASK6="${JMASK}"
26fi
27
28JAILNAME="${HOST}"
29JAILDIR="${JDIR}/${JAILNAME}"
30
31if [ -z "${IFILE}" -o ! -e "${IFILE}" ]
32then
33  echo "ERROR: No jail specified or invalid file!"
34  exit 5
35fi
36
37if [ -z "${JDIR}" ]
38then
39  echo "ERROR: JDIR is unset!!!!"
40  exit 5
41fi
42
43if [ "${IP4}" != "OFF" ]
44then
45  for i in `ls -d ${JDIR}/.*.meta 2>/dev/null`
46  do
47    if [ "`cat ${i}/ipv4 2>/dev/null`" = "${IP4}/${MASK4}" ] ; then
48      echo "ERROR: A Jail exists with IP: ${IP4}"
49      exit 5
50    fi
51  done
52fi
53if [ "${IP6}" != "OFF" ]
54then
55  for i in `ls -d ${JDIR}/.*.meta 2>/dev/null`
56  do
57    _ipv6=`cat ${i}/ipv6 2>/dev/null|tr a-z A-Z`
58    _nipv6="`echo ${IP6}|tr a-z A-Z`/${MASK6}"
59    if [ "${ipv6}" = "${_nipv6}" ] ; then
60      echo "ERROR: A Jail exists with IP: ${IP6}"
61      exit 5
62    fi
63  done
64fi
65set_warden_metadir
66
67# Lets start importing the jail now
68######################################################################
69
70
71chk=`echo "${IFILE}" | cut -c 1-1`
72if [ "$chk" != "/" ]
73then
74  IFILE="`pwd`/${IFILE}"
75fi
76
77
78# Extract the header info of the file
79cd ${WTMP}
80rm -rf tmp.$$ >/dev/null
81mkdir tmp.$$
82cd tmp.$$
83
84tar xvzf ${IFILE} >/dev/null 2>/dev/null
85if [ "${?}" != "0" ]
86then
87    echo "ERROR: Extracting header info failed! "
88    cd ..
89    rm -rf tmp.$$
90    exit 5
91fi
92
93# Blank our variables and read in the header information
94VER=""
95OS=""
96FILES=""
97FIP4=""
98FIP6=""
99FHOST=""
100
101HEADER=`ls *.header`
102
103while read line
104do
105  #Check for the file version
106  echo "$line" | grep -q "Ver:"
107  if [ $? -eq 0 ]; then
108    VER="`echo $line | cut -d ' ' -f 2-10`"
109  fi
110
111  # Check for the OS Platform
112  echo "$line" | grep -q "OS:"
113  if [ $? -eq 0 ]; then
114    OS="`echo $line | cut -d ' ' -f 2-10`"
115  fi
116
117  # Check for the File Number
118  echo "$line" | grep -q "Files:"
119  if [ $? -eq 0 ]; then
120    FILES="`echo $line | cut -d ' ' -f 2-10`"
121  fi
122 
123  # Check for the built in IP4
124  echo "$line" | grep -q "IP4:"
125  if [ $? -eq 0 ]; then
126    FIP4="`echo $line | cut -d ' ' -f 2-10`"
127  fi
128
129  # Check for the built in IP6
130  echo "$line" | grep -q "IP6:"
131  if [ $? -eq 0 ]; then
132    FIP6="`echo $line | cut -d ' ' -f 2-10`"
133  fi
134 
135  # Check for the built in HOST
136  echo "$line" | grep -q "HOST:"
137  if [ $? -eq 0 ]; then
138    FHOST="`echo $line | cut -d ' ' -f 2-10`"
139  fi
140
141done < $HEADER 
142
143cd ..
144
145# Make sure this is a file version we understand
146if [ "${VER}" != "1.0" ]; then
147    echo "ERROR: Specified file is a incompatiable .wdn file!"
148    rm -rf tmp.$$ 2>/dev/null
149    exit 7
150fi
151
152# Check that we are on the same OS platform
153if [ "${OS}" != "`uname -r | cut -d '-' -f 1`" ]
154then
155    echo "WARNING: This .wdn file was created on $OS, while this host is `uname -r | cut -d '-' -f 1`"
156    echo "This jail may not work...Importing anyway..."
157fi
158
159if [ "${IP4}" = "OFF" ]
160then
161  for i in `ls -d ${JDIR}/.*.meta 2>/dev/null`
162  do
163    if [ -n "${FIP4}" ] ; then
164      if [ "`cat ${i}/ipv4`" = "${FIP4}" ] ; then
165        echo "ERROR: A Jail already exists with IP: $FIP4"
166        rm -rf tmp.$$ 2>/dev/null
167        exit 7
168      fi
169    fi
170  done
171 
172  # The user didn't specify a new IPv4 address, so use the built in one
173  get_ip_and_netmask "${FIP4}"
174  IP4="${JIP}"
175  MASK4="${JMASK}"
176fi
177
178if [ "${IP6}" = "OFF" ]
179then
180  for i in `ls -d ${JDIR}/.*.meta 2>/dev/null`
181  do
182    if [ -n "${FIP6}" ] ; then
183
184      _ipv6=`cat ${i}/ipv6 2>/dev/null|tr a-z A-Z`
185      _nipv6=`echo ${FIP6}|tr a-z A-Z`
186      if [ "${ipv6}" = "${_nipv6}" ] ; then
187        echo "ERROR: A Jail already exists with IP: $FIP6"
188        rm -rf tmp.$$ 2>/dev/null
189        exit 7
190      fi
191    fi
192  done
193 
194  # The user didn't specify a new IPv6 address, so use the built in one
195  get_ip_and_netmask "${FIP6}"
196  IP6="${JIP}"
197  MASK6="${JMASK}"
198fi
199
200SKIP="`awk '/^___WARDEN_START___/ { print NR + 1; exit 0; }' ${IFILE}`"
201if [ -n "${IP4}" ] ; then
202  echo "Importing ${IFILE} with IP: ${IP4}..."
203elif [ -n "${IP6}" ] ; then
204  echo "Importing ${IFILE} with IP: ${IP6}..."
205fi
206
207# Make the new directory
208JAILDIR="${JDIR}/${HOST}"
209isDirZFS "${JDIR}"
210if [ $? -eq 0 ] ; then
211  # Create ZFS mount
212  tank=`getZFSTank "$JDIR"`
213  rp=`getZFSRelativePath "${JAILDIR}"`
214  zfs create -p ${tank}${rp}
215else
216  mkdir -p "${JAILDIR}"
217fi
218
219# Get next unique ID
220META_ID=0
221for i in `ls -d ${JDIR}/.*.meta 2>/dev/null`
222do
223  id=`cat ${i}/id`
224  if [ "${id}" -gt "${META_ID}" ] ; then
225    META_ID="${id}"
226  fi
227done
228: $(( META_ID += 1 ))
229
230# Create the meta-dir
231set_warden_metadir
232mkdir ${JMETADIR}
233
234# Copy over extra jail flags
235cp tmp.$$/jail-* ${JMETADIR}/ 2>/dev/null
236
237# give new jail an id
238echo "${META_ID}" > ${JMETADIR}/id
239
240# Cleanup tmp meta-dir
241rm -rf tmp.$$ 2>/dev/null
242
243# Extract the jail contents
244tail +${SKIP} ${IFILE} | tar xpf - -C "${JAILDIR}" 2>/dev/null
245
246# Make sure we have an IP address saved
247if [ -n "${IP4}" ] ; then
248  echo "${IP4}/${MASK4}" >"${JMETADIR}/ipv4"
249fi
250if [ -n "${IP6}" ] ; then
251  echo "${IP6}/${MASK6}" >"${JMETADIR}/ipv6"
252fi
253
254# Save the jail flags
255if [ -n "$JFLAGS" ] ; then
256   echo "$JFLAGS" > "${JMETADIR}/jail-flags"
257fi
258
259if [ "$HOST" = "OFF" -o -z "${HOST}" ] ; then
260  HOST="$FHOST"
261fi
262
263# Create the host for this device
264if [ "${HOST}" != "OFF" -a ! -z "${HOST}" ]; then
265  # Save the details to the .wardenhost file
266  echo "${HOST}" >"${JMETADIR}/host"
267
268  # Change the hostname in rc.conf
269  if [ -e "${JAILDIR}/etc/rc.conf" ] ; then
270    cat "${JAILDIR}/etc/rc.conf" | grep -v "hostname=" >${JAILDIR}/.rc.conf
271    echo "hostname=\"${HOST}\"" >>"${JAILDIR}/.rc.conf"
272    mv "${JAILDIR}/.rc.conf" "${JAILDIR}/etc/rc.conf"
273  fi
274
275# Setup /etc/hosts now
276cat<<__EOF__>"${JAILDIR}/etc/hosts"
277echo "# : src/etc/hosts,v 1.16 2003/01/28 21:29:23 dbaker Exp $
278#
279# Host Database
280#
281# This file should contain the addresses and aliases for local hosts that
282# share this file.  Replace 'my.domain' below with the domainname of your
283# machine.
284#
285# In the presence of the domain name service or NIS, this file may
286# not be consulted at all; see /etc/nsswitch.conf for the resolution order.
287#
288#
289::1                     localhost localhost.localdomain
290127.0.0.1               localhost localhost.localdomain ${HOST}
291__EOF__
292
293if [ -n "${IP4}" ] ; then
294  echo "${IP4}                  ${HOST}" >> "${JAILDIR}/etc/hosts"
295fi
296if [ -n "${IP6}" ] ; then
297  echo "${IP6}                  ${HOST}" >> "${JAILDIR}/etc/hosts"
298fi
299
300# End Hostname setup
301fi
302
303echo "Done"
Note: See TracBrowser for help on using the repository browser.