source: src-sh/warden/scripts/backend/functions.sh @ 60738f3

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

Tag warden as version 1.3

  • Property mode set to 100755
File size: 19.3 KB
Line 
1#!/bin/sh
2# Functions / variables for warden
3######################################################################
4# DO NOT EDIT
5
6# Source local functions
7. /usr/local/share/pcbsd/scripts/functions.sh
8
9# Installation directory
10PROGDIR="/usr/local/share/warden"
11
12# Jail location
13JDIR="$(grep ^JDIR: /usr/local/etc/warden.conf | cut -d' ' -f2)"
14export JDIR
15
16# Set arch type
17REALARCH=`uname -m`
18export REALARCH
19if [ -z "$ARCH" ] ; then
20  ARCH="$REALARCH"
21  export ARCH
22fi
23
24# Location of pcbsd.conf file
25PCBSD_ETCCONF="/usr/local/etc/pcbsd.conf"
26
27# Network interface to use
28NIC="$(grep ^NIC: /usr/local/etc/warden.conf | cut -d' ' -f2)"
29export NIC
30
31# Tmp directory
32WTMP="$(grep ^WTMP: /usr/local/etc/warden.conf | cut -d' ' -f2)"
33export WTMP
34
35# Temp file for dialog responses
36ATMP="/tmp/.wans"
37export ATMP
38
39# Warden Version
40WARDENVER="1.3"
41export WARDENVER
42
43# Dirs to nullfs mount in X jail
44NULLFS_MOUNTS="/tmp /media /usr/home"
45
46# Clone directory
47CDIR="${JDIR}/clones"
48
49downloadpluginjail() {
50  local _ver="${1}"
51
52  SYSVER=`echo "${_ver}" | sed -E 's|^FreeNAS-(([0-9]+\.){2}[0-9]+).*|\1|'`
53  SYSREL=`echo "${_ver}" | sed -E 's|^FreeNAS-([0-9]+\.){2}[0-9]+-([a-zA-Z0-9]+)-.*|\2|'`
54  SYSARCH=`echo "${_ver}" | sed -E 's#^(.*)(x86|x64)#\2#'`
55
56  SF="http://downloads.sourceforge.net/project/freenas"
57  URL="${SF}/FreeNAS-${SYSVER}/${SYSREL}/${SYSARCH}/plugins"
58
59  PJAIL="FreeNAS-${SYSVER}-${SYSREL}-${SYSARCH}.Plugins_Jail.pbi"
60  PJAILSHA256="${PJAIL}.sha256"
61
62  if [ ! -d "${JDIR}" ] ; then mkdir -p "${JDIR}" ; fi
63  cd ${JDIR}
64
65  echo "Fetching jail environment. This may take a while..."
66
67  if [ ! -e "${PJAIL}" ] ; then
68     echo "Downloading ${URL}/${PJAIL} ..."
69     get_file "${URL}/${PJAIL}" "${PJAIL}" 3
70     [ $? -ne 0 ] && printerror "Error while downloading the pluginjail."
71  fi
72
73  if [ ! -e "${PJAILSHA256}" ] ; then
74     echo "Downloading ${URL}/${PJAILSHA256} ..."
75     get_file "${URL}/${PJAILSHA256}" "${PJAILSHA256}" 3
76     [ $? -ne 0 ] && printerror "Error while downloading the pluginjail sha256."
77  fi
78
79  [ "$(sha256 -q ${PJAIL})" != "$(cat ${PJAILSHA256})" ] &&
80    printerror "Error in download data, checksum mismatch. Please try again later."
81
82  # Creating ZFS dataset?
83  isDirZFS "${JDIR}"
84  if [ $? -eq 0 ] ; then
85    local zfsp=`getZFSRelativePath "${WORLDCHROOT}"`
86
87    # Use ZFS base for cloning
88    echo "Creating ZFS ${WORLDCHROOT} dataset..."
89    tank=`getZFSTank "${JDIR}"`
90    isDirZFS "${WORLDCHROOT}" "1"
91    if [ $? -ne 0 ] ; then
92       zfs create -o mountpoint=/${tank}${zfsp} -p ${tank}${zfsp}
93       if [ $? -ne 0 ] ; then exit_err "Failed creating ZFS base dataset"; fi
94       mkdir -p "${WORLDCHROOT}/.plugins" >/dev/null 2>&1
95    fi
96
97    pbi_add -e --no-checksig -p ${WORLDCHROOT} ${PJAIL}
98    if [ $? -ne 0 ] ; then exit_err "Failed extracting ZFS chroot environment"; fi
99
100    zfs snapshot ${tank}${zfsp}@clean
101    if [ $? -ne 0 ] ; then exit_err "Failed creating clean ZFS base snapshot"; fi
102    rm ${PJAIL}
103  else
104    # Save the chroot tarball
105    mv ${PJAIL} ${WORLDCHROOT}
106  fi
107  rm ${PJAILSHA256}
108};
109
110### Download the chroot
111downloadchroot() {
112  # XXX If this is PCBSD, pbreg get /PC-BSD/Version
113  SYSVER=`uname -r | cut -f1 -d'-'`
114  FBSD_TARBALL="fbsd-release.tbz"
115  FBSD_TARBALL_CKSUM="${FBSD_TARBALL}.md5"
116
117  # Set the mirror URL, may be overridden by setting MIRRORURL environment variable
118  if [ -z "${MIRRORURL}" ]; then
119    get_mirror
120    MIRRORURL="$VAL"
121  fi
122
123  if [ ! -d "${JDIR}" ] ; then mkdir -p "${JDIR}" ; fi
124  cd ${JDIR}
125
126  echo "Fetching jail environment. This may take a while..."
127  echo "Downloading ${MIRRORURL}/${SYSVER}/${ARCH}/netinstall/${FBSD_TARBALL} ..."
128
129  if [ ! -e "$FBSD_TARBALL" ] ; then
130     get_file "${MIRRORURL}/${SYSVER}/${ARCH}/netinstall/${FBSD_TARBALL}" "$FBSD_TARBALL" 3
131     [ $? -ne 0 ] && printerror "Error while downloading the portsjail."
132  fi
133
134  if [ ! -e "$FBSD_TARBALL_CKSUM" ] ; then
135     get_file "${MIRRORURL}/${SYSVER}/${ARCH}/netinstall/${FBSD_TARBALL_CKSUM}" "$FBSD_TARBALL_CKSUM" 3
136     [ $? -ne 0 ] && printerror "Error while downloading the portsjail."
137  fi
138
139  [ "$(md5 -q ${FBSD_TARBALL})" != "$(cat ${FBSD_TARBALL_CKSUM})" ] &&
140    printerror "Error in download data, checksum mismatch. Please try again later."
141
142  # Creating ZFS dataset?
143  isDirZFS "${JDIR}"
144  if [ $? -eq 0 ] ; then
145    local zfsp=`getZFSRelativePath "${WORLDCHROOT}"`
146
147    # Use ZFS base for cloning
148    echo "Creating ZFS ${WORLDCHROOT} dataset..."
149    tank=`getZFSTank "${JDIR}"`
150    isDirZFS "${WORLDCHROOT}" "1"
151    if [ $? -ne 0 ] ; then
152       zfs create -o mountpoint=/${tank}${zfsp} -p ${tank}${zfsp}
153       if [ $? -ne 0 ] ; then exit_err "Failed creating ZFS base dataset"; fi
154    fi
155
156    tar xvpf ${FBSD_TARBALL} -C ${WORLDCHROOT} 2>/dev/null
157    if [ $? -ne 0 ] ; then exit_err "Failed extracting ZFS chroot environment"; fi
158
159    zfs snapshot ${tank}${zfsp}@clean
160    if [ $? -ne 0 ] ; then exit_err "Failed creating clean ZFS base snapshot"; fi
161    rm ${FBSD_TARBALL}
162  else
163    # Save the chroot tarball
164    mv ${FBSD_TARBALL} ${WORLDCHROOT}
165  fi
166  rm ${FBSD_TARBALL_CKSUM}
167};
168
169
170### Mount all needed filesystems for the jail
171mountjailxfs() {
172  for nullfs_mount in ${NULLFS_MOUNTS}; do
173    if [ ! -d "${JDIR}/${1}${nullfs_mount}" ] ; then
174      mkdir -p "${JDIR}/${1}${nullfs_mount}"
175    fi
176    if is_symlinked_mountpoint ${nullfs_mount}; then
177      echo "${nullfs_mount} has symlink as parent, not mounting"
178      continue
179    fi
180
181    echo "Mounting ${JDIR}/${1}${nullfs_mount}"
182    mount_nullfs ${nullfs_mount} ${JDIR}/${1}${nullfs_mount}
183  done
184
185  # Add support for linprocfs for ports that need linprocfs to build/run
186  if [  ! -d "${JDIR}/${1}/compat/linux/proc" ]; then
187    mkdir -p ${JDIR}/${1}/compat/linux/proc
188  fi
189  if is_symlinked_mountpoint ${JDIR}/${1}/compat/linux/proc; then
190    echo "${JDIR}/${1}/compat/linux/proc has symlink as parent, not mounting"
191    return
192  fi
193  echo "Enabling linprocfs support."
194  mount -t linprocfs linprocfs ${JDIR}/${1}/compat/linux/proc
195}
196
197### Umount all the jail's filesystems
198umountjailxfs() {
199  status="0"
200  # Umount all filesystems that are mounted into the portsjail
201  for mountpoint in $(mount | grep ${JDIR}/${1}/ | cut -d" " -f3); do
202    if [ "$mountpoint" = "${JDIR}/${1}/dev" ] ; then continue ; fi
203    if [ "$mountpoint" = "${JDIR}/${1}/" ] ; then continue ; fi
204    if [ "$mountpoint" = "${JDIR}/${1}" ] ; then continue ; fi
205    echo "Unmounting $mountpoint"
206    umount -f ${mountpoint}
207    if [ $? -ne 0 ] ; then status="1" ; fi
208  done
209  # Now try to umount /dev
210  umount -f ${JDIR}/${1}/dev 2>/dev/null >/dev/null
211  return $status
212}
213
214# Check if PBI scripts are loaded in jail
215checkpbiscripts() {
216  if [ -z "${1}" ] ; then return ; fi
217  if [ ! -e "${1}/usr/local/sbin/pbi_info" ] ; then
218    copypbiscripts "${1}"
219  elif [ "`ls -l /usr/local/sbin/pbi_info | awk '{print $5}'`" != "`ls -l ${1}/usr/local/sbin/pbi_info | awk '{print $5}'`" ] ; then
220    copypbiscripts "${1}"
221  fi
222}
223
224# Copy PBI scripts to jail
225copypbiscripts() {
226  if [ -z "${1}" ] ; then return ; fi
227  mkdir -p ${1}/usr/local/sbin >/dev/null 2>/dev/null
228  cp /usr/local/sbin/pbi* ${1}/usr/local/sbin/
229  chmod 755 ${1}/usr/local/sbin/pbi*
230
231  # Copy rc.d pbid script
232  mkdir -p ${1}/usr/local/etc/rc.d >/dev/null 2>/dev/null
233  cp /usr/local/etc/rc.d/pbid ${1}/usr/local/etc/rc.d/
234
235  # Copy any PBI manpages
236  for man in `find /usr/local/man | grep pbi`
237  do
238    if [ ! -d "${1}`dirname $man`" ] ; then
239      mkdir -p "${1}`dirname $man`"
240    fi
241    cp "${man}" "${1}${man}"
242  done
243}
244
245mkportjail() {
246  if [ -z "${1}" ] ; then return ; fi
247  ETCFILES="resolv.conf passwd master.passwd spwd.db pwd.db group localtime"
248  for file in ${ETCFILES}; do
249    rm ${1}/etc/${file} >/dev/null 2>&1
250    cp /etc/${file} ${1}/etc/${file}
251  done
252 
253  # Need to symlink /home
254  chroot ${1} ln -fs /usr/home /home
255
256  # Make sure we remove our cleartmp rc.d script, causes issues
257  [ -e "${1}/etc/rc.d/cleartmp" ] && rm ${1}/etc/rc.d/cleartmp
258  # Flag this type
259  touch ${JMETADIR}/jail-portjail
260}
261
262mkpluginjail() {
263  if [ -z "${1}" ] ; then return ; fi
264  ETCFILES="resolv.conf passwd master.passwd spwd.db pwd.db group localtime"
265  for file in ${ETCFILES}; do
266    rm ${1}/etc/${file} >/dev/null 2>&1
267    cp /etc/${file} ${1}/etc/${file}
268  done
269 
270  # Need to symlink /home
271  chroot ${1} ln -fs /usr/home /home
272
273  # Make sure we remove our cleartmp rc.d script, causes issues
274  [ -e "${1}/etc/rc.d/cleartmp" ] && rm ${1}/etc/rc.d/cleartmp
275  # Flag this type
276  touch ${JMETADIR}/jail-pluginjail
277}
278
279mkZFSSnap() {
280  isDirZFS "${1}" "1"
281  if [ $? -ne 0 ] ; then printerror "Not a ZFS volume: ${1}" ; fi
282  tank=`getZFSTank "$1"`
283  rp=`getZFSRelativePath "$1"`
284  zdate=`date +%Y-%m-%d-%H-%M-%S`
285  zfs snapshot $tank${rp}@$zdate
286}
287
288listZFSSnap() {
289  isDirZFS "${1}" "1"
290  if [ $? -ne 0 ] ; then printerror "Not a ZFS volume: ${1}" ; fi
291  tank=`getZFSTank "$1"`
292  rp=`getZFSRelativePath "$1"`
293  zfs list -t snapshot | grep -w "^${tank}${rp}" | cut -d '@' -f 2 | awk '{print $1}'
294}
295
296listZFSClone() {
297  isDirZFS "${1}" "1"
298  if [ $? -ne 0 ] ; then printerror "Not a ZFS volume: ${1}" ; fi
299  tank=`getZFSTank "$1"`
300  cdir=`getZFSRelativePath "${CDIR}"` 
301  echo "Clone Directory: ${CDIR}"
302  echo "-----------------------------------"
303  zfs list | grep -w "^${tank}${cdir}/${2}" | awk '{print $5}' | sed "s|${CDIR}/${2}-||g"
304}
305
306rmZFSClone() {
307  CLONEDIR="${CDIR}/${3}-${2}"
308  isDirZFS "${CLONEDIR}" "1"
309  if [ $? -ne 0 ] ; then printerror "Not a ZFS volume: ${CLONEDIR}" ; fi
310  tank=`getZFSTank "${CLONEDIR}"`
311  rp=`getZFSRelativePath "${CLONEDIR}"`
312  zfs destroy ${tank}${rp}
313}
314
315rmZFSSnap() {
316  isDirZFS "${1}" "1"
317  if [ $? -ne 0 ] ; then printerror "Not a ZFS volume: ${1}" ; fi
318  tank=`getZFSTank "$1"`
319  rp=`getZFSRelativePath "$1"`
320  zfs destroy $tank${rp}@$2
321}
322
323revertZFSSnap() {
324  isDirZFS "${1}" "1"
325  if [ $? -ne 0 ] ; then printerror "Not a ZFS volume: ${1}" ; fi
326  tank=`getZFSTank "$1"`
327  rp=`getZFSRelativePath "$1"`
328
329  # Make sure this is a valid snapshot
330  zfs list -t snapshot | grep -w "^${tank}${rp}" | cut -d '@' -f 2 | awk '{print $1}' | grep -q ${2}
331  if [ $? -ne 0 ] ; then printerror "Invalid ZFS snapshot!" ; fi
332
333  # Check if the jail is running first
334  ${PROGDIR}/scripts/backend/checkstatus.sh "${3}"
335  if [ "$?" = "0" ]; then
336    restartJail="YES"
337    # Make sure the jail is stopped
338    ${PROGDIR}/scripts/backend/stopjail.sh "${3}"
339    ${PROGDIR}/scripts/backend/checkstatus.sh "${3}"
340    if [ "$?" = "0" ]; then
341      printerror "Could not stop jail... Halting..."
342    fi
343  fi
344
345  # Rollback the snapshot
346  zfs rollback -R -f ${tank}${rp}@$2
347
348  # If it was started, restart the jail now
349  if [ "$restartJail" = "YES" ]; then
350    ${PROGDIR}/scripts/backend/startjail.sh "${3}"
351  fi
352 
353}
354
355cloneZFSSnap() {
356  isDirZFS "${1}" "1"
357  if [ $? -ne 0 ] ; then printerror "Not a ZFS volume: ${1}" ; fi
358  tank=`getZFSTank "$1"`
359  rp=`getZFSRelativePath "$1"`
360  cdir=`getZFSRelativePath "${CDIR}"`
361
362  # Make sure this is a valid snapshot
363  zfs list -t snapshot | grep -w "^${tank}${rp}" | cut -d '@' -f 2 | awk '{print $1}' | grep -q ${2}
364  if [ $? -ne 0 ] ; then printerror "Invalid ZFS snapshot!" ; fi
365
366  if [ -d "${CDIR}/${3}-${2}" ] ; then
367     printerror "This snapshot is already cloned and mounted at: ${CDIR}/${3}-${2}"
368  fi
369
370  # Clone the snapshot
371  zfs clone -p ${tank}${rp}@$2 ${tank}${cdir}/${3}-${2}
372
373  echo "Snapshot cloned and mounted to: ${CDIR}/${3}-${2}"
374}
375
376set_warden_metadir()
377{
378   JMETADIR="${JDIR}/.${JAILNAME}.meta"
379   export JMETADIR
380}
381
382get_ip_and_netmask()
383{
384   JIP=`echo "${1}" | cut -f1 -d'/'`
385   JMASK=`echo "${1}" | cut -f2 -d'/' -s`
386}
387
388get_interface_addresses()
389{
390   ifconfig ${1} | grep -w inet | awk '{ print $2 }'
391}
392
393get_interface_ipv4_addresses()
394{
395   ifconfig ${1} | grep -w inet | awk '{ print $2 }'
396}
397
398get_interface_ipv6_addresses()
399{
400   ifconfig ${1} | grep -w inet6 | awk '{ print $2 }'
401}
402
403get_interface_address()
404{
405   ifconfig ${1} | grep -w inet | head -1 | awk '{ print $2 }'
406}
407
408get_interface_ipv4_address()
409{
410   ifconfig ${1} | grep -w inet | head -1 | awk '{ print $2 }'
411}
412
413get_interface_ipv6_address()
414{
415   ifconfig ${1} | grep -w inet6 | head -1 | awk '{ print $2 }'
416}
417
418get_interface_aliases()
419{
420   local _count
421
422   _count=`ifconfig ${1} | grep -w inet | wc -l`
423   _count="$(echo "${_count} - 1" | bc)"
424
425   ifconfig ${1} | grep -w inet | tail -${_count} | awk '{ print $2 }'
426}
427
428get_interface_ipv4_aliases()
429{
430   local _count
431
432   _count=`ifconfig ${1} | grep -w inet | wc -l`
433   _count="$(echo "${_count} - 1" | bc)"
434
435   ifconfig ${1} | grep -w inet | tail -${_count} | awk '{ print $2 }'
436}
437
438get_interface_ipv6_aliases()
439{
440   local _count
441
442   _count=`ifconfig ${1} | grep -w inet | wc -l`
443   _count="$(echo "${_count} - 1" | bc)"
444
445   ifconfig ${1} | grep -w inet6 | tail -${_count} | awk '{ print $2 }'
446}
447
448get_default_route()
449{
450   netstat -f inet -nr | grep '^default' | awk '{ print $2 }'
451}
452
453get_default_interface()
454{
455   netstat -f inet -nrW | grep '^default' | awk '{ print $7 }'
456}
457
458get_bridge_interfaces()
459{
460   ifconfig -a | grep -E '^bridge[0-9]+' | cut -f1 -d:
461}
462
463get_bridge_members()
464{
465   ifconfig ${1} | grep -w member | awk '{ print $2 }'
466}
467
468get_bridge_interface_by_ipv4_network()
469{
470   local network="${1}"
471   local bridges="$(get_bridge_interfaces)"
472
473   if [ -z "${network}" ]
474   then
475      return 1
476   fi
477
478   for _bridge in ${bridges}
479   do
480      local ips="$(get_interface_ipv4_aliases "${_bridge}")"
481      for _ip in ${ips}
482      do
483         if in_ipv4_network "${_ip}" "${network}"
484         then
485            echo "${_bridge}"
486            return 0
487         fi
488      done
489   done
490
491   return 1
492}
493
494get_bridge_interface_by_ipv6_network()
495{
496   local network="${1}"
497   local bridges="$(get_bridge_interfaces)"
498
499   if [ -z "${network}" ]
500   then
501      return 1
502   fi
503
504   for _bridge in ${bridges}
505   do
506      local ips="$(get_interface_ipv6_aliases "${_bridge}")"
507      for _ip in ${ips}
508      do
509         if in_ipv6_network "${_ip}" "${network}"
510         then
511            echo "${_bridge}"
512            return 0
513         fi
514      done
515   done
516
517   return 1
518}
519
520is_bridge_member()
521{
522   local _bridge="${1}"
523   local _iface="${2}"
524
525   for _member in `get_bridge_members ${_bridge}`
526   do
527      if [ "${_member}" = "${_iface}" ] ; then
528         return 0
529      fi
530   done
531
532   return 1
533}
534
535jail_interfaces_down()
536{
537   local _jid="${1}"
538   local _bridgeif
539   local _epaira
540   local _epairb
541
542   _epairb=`jexec ${_jid} ifconfig -a | grep '^epair' | cut -f1 -d:`
543   if [ -n "${_epairb}" ] ; then
544      _epaira=`echo ${_epairb} | sed -E 's|b$|a|'`
545      _bridgeif=
546
547      for _bridge in `ifconfig -a | grep -E '^bridge[0-9]+' | cut -f1 -d:`
548      do
549         for _member in `ifconfig ${_bridge} | grep member | awk '{ print $2 }'`
550         do
551            if [ "${_member}" = "${_epaira}" ] ; then
552               _bridgeif="${_bridge}"
553                break
554            fi
555         done
556         if [ -n "${_bridgeif}" ] ; then
557            break
558         fi
559      done
560
561      jexec ${_jid} ifconfig ${_epairb} down
562      ifconfig ${_epaira} down
563      ifconfig ${_epaira} destroy
564      _count=`ifconfig ${_bridgeif} | grep member | awk '{ print $2 }' | wc -l`
565      if [ "${_count}" -le "1" ] ; then
566         ifconfig ${_bridgeif} destroy
567      fi
568   fi
569}
570
571enable_cron()
572{
573   cronscript="${PROGDIR}/scripts/backend/cronsnap.sh"
574   grep -q "${cronscript}" /etc/crontab
575   if [ $? -eq 0 ] ; then return 0 ; fi
576   echo "2     *        *       *       *        root    ${cronscript}" >> /etc/crontab
577   # Restart cron
578   /etc/rc.d/cron restart >/dev/null 2>/dev/null
579}
580
581fix_old_meta()
582{
583   for i in `ls -d ${JDIR}/.*.meta 2>/dev/null`
584   do
585      if [ -e "${i}/xjail" ] ; then
586         touch ${i}/jail-portjail 2>/dev/null
587      fi
588      if [ -e "${i}/linuxjail" ] ; then
589         touch ${i}/jail-linux 2>/dev/null
590      fi
591   done
592}
593
594is_ipv4()
595{
596   local addr="${1}"
597   local res=1
598
599   local ipv4="$(/usr/local/bin/sipcalc "${addr}"|head -1|cut -f2 -d'['|awk '{ print $1 }')"
600   if [ "${ipv4}" = "ipv4" ]
601   then
602      res=0
603   fi
604
605   return ${res}
606}
607
608is_ipv6()
609{
610   local addr="${1}"
611   local res=1
612
613   local ipv6="$(/usr/local/bin/sipcalc "${addr}"|head -1|cut -f2 -d'['|awk '{ print $1 }')"
614   if [ "${ipv6}" = "ipv6" ]
615   then
616      res=0
617   fi
618
619   return ${res}
620}
621
622in_ipv4_network()
623{
624   local addr="${1}"
625   local network="${2}"
626   local res=1
627
628   local start="$(/usr/local/bin/sipcalc "${network}"|awk '/^Usable/ { print $4 }')"
629   local end="$(/usr/local/bin/sipcalc "${network}"|awk '/^Usable/ { print $6 }')"
630
631   local iaddr="$(/usr/local/bin/sipcalc "${addr}"|awk '/(decimal)/ { print $5 }')"
632   local istart="$(/usr/local/bin/sipcalc "${start}"|awk '/(decimal)/ { print $5 }')"
633   local iend="$(/usr/local/bin/sipcalc "${end}"|awk '/(decimal)/ { print $5 }')"
634
635   if [ "${iaddr}" -ge "${istart}" -a "${iaddr}" -le "${iend}" ]
636   then
637      res=0
638   fi
639
640   return ${res}
641}
642
643ipv6_to_binary()
644{
645   echo ${1}|awk '{
646      split($1, octets, ":");
647      olen = length(octets);
648               
649      bnum = "";
650      for (i = 1;i <= olen;i++) {
651         tbnum = "";
652         dnum = int(sprintf("0x%s", octets[i]));
653         for (;;) {
654            rem = int(dnum % 2);
655            if (rem == 0)
656               tbnum = sprintf("0%s", tbnum);
657            else               
658               tbnum = sprintf("1%s", tbnum);
659            dnum /= 2;
660            if (dnum < 1)
661               break;
662         }
663         bnum = sprintf("%s%016s", bnum, tbnum);
664      }
665      printf("%s", bnum);
666   }'
667}
668
669in_ipv6_network()
670{
671   local addr="${1}"
672   local network="${2}"
673   local mask="$(echo "${network}"|cut -f2 -d'/' -s)"
674   local res=1
675
676   local addr="$(/usr/local/bin/sipcalc "${addr}"|awk \
677      '/^Expanded/ { print $4}')"
678   local start="$(/usr/local/bin/sipcalc "${network}"|egrep \
679      '^Network range'|awk '{ print $4 }')"
680
681   local baddr="$(ipv6_to_binary "${addr}")"
682   local bstart="$(ipv6_to_binary "${start}")"
683
684   local baddrnet="$(echo "${baddr}"|awk -v mask="${mask}" \
685      '{ s = substr($0, 1, mask); printf("%s", s); }')"
686   local bstartnet="$(echo "${bstart}"|awk -v mask="${mask}" \
687      '{ s = substr($0, 1, mask); printf("%s", s); }')"
688
689   if [ "${baddrnet}" = "${bstartnet}" ]
690   then
691      res=0
692   fi
693
694   return ${res}
695}
696
697bootstrap_pkgng()
698{
699  cd ${1} 
700  SYSVER="$(uname -r)"
701  echo "Boot-strapping pkgng"
702  mkdir -p ${1}/usr/local/etc
703  cp /usr/local/etc/pkg-pubkey.cert ${1}/usr/local/etc/
704  if [ $? -ne 0 ] ; then
705     echo "Failed copying /usr/local/etc/pkg-pubkey.cert"
706  fi
707
708  echo '#!/bin/sh
709  tar xvf pkg.txz --exclude +MANIFEST --exclude +MTREE_DIRS 2>/dev/null
710  pkg add pkg.txz
711  rm pkg.txz
712  ARCH=$(uname -m)
713  REL=$(uname -r)
714  echo "packagesite: http://ftp.pcbsd.org/pub/mirror/packages/$REL/$ARCH" >/usr/local/etc/pkg.conf
715  echo "PUBKEY: /usr/local/etc/pkg-pubkey.cert" >>/usr/local/etc/pkg.conf
716  echo "PKG_CACHEDIR: /usr/local/tmp" >>/usr/local/etc/pkg.conf
717  pkg install -y pcbsd-utils
718  exit $?
719' > ${1}/bootstrap-pkgng
720  chmod 755 ${1}/bootstrap-pkgng
721
722  if [ -e "pkg.txz" ] ; then rm pkg.txz ; fi
723  get_file_from_mirrors "/packages/${SYSVER}/${ARCH}/Latest/pkg.txz" "pkg.txz"
724  if [ $? -eq 0 ] ; then
725    chroot ${1} /bootstrap-pkgng
726    if [ $? -eq 0 ] ; then
727      rm ${1}/bootstrap-pkgng
728      chroot ${1} pc-extractoverlay server --sysinit
729      return 0
730    fi
731  fi
732  echo "Failed boot-strapping PKGNG, most likely cause is internet connection failure."
733  rm ${1}/bootstrap-pkgng
734  return 1
735}
736
737ipv4_configured()
738{
739   local iface="${1}"
740   local jid="${2}"
741   local jexec=
742
743   if [ -n "${jid}" ] ; then
744      jexec="jexec ${jid}"
745   fi
746
747   ${jexec} ifconfig "${iface}" | grep -qw inet 2>/dev/null
748}
749
750ipv6_configured()
751{
752   local iface="${1}"
753   local jid="${2}"
754   local jexec=
755
756   if [ -n "${jid}" ] ; then
757      jexec="jexec ${jid}"
758   fi
759
760   ${jexec} ifconfig "${iface}" | grep -qw inet6 2>/dev/null
761}
Note: See TracBrowser for help on using the repository browser.