Changeset d8b454f


Ignore:
Timestamp:
06/27/14 07:54:41 (13 months ago)
Author:
Kris Moore <kris@…>
Branches:
master, enter/10, releng/10.0.3, releng/10.1, releng/10.1.1, releng/10.1.2
Children:
19bab26
Parents:
6a63024
Message:

Update / improve the Warden's handling of import/export .wdn files.

Now we will preserve the entire "metadata" for a jail, while keeping
the .wdn file as a straight-up tarball, allowing for easy viewing.

Added backwards functionality to import old-style .wdn files, and also
the ability to import generic "tar" files, in case the user made their
own that they want to import into Warden.

Location:
src-sh/warden
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src-sh/warden/bin/warden

    r4905ba9 rd8b454f  
    458458Usage: 
    459459 
    460   warden import <.wdn file> 
    461  
    462 Example: 
    463  
    464   warden import /home/kris/myjail.wdn 
     460  warden import <jailname> <.wdn file> 
     461 
     462Example: 
     463 
     464  warden import myjail /home/kris/myjail.wdn 
    465465" 
    466466}; 
     
    898898 import) require_root 
    899899         # The user wants to import a jail, lets do it! 
    900          IFILE="$2" 
    901          if [ ! -e "$IFILE" ]; then exit_err "Invalid filename: ${IFILE}"; fi 
    902  
    903          HOST=`basename "${IFILE}" | sed 's|\.wdn$||'` 
    904  
     900         NAME="$2" 
     901         IFILE="$3" 
     902         if [ ! -e "$IFILE" -o -z "$IFILE" ]; then exit_err "Invalid filename: ${IFILE}"; fi 
     903 
     904         # Parse the IP / Host flags 
     905         HOST="OFF" 
     906         IP4="OFF" 
     907         IP6="OFF" 
    905908         get_ip_host_flags "$@" 
    906  
    907909         if [ "${IP4}" != "OFF" ] ; then 
    908910            IP4="${IP4}/${MASK4}" 
     
    913915 
    914916         # Import the jail now! 
    915          ${PROGDIR}/scripts/backend/importjail.sh "${IFILE}" "${HOST}" "${IP4}" "${IP6}" 
     917         ${PROGDIR}/scripts/backend/importjail.sh "$NAME" "$IFILE" "$HOST" "$IP4" "$IP6" 
    916918        ;; 
    917919 
  • src-sh/warden/scripts/backend/exportjail.sh

    r4efda4d rd8b454f  
    3838 
    3939# First check if this jail is running, and stop it 
    40 echo "Checking jail status..." 
    4140${PROGDIR}/scripts/backend/checkstatus.sh "${EXPORTNAME}" 
    4241if [ "$?" = "0" ] 
     
    6867MASK6="${JMASK}" 
    6968 
    70 echo "Creating compressed archive of ${EXPORTNAME}... Please Wait..." 
    71 tar cvJf "${WTMP}/${EXPORTNAME}.txz" -C "${JAILDIR}" . 2>${WTMP}/${EXPORTNAME}.files 
    72  
    73 cd ${WTMP} 
    74  
    75 echo "Creating jail metadata..." 
    76 LINES="`wc -l ${EXPORTNAME}.files | sed -e 's, ,,g' | cut -d '.' -f 1`" 
    77  
    78 # Finished, now make the header info 
    79 cd ${WTMP} 
    80 echo "[Warden file] 
    81 Ver: 1.0  
    82 OS: `uname -r | cut -d '-' -f 1` 
    83 Files: $LINES 
    84 IP4: ${IP4}/${MASK4} 
    85 IP6: ${IP6}/${MASK6} 
    86 HOST: ${HOST} 
    87 " >${WTMP}/${EXPORTNAME}.header 
    88  
    89 # Copy over jail extra meta-data 
    90 cp ${JMETADIR}/jail-* ${WTMP}/ 2>/dev/null 
    91  
    92 # Compress the header file 
    93 tar cvzf ${EXPORTNAME}.header.tgz ${EXPORTNAME}.header jail-* 2>/dev/null 
    94  
    95 # Create our spacer 
    96 echo " 
    97 ___WARDEN_START___" > .spacer 
    98  
    99 # Make the .wdn file now 
    100 cat ${EXPORTNAME}.header.tgz .spacer ${EXPORTNAME}.txz > ${EXPORTNAME}.wdn 
    101  
    102 # Remove the old files 
    103 rm ${EXPORTNAME}.header 
    104 rm ${EXPORTNAME}.files 
    105 rm ${EXPORTNAME}.txz 
    106 rm .spacer 
    107 rm ${EXPORTNAME}.header.tgz 
    108  
    109 # Remove any extra jail meta-files from WTMP 
    110 for i in `ls ${JMETADIR}/jail-* 2>/dev/null` 
    111 do 
    112   mFile=`basename $i` 
    113   rm $mFile 
    114 done 
    115  
    11669if [ ! -d "$OUTDIR" ] ; then 
    11770  mkdir -p ${OUTDIR} 
    11871fi 
    119 if [ "$OUTDIR" != "$WTMP" ] ; then 
    120   mv ${EXPORTNAME}.wdn ${OUTDIR}/ 
     72 
     73TDIR="`mktemp -d ${WTMP}/exportJail-XXXXXXXXX`" 
     74cd ${TDIR} 
     75 
     76# Copy over jail extra meta-data 
     77cp ${JMETADIR}/* ${TDIR}/ 2>/dev/null 
     78 
     79# Compress the info dir 
     80cd ${TDIR} 
     81tar cvJf "${JAILDIR}/jailinfo.txz" . 2>/dev/null 
     82if [ $? -ne 0 ] ; then 
     83   exit_err "Failed creating jail info file" 
    12184fi 
     85 
     86# Cleanup the old TDIR 
     87cd ${OUTDIR} 
     88rm -rf "$TDIR" 
     89 
     90echo "Creating compressed archive of ${EXPORTNAME}... Please Wait..." 
     91tar cvJf "${OUTDIR}/${EXPORTNAME}.wdn" -C "${JAILDIR}" . 2>/dev/null 
     92if [ $? -ne 0 ] ; then 
     93   exit_err "Failed creating warden archive" 
     94fi 
     95 
     96rm "${JAILDIR}/jailinfo.txz" 2>/dev/null 
     97 
    12298echo "Created ${EXPORTNAME}.wdn in ${OUTDIR}" >&1 
    12399 
  • src-sh/warden/scripts/backend/importjail.sh

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