source: src-sh/pc-thinclient/pc-thinclient @ d3a62a0

9.2-releasereleng/10.0releng/10.0.1
Last change on this file since d3a62a0 was d3a62a0, checked in by pkgdemon <pkgdemonteam@…>, 10 months ago

Update pc-thinclient

Clean up message at end of script.

  • Property mode set to 100644
File size: 15.3 KB
Line 
1#!/bin/sh
2#
3# Copyright 2012 Kris Moore / iXsystems
4# All rights reserved
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted providing that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25# POSSIBILITY OF SUCH DAMAGE.
26#
27#        Name: pc-thinclient
28# Description: Helper script to build / install the necessary bits to turn
29#              a PC-BSD system into a thin-client server
30#
31# Modified for external dhcp server support by Joe Maloney
32
33# Source our functions
34. /usr/local/share/pcbsd/scripts/functions.sh
35
36# Set some universal variables
37PROGDIR="/usr/local/share/pcbsd/pc-thinclient"
38DHCPPORT="isc-dhcp41-server"
39PXEWORLD="/usr/home/thinclient"
40WORLDPORTS="x11/xorg graphics/xv"
41export WORLDPORTS
42SYSVER="`uname -r | cut -d '-' -f 1-2`"
43
44# Start by sourcing /etc/profile
45# This grabs any HTTP_ / FTP_ PROXY variables
46. /etc/profile
47
48# Start configuring the base system
49check_configsystem() {
50        echo "Setting up system for PXE booting..."
51
52        # Setup the new pxeboot user with a default password
53        cat /etc/passwd | grep pxeboot >/dev/null 2>/dev/null
54        if [ "$?" != "0" ] ; then
55                echo "thinclient" | pw useradd -n "pxeboot" -h 0 -s /bin/tcsh -d ${PXEWORLD}/mnt/xorg-config -c "pxeboot"
56                chown -R pxeboot:pxeboot ${PXEWORLD}/mnt/xorg-config
57        fi
58
59        # Copy over the default dhcpd.conf file
60        cp ${PROGDIR}/resources/dhcpd/dhcpd.conf /usr/local/etc/dhcpd.conf
61
62        # Ask for the NIC we want to run on
63        while
64        z=1
65        do
66                echo "What NIC do you wish DHCPD to listen on? (I.E. re0)"
67                echo -e "nic) \c"
68                read NIC
69               
70                ifconfig $NIC >/dev/null 2>/dev/null
71                if [ $? -ne 0 -o -z "$NIC" ] ; then
72                        echo "Invalid nic entered, please try again!"
73                        sleep 1
74                else
75                        break
76                fi
77        done
78
79        # Save the rc.conf glue
80        cat /etc/rc.conf | grep "# pc-thinclient" >/dev/null 2>/dev/null
81        if [ "$?" != "0" ] ; then
82                echo "# pc-thinclient configuration
83dhcpd_enable=\"YES\"
84dhcpd_ifaces=\"${NIC}\"
85portmap_enable=\"YES\"
86nfs_server_enable=\"YES\"
87inetd_enable=\"YES\"
88ifconfig_${NIC}=\"192.168.2.2\"" >> /etc/rc.conf
89        fi
90
91        # Add firewall exception
92        if [ -e "/etc/pf.conf" ] ; then
93          cat /etc/pf.conf | grep "pass in on ${NIC} all" >/dev/null 2>/dev/null
94          if [ "$?" != "0" ] ; then
95                # Setup the firewall exclusion for this NIC
96                echo "pass in on ${NIC} all" >> /etc/pf.conf
97          fi
98        fi
99
100        # Add some entries for /etc/exports
101        cat /etc/exports 2>/dev/null | grep "$PXEWORLD" >/dev/null 2>/dev/null
102        if [ "$?" != "0" ] ; then
103                echo "$PXEWORLD -maproot=nobody -ro -network 192.168.2 -mask 255.255.255" >>/etc/exports
104        fi
105
106        # Setup tftp
107        cat /etc/inetd.conf | grep "$PXEWORLD" >/dev/null 2>/dev/null
108        if [ "$?" != "0" ] ; then
109                echo "tftp   dgram   udp     wait    root    /usr/libexec/tftpd      tftpd -l -s ${PXEWORLD}" >> /etc/inetd.conf
110        fi
111
112        # Setup rcpbind entries
113        cat /etc/hosts.allow 2>/dev/null | grep "192.168.2.0" >/dev/null 2>/dev/null
114        if [ "$?" != "0" ] ; then
115                sed  -i '' 's|rpcbind : ALL : deny|rpcbind : 192.168.2.0/255.255.255.0 : allow\
116portmap : 192.168.2.0/255.255.255.0 : allow\
117rpcbind : ALL : deny|' /etc/hosts.allow
118        fi
119
120        # Add a bulk of IPs to /etc/hosts this fixes bugs with RPC timeouts
121        # when mounting NFS
122        grep -q 'thinclient100' /etc/hosts
123        if [ $? -ne 0 ] ; then
124                i="100"
125                while
126                z="1"
127                do
128                        if [ "${i}" = "200" ]; then break; fi
129                        echo "192.168.2.${i}  thinclient${i}" >>/etc/hosts
130                        i="`expr ${i} + 1`"
131                done
132        fi
133
134        # Make sure the NIC is set to the right IP before bringing up dhcpd
135        ifconfig $NIC 192.168.2.2
136
137        # Start the services
138        cmds="/etc/rc.d/nfsd /etc/rc.d/inetd /usr/local/etc/rc.d/isc-dhcpd"
139        for _sC in $cmds
140        do
141                echo -e "Starting ${_sC}...\c"
142                ${_sC} restart >/dev/null 2>/dev/null
143                if [ "$?" != "0" ] ; then
144                        echo -e "FAILED! Please run try running it manually."
145                else
146                        echo -e "OK"
147                fi
148        done
149}
150
151# Start configuring the base system without DHCP
152check_configsystem_ignore_dhcp() {
153        echo "Setting up system for PXE booting..."
154
155        # Setup the new pxeboot user with a default password
156        cat /etc/passwd | grep pxeboot >/dev/null 2>/dev/null
157        if [ "$?" != "0" ] ; then
158                echo "thinclient" | pw useradd -n "pxeboot" -h 0 -s /bin/tcsh -d ${PXEWORLD}/mnt/xorg-config -c "pxeboot"
159                chown -R pxeboot:pxeboot ${PXEWORLD}/mnt/xorg-config
160        fi
161
162        # Ask for the NIC we want to run on
163        while
164        z=1
165        do
166                echo "What NIC do you wish to listen on? (I.E. re0)"
167                echo -e "nic) \c"
168                read NIC
169
170                ifconfig $NIC >/dev/null 2>/dev/null
171                if [ $? -ne 0 -o -z "$NIC" ] ; then
172                        echo "Invalid nic entered, please try again!"
173                        sleep 1
174                else
175                        break
176                fi
177        done
178       
179        # Ask for the IP Address to be used on the NIC for PXE booting
180        while
181        z=1
182        do
183                echo "What IP address will pc-thinclient be listening on? (I.E. 192.168.2.2)"
184                echo -e "ipaddr) \c"
185                read ipaddr
186        break
187        done
188                # Ask for the network id of the local subnet
189        while
190        z=1
191        do
192                echo "What is the network id for your local subnet? (I.E. 192.168.2.0)"
193                echo -e "netid) \c"
194                read netid
195        break
196        done
197       
198                # Ask for the network mask of the local subnet
199        while
200        z=1
201        do
202                echo "What is the network mask for your local subnet? (I.E. 255.255.255.0)"
203                echo -e "netmaskid) \c"
204                read netmaskid
205        break
206        done
207
208
209portmap_enable=\"YES\"
210nfs_server_enable=\"YES\"
211inetd_enable=\"YES\"
212
213        # Add firewall exception
214        if [ -e "/etc/pf.conf" ] ; then
215          cat /etc/pf.conf | grep "pass in on ${NIC} all" >/dev/null 2>/dev/null
216          if [ "$?" != "0" ] ; then
217                # Setup the firewall exclusion for this NIC
218                echo "pass in on ${NIC} all" >> /etc/pf.conf
219          fi
220        fi
221
222        # Add some entries for /etc/exports based on manual user input
223        echo "-maproot=nobody -ro -network $netid -mask $netmaskid" >> /etc/exports
224
225        # Setup tftp
226        cat /etc/inetd.conf | grep "$PXEWORLD" >/dev/null 2>/dev/null
227        if [ "$?" != "0" ] ; then
228                echo "tftp   dgram   udp     wait    root    /usr/libexec/tftpd      tftpd -l -s ${PXEWORLD}" >> /etc/inetd.conf
229        fi
230       
231        # Setup rcpbind entries
232        sed -i -e "s|rpcbind : ALL : deny|rpcbind : $netid/$netmaskid : allow\\
233portmap : $netid/$netmaskid : allow\\
234rpcbind : ALL : deny|" /etc/hosts.allow
235
236        # Add a bulk of IPs to /etc/hosts this fixes bugs with RPC timeouts
237        # when mounting NFS
238        grep -q 'thinclient100' /etc/hosts
239        if [ $? -ne 0 ] ; then
240                i="100"
241                while
242                z="1"
243                do
244                        if [ "${i}" = "200" ]; then break; fi
245                        echo "192.168.2.${i}  thinclient${i}" >>/etc/hosts
246                        i="`expr ${i} + 1`"
247                done
248        fi
249
250        # Start the services
251        cmds="/etc/rc.d/nfsd /etc/rc.d/inetd"
252        for _sC in $cmds
253        do
254                echo -e "Starting ${_sC}...\c"
255                ${_sC} restart >/dev/null 2>/dev/null
256                if [ "$?" != "0" ] ; then
257                        echo -e "FAILED! Please run try running it manually."
258                else
259                        echo -e "OK"
260                fi
261        done
262}
263
264
265# Check if we need to install custom config
266check_installconfig() {
267        if [ -e "${PXEWORLD}/etc/scripts/tcslogin.sh" ] ; then return ; fi
268
269        # Lets copy over the /etc/scripts directory
270        rm -rf ${PXEWORLD}/etc/scripts >/dev/null 2>/dev/null
271        cp -r ${PROGDIR}/resources/scripts ${PXEWORLD}/etc/scripts
272
273        # Remove a few rc.d things we dont need on clients
274        rm ${PXEWORLD}/etc/rc.d/cron
275        rm ${PXEWORLD}/etc/rc.d/sendmail
276
277        # Lets copy over all the /etc/ files we need
278        cp ${PROGDIR}/resources/etc/fstab ${PXEWORLD}/etc/
279        cp ${PROGDIR}/resources/etc/gettytab ${PXEWORLD}/etc/
280        cp ${PROGDIR}/resources/etc/hosts ${PXEWORLD}/etc/
281        cp ${PROGDIR}/resources/etc/motd ${PXEWORLD}/etc/
282        cp ${PROGDIR}/resources/etc/rc.conf ${PXEWORLD}/etc/
283        cp ${PROGDIR}/resources/etc/ttys ${PXEWORLD}/etc/
284
285        # Copy over rc.d / boot / root files
286        cp ${PROGDIR}/resources/boot/beastie.4th ${PXEWORLD}/boot/
287        cp ${PROGDIR}/resources/root/dot.login ${PXEWORLD}/root/.login
288
289        # Create a few directories used on client
290        mkdir -p ${PXEWORLD}/mnt/xorg-config
291
292        # Create the diskless configuration
293        mkdir -p ${PXEWORLD}/conf/base
294        mkdir -p ${PXEWORLD}/conf/base/etc
295        mkdir -p ${PXEWORLD}/conf/base/var
296        mkdir -p ${PXEWORLD}/conf/base/root
297        echo "10m" > ${PXEWORLD}/conf/base/etc/md_size
298        echo "20m" > ${PXEWORLD}/conf/base/var/md_size
299        echo "30m" > ${PXEWORLD}/conf/base/root/md_size
300        chroot ${PXEWORLD} tar cvf conf/base/etc.cpio.gz --format cpio --gzip etc 2>/dev/null
301        chroot ${PXEWORLD} tar cvf conf/base/var.cpio.gz --exclude var/db/pkg --format cpio --gzip var 2>/dev/null
302        chroot ${PXEWORLD} tar cvf conf/base/root.cpio.gz --format cpio --gzip root 2>/dev/null
303       
304}
305
306# Check if we need to build the world environment
307check_worldports() {
308        if [ -e "${PXEWORLD}/usr/local/bin/xv" ] ; then return ; fi
309
310        if [ ! -d "${PXEWORLD}/usr/ports/x11/xorg" -a ! -d "/usr/ports/x11/xorg" ] ; then
311                exit_err "Missing /usr/ports/x11/xorg, please checkout ports tree to continue"
312        fi
313
314        if [ ! -d "${PXEWORLD}/usr/ports/x11/xorg" ] ; then
315                rm -rf "${PXEWORLD}/usr/ports"
316                echo "Copying /usr/ports -> ${PXEWORLD}/usr/ports"
317                cp -r /usr/ports ${PXEWORLD}/usr/ports
318        fi
319
320        # Building ports inside world
321        mount -t devfs devfs ${PXEWORLD}/dev
322        cp /etc/resolv.conf ${PXEWORLD}/etc/resolv.conf
323        echo "BATCH=yes" >> ${PXEWORLD}/etc/make.conf
324        echo '#!/bin/sh
325
326/etc/rc.d/ldconfig start
327chmod 777 /tmp
328MACHINE=i386 ; export MACHINE
329UNAME_p=i386 ; export UNAME_p
330UNAME_m=i386 ; export UNAME_m
331
332for p in $WORLDPORTS
333do
334        cd /usr/ports/$p
335        make install
336        if [ "$?" != "0" ] ; then
337                exit 1
338        fi
339done
340' > ${PXEWORLD}/.mkports.sh
341
342        chmod 755 ${PXEWORLD}/.mkports.sh
343        chroot ${PXEWORLD} /.mkports.sh
344        if [ "$?" != "0" ] ; then
345                exit_err "Failed building thinclient world ports!"
346        fi
347        rm ${PXEWORLD}/.mkports.sh
348        umount ${PXEWORLD}/dev
349
350}
351
352# Check if we need to build the world environment
353check_world() {
354  if [ -e "${PXEWORLD}/COPYRIGHT" ] ; then return ; fi
355       
356  mkdir -p "${PXEWORLD}"
357  cd "${PXEWORLD}"
358
359  # Default pcbsd.conf file
360  PCBSD_ETCCONF="/usr/local/etc/pcbsd.conf"
361
362  # Set the mirror URL
363  get_mirror
364  MIRRORURL="$VAL"
365
366  # Set the system arch type
367  if [ "$SYSTYPE" = "desktop" ] ; then
368    # If building remote X server, we don't need to run amd64
369    ARCH="i386"
370  else
371    ARCH="`uname -m`"
372  fi
373
374  local dFiles="base.txz doc.txz kernel.txz games.txz"
375  if [ "$ARCH" = "amd64" ] ; then
376     dFiles="$dFiles lib32.txz"
377  fi
378
379  # To fetch the jail environment
380  echo "Fetching FreeBSD environment. This may take a while..."
381  for i in $dFiles
382  do
383    echo "Downloading ${MIRRORURL}/${SYSVER}/${ARCH}/dist/${i} ..."
384   
385    get_file_from_mirrors "/${SYSVER}/${ARCH}/dist/${i}" "$i"
386    [ $? -ne 0 ] && exit_err "Error while downloading the freebsd world."
387  done
388
389  # Save the archive as our example world environment
390  mkdir -p ${PXEWORLD}/installarchive/
391
392  echo "Extracting FreeBSD environment... This may take a while..."
393  # Extract dist files
394  for i in $dFiles
395  do
396    tar xvpf ${i} 2>/dev/null
397    if [ $? -ne 0 ] ; then exit_err "Failed extracting FreeBSD environment"; fi
398
399    # Save the archive file
400    mv ${i} ${PXEWORLD}/installarchive/
401  done
402
403}
404
405# Function to check if dhcpd is installed
406check_dhcpd() {
407        which dhcpd >/dev/null 2>/dev/null
408        if [ "$?" = "0" ] ; then return; fi
409
410        echo "Installing $DHCPPORT"
411        pkg install -y ${DHCPPORT}
412        if [ "$?" != "0" ] ; then exit_err "Failed installing ${DHCPPORT}"; fi
413}
414
415# Function to display what information to add to external DHCP server
416ignore_dhcpd() {
417        echo "Add the following information to your external DHCP Server"
418        echo "Will display here when this works"
419}
420
421# Function which checks and sets up the thinclient as an install server
422check_installdirs() {
423        if [ -e "${PXEWORLD}/installscripts/pc-sysinstall.example" ]; then
424           return
425        fi
426        touch ${PXEWORLD}/etc/installserver
427        mkdir ${PXEWORLD}/installscripts
428        cp ${PROGDIR}/resources/scripts/pc-sysinstall.example ${PXEWORLD}/installscripts/pc-sysinstall.example
429        echo "zfs_load=\"YES\"" > ${PXEWORLD}/boot/loader.conf
430        echo "geom_mirror_load=\"YES\"" >> ${PXEWORLD}/boot/loader.conf
431        echo "geom_eli_load=\"YES\"" >> ${PXEWORLD}/boot/loader.conf
432}
433
434do_removal() {
435        if [ -d "${PXEWORLD}" ] ; then
436                echo "Removing ${PXEWORLD}"
437                rm -rf ${PXEWORLD} 2>/dev/null
438                chflags -R noschg ${PXEWORLD} 2>/dev/null
439                rm -rf ${PXEWORLD} 2>/dev/null
440        fi
441}
442
443# Make sure we are root
444if [ `id -u` != "0" ] ; then exit_err "Must be run as root!"; fi
445
446# Check if we are removing the existing thinclient
447if [ "$1" = "-remove" -o "$1" = "remove" ] ;  then
448  do_removal
449  exit 0
450fi
451
452echo "$0 will install the components to convert this system into a thin-client server."
453echo -e "Continue? (Y/N) \c"
454read tmp
455if [ "$tmp" != "Y" -a "$tmp" != "y" ] ; then
456        exit 0
457fi
458
459echo "Do you wish to install the dhcpd server port or use an external server?"
460echo "If you wish to use an external server please make sure it supports adding" 
461echo "next server and bootfile name options."
462echo -e "(d/e) \c"
463read tmp
464if [ "$tmp" = "D" -o "$tmp" = "d" ] ; then
465   DHCPTYPE="internal"
466else
467   DHCPTYPE="external"
468fi
469
470echo "Do you wish to make this a remote X desktop server or install server?"
471echo -e "(r/i) \c"
472read tmp
473if [ "$tmp" = "I" -o "$tmp" = "i" ] ; then
474   SYSTYPE="install"
475else
476   SYSTYPE="desktop"
477fi
478
479
480if [ "$DHCPTYPE" = "internal" ] ; then
481  # Start by installing dhcpd
482  check_dhcpd
483else
484  # Install without dhcpd
485  ignore_dhcpd
486
487# Start by setting up a new buildworld
488check_world
489
490if [ "$SYSTYPE" = "desktop" ] ; then
491  # Build the ports inside the world environment
492  check_worldports
493else
494  # Setup the installation directories
495  check_installdirs
496fi
497
498# Install the thinclient configuration files
499check_installconfig
500
501if [ "$DHCPTYPE" = "internal" ] ; then
502  # Tweak the base system to enable the thinclient
503  check_configsystem
504else
505  # Tweak the base system to enable the thinclient without dhcpd
506  check_configsystem_ignore_dhcp
507fi
508
509if [ "$SYSTYPE" = "desktop" ] ; then
510  echo ""
511  echo "You will now need to enable remote desktop."
512  echo "This can be done via the PC-BSD Control Panel -> GDM Configuration"
513  echo "or by manually editing /usr/local/etc/gdm/custom.conf"
514else
515  echo ""
516  echo "To perform system installations, place your custom pc-sysinstall scripts in:"
517  echo "/usr/home/thinclient/installscripts"
518  echo ""
519  echo "An example script is provided in the above directory"
520  echo ""
521  echo "For unattended installations, save your pc-sysinstall script as:"
522  echo "/usr/home/thinclient/installscripts/unattended.cfg"
523fi
524
525if [ "$DHCPTYPE" = "internal" ] ; then
526  echo ""
527  echo "Your system is now setup to do PXE booting!"
528  exit 0
529else
530  echo "You will need to modify the following options in your dhcp server:"
531  echo ""
532  echo "filename "boot/pxeboot"
533  echo "next-server $ipaddr"
534  echo "option root-path "/usr/home/thinclient"
535  echo ""
536  echo "Then you can begin to use PXE Boot."
537  exit 0
538fi
539fi
Note: See TracBrowser for help on using the repository browser.