source: src-sh/pc-thinclient/pc-thinclient @ 23594f1

9.2-releasereleng/10.0releng/10.0.1releng/10.0.2
Last change on this file since 23594f1 was 23594f1, checked in by Kris Moore <kris@…>, 13 months ago

Restart pf after we setup the rules for pc-thinclient

  • Property mode set to 100644
File size: 15.6 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                /etc/rc.d/pf restart
98          fi
99        fi
100
101        # Add some entries for /etc/exports
102        cat /etc/exports 2>/dev/null | grep "$PXEWORLD" >/dev/null 2>/dev/null
103        if [ "$?" != "0" ] ; then
104                echo "$PXEWORLD -maproot=nobody -ro -network 192.168.2 -mask 255.255.255" >>/etc/exports
105        fi
106
107        # Setup tftp
108        cat /etc/inetd.conf | grep "$PXEWORLD" >/dev/null 2>/dev/null
109        if [ "$?" != "0" ] ; then
110                echo "tftp   dgram   udp     wait    root    /usr/libexec/tftpd      tftpd -l -s ${PXEWORLD}" >> /etc/inetd.conf
111        fi
112
113        # Setup rcpbind entries
114        cat /etc/hosts.allow 2>/dev/null | grep "192.168.2.0" >/dev/null 2>/dev/null
115        if [ "$?" != "0" ] ; then
116                sed  -i '' 's|rpcbind : ALL : deny|rpcbind : 192.168.2.0/255.255.255.0 : allow\
117portmap : 192.168.2.0/255.255.255.0 : allow\
118rpcbind : ALL : deny|' /etc/hosts.allow
119        fi
120
121        # Add a bulk of IPs to /etc/hosts this fixes bugs with RPC timeouts
122        # when mounting NFS
123        grep -q 'thinclient100' /etc/hosts
124        if [ $? -ne 0 ] ; then
125                i="100"
126                while
127                z="1"
128                do
129                        if [ "${i}" = "200" ]; then break; fi
130                        echo "192.168.2.${i}  thinclient${i}" >>/etc/hosts
131                        i="`expr ${i} + 1`"
132                done
133        fi
134
135        # Make sure the NIC is set to the right IP before bringing up dhcpd
136        ifconfig $NIC 192.168.2.2
137
138        # Start the services
139        cmds="/etc/rc.d/nfsd /etc/rc.d/inetd /usr/local/etc/rc.d/isc-dhcpd"
140        for _sC in $cmds
141        do
142                echo -e "Starting ${_sC}...\c"
143                ${_sC} restart >/dev/null 2>/dev/null
144                if [ "$?" != "0" ] ; then
145                        echo -e "FAILED! Please run try running it manually."
146                else
147                        echo -e "OK"
148                fi
149        done
150}
151
152# Start configuring the base system without DHCP
153check_configsystem_ignore_dhcp() {
154        echo "Setting up system for PXE booting..."
155
156        # Setup the new pxeboot user with a default password
157        cat /etc/passwd | grep pxeboot >/dev/null 2>/dev/null
158        if [ "$?" != "0" ] ; then
159                echo "thinclient" | pw useradd -n "pxeboot" -h 0 -s /bin/tcsh -d ${PXEWORLD}/mnt/xorg-config -c "pxeboot"
160                chown -R pxeboot:pxeboot ${PXEWORLD}/mnt/xorg-config
161        fi
162
163        # Ask for the NIC we want to run on
164        while
165        z=1
166        do
167                echo "What NIC do you wish to listen on? (I.E. re0)"
168                echo -e "nic) \c"
169                read NIC
170
171                ifconfig $NIC >/dev/null 2>/dev/null
172                if [ $? -ne 0 -o -z "$NIC" ] ; then
173                        echo "Invalid nic entered, please try again!"
174                        sleep 1
175                else
176                        break
177                fi
178        done
179       
180        # Ask for the IP Address to be used on the NIC for PXE booting
181        while
182        z=1
183        do
184                echo "What IP address will pc-thinclient be listening on? (I.E. 192.168.2.2)"
185                echo -e "ipaddr) \c"
186                read ipaddr
187        break
188        done
189                # Ask for the network id of the local subnet
190        while
191        z=1
192        do
193                echo "What is the network id for your local subnet? (I.E. 192.168.2.0)"
194                echo -e "netid) \c"
195                read netid
196        break
197        done
198       
199                # Ask for the network mask of the local subnet
200        while
201        z=1
202        do
203                echo "What is the network mask for your local subnet? (I.E. 255.255.255.0)"
204                echo -e "netmaskid) \c"
205                read netmaskid
206        break
207        done
208
209        # Save the rc.conf glue
210        cat /etc/rc.conf | grep "# pc-thinclient" >/dev/null 2>/dev/null
211        if [ "$?" != "0" ] ; then
212                echo "# pc-thinclient configuration
213portmap_enable=\"YES\"
214nfs_server_enable=\"YES\"
215inetd_enable=\"YES\"" >> /etc/rc.conf
216        fi
217       
218        # Add firewall exception
219        if [ -e "/etc/pf.conf" ] ; then
220          cat /etc/pf.conf | grep "pass in on ${NIC} all" >/dev/null 2>/dev/null
221          if [ "$?" != "0" ] ; then
222                # Setup the firewall exclusion for this NIC
223                echo "pass in on ${NIC} all" >> /etc/pf.conf
224          fi
225        fi
226
227        # Add some entries for /etc/exports based on manual user input
228        echo "$PXEWORLD -maproot=nobody -ro -network $netid -mask $netmaskid" >> /etc/exports
229
230        # Setup tftp
231        cat /etc/inetd.conf | grep "$PXEWORLD" >/dev/null 2>/dev/null
232        if [ "$?" != "0" ] ; then
233                echo "tftp   dgram   udp     wait    root    /usr/libexec/tftpd      tftpd -l -s ${PXEWORLD}" >> /etc/inetd.conf
234        fi
235       
236        # Setup rcpbind entries
237        sed -i -e "s|rpcbind : ALL : deny|rpcbind : $netid/$netmaskid : allow\\
238portmap : $netid/$netmaskid : allow\\
239rpcbind : ALL : deny|" /etc/hosts.allow
240
241        # Start the services
242        cmds="/etc/rc.d/nfsd /etc/rc.d/inetd"
243        for _sC in $cmds
244        do
245                echo -e "Starting ${_sC}...\c"
246                ${_sC} restart >/dev/null 2>/dev/null
247                if [ "$?" != "0" ] ; then
248                        echo -e "FAILED! Please run try running it manually."
249                else
250                        echo -e "OK"
251                fi
252        done
253}
254
255
256# Check if we need to install custom config
257check_installconfig() {
258        if [ -e "${PXEWORLD}/etc/scripts/tcslogin.sh" ] ; then return ; fi
259
260        # Lets copy over the /etc/scripts directory
261        rm -rf ${PXEWORLD}/etc/scripts >/dev/null 2>/dev/null
262        cp -r ${PROGDIR}/resources/scripts ${PXEWORLD}/etc/scripts
263
264        # Remove a few rc.d things we dont need on clients
265        rm ${PXEWORLD}/etc/rc.d/cron
266        rm ${PXEWORLD}/etc/rc.d/sendmail
267
268        # Lets copy over all the /etc/ files we need
269        cp ${PROGDIR}/resources/etc/fstab ${PXEWORLD}/etc/
270        cp ${PROGDIR}/resources/etc/gettytab ${PXEWORLD}/etc/
271        cp ${PROGDIR}/resources/etc/hosts ${PXEWORLD}/etc/
272        cp ${PROGDIR}/resources/etc/motd ${PXEWORLD}/etc/
273        cp ${PROGDIR}/resources/etc/rc.conf ${PXEWORLD}/etc/
274        cp ${PROGDIR}/resources/etc/ttys ${PXEWORLD}/etc/
275
276        # Copy over rc.d / boot / root files
277        cp ${PROGDIR}/resources/boot/beastie.4th ${PXEWORLD}/boot/
278        cp ${PROGDIR}/resources/root/dot.login ${PXEWORLD}/root/.login
279
280        # Create a few directories used on client
281        mkdir -p ${PXEWORLD}/mnt/xorg-config
282
283        # Create the diskless configuration
284        mkdir -p ${PXEWORLD}/conf/base
285        mkdir -p ${PXEWORLD}/conf/base/etc
286        mkdir -p ${PXEWORLD}/conf/base/var
287        mkdir -p ${PXEWORLD}/conf/base/root
288        echo "10m" > ${PXEWORLD}/conf/base/etc/md_size
289        echo "20m" > ${PXEWORLD}/conf/base/var/md_size
290        echo "30m" > ${PXEWORLD}/conf/base/root/md_size
291        chroot ${PXEWORLD} tar cvf conf/base/etc.cpio.gz --format cpio --gzip etc 2>/dev/null
292        chroot ${PXEWORLD} tar cvf conf/base/var.cpio.gz --exclude var/db/pkg --format cpio --gzip var 2>/dev/null
293        chroot ${PXEWORLD} tar cvf conf/base/root.cpio.gz --format cpio --gzip root 2>/dev/null
294
295        # Fix the ZFS zpool.cache
296        rmdir ${PXEWORLD}/boot/zfs 2>/dev/null
297        ln -fs /tmp ${PXEWORLD}/boot/zfs
298}
299
300# Check if we need to build the world environment
301check_worldports() {
302        mkdir ${PXEWORLD}/usr/local/etc 2>/dev/null
303        cp /etc/resolv.conf ${PXEWORLD}/etc/
304        cp /usr/local/etc/pkg.conf ${PXEWORLD}/usr/local/etc/
305        cp /usr/local/etc/pkg-pubkey.cert ${PXEWORLD}/usr/local/etc/
306
307        # Start by adding pcbsd-utils
308        pkg -c ${PXEWORLD} install -y pcbsd-utils
309
310        # If we are not doing a desktop we can stop here
311        if [ "$SYSTYPE" != "desktop" ] ; then return; fi
312
313        if [ -e "${PXEWORLD}/usr/local/bin/xv" ] ; then return ; fi
314
315        if [ ! -d "${PXEWORLD}/usr/ports/x11/xorg" -a ! -d "/usr/ports/x11/xorg" ] ; then
316                exit_err "Missing /usr/ports/x11/xorg, please checkout ports tree to continue"
317        fi
318
319        if [ ! -d "${PXEWORLD}/usr/ports/x11/xorg" ] ; then
320                rm -rf "${PXEWORLD}/usr/ports"
321                echo "Copying /usr/ports -> ${PXEWORLD}/usr/ports"
322                cp -r /usr/ports ${PXEWORLD}/usr/ports
323        fi
324
325        # Building ports inside world
326        mount -t devfs devfs ${PXEWORLD}/dev
327        cp /etc/resolv.conf ${PXEWORLD}/etc/resolv.conf
328        echo "BATCH=yes" >> ${PXEWORLD}/etc/make.conf
329        echo '#!/bin/sh
330
331/etc/rc.d/ldconfig start
332chmod 777 /tmp
333MACHINE=i386 ; export MACHINE
334UNAME_p=i386 ; export UNAME_p
335UNAME_m=i386 ; export UNAME_m
336
337for p in $WORLDPORTS
338do
339        cd /usr/ports/$p
340        make install
341        if [ "$?" != "0" ] ; then
342                exit 1
343        fi
344done
345' > ${PXEWORLD}/.mkports.sh
346
347        chmod 755 ${PXEWORLD}/.mkports.sh
348        chroot ${PXEWORLD} /.mkports.sh
349        if [ "$?" != "0" ] ; then
350                exit_err "Failed building thinclient world ports!"
351        fi
352        rm ${PXEWORLD}/.mkports.sh
353        umount ${PXEWORLD}/dev
354
355}
356
357# Check if we need to build the world environment
358check_world() {
359  if [ -e "${PXEWORLD}/COPYRIGHT" ] ; then return ; fi
360       
361  mkdir -p "${PXEWORLD}"
362  cd "${PXEWORLD}"
363
364  # Default pcbsd.conf file
365  PCBSD_ETCCONF="/usr/local/etc/pcbsd.conf"
366
367  # Set the system arch type
368  if [ "$SYSTYPE" = "desktop" ] ; then
369    # If building remote X server, we don't need to run amd64
370    ARCH="i386"
371  else
372    ARCH="`uname -m`"
373  fi
374
375  local dFiles="base.txz doc.txz kernel.txz games.txz"
376  if [ "$ARCH" = "amd64" ] ; then
377     dFiles="$dFiles lib32.txz"
378  fi
379
380  # To fetch the jail environment
381  echo "Fetching FreeBSD environment. This may take a while..."
382  for i in $dFiles
383  do
384    echo "Downloading ${SYSVER}/${ARCH}/dist/${i} ..."
385   
386    get_file_from_mirrors "/${SYSVER}/${ARCH}/dist/${i}" "$i" "iso"
387    [ $? -ne 0 ] && exit_err "Error while downloading the freebsd world."
388  done
389
390  # Save the archive as our example world environment
391  mkdir -p ${PXEWORLD}/installarchive/
392
393  echo "Extracting FreeBSD environment... This may take a while..."
394  # Extract dist files
395  for i in $dFiles
396  do
397    tar xvpf ${i} 2>/dev/null
398    if [ $? -ne 0 ] ; then exit_err "Failed extracting FreeBSD environment"; fi
399
400    # Save the archive file
401    mv ${i} ${PXEWORLD}/installarchive/
402  done
403
404}
405
406# Function to check if dhcpd is installed
407check_dhcpd() {
408        which dhcpd >/dev/null 2>/dev/null
409        if [ "$?" = "0" ] ; then return; fi
410
411        echo "Installing $DHCPPORT"
412        pkg install -y ${DHCPPORT}
413        if [ "$?" != "0" ] ; then exit_err "Failed installing ${DHCPPORT}"; fi
414}
415
416# Function to display what information to add to external DHCP server
417ignore_dhcpd() {
418        echo "Add the following information to your external DHCP Server"
419        echo "Will display here when this works"
420}
421
422# Function which checks and sets up the thinclient as an install server
423check_installdirs() {
424        if [ -e "${PXEWORLD}/installscripts/pc-sysinstall.example" ]; then
425           return
426        fi
427        touch ${PXEWORLD}/etc/installserver
428        mkdir ${PXEWORLD}/installscripts
429        cp ${PROGDIR}/resources/scripts/pc-sysinstall.example ${PXEWORLD}/installscripts/pc-sysinstall.example
430        echo "zfs_load=\"YES\"" > ${PXEWORLD}/boot/loader.conf
431        echo "geom_mirror_load=\"YES\"" >> ${PXEWORLD}/boot/loader.conf
432        echo "geom_eli_load=\"YES\"" >> ${PXEWORLD}/boot/loader.conf
433}
434
435do_removal() {
436        if [ -d "${PXEWORLD}" ] ; then
437                echo "Removing ${PXEWORLD}"
438                rm -rf ${PXEWORLD} 2>/dev/null
439                chflags -R noschg ${PXEWORLD} 2>/dev/null
440                rm -rf ${PXEWORLD} 2>/dev/null
441        fi
442}
443
444# Make sure we are root
445if [ `id -u` != "0" ] ; then exit_err "Must be run as root!"; fi
446
447# Check if we are removing the existing thinclient
448if [ "$1" = "-remove" -o "$1" = "remove" ] ;  then
449  do_removal
450  exit 0
451fi
452
453echo "$0 will install the components to convert this system into a thin-client server."
454echo -e "Continue? (Y/N) \c"
455read tmp
456if [ "$tmp" != "Y" -a "$tmp" != "y" ] ; then
457        exit 0
458fi
459
460echo "Do you wish to install the dhcpd server port or use an external server?"
461echo "If you wish to use an external server please make sure it supports adding" 
462echo "next server and bootfile name options."
463echo -e "(d/e) \c"
464read tmp
465if [ "$tmp" = "D" -o "$tmp" = "d" ] ; then
466   DHCPTYPE="internal"
467else
468   DHCPTYPE="external"
469fi
470
471echo "Do you wish to make this a remote X desktop server or install server?"
472echo -e "(r/i) \c"
473read tmp
474if [ "$tmp" = "I" -o "$tmp" = "i" ] ; then
475   SYSTYPE="install"
476else
477   SYSTYPE="desktop"
478fi
479
480
481if [ "$DHCPTYPE" = "internal" ] ; then
482  # Start by installing dhcpd
483  check_dhcpd
484else
485  # Install without dhcpd
486  ignore_dhcpd
487fi
488
489# Start by setting up a new buildworld
490check_world
491
492# Build the ports inside the world environment
493check_worldports
494
495if [ "$SYSTYPE" != "desktop" ] ; then
496  # Setup the installation directories
497  check_installdirs
498fi
499
500# Install the thinclient configuration files
501check_installconfig
502
503if [ "$DHCPTYPE" = "internal" ] ; then
504  # Tweak the base system to enable the thinclient
505  check_configsystem
506else
507  # Tweak the base system to enable the thinclient without dhcpd
508  check_configsystem_ignore_dhcp
509fi
510
511if [ "$SYSTYPE" = "desktop" ] ; then
512  echo ""
513  echo "You will now need to enable remote desktop."
514  echo "This can be done via the PC-BSD Control Panel -> GDM Configuration"
515  echo "or by manually editing /usr/local/etc/gdm/custom.conf"
516else
517  echo ""
518  echo "To perform system installations, place your custom pc-sysinstall scripts in:"
519  echo "/usr/home/thinclient/installscripts"
520  echo ""
521  echo "An example script is provided in the above directory"
522  echo ""
523  echo "For unattended installations, save your pc-sysinstall script as:"
524  echo "/usr/home/thinclient/installscripts/unattended.cfg"
525fi
526
527if [ "$DHCPTYPE" = "internal" ] ; then
528  echo ""
529  echo "Your system is now setup to do PXE booting!"
530  exit 0
531else
532  echo "You will need to modify the following options in your dhcp server:"
533  echo ""
534  echo "filename boot/pxeboot"
535  echo "next-server $ipaddr"
536  echo "option root-path $PXEWORLD"
537  echo ""
538  echo "Then you can begin to use PXE Boot."
539  exit 0
540fi
Note: See TracBrowser for help on using the repository browser.