Ticket #487: pcbsd-portjail-script-improvements.patch

File pcbsd-portjail-script-improvements.patch, 14.9 KB (added by gcooper, 3 years ago)
  • portjail.sh

     
    22# Script to manage the Port Jail 
    33############################################################# 
    44 
    5 PJDIR="/usr/jails/portjail" 
     5set -e 
     6 
     7DEFAULT_PJ_TARGET=$(uname -p) 
     8DEFAULT_PJ_TARGET_ARCH=$(uname -m) 
     9if [ "$DEFAULT_PJ_TARGET" = "$DEFAULT_PJ_TARGET_ARCH" ]; then 
     10        DEFAULT_PJ_TARGET_PAIR=$DEFAULT_PJ_TARGET 
     11else 
     12        DEFAULT_PJ_TARGET_PAIR="$DEFAULT_PJ_TARGET_ARCH:$DEFAULT_PJ_TARGET" 
     13fi 
     14DEFAULT_PJ_DESTDIR="/usr/jails/portjail" 
     15DEFAULT_PJ_HOSTNAME=$(hostname) 
     16DEFAULT_PJ_SRCDIR="/usr/src" 
     17 
    618PBREG="/usr/local/bin/pbreg" 
    719JAILME="/usr/local/sbin/jailme" 
    8 PJHOST="$(hostname)" 
    9 ARCH="$(uname -m)" 
    1020ID="$(id -u)" 
    1121# Default pcbsd.conf file 
    1222PCBSD_ETCCONF="/usr/local/etc/pcbsd.conf" 
    1323# Set manpath to enable whatis to work 
    14 MANPATH="/usr/local/man" ; export MANPATH 
     24: ${MANPATH=/usr/local/man} ; export MANPATH 
    1525MIN_FBSD_VERSION=9 
    1626# A list of directories that are mounted into the jail 
    17 NULLFS_MOUNTS="/tmp /media /usr/home /usr/src" 
     27DELIM=",-," 
     28NULLFS_MOUNTS="/tmp$DELIM/tmp /media$DELIM/media /usr/home$DELIM/usr/home" 
    1829 
    1930### Usage and exit 
    2031display_help() { 
    21   echo "PC-BSD Port Jail Management" 
    22   echo "--------------------------------" 
    23   echo "Usage:" 
    24   echo "  portjail start        - Starts the jail, root only" 
    25   echo "  portjail stop         - Stops the jail, root only" 
    26   echo "  portjail restart      - Restarts the jail, root only" 
    27   echo "  portjail console      - Starts a shell session within the jail" 
    28   echo "  portjail run <cmd>    - Runs the specified command within the jail" 
    29   echo "  portjail init         - Setup the jail environment" 
    30   echo "  portjail status       - Show the portjail's status" 
    31   echo "  portjail delete       - Deletes the jail" 
     32        cat <<EOF 
     33The PC-BSD Port Jail Management Tool 
     34----------------------------------------------------------------------- 
     35usage: 
     36    ${0##*/} [options] {console,delete,init,restart,run,status} 
     37    ${0##*/} [options] run [command] 
    3238 
    33   exit 1 
     39======================================================================= 
     40Options 
     41======================================================================= 
     42-A arch         - An architecture to run the port jail under (please 
     43   arch:cpu       note that the specified architecture must be binary 
     44                  compatible, e.g. i386 on amd64, powerpc on powerpc64, 
     45                  etc). 
     46 
     47                  When using architectures like powerpc where the CPU 
     48                  isn't the same as the architecture, you should specify 
     49                  the pair like so: 
     50 
     51                        powerpc:powerpc64 
     52 
     53                  Defaults to: '$DEFAULT_PJ_TARGET_PAIR'. When 
     54-D directory    - A directory to use for installing the port jail. 
     55                  Defaults to: '$DEFAULT_PJ_DESTDIR'. 
     56-h hostname     - Hostname to use for the jail. Defaults to 
     57                  '$DEFAULT_PJ_HOSTNAME'. 
     58-m directory    - A directory to use for building the port jail. 
     59                  Defaults to: '$DEFAULT_PJ_SRCDIR'. 
     60-M [b|d]        - Portjail init method: b for build, d for download. 
     61                  Prompts interactively by default. 
     62 
     63======================================================================= 
     64Command         - Description 
     65======================================================================= 
     66 
     67Unprivileged (non-root) commands: 
     68======================================================================= 
     69 
     70console         - Starts a shell session within the jail 
     71delete          - Deletes the jail 
     72init            - Setup the jail environment 
     73run [command]   - Runs the command specified by [command] in the jail 
     74status          - Show the portjail's status 
     75 
     76Privileged (root) commands: 
     77======================================================================= 
     78 
     79restart         - Restarts the jail 
     80start           - Starts the jail 
     81stop            - Stops the jail 
     82 
     83EOF 
     84        exit 1 
    3485} 
    3586 
    3687### Print an error on STDERR and bail out 
     
    61112### Check if the running user is root 
    62113checkroot() { 
    63114  if [ ${ID} -ne 0 ]; then 
    64     [ -n "$1" ] && echo checkroot "Error: You must be root to $1 the ports jail." 
     115    [ -n "$1" ] && echo checkroot "Error: You must be root to $1 the portsjail." 
    65116    exit 1 
    66117  fi 
    67118  return 0 
     
    69120 
    70121### Check if the jail is installed or not 
    71122checkjailinstalled() { 
    72   if [ -d "${PJDIR}/etc" ]; then 
     123  if [ -d "${PJ_DESTDIR}/etc" ]; then 
    73124    return 0 
    74125  else 
    75126    return 1 
     
    78129 
    79130### Check if the jail is currently running 
    80131checkjailrunning() { 
    81   jailrunning="$(jls | awk -v PJDIR=${PJDIR} '$4 == PJDIR {print $4}')" 
     132  jailrunning="$(jls | awk -v PJ_DESTDIR=${PJ_DESTDIR} '$4 == PJ_DESTDIR {print $4}')" 
    82133  if [ -n "${jailrunning}" ]; then 
    83134    return 0 
    84135  else 
     
    110161    [ -z "${MIRRORURL}" ] && MIRRORURL="ftp://ftp.pcbsd.org/pub/mirror" 
    111162  fi 
    112163 
    113   cd ${PJDIR} 
     164  cd ${PJ_DESTDIR} 
    114165 
    115166  echo "Fetching PC-BSD environment. This may take a while..." 
    116   echo "Downloading ${MIRRORURL}/${SYSVER}/${ARCH}/netinstall/${FBSD_TARBALL} ..." 
    117   fetch -a ${MIRRORURL}/${SYSVER}/${ARCH}/netinstall/${FBSD_TARBALL} \ 
    118            ${MIRRORURL}/${SYSVER}/${ARCH}/netinstall/${FBSD_TARBALL_CKSUM} 
     167  echo "Downloading ${MIRRORURL}/${SYSVER}/${PJ_TARGET_PAIR%:*}/netinstall/${FBSD_TARBALL} ..." 
     168  fetch -a ${MIRRORURL}/${SYSVER}/${PJ_TARGET_PAIR%:*}/netinstall/${FBSD_TARBALL} \ 
     169           ${MIRRORURL}/${SYSVER}/${PJ_TARGET_PAIR%:*}/netinstall/${FBSD_TARBALL_CKSUM} 
    119170  [ $? -ne 0 ] && printerror "Error while downloading the portsjail." 
    120171 
    121172  [ "$(md5 -q ${FBSD_TARBALL})" != "$(cat ${FBSD_TARBALL_CKSUM})" ] && 
    122173    printerror "Error in download data, checksum mismatch. Please try again later." 
    123174 
    124175  echo "Extracting FreeBSD environment... This may take a while..." 
    125   tar xvpf ${FBSD_TARBALL} -C ${PJDIR} 
     176  tar xvpf ${FBSD_TARBALL} -C ${PJ_DESTDIR} 
    126177  # Cleanup 
    127178  rm ${FBSD_TARBALL} ${FBSD_TARBALL_CKSUM} 
    128179} 
     
    136187    echo "9.0-RELEASE" 
    137188    read ANSWER 
    138189    if [ "${ANSWER}" = "c" ]; then 
    139       PJMETHOD="b" 
     190      PJ_METHOD="b" 
    140191      initjail 
    141192    else 
    142193      SYSVER="${ANSWER}" 
     
    148199    read PROTOCOL 
    149200    [ -z "${PJAILMIRROR}" ] && PJAILMIRROR="ftp.freebsd.org" 
    150201    [ -z "${PROTOCOL}" ] && PROTOCOL="ftp" 
    151     cd ${PJDIR} 
     202    cd ${PJ_DESTDIR} 
    152203    echo "Fetching FreeBSD environment. This may take a while..." 
    153     echo "Downloading ${PROTOCOL}://${PJAILMIRROR}/pub/FreeBSD/releases/${ARCH}/${ARCH}/${SYSVER}/${FBSD_TARBALL}" 
    154     fetch -a ${PROTOCOL}://${PJAILMIRROR}/pub/FreeBSD/releases/${ARCH}/${ARCH}/${SYSVER}/${FBSD_TARBALL} 
     204    echo "Downloading ${PROTOCOL}://${PJAILMIRROR}/pub/FreeBSD/releases/${PJ_TARGET_PAIR%:*}/${PJ_TARGET_PAIR##*:}/${SYSVER}/${FBSD_TARBALL}" 
     205    fetch -a ${PROTOCOL}://${PJAILMIRROR}/pub/FreeBSD/releases/${PJ_TARGET_PAIR%:*}/${PJ_TARGET_PAIR##*:}/${SYSVER}/${FBSD_TARBALL} 
    155206    [ $? -ne 0 ] && printerror "Error: Download failed!" 
    156     fetch -a ${PROTOCOL}://${PJAILMIRROR}/pub/FreeBSD/releases/${ARCH}/${ARCH}/${SYSVER}/${FBSD_TARBALL_CKSUM} 
     207    fetch -a ${PROTOCOL}://${PJAILMIRROR}/pub/FreeBSD/releases/${PJ_TARGET_PAIR%:*}/${PJ_TARGET_PAIR##*:}/${SYSVER}/${FBSD_TARBALL_CKSUM} 
    157208    [ $? -ne 0 ] && printerror "Error: Download failed!" 
    158209 
    159210    [ "$(sha256 -q ${FBSD_TARBALL})" != \ 
     
    167218 
    168219### Mount all needed filesystems for the jail 
    169220mountjailfs() { 
    170   for nullfs_mount in ${NULLFS_MOUNTS}; do 
    171     mount_nullfs ${nullfs_mount} ${PJDIR}${nullfs_mount} 
     221  for nullfs_mount_pair in ${NULLFS_MOUNTS}; do 
     222    # external-path mountpoint 
     223    set -- $(echo "$nullfs_mount_pair" | sed -e "s/$DELIM/ /") 
     224    if [ $# -ne 2 ]; then 
     225        echo >&2 "The nullfs mountpoint pair cannot contain spaces in it" 
     226        exit 1 
     227    fi 
     228    mount_nullfs ${1} ${PJ_DESTDIR}${2} 
    172229  done 
    173230 
    174   mount -t devfs devfs ${PJDIR}/dev 
    175   mount -t procfs procfs ${PJDIR}/proc 
     231  [ -c $PJ_DESTDIR/dev/null ] || mount -t devfs devfs ${PJ_DESTDIR}/dev 
     232  mount -t procfs procfs ${PJ_DESTDIR}/proc 
    176233 
    177234  # Add support for linprocfs for ports that need linprocfs to build/run 
    178   if [ -d "${PJDIR}/compat/linux/proc" ]; then 
    179     mount -t linprocfs linprocfs ${PJDIR}/compat/linux/proc 
     235  if [ -d "${PJ_DESTDIR}/compat/linux/proc" ]; then 
     236    mount -t linprocfs linprocfs ${PJ_DESTDIR}/compat/linux/proc 
    180237  else 
    181238    echo "/compat/linux/proc does not exist. Adding linprocfs support." 
    182     mkdir -p ${PJDIR}/compat/linux/proc 
    183     mount -t linprocfs linprocfs ${PJDIR}/compat/linux/proc 
     239    mkdir -p ${PJ_DESTDIR}/compat/linux/proc 
     240    mount -t linprocfs linprocfs ${PJ_DESTDIR}/compat/linux/proc 
    184241  fi 
    185242} 
    186243 
    187244### Umount all the jail's filesystems 
    188245umountjailfs() { 
    189246  # Umount all filesystems that are mounted into the portsjail 
    190   for mountpoint in $(mount | grep ${PJDIR} | cut -d" " -f3); do 
     247  for mountpoint in $(mount | grep ${PJ_DESTDIR} | cut -d" " -f3); do 
    191248    umount -f ${mountpoint} || return 1 
    192249  done 
    193250} 
    194251 
    195252### Start the jail 
    196253startjail() { 
    197   [ ! -d "${PJDIR}/etc" -a "$1" = "rc" ] && exit 0 
     254  [ ! -d "${PJ_DESTDIR}/etc" -a "$1" = "rc" ] && exit 0 
    198255 
    199256  echo "Starting the portjail..." 
    200257  # Create some hard-links for the portjail 
    201258  ETCFILES="resolv.conf passwd master.passwd spwd.db pwd.db group localtime" 
    202259  for file in ${ETCFILES}; do 
    203     rm ${PJDIR}/etc/${file} >/dev/null 2>&1 
    204     cp /etc/${file} ${PJDIR}/etc/${file} 
     260    rm ${PJ_DESTDIR}/etc/${file} >/dev/null 2>&1 
     261    cp /etc/${file} ${PJ_DESTDIR}/etc/${file} 
    205262  done 
    206263 
    207264  # Figure out our default interfaces, otherwise try all. 
     
    220277  fi 
    221278 
    222279  # Make sure we remove our cleartmp rc.d script, causes issues 
    223   [ -e "${PJDIR}/etc/rc.d/cleartmp" ] && rm ${PJDIR}/etc/rc.d/cleartmp 
     280  [ -e "${PJ_DESTDIR}/etc/rc.d/cleartmp" ] && rm ${PJ_DESTDIR}/etc/rc.d/cleartmp 
    224281 
    225282  # Add the hostname to the portjails /etc/hosts file, to prevent sendmail warnings 
    226   if [ -e ${PJDIR} ]; then 
    227     sed -i -e '/^127.0.0.1.*/d' ${PJDIR}/etc/hosts 
    228     sed -i -e '/^::1.*/d' ${PJDIR}/etc/hosts 
     283  if [ -e ${PJ_DESTDIR} ]; then 
     284    sed -i -e '/^127.0.0.1.*/d' ${PJ_DESTDIR}/etc/hosts 
     285    sed -i -e '/^::1.*/d' ${PJ_DESTDIR}/etc/hosts 
    229286  fi 
    230   echo "::1             localhost localhost.my.domain ${PJHOST}" >>${PJDIR}/etc/hosts 
    231   echo "127.0.0.1       localhost localhost.my.domain ${PJHOST}" >>${PJDIR}/etc/hosts 
     287  echo "::1             localhost localhost.my.domain ${PJ_HOSTNAME}" >>${PJ_DESTDIR}/etc/hosts 
     288  echo "127.0.0.1       localhost localhost.my.domain ${PJ_HOSTNAME}" >>${PJ_DESTDIR}/etc/hosts 
    232289 
    233290  # Make sure the /etc/rc.conf HOSTNAME values match 
    234   : > ${PJDIR}/etc/rc.conf 
    235   cat > ${PJDIR}/etc/rc.conf <<-EOF 
    236 hostname="$PJHOST" 
     291  cat > ${PJ_DESTDIR}/etc/rc.conf <<-EOF 
     292hostname="$PJ_HOSTNAME" 
    237293cron_enable="NO" 
    238294syslogd_enable="NO" 
    239295sendmail_enable="NO" 
     
    245301  # Mount all needed filesystems into the portjail path 
    246302  mountjailfs 
    247303  # Actually create and start the jail 
    248   jail -c name=portjail path=${PJDIR} host.hostname=${PJHOST} ${PJIP} persist 
     304  jail -c name=portjail path=${PJ_DESTDIR} host.hostname=${PJ_HOSTNAME} ${PJIP} persist 
    249305  jexec portjail /bin/sh /etc/rc 
    250306} 
    251307 
    252308### Stop the jail 
    253309stopjail() { 
    254   [ ! -d "${PJDIR}/etc" -a "$1" = "rc" ] && exit 0 
     310  [ ! -d "${PJ_DESTDIR}/etc" -a "$1" = "rc" ] && exit 0 
    255311 
    256312  echo "Stopping the portjail..." 
    257313  # Stop the Jail 
     
    286342  echo "Portjail is${installed} installed." 
    287343  echo "Portjail is${running} running." 
    288344  [ -z "${installed}" ] && 
    289     echo "There are $(PKG_DBDIR=${PJDIR}/var/db/pkg pkg_info 2>/dev/null | grep -c .) packages installed." 
     345    echo "There are $(PKG_DBDIR=${PJ_DESTDIR}/var/db/pkg pkg_info 2>/dev/null | grep -c .) packages installed." 
    290346  exit 0 
    291347} 
    292348 
    293349### Build / download, install and setup the jail 
    294350initjail() { 
    295351  # Setup a new portjail 
    296   if [ -d ${PJDIR}/etc ]; then 
     352  if [ -d ${PJ_DESTDIR}/etc ]; then 
    297353     echo "The portsjail is already initialized. Re-initializing it will delete its" 
    298      echo "contents. Do you want to continue? [y|n]" 
     354     echo "contents. Do you want to continue? [y/N]" 
    299355     read DOIT 
    300356     if [ "${DOIT}" = "y" ]; then 
    301357       # Unmount first, so we don't delete /home and stuff :) 
    302358       umountjailfs 
    303359       if [ $? -eq 0 ]; then 
    304          rm -r ${PJDIR} 
     360         rm -r ${PJ_DESTDIR} 
    305361       else 
    306362         printerror "Error: An error occured while unmounting the portjail filesystems. \ 
    307363                   Aborting re-initialization. Please check if there are any files opened \ 
     
    312368     fi 
    313369   fi 
    314370 
    315   while [ "${PJMETHOD}" != "d" -a "${PJMETHOD}" != "b" ]; do 
     371  while [ "${PJ_METHOD}" != "d" -a "${PJ_METHOD}" != "b" ]; do 
    316372    printf "Would you like to download a pre-compiled base jail from a FreeBSD mirror via\n" 
    317373    printf "FTP/HTTP or would you like to build one from source? Enter d for download or\n" 
    318374    printf "b to build from source: [d/b] " 
    319     read PJMETHOD 
     375    read PJ_METHOD 
    320376    printf "\n" 
    321377  done 
    322378 
    323379  # Create the jail dir 
    324   [ ! -d "${PJDIR}" ] && mkdir -p "${PJDIR}" 
     380  [ -d "${PJ_DESTDIR}" ] || mkdir -p "${PJ_DESTDIR}" 
    325381 
    326   case "${PJMETHOD}" in 
     382  case "${PJ_METHOD}" in 
    327383    b) 
    328     if [ ! -e "/usr/src/COPYRIGHT" ] 
     384    if [ ! -e "$PJ_SRCDIR/COPYRIGHT" ] 
    329385    then 
    330       echo "Error: You will need a copy of FreeBSD sources in /usr/src to build the portjail." 
     386      echo "Error: You will need a copy of FreeBSD sources in $PJ_SRCDIR to build the portjail." 
    331387      echo "You may checkout sources via the System Manager, CVS, SVN or other method." 
    332388      exit 1 
    333389    fi 
     
    335391    # Preparing to build the jail 
    336392    echo "Starting build of portsjail, this may take a while..." 
    337393    sleep 5 
    338     cd /usr/src 
    339     make buildworld installworld distribution DESTDIR=${PJDIR} 
     394    cd $PJ_SRCDIR 
     395    for make_target in buildworld installworld distribution; do 
     396      make $make_target \ 
     397        $MAKEFLAGS \ 
     398        DESTDIR=${PJ_DESTDIR} \ 
     399        TARGET=${PJ_TARGET_PAIR%:*} \ 
     400        TARGET_ARCH=${PJ_TARGET_PAIR##*:} 
     401    done 
    340402    [ $? -ne 0 ] && 
    341403      printerror "Error: The portjail build failed! Please check your sources and try again." 
    342404    ;; 
     
    356418  esac 
    357419 
    358420  # Make the home link 
    359   mkdir -p ${PJDIR}/usr/home 
    360   ln -sf /usr/home ${PJDIR}/home 
     421  mkdir -p ${PJ_DESTDIR}/usr/home 
     422  ln -sf /usr/home ${PJ_DESTDIR}/home 
    361423 
    362424  echo "Portjail setup finished! Please run 'portjail start' to enable the jail." 
    363425} 
     
    368430    echo "Failed unmounting the jail!" 
    369431    exit 1 
    370432  fi 
    371   echo "Deleting portjail: ${PJDIR}" 
    372   chflags -R noschg ${PJDIR} 
    373   rm -rf ${PJDIR} 
     433  echo "Deleting portjail: ${PJ_DESTDIR}" 
     434  chflags -R noschg ${PJ_DESTDIR} 
     435  rm -rf ${PJ_DESTDIR} 
    374436  echo "Portjail deleted." 
    375437} 
    376438 
    377439########################## MAIN ############################### 
    378440 
    379 # if we are called without a flag, warn the user and exit 
     441while getopts 'A:D:h:m:M:' optch; do 
     442        case "$optch" in 
     443        A) 
     444                [ -n "${OPTARG:-}" ] || display_help 
     445                PJ_TARGET_PAIR=$OPTARG 
     446                ;; 
     447        D) 
     448                PJ_DESTDIR=$OPTARG 
     449                ;; 
     450        h) 
     451                [ -n "${OPTARG:-}" ] || display_help 
     452                PJ_HOSTNAME=$OPTARG 
     453                ;; 
     454        m) 
     455                PJ_SRCDIR=$OPTARG 
     456                ;; 
     457        M) 
     458                PJ_METHOD=$OPTARG 
     459                ;; 
     460        *) 
     461                display_help 
     462                ;; 
     463        esac 
     464done 
     465 
     466shift $(( $OPTIND - 1 )) 
     467 
     468# Set defaults. 
     469: ${PJ_DESTDIR=$DEFAULT_PJ_DESTDIR} 
     470: ${PJ_HOSTNAME=$DEFAULT_PJ_HOSTNAME} 
     471: ${PJ_SRCDIR=$DEFAULT_PJ_SRCDIR} 
     472: ${PJ_TARGET_PAIR=$DEFAULT_PJ_TARGET_PAIR} 
     473 
     474# if we are called without a command, warn the user and exit 
    380475[ -z "$1" ] && display_help 
    381476 
    382477case "$1" in