[PC-BSD Commits] r13456 - pcbsd/current/system-overlay/usr/local/share/pcbsd/scripts

svn at pcbsd.org svn at pcbsd.org
Thu Oct 20 06:03:18 PDT 2011


Author: kris
Date: 2011-10-20 06:03:18 -0700 (Thu, 20 Oct 2011)
New Revision: 13456

Modified:
   pcbsd/current/system-overlay/usr/local/share/pcbsd/scripts/portjail.sh
Log:

Add new portjail.sh script from Lars Engels, thanks!!



Modified: pcbsd/current/system-overlay/usr/local/share/pcbsd/scripts/portjail.sh
===================================================================
--- pcbsd/current/system-overlay/usr/local/share/pcbsd/scripts/portjail.sh	2011-10-20 12:59:43 UTC (rev 13455)
+++ pcbsd/current/system-overlay/usr/local/share/pcbsd/scripts/portjail.sh	2011-10-20 13:03:18 UTC (rev 13456)
@@ -1,98 +1,210 @@
 #!/bin/sh
-# Script to start / stop the ports jail
+# Script to manage the Port Jail
 #############################################################
 
 PJDIR="/usr/jails/portjail"
-PJHOST="`hostname`"
-
+PBREG="/usr/local/bin/pbreg"
+JAILME="/usr/local/sbin/jailme"
+PJHOST="$(hostname)"
+ARCH="$(uname -m)"
+ID="$(id -u)"
+# Default pcbsd.conf file
+PCBSD_ETCCONF="/usr/local/etc/pcbsd.conf"
 # Set manpath to enable whatis to work
-MANPATH=/usr/local/man
-export MANPATH
+MANPATH="/usr/local/man" ; export MANPATH
+MIN_FBSD_VERSION=9
+# A list of directories that are mounted into the jail
+NULLFS_MOUNTS="/tmp /media /usr/home /usr/src"
 
-display_help()
-{
+### Usage and exit
+display_help() {
   echo "PC-BSD Port Jail Management"
   echo "--------------------------------"
   echo "Usage:"
   echo "  portjail start 	- Starts the jail, root only"
   echo "  portjail stop  	- Stops the jail, root only"
+  echo "  portjail restart 	- Restarts the jail, root only"
   echo "  portjail console  	- Starts a shell session within the jail"
   echo "  portjail run <cmd>  	- Runs the specified command within the jail"
   echo "  portjail init  	- Setup the jail environment"
+  echo "  portjail status       - Show the portjail's status"
+  echo "  portjail delete  	- Deletes the jail"
 
-};
+  exit 1
+}
 
-
-# if we are called without a flag, warn the user and exit
-if [ -z "$1" ]
-then
-  display_help
+### Print an error on STDERR and bail out
+printerror() {
+  echo >&2 "$*"
   exit 1
-fi
+}
 
-if [ "$1" = "start" ]
-then
-  if [ ! -d "${PJDIR}/etc" ] ; then
-    if [ "$2" = "rc" ] ; then exit 0 ; fi
-    echo "Error: The portjail is not yet installed! Please run 'portjail init' as root."
-    exit 1
+### Check if we're running PCBSD or FreeBSD
+checkpcbsd() {
+  if [ -e "${PCBSD_ETCCONF}" -a -e "${PBREG}" ]; then
+    ISPCBSD="true"
+    SYSVER="$(pbreg get /PC-BSD/Version)"
+    FBSD_TARBALL="fbsd-release.tbz"
+    FBSD_TARBALL_CKSUM="${FBSD_TARBALL}.md5"
+  else
+    ISPCBSD=""
+    SYSVER="$(uname -r | cut -d- -f1-2)"
+    FBSD_TARBALL="base.txz"
+    FBSD_TARBALL_CKSUM="MANIFEST"
+    [ $(echo ${SYSVER} | sed 's/^\([0-9][^.-]*\).*/\1/') -lt ${MIN_FBSD_VERSION} ] &&
+      printerror "Sorry, you need at least FreeBSD ${MIN_FBSD_VERSION}."
   fi
+}
 
-  if [ "`id -u`" != "0" ]
-  then
-    echo "Error: You must be root to start the ports jail"
+### Check if the running user is root
+checkroot() {
+  if [ ${ID} -ne 0 ]; then
+    [ -n "$1" ] && echo checkroot "Error: You must be root to $1 the ports jail."
     exit 1
   fi
+  return 0
+}
 
-  # Create some hard-links for the portjail
-  rm ${PJDIR}/etc/resolv.conf >/dev/null 2>/dev/null
-  cp /etc/resolv.conf ${PJDIR}/etc/resolv.conf
-  rm ${PJDIR}/etc/passwd >/dev/null 2>/dev/null
-  cp /etc/passwd ${PJDIR}/etc/passwd
-  rm ${PJDIR}/etc/master.passwd >/dev/null 2>/dev/null
-  cp /etc/master.passwd ${PJDIR}/etc/master.passwd
-  rm ${PJDIR}/etc/spwd.db >/dev/null 2>/dev/null
-  cp /etc/spwd.db ${PJDIR}/etc/spwd.db
-  rm ${PJDIR}/etc/pwd.db >/dev/null 2>/dev/null
-  cp /etc/pwd.db ${PJDIR}/etc/pwd.db
-  rm ${PJDIR}/etc/group >/dev/null 2>/dev/null
-  cp /etc/group ${PJDIR}/etc/group
-  rm ${PJDIR}/etc/localtime >/dev/null 2>/dev/null
-  cp /etc/localtime ${PJDIR}/etc/localtime
+### Check if the jail is installed or not
+checkjailinstalled() {
+  if [ -d "${PJDIR}/etc" ]; then
+    return 0
+  else
+    return 1
+  fi
+}
 
-#  # Create our pseudo device for networking
-#  ifconfig lo1 create
-#  ifconfig lo1 inet ${PJIP} netmask 255.255.255.0 
-  
+### Check if the jail is currently running
+checkjailrunning() {
+  jailrunning="$(jls | awk -v PJDIR=${PJDIR} '$4 == PJDIR {print $4}')"
+  if [ -n "${jailrunning}" ]; then
+    return 0
+  else
+    return 1
+  fi
+}
 
-#  # Configure NAT with PF
-#  sed -i -e '/.*from lo1:network to any/d' /etc/pf.conf
+### Warn and exit if the jail is not yet installed
+checkinitneeded() {
+  checkjailinstalled || printerror "Error: The jail is not installed. Please run 'portjail init' as root."
+}
 
-#  TMPIF=`ifconfig -l`
-#  for i in ${TMPIF}
-#  do
-#    case $i in 
-#      fwe0|fwip0|pflog0|pfsync0|lo0|lo1) ;;
-#      *) cp /etc/pf.conf /etc/.pftmp.conf
-#         sed "s/scrub in all/scrub in all\\`echo -e '\r'`nat on ${i} from lo1:network to any -> (${i})/" /etc/.pftmp.conf  | tr -s "\r" "\n" >/etc/pf.conf ;;
-#    esac
-#  done
-#
-#  rm /etc/.pftmp.conf >/dev/null 2>/dev/null
-#  /etc/rc.d/pf restart >/dev/null 2>/dev/null
+### Check if jailme is installed
+checkjailme() {
+  [ -e ${JAILME} ] || printerror "You need to install sysutils/jailme first."
+}
 
-  # Figure out our default itnerfaces, otherwise try all.
-  # Get the first IP address we find that is not special and use that for the jail.
-  IP6_DFLT_IFACE=`netstat -Warn -f inet6 | awk '/^default/ { print $7 }'`
-  IP4_DFLT_IFACE=`netstat -Warn -f inet  | awk '/^default/ { print $7 }'`
-  if [ -z "${IP6_DFLT_IFACE}" ]; then
-	IP6_DFLT_IFACE="-a"
+### Download the PCBSD version of the portsjail
+downloadpcbsd() {
+  # Set the mirror URL, may be overridden by setting MIRRORURL environment variable
+  if [ -z "${MIRRORURL}" ]; then
+    MIRRORURL="$(grep ^PCBSD_MIRROR: ${PCBSD_ETCCONF} | cut -d' ' -f2)"
+    # Use the default mirror, if no other mirror is found.
+    [ -z "${MIRRORURL}" ] && MIRRORURL="ftp://ftp.pcbsd.org/pub/mirror"
   fi
-  if [ -z "${IP4_DFLT_IFACE}" ]; then
-	IP4_DFLT_IFACE="-a"
+
+  cd ${PJDIR}
+
+  echo "Fetching PC-BSD environment. This may take a while..."
+  echo "Downloading ${MIRRORURL}/${SYSVER}/${ARCH}/netinstall/${FBSD_TARBALL} ..."
+  fetch -a ${MIRRORURL}/${SYSVER}/${ARCH}/netinstall/${FBSD_TARBALL} \
+           ${MIRRORURL}/${SYSVER}/${ARCH}/netinstall/${FBSD_TARBALL_CKSUM}
+  [ $? -ne 0 ] && printerror "Error while downloading the portsjail."
+
+  [ "$(md5 -q ${FBSD_TARBALL})" != "$(cat ${FBSD_TARBALL_CKSUM})" ] &&
+    printerror "Error in download data, checksum mismatch. Please try again later."
+
+  echo "Extracting FreeBSD environment... This may take a while..."
+  tar xvpf ${FBSD_TARBALL} -C ${PJDIR}
+  # Cleanup
+  rm ${FBSD_TARBALL} ${FBSD_TARBALL_CKSUM}
+}
+
+### Download a plain FreeBSD base.txz
+downloadfreebsd() {
+  echo ${SYSVER} | egrep -q '(CURRENT)|(STABLE)'
+  if [ $? -eq 0 ]; then
+    echo "It appears that there are no pre-compiled tarballs for your version ${SYSVER}."
+    echo "You may use the compile option (c), or provide a different release name, e.g."
+    echo "9.0-RELEASE"
+    read ANSWER
+    if [ "${ANSWER}" = "c" ]; then
+      PJMETHOD="b"
+      initjail
+    else
+      SYSVER="${ANSWER}"
+    fi 
   fi
-  PJIP6=`ifconfig ${IP6_DFLT_IFACE} inet6 | awk '{ if (/fe80:/) { next; }; if (/ ::1 /) { next; }; if (/inet6 /) { print $2 }; }' | head -1`
-  PJIP4=`ifconfig ${IP6_DFLT_IFACE} inet | awk '{ if (/127.0.0./) { next; }; if (/inet /) { print $2 }; }' | head -1`
+    echo "You may enter a mirror server manually, else ftp.freebsd.org is used: [ftp.frebsd.org]"
+    read PJAILMIRROR
+    echo "Which protocol do you want to use ftp or http? [ftp]"
+    read PROTOCOL
+    [ -z "${PJAILMIRROR}" ] && PJAILMIRROR="ftp.freebsd.org"
+    [ -z "${PROTOCOL}" ] && PROTOCOL="ftp"
+    cd ${PJDIR}
+    echo "Fetching FreeBSD environment. This may take a while..."
+    echo "Downloading ${PROTOCOL}://${PJAILMIRROR}/pub/FreeBSD/releases/${ARCH}/${ARCH}/${SYSVER}/${FBSD_TARBALL}"
+    fetch -a ${PROTOCOL}://${PJAILMIRROR}/pub/FreeBSD/releases/${ARCH}/${ARCH}/${SYSVER}/${FBSD_TARBALL}
+    [ $? -ne 0 ] && printerror "Error: Download failed!"
+    fetch -a ${PROTOCOL}://${PJAILMIRROR}/pub/FreeBSD/releases/${ARCH}/${ARCH}/${SYSVER}/${FBSD_TARBALL_CKSUM}
+    [ $? -ne 0 ] && printerror "Error: Download failed!"
+
+    [ "$(sha256 -q ${FBSD_TARBALL})" != \
+      "$(awk -v FBSD_TARBALL=${FBSD_TARBALL} '$1 == FBSD_TARBALL {print $2}' ${FBSD_TARBALL_CKSUM})" ] &&
+      printerror "Error in download data, checksum mismatch. Please try again later."
+
+    tar --unlink -xvpJf ${FBSD_TARBALL}
+    [ $? -ne 0 ] && printerror "Error: Extraction failed!"
+    rm ${FBSD_TARBALL} ${FBSD_TARBALL_CKSUM}
+}
+
+### Mount all needed filesystems for the jail
+mountjailfs() {
+  for nullfs_mount in ${NULLFS_MOUNTS}; do
+    mount_nullfs ${nullfs_mount} ${PJDIR}${nullfs_mount}
+  done
+
+  mount -t devfs devfs ${PJDIR}/dev
+  mount -t procfs procfs ${PJDIR}/proc
+
+  # Add support for linprocfs for ports that need linprocfs to build/run
+  if [ -d "${PJDIR}/compat/linux/proc" ]; then
+    mount -t linprocfs linprocfs ${PJDIR}/compat/linux/proc
+  else
+    echo "/compat/linux/proc does not exist. Adding linprocfs support."
+    mkdir -p ${PJDIR}/compat/linux/proc
+    mount -t linprocfs linprocfs ${PJDIR}/compat/linux/proc
+  fi
+}
+
+### Umount all the jail's filesystems
+umountjailfs() {
+  # Umount all filesystems that are mounted into the portsjail
+  for mountpoint in $(mount | grep ${PJDIR} | cut -d" " -f3); do
+    umount ${mountpoint} || return 1
+  done
+}
+
+### Start the jail
+startjail() {
+  [ ! -d "${PJDIR}/etc" -a "$1" = "rc" ] && exit 0
+
+  echo "Starting the portjail..."
+  # Create some hard-links for the portjail
+  ETCFILES="resolv.conf passwd master.passwd spwd.db pwd.db group localtime"
+  for file in ${ETCFILES}; do
+    rm ${PJDIR}/etc/${file} >/dev/null 2>&1
+    cp /etc/${file} ${PJDIR}/etc/${file}
+  done
+
+  # Figure out our default interfaces, otherwise try all.
+  # Get the first IP address we find that is not special and use that for the jail.
+  IP6_DFLT_IFACE=$(netstat -Warn -f inet6 | awk '/^default/ { print $7 }')
+  IP4_DFLT_IFACE=$(netstat -Warn -f inet  | awk '/^default/ { print $7 }')
+  [ -z "${IP6_DFLT_IFACE}" ] && IP6_DFLT_IFACE="-a"
+  [ -z "${IP4_DFLT_IFACE}" ] && IP4_DFLT_IFACE="-a"
+  PJIP6=$(ifconfig ${IP6_DFLT_IFACE} inet6 | awk '{ if (/fe80:/) { next; }; if (/ ::1 /) { next; }; if (/inet6 /) { print $2 }; }' | head -1)
+  PJIP4=$(ifconfig ${IP6_DFLT_IFACE} inet | awk '{ if (/127.0.0./) { next; }; if (/inet /) { print $2 }; }' | head -1)
   PJIP="ip6.addr=${PJIP6}"
   if [ -n "${PJIP}" -a -n "${PJIP4}" ]; then
 	PJIP="${PJIP} ip4.addr=${PJIP4}"
@@ -101,158 +213,220 @@
   fi
 
   # Make sure we remove our cleartmp rc.d script, causes issues
-  if [ -e "${PJDIR}/etc/rc.d/cleartmp" ]
-  then
-    rm ${PJDIR}/etc/rc.d/cleartmp
-  fi
+  [ -e "${PJDIR}/etc/rc.d/cleartmp" ] && rm ${PJDIR}/etc/rc.d/cleartmp
 
   # Add the hostname to the portjails /etc/hosts file, to prevent sendmail warnings
-  sed -i -e '/^127.0.0.1.*/d' -e '/^::1.*/d' ${PJDIR}/etc/hosts
+  if [ -e ${PJDIR} ]; then
+    sed -i -e '/^127.0.0.1.*/d' ${PJDIR}/etc/hosts
+    sed -i -e '/^::1.*/d' ${PJDIR}/etc/hosts
+  fi
   echo "::1		localhost localhost.my.domain ${PJHOST}" >>${PJDIR}/etc/hosts
   echo "127.0.0.1	localhost localhost.my.domain ${PJHOST}" >>${PJDIR}/etc/hosts
 
   # Make sure the /etc/rc.conf HOSTNAME values match
-  #sed -i -e '/hostname=.*/d' ${PJDIR}/etc/rc.conf
   : > ${PJDIR}/etc/rc.conf
-  echo "hostname=\"$PJHOST\"" >> ${PJDIR}/etc/rc.conf
-  echo "cron_enable=\"NO\"" >> ${PJDIR}/etc/rc.conf
-  echo "syslogd_enable=\"NO\"" >> ${PJDIR}/etc/rc.conf
-  echo "sendmail_enable=\"NO\"" >> ${PJDIR}/etc/rc.conf
-  echo "sendmail_submit_enable=\"NO\"" >> ${PJDIR}/etc/rc.conf
-  echo "sendmail_outbound_enable=\"NO\"" >> ${PJDIR}/etc/rc.conf
-  echo "sendmail_msp_queue_enable=\"NO\"" >> ${PJDIR}/etc/rc.conf
+  cat > ${PJDIR}/etc/rc.conf <<-EOF
+hostname="$PJHOST"
+cron_enable="NO"
+syslogd_enable="NO"
+sendmail_enable="NO"
+sendmail_submit_enable="NO"
+sendmail_outbound_enable="NO"
+sendmail_msp_queue_enable="NO"
+EOF
 
-  mount_nullfs /tmp ${PJDIR}/tmp
-  mount_nullfs /media ${PJDIR}/media
-  mount_nullfs /usr/home ${PJDIR}/usr/home
-  mount -t devfs devfs ${PJDIR}/dev
-  mount -t procfs procfs ${PJDIR}/proc
-  # Add support for linprocfs and /usr/src for ports that need system source and linprocfs to build/run
-  mount_nullfs /usr/src ${PJDIR}/usr/src
-  if [ -d "${PJDIR}/compat/linux/proc" ]
-  then
-    mount -t linprocfs linprocfs ${PJDIR}/compat/linux/proc
-  else
-    echo "compat/linux/proc does not exist.  Adding linprocfs support"
-    mkdir -p ${PJDIR}/compat/linux/proc
-    mount -t linprocfs linprocfs ${PJDIR}/compat/linux/proc
-  fi
+  # Mount all needed filesystems into the portjail path
+  mountjailfs
+  # Actually create and start the jail
   jail -c name=portjail path=${PJDIR} host.hostname=${PJHOST} ${PJIP} persist
   jexec portjail /bin/sh /etc/rc
+}
 
-elif [ "$1" = "stop" ]
-then
+### Stop the jail
+stopjail() {
+  [ ! -d "${PJDIR}/etc" -a "$1" = "rc" ] && exit 0
 
-  if [ ! -d "${PJDIR}/etc" ] ; then
-    if [ "$2" = "rc" ] ; then exit 0 ; fi
-    echo "Error: The portjail is not yet installed! Please run 'portjail init' as root."
-    exit 1
-  fi
-
-  if [ "`id -u`" != "0" ]
-  then
-    echo "Error: You must be root to stop the ports jail"
-    exit 1
-  fi
-
-  # Cleanup /etc/pf.conf
-  #sed -i "" -e '/.*from lo1:network to any/d' /etc/pf.conf
-
+  echo "Stopping the portjail..."
   # Stop the Jail
   jexec portjail /bin/sh /etc/rc.shutdown
   jail -r portjail
-  umount ${PJDIR}/compat/linux/proc
-  umount ${PJDIR}/proc
-  umount ${PJDIR}/dev
-  umount ${PJDIR}/tmp
-  umount ${PJDIR}/media
-  umount ${PJDIR}/usr/home
-  umount ${PJDIR}/usr/src
-  #ifconfig lo1 destroy
 
-elif [ "$1" = "console" ]
-then
+  # Unmount all of the portjail's filesystems
+  umountjailfs
 
-  if [ ! -d "${PJDIR}/etc" ] ; then
-    echo "Error: The portjail is not yet installed! Please run 'portjail init' as root."
-    echo "Press ENTER to exit"
-    read tmp
-    exit 1
-  fi
+}
 
+### Start a console inside the jail
+jailconsole() {
   DBUS_SESSION_BUS_ADDRESS="" ; export DBUS_SESSION_BUS_ADDRESS
-  #PJID=`jls | grep "${PJDIR}" | tr -s " " | awk '{ print $1 }'`
-  PJID=`jls -s -j portjail -n jid | awk -F= '{ print $2 }'`
-  /usr/local/sbin/jailme $PJID /bin/csh
+  PJID=$(jls -s -j portjail -n jid | awk -F= '{ print $2 }')
+  ${JAILME} ${PJID} /bin/csh
+}
 
-elif [ "$1" = "run" ]
-then
+### Run a command inside the jail
+runjailcommand() {
+  [ -z "$1" ] && printerror "Error: No command specified!"
 
-  if [ ! -d "${PJDIR}/etc" ] ; then
-    echo "Error: The portjail is not yet installed! Please run 'portjail init' as root."
-    echo "Press ENTER to exit"
-    read tmp
-    exit 1
-  fi
-
-  if [ -z "$2" ]
-  then
-    echo "Error: No command specified!"
-    exit 1
-  fi
-
   DBUS_SESSION_BUS_ADDRESS="" ; export DBUS_SESSION_BUS_ADDRESS
-  #PJID=`jls | grep "${PJDIR}" | tr -s " " | awk '{ print $1 }'`
-  PJID=`jls -s -j portjail -n jid | awk -F= '{ print $2 }'`
-  /usr/local/sbin/jailme $PJID $2
+  PJID=$(jls -s -j portjail -n jid | awk -F= '{ print $2 }')
+  ${JAILME} ${PJID} "$1"
+}
 
-elif [ "$1" = "init" ]
-then
+### Show some information about the jail
+checkstatus() {
+  checkjailinstalled || installed=" not"
+  checkjailrunning   || running=" not"
+  echo "Portjail is${installed} installed."
+  echo "Portjail is${running} running."
+  [ -z "${installed}" ] &&
+    echo "There are $(PKG_DBDIR=${PJDIR}/var/db/pkg pkg_info 2>/dev/null | grep -c .) packages installed."
+  exit 0
+}
 
+### Build / download, install and setup the jail
+initjail() {
   # Setup a new portjail
+  if [ -d ${PJDIR}/etc ]; then
+     echo "The portsjail is already initialized. Re-initializing it will delete its"
+     echo "contents. Do you want to continue? [y|n]"
+     read DOIT
+     if [ "${DOIT}" = "y" ]; then
+       # Unmount first, so we don't delete /home and stuff :)
+       umountjailfs
+       if [ $? -eq 0 ]; then
+         rm -r ${PJDIR}
+       else
+         printerror "Error: An error occured while unmounting the portjail filesystems. \
+         	   Aborting re-initialization. Please check if there are any files opened \
+         	   in one of the portjails's filesystems and try again after closing them."
+       fi
+     else
+       exit 1
+     fi
+   fi
 
-  if [ "`id -u`" != "0" ] ; then
-    echo "Error: You must be root to setup the ports jail."
-    exit 1
-  fi
+  while [ "${PJMETHOD}" != "d" -a "${PJMETHOD}" != "b" ]; do
+    printf "Do you like to download a pre-compiled base jail from a FreeBSD mirror via\n"
+    printf "FTP/HTTP or do you like to build one from source? Enter d for download or\n"
+    printf "b for build from source: [d/b] "
+    read PJMETHOD
+    printf "\n"
+  done
 
   # Create the jail dir
-  if [ ! -d "${PJDIR}" ] ; then mkdir -p "${PJDIR}" ; fi
+  [ ! -d "${PJDIR}" ] && mkdir -p "${PJDIR}"
 
-  # Default pcbsd.conf file
-  PCBSD_ETCCONF="/usr/local/etc/pcbsd.conf"
+  case "${PJMETHOD}" in
+    b)
+    if [ ! -e "/usr/src/COPYRIGHT" ]
+    then
+      echo "Error: You will need a copy of FreeBSD sources in /usr/src to build the portjail."
+      echo "You may checkout sources via the System Manager, CVS, SVN or other method."
+      exit 1
+    fi
 
-  # Set the mirror URL
-  MIRRORURL="`cat ${PCBSD_ETCCONF} | grep 'PCBSD_MIRROR: ' | sed 's|PCBSD_MIRROR:||g'`"
+    # Preparing to build the jail
+    echo "Starting build of portsjail, this may take a while..."
+    sleep 5
+    cd /usr/src
+    make buildworld installworld distribution DESTDIR=${PJDIR}
+    [ $? -ne 0 ] &&
+      printerror "Error: The portjail build failed! Please check your sources and try again."
+    ;;
 
-  # Get the system version we are checking for updates to
-  SYSVER="`pbreg get /PC-BSD/Version`" ; export SYSVER
+    d)
+    if [ -n "${ISPCBSD}" ]; then
+      downloadpcbsd
+    else
+      downloadfreebsd
+    fi
+    echo "Extraction finished."
+    ;;
 
-  # Set the system arch type
-  ARCH=`uname -m`
+    *)
+    # NOTREACHED
+    ;;
+  esac
 
-  # To fetch the jail environment
-  echo "Fetching FreeBSD environment... This may take a while..."
-  cd ${PJDIR}
-  fetch -o rel.tbz ${MIRRORURL}/${SYSVER}/${ARCH}/netinstall/fbsd-release.tbz
-  fetch -o rel.md5 ${MIRRORURL}/${SYSVER}/${ARCH}/netinstall/fbsd-release.tbz.md5
+  # Make the home link
+  mkdir -p ${PJDIR}/usr/home
+  ln -sf /usr/home ${PJDIR}/home
 
-  if [ `md5 -q rel.tbz` != `cat rel.md5` ] ; then
-    echo "Error in download data, checksum mismatch.. Please try again later."
-    exit 1
+  echo "Portjail setup finished! Please run 'portjail start' to enable the jail."
+}
+
+deletejail() {
+  echo "Deleting portjail: ${PJDIR}"
+  chflags -R noschg ${PJDIR}
+  rm -rf ${PJDIR}
+  echo "Portjail deleted."
+}
+
+########################## MAIN ###############################
+
+# if we are called without a flag, warn the user and exit
+[ -z "$1" ] && display_help
+
+case "$1" in
+  start)
+  checkinitneeded
+  checkroot $1
+  checkjailrunning || startjail $2
+  ;;
+
+  stop)
+  # The portjail gets only stopped if the jail is installed and we're root.
+  checkinitneeded
+  checkroot $1
+  checkjailrunning && stopjail $2 || printerror "The jail is not running."
+  ;;
+
+  restart)
+  checkinitneeded
+  checkroot $1
+  checkjailrunning && stopjail $2 || printerror "The jail is not running."
+  startjail $2
+  ;;
+
+  console)
+  # Check if the portjail is already installed and running
+  checkinitneeded
+  checkjailme
+  checkjailrunning && jailconsole || printerror "The jail is not running."
+  ;;
+
+  run)
+  checkinitneeded
+  checkjailme
+  checkjailrunning && runjailcommand $2 || printerror "The jail is not running."
+  ;;
+
+  init)
+  checkroot $1
+  checkpcbsd
+  if ! checkjailrunning && ! checkjailinstalled; then
+    initjail
+  else
+    printerror "The jail is already installed."
   fi
+  ;;
+  status)
+  checkstatus
+  ;;
 
-  echo "Extracting FreeBSD environment... This may take a while..."
-  tar xvpf rel.tbz 2>/dev/null
+  delete)
+  checkroot $1
+  echo "You are about to delete the portjail. Do you really want to continue? [y|n]"
+  read DELETE
+  if [ "${DELETE}" = "y" ]; then
+    checkjailrunning && stopjail $2
+    deletejail
+  fi
+  ;;
 
-  # Cleanup
-  rm rel.tbz
-  rm rel.md5
+  *)
+  display_help
+  ;;
 
-  # Make the home link
-  mkdir -p ${PJDIR}/usr/home
-  ln -s /usr/home ${PJDIR}/home
+esac
 
-  echo "Portjail setup finished! Please run 'portjail start' to enable the jail."
-fi
-



More information about the Commits mailing list