source: src-sh/pc-thinclient/pc-thinclient @ 4c50941

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

Update pc-thinclient, now when we setup a remote desktop server
it won't require the ports tree, and will just grab pkgng packages to build
the PXE world environment. Also it will enable GDM / XDMCP automatically for the user

  • Property mode set to 100644
File size: 15.2 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        # Remove old pc-sysinstall so we use new one in /usr/local/sbin
311        rm ${PXEWORLD}/usr/sbin/pc-sysinstall 2>/dev/null
312
313        # If we are not doing a desktop we can stop here
314        if [ "$SYSTYPE" != "desktop" ] ; then return; fi
315
316        if [ -e "${PXEWORLD}/usr/local/bin/xv" ] ; then return ; fi
317
318        # Install Xorg
319        pkg -c ${PXEWORLD} install -y xorg
320
321        # Need to install / use GDM on the base system
322        if [ ! -e "/usr/local/sbin/gdm" ] ; then
323                pkg install -y gdm
324        fi
325
326        # Disable PCDM since it doesn't do XDMCP
327        grep -q '^pcdm_enable="NO"' /etc/rc.conf
328        if [ $? -ne 0 ] ; then
329                echo 'pcdm_enable="NO"' >> /etc/rc.conf
330        fi
331
332        # Enable GDM for its XDMCP support
333        grep -q '^gdm_enable="YES"' /etc/rc.conf
334        if [ $? -ne 0 ] ; then
335                echo 'gdm_enable="YES"' >> /etc/rc.conf
336        fi
337
338        # Copy GDM pam & rc files
339        cp ${PROGDIR}/resources/gdm/gdm-pam /usr/local/etc/pam.d/gdm
340        cp ${PROGDIR}/resources/gdm/gdm-rc /usr/local/etc/rc.d/gdm
341        cp ${PROGDIR}/resources/gdm/custom.conf /usr/local/etc/gdm/custom.conf
342}
343
344# Check if we need to build the world environment
345check_world() {
346  if [ -e "${PXEWORLD}/COPYRIGHT" ] ; then return ; fi
347       
348  mkdir -p "${PXEWORLD}"
349  cd "${PXEWORLD}"
350
351  # Default pcbsd.conf file
352  PCBSD_ETCCONF="/usr/local/etc/pcbsd.conf"
353
354  # Set the system arch type
355  ARCH="`uname -m`"
356
357  local dFiles="base.txz doc.txz kernel.txz games.txz"
358  if [ "$ARCH" = "amd64" ] ; then
359     dFiles="$dFiles lib32.txz"
360  fi
361
362  # To fetch the jail environment
363  echo "Fetching FreeBSD environment. This may take a while..."
364  for i in $dFiles
365  do
366    echo "Downloading ${SYSVER}/${ARCH}/dist/${i} ..."
367   
368    get_file_from_mirrors "/${SYSVER}/${ARCH}/dist/${i}" "$i" "iso"
369    [ $? -ne 0 ] && exit_err "Error while downloading the freebsd world."
370  done
371
372  # Save the archive as our example world environment
373  mkdir -p ${PXEWORLD}/installarchive/
374
375  echo "Extracting FreeBSD environment... This may take a while..."
376  # Extract dist files
377  for i in $dFiles
378  do
379    tar xvpf ${i} 2>/dev/null
380    if [ $? -ne 0 ] ; then exit_err "Failed extracting FreeBSD environment"; fi
381
382    # Save the archive file
383    mv ${i} ${PXEWORLD}/installarchive/
384  done
385
386}
387
388# Function to check if dhcpd is installed
389check_dhcpd() {
390        which dhcpd >/dev/null 2>/dev/null
391        if [ "$?" = "0" ] ; then return; fi
392
393        echo "Installing $DHCPPORT"
394        pkg install -y ${DHCPPORT}
395        if [ "$?" != "0" ] ; then exit_err "Failed installing ${DHCPPORT}"; fi
396}
397
398# Function to display what information to add to external DHCP server
399ignore_dhcpd() {
400        echo "Add the following information to your external DHCP Server"
401        echo "Will display here when this works"
402}
403
404# Function which checks and sets up the thinclient as an install server
405check_installdirs() {
406        if [ -e "${PXEWORLD}/installscripts/pc-sysinstall.example" ]; then
407           return
408        fi
409        touch ${PXEWORLD}/etc/installserver
410        mkdir ${PXEWORLD}/installscripts
411        cp ${PROGDIR}/resources/scripts/pc-sysinstall.example ${PXEWORLD}/installscripts/pc-sysinstall.example
412        echo "zfs_load=\"YES\"" > ${PXEWORLD}/boot/loader.conf
413        echo "geom_mirror_load=\"YES\"" >> ${PXEWORLD}/boot/loader.conf
414        echo "geom_eli_load=\"YES\"" >> ${PXEWORLD}/boot/loader.conf
415}
416
417do_removal() {
418        if [ -d "${PXEWORLD}" ] ; then
419                echo "Removing ${PXEWORLD}"
420                rm -rf ${PXEWORLD} 2>/dev/null
421                chflags -R noschg ${PXEWORLD} 2>/dev/null
422                rm -rf ${PXEWORLD} 2>/dev/null
423        fi
424}
425
426# Make sure we are root
427if [ `id -u` != "0" ] ; then exit_err "Must be run as root!"; fi
428
429# Check if we are removing the existing thinclient
430if [ "$1" = "-remove" -o "$1" = "remove" ] ;  then
431  do_removal
432  exit 0
433fi
434
435echo "$0 will install the components to convert this system into a thin-client server."
436echo -e "Continue? (Y/N) \c"
437read tmp
438if [ "$tmp" != "Y" -a "$tmp" != "y" ] ; then
439        exit 0
440fi
441
442echo "Do you wish to install the dhcpd server port or use an external server?"
443echo "If you wish to use an external server please make sure it supports adding" 
444echo "next server and bootfile name options."
445echo -e "(d/e) \c"
446read tmp
447if [ "$tmp" = "D" -o "$tmp" = "d" ] ; then
448   DHCPTYPE="internal"
449else
450   DHCPTYPE="external"
451fi
452
453echo "Do you wish to make this a remote X desktop server or install server?"
454echo -e "(r/i) \c"
455read tmp
456if [ "$tmp" = "I" -o "$tmp" = "i" ] ; then
457   SYSTYPE="install"
458else
459   SYSTYPE="desktop"
460fi
461
462
463if [ "$DHCPTYPE" = "internal" ] ; then
464  # Start by installing dhcpd
465  check_dhcpd
466else
467  # Install without dhcpd
468  ignore_dhcpd
469fi
470
471# Start by setting up a new buildworld
472check_world
473
474# Build the ports inside the world environment
475check_worldports
476
477if [ "$SYSTYPE" != "desktop" ] ; then
478  # Setup the installation directories
479  check_installdirs
480fi
481
482# Install the thinclient configuration files
483check_installconfig
484
485if [ "$DHCPTYPE" = "internal" ] ; then
486  # Tweak the base system to enable the thinclient
487  check_configsystem
488else
489  # Tweak the base system to enable the thinclient without dhcpd
490  check_configsystem_ignore_dhcp
491fi
492
493if [ "$SYSTYPE" = "desktop" ] ; then
494  echo ""
495  echo "You will need to reboot the system for the login manager changes"
496  echo "to take effect."
497else
498  echo ""
499  echo "To perform system installations, place your custom pc-sysinstall scripts in:"
500  echo "/usr/home/thinclient/installscripts"
501  echo ""
502  echo "An example script is provided in the above directory"
503  echo ""
504  echo "For unattended installations, save your pc-sysinstall script as:"
505  echo "/usr/home/thinclient/installscripts/unattended.cfg"
506fi
507
508if [ "$DHCPTYPE" = "internal" ] ; then
509  echo ""
510  echo "Your system is now setup to do PXE booting!"
511  exit 0
512else
513  echo "You will need to modify the following options in your dhcp server:"
514  echo ""
515  echo "filename boot/pxeboot"
516  echo "next-server $ipaddr"
517  echo "option root-path $PXEWORLD"
518  echo ""
519  echo "Then you can begin to use PXE Boot."
520  exit 0
521fi
Note: See TracBrowser for help on using the repository browser.