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

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

Run pc-extractoverlay ports after setting up GDM, to ensure that our
xsessions gets cleaned

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