home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 4.0 PR1 / NeXT_NEXTSTEP_4.0_PR1_(beta).rar / Openstep4-Pr1User.iso / NextAdmin / NetInstallHelper.app / setup_netinstall_server < prev   
Text File  |  1995-01-31  |  9KB  |  333 lines

  1. #!/bin/csh -f
  2. #
  3. # Set up a NetInstall server
  4. # Copyright (C) 1994 by NeXT Computer, Inc.  All rights reserved.
  5.  
  6. # usage: server [-cdrom dir] [-image dir]
  7. # options:
  8. #         -cdrom dir   Name of NEXTSTEP release CDROM
  9. #         -image dir   Directory for NEXTSTEP image
  10.  
  11. # A Network Install server has an NFS-exported directory that 
  12. # contains an image of the NEXTSTEP release CD-ROM.  The server
  13. # is also recorded in the NetInfo root domain, in the directory
  14. # /locations/install_servers/<servername>.  A soft, read-only 
  15. # mount is placed in the root domain.
  16.  
  17. # Configuration variables.  Change these if you with to use this
  18. # script with a custom CD-ROM or a different name for the CD-ROM 
  19. # root image directory.  You can over-ride both of these variables
  20. # with the -cdrom and -image command-line options.
  21.  
  22. set CDROM=/NEXTSTEP_3.3risc
  23. set ROOT_IMAGE=/NEXTSTEP_INSTALL
  24.  
  25. # Trap SIGINT. This keeps the tty from getting messed up if
  26. # someone types ^C while the shell is reading a password.
  27.  
  28. onintr -
  29.  
  30. # All command names are stored in variables 
  31.  
  32. set AWK=/bin/awk
  33. set BASENAME=/usr/bin/basename
  34. set CAT=/bin/cat
  35. set DF=/bin/df
  36. set ECHO=/bin/echo
  37. set ERROR=$ECHO
  38. set EXPORTFS=/usr/etc/exportfs
  39. set GREP=/bin/grep
  40. set HOSTNAME=/bin/hostname
  41. set IFCONFIG=/usr/etc/ifconfig
  42. set LS=/bin/ls
  43. set MKDIRS=/bin/mkdirs
  44. set MOUNTD=/usr/etc/rpc.mountd
  45. set NFSD=/usr/etc/nfsd
  46. set NIUTIL=/usr/bin/niutil
  47. set PS=/bin/ps
  48. set RM=/bin/rm
  49. set SLEEP=/usr/bin/sleep
  50. set STTY=/bin/stty
  51. set TAIL=/usr/ucb/tail
  52. set WC=/usr/ucb/wc
  53.  
  54. set TRUE=1
  55. set FALSE=0
  56.  
  57. set CONFIG_DOMAIN=/
  58. set RELEASE_NAME=`$BASENAME $CDROM ""`
  59. set HOST_NAME=`$HOSTNAME`
  60. set PROGRAM_NAME=`$BASENAME ${0} .csh`
  61. $ECHO -n "usage: ${PROGRAM_NAME} " > /tmp/_usage_$$
  62. $ECHO '[-cdrom dir] [-image dir] [-config_domain name]' >> /tmp/_usage_$$
  63. $ECHO 'options:' >> /tmp/_usage_$$
  64. $ECHO '    -cdrom dir        Name of NEXTSTEP release CDROM' >> /tmp/_usage_$$
  65. $ECHO '    -image dir        Directory for NEXTSTEP image' >> /tmp/_usage_$$
  66. $ECHO '    -config_domain    Where to record servers (default is "/")' >> /tmp/_usage_$$
  67.  
  68. # This program must be run by root
  69.  
  70. set WHO=`$PS -l $$ | $TAIL -1 | $AWK '{print $2}'`
  71. if (${WHO}x != "0x") then
  72.     $ERROR 'You must run this program as "root".'
  73.     exit 1
  74. endif
  75.  
  76. # Parse command-line options
  77.  
  78. while ($#argv > 0)
  79.     switch (${1})
  80.     case "-help": # Print help message
  81.         $CAT /tmp/_usage_$$
  82.         $RM /tmp/_usage_$$
  83.         exit 1
  84.         breaksw
  85.     case "-cdrom": # CDROM name
  86.         if ($#argv < 2) then
  87.             $ERROR "${PROGRAM_NAME}: missing argument to -cdrom"
  88.             $CAT /tmp/_usage_$$
  89.             $RM /tmp/_usage_$$
  90.             exit 1
  91.         endif
  92.         set CDROM="${2}"
  93.         shift
  94.         breaksw
  95.     case "-image": # ROOT_IMAGE name
  96.         if ($#argv < 2) then
  97.             $ERROR "${PROGRAM_NAME}: missing argument to -image"
  98.             $CAT /tmp/_usage_$$
  99.             $RM /tmp/_usage_$$
  100.             exit 1
  101.         endif
  102.         set ROOT_IMAGE="${2}"
  103.         shift
  104.         breaksw
  105.     case "-config_domain": # Where servers are recorded
  106.         if ($#argv < 2) then
  107.             $ERROR "${PROGRAM_NAME}: missing argument to -config_domain."
  108.             $CAT /tmp/_usage_$$
  109.             $RM /tmp/_usage_$$
  110.             exit 1
  111.         endif
  112.         set CONFIG_DOMAIN="${2}"
  113.         shift
  114.         breaksw
  115.     default: # Unknown flag
  116.         $ERROR "${PROGRAM_NAME}: unknown option ${1}"
  117.         $CAT /tmp/_usage_$$
  118.         $RM /tmp/_usage_$$
  119.         exit 1
  120.         breaksw
  121.     endsw
  122.     shift
  123. end
  124.  
  125. $RM /tmp/_usage_$$
  126.  
  127. # Get my address and netmask
  128. # Try en0 first, then tr0
  129.  
  130. set INTERFACE=en0
  131. $IFCONFIG $INTERFACE > /dev/null
  132. if ($status != 0) then
  133.     # No en0 interface, try tr0
  134.     INTERFACE=tr0
  135.     $IFCONFIG $INTERFACE > /dev/null
  136.     if ($status != 0) then
  137.         # No interfaces
  138.         $ERROR "${PROGRAM_NAME}: Can't find a working network interface."
  139.         exit 1
  140.     endif
  141. endif
  142.  
  143. set IPADDRESS=`$IFCONFIG $INTERFACE | $TAIL -1 | $AWK '{print $2}'`
  144. set NETMASK=`$IFCONFIG $INTERFACE | $TAIL -1 | $AWK '{print $4}'`
  145. set NM=`$ECHO $NETMASK | tr 'a-f' 'A-F' | $AWK '{printf("16i %s p %s p %s p %s pq\n",substr($1,1,2), substr($1,3,2) ,substr($1,5,2), substr($1,7,2))}' | dc`
  146. set NETMASK=`$ECHO $NM[1].$NM[2].$NM[3].$NM[4]`
  147.  
  148. # Find out if CDROM is present
  149.  
  150. while (! -d $CDROM)
  151.     $ECHO -n 'Please insert the CDROM "'
  152.     $ECHO -n $CDROM
  153.     $ECHO '" in the drive.'
  154.     $ECHO -n 'Press Enter to proceed or type "quit" to exit: '
  155.     set USER_INPUT=$<
  156.  
  157.     if (${USER_INPUT}x == "quitx") then
  158.         exit 0
  159.     endif
  160.  
  161.     if (! -d $CDROM) then
  162.         $SLEEP 10
  163.     endif
  164.     $ECHO ""
  165. end
  166.  
  167. if (! -d $CDROM) then
  168.     $ERROR -n "${PROGRAM_NAME}: NEXTSTEP release CDROM "
  169.     $ERROR -n '"'
  170.     $ERROR -n $CDROM
  171.     $ERROR '" is not available.'
  172.     exit 1
  173. endif
  174.  
  175. set RELEASE_SIZE=`$DF $CDROM | $TAIL -1 | $AWK '{print $3}'`
  176.  
  177. # Determine if release image directory exists
  178.  
  179. if (! -w $ROOT_IMAGE) then
  180.     if (! -d $ROOT_IMAGE) then
  181.         # Directory doesn't exist: create it
  182.         $MKDIRS $ROOT_IMAGE
  183.         if (! -w $ROOT_IMAGE) then
  184.             $ERROR -n "${PROGRAM_NAME}: Can't create directory "
  185.             $ERROR -n '"'
  186.             $ERROR -n $ROOT_IMAGE
  187.             $ERROR '".'
  188.             exit 1
  189.         endif
  190.     else
  191.         $ERROR -n "${PROGRAM_NAME}: No write permission for directory "
  192.         $ERROR -n '"'
  193.         $ERROR -n $ROOT_IMAGE
  194.         $ERROR '".'
  195.         exit 1
  196.     endif
  197. endif
  198.  
  199. # Check if release image directory is a local directory
  200.  
  201. set WHERE=`$DF -i $ROOT_IMAGE | $TAIL -1 | $AWK '{print $3}'`
  202. if (${WHERE}x == "-1x") then
  203.     $ERROR -n "${PROGRAM_NAME}: Directory "
  204.     $ERROR -n '"'
  205.     $ERROR -n $ROOT_IMAGE
  206.     $ERROR '" is not on a local disk.'
  207.     exit 1
  208. endif
  209.  
  210. # Check if release image directory is empty
  211.  
  212. set FILES=`$LS -1a $ROOT_IMAGE | $GREP -v "lost+found" | $WC -l`
  213. if (${FILES}x != "2x") then
  214.     $ERROR -n "${PROGRAM_NAME}: Directory "
  215.     $ERROR -n '"'
  216.     $ERROR -n $ROOT_IMAGE
  217.     $ERROR '" is not empty.'
  218.     exit 1
  219. endif
  220.  
  221. # Check if release image directory has enough space
  222.  
  223. set AVAIL_SIZE=`$DF $ROOT_IMAGE | $TAIL -1 | $AWK '{print $4}'`
  224. if ($AVAIL_SIZE < $RELEASE_SIZE) then
  225.     $ERROR -n "${PROGRAM_NAME}: Insufficent space available in directory "
  226.     $ERROR -n '"'
  227.     $ERROR $ROOT_IMAGE
  228.     $ERROR '".'
  229.     $ERROR "${PROGRAM_NAME}: $AVAIL_SIZE Kbytes are available. $RELEASE_SIZE Kbytes are required."
  230.     exit 1
  231. endif
  232.  
  233. # Acquire root password for "config" NetInfo domain
  234.  
  235. set NIUTIL_33=$CDROM$NIUTIL
  236.  
  237. set GET_PW=$TRUE
  238. $ECHO -n "Please enter the root password for the NetInfo domain: "
  239. $ECHO -n '"'
  240. $ECHO -n $CONFIG_DOMAIN
  241. $ECHO '"'
  242.  
  243. while ($GET_PW == $TRUE)
  244.     $STTY -echo
  245.     $ECHO -n "Password: "
  246.     set ROOT_PASSWORD=$<
  247.     $STTY echo
  248.     $ECHO ""
  249.     if (${ROOT_PASSWORD}x == "x") then
  250.         set ROOT_PASSWORD='""'
  251.     endif
  252.  
  253.     # Check password 
  254.  
  255.     $NIUTIL_33 -createprop -P $ROOT_PASSWORD $CONFIG_DOMAIN / _test_$$
  256.     set STATUS=`$NIUTIL_33 -read $CONFIG_DOMAIN / | $GREP _test_$$`
  257.     if (${STATUS}x == "x") then
  258.         $ECHO -n "Password incorrect.  Do you wish to retry? [yn] "
  259.         set USER_INPUT=$<
  260.         if (${USER_INPUT}x == "nx" || ${USER_INPUT}x == "Nx") then
  261.             exit 1
  262.         endif
  263.         $SLEEP 2
  264.     else
  265.         $NIUTIL_33 -destroyprop -P $ROOT_PASSWORD $CONFIG_DOMAIN / _test_$$
  266.         set GET_PW=$FALSE
  267.     endif
  268. end
  269.  
  270. # Create /locations/install_servers entry
  271.  
  272. set NIDIR=/locations/install_servers/$HOST_NAME
  273. set RELEASE_SIZE=`$DF $CDROM | $TAIL -1 | $AWK '{print $4}'`
  274.  
  275. $NIUTIL_33 -create -P $ROOT_PASSWORD $CONFIG_DOMAIN $NIDIR
  276. $NIUTIL_33 -createprop -P $ROOT_PASSWORD $CONFIG_DOMAIN $NIDIR ip_address $IPADDRESS
  277. $NIUTIL_33 -createprop -P $ROOT_PASSWORD $CONFIG_DOMAIN $NIDIR netmask $NETMASK
  278. $NIUTIL_33 -createprop -P $ROOT_PASSWORD $CONFIG_DOMAIN $NIDIR install_root $ROOT_IMAGE
  279. $NIUTIL_33 -createprop -P $ROOT_PASSWORD $CONFIG_DOMAIN $NIDIR release $RELEASE_NAME
  280. $NIUTIL_33 -createprop -P $ROOT_PASSWORD $CONFIG_DOMAIN $NIDIR size $RELEASE_SIZE
  281. $NIUTIL_33 -createprop -P $ROOT_PASSWORD $CONFIG_DOMAIN $NIDIR clients
  282. $NIUTIL_33 -createprop -P $ROOT_PASSWORD $CONFIG_DOMAIN $NIDIR client_domains
  283.  
  284. # Create root domain /mounts entry
  285.  
  286. set NIDIR=/mounts/${HOST_NAME}-temp-$$
  287.  
  288. $NIUTIL_33 -create -P $ROOT_PASSWORD $CONFIG_DOMAIN $NIDIR
  289. $NIUTIL_33 -createprop -P $ROOT_PASSWORD $CONFIG_DOMAIN $NIDIR dir /Net
  290. $NIUTIL_33 -createprop -P $ROOT_PASSWORD $CONFIG_DOMAIN $NIDIR opts ro bg soft net
  291. $NIUTIL_33 -createprop -P $ROOT_PASSWORD $CONFIG_DOMAIN $NIDIR name ${HOST_NAME}:$ROOT_IMAGE
  292.  
  293.  
  294. # Copy CDROM to release image
  295.  
  296. $ECHO Copying $CDROM to $ROOT_IMAGE
  297.  
  298. $CDROM/usr/etc/ditto $CDROM $ROOT_IMAGE
  299.  
  300. # Export the root image
  301.  
  302. $EXPORTFS -i $ROOT_IMAGE >& /tmp/_test_$$
  303. $GREP "already exported" /tmp/_test_$$ > /dev/null
  304. if ($status != 0) then
  305.     $NIUTIL_33 -create . /exports/netinstall.$$
  306.     $NIUTIL_33 -createprop . /exports/netinstall.$$ opts ro anon=0
  307.     $NIUTIL_33 -createprop . /exports/netinstall.$$ name $ROOT_IMAGE
  308. endif
  309.  
  310. $RM /tmp/_test_$$
  311.  
  312. $EXPORTFS -au >& /dev/null
  313. $EXPORTFS -a >& /dev/null
  314.  
  315. # Start nfsd if it's not already running
  316.  
  317. $PS -ax | $GREP '(nfsd)' | $GREP -v 'grep' > /dev/null
  318. if ($status != 0) then
  319.     $NFSD 6
  320. endif
  321.  
  322. # Start rpc.mountd if it's not already running
  323.  
  324. $PS -ax | $GREP '(rpc.mountd)' | $GREP -v 'grep' > /dev/null
  325. if ($status != 0) then
  326.     $MOUNTD
  327. endif
  328.  
  329. # All done
  330.  
  331. $ECHO "NetInstall server configuration complete."
  332. exit 0
  333.