source: src-sh/pc-thinclient/pc-thinclient @ 9788f38

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

Switch us over to the new PC-BSD CDN

  • Property mode set to 100644
File size: 11.4 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# Source our functions
32. /usr/local/share/pcbsd/scripts/functions.sh
33
34# Set some universal variables
35PROGDIR="/usr/local/share/pcbsd/pc-thinclient"
36DHCPPORT="isc-dhcp41-server"
37PXEWORLD="/usr/home/thinclient"
38WORLDPORTS="x11/xorg graphics/xv"
39export WORLDPORTS
40SYSVER="`uname -r | cut -d '-' -f 1-2`"
41
42# Start by sourcing /etc/profile
43# This grabs any HTTP_ / FTP_ PROXY variables
44. /etc/profile
45
46# Start configuring the base system
47check_configsystem() {
48        echo "Setting up system for PXE booting..."
49
50        # Setup the new pxeboot user with a default password
51        cat /etc/passwd | grep pxeboot >/dev/null 2>/dev/null
52        if [ "$?" != "0" ] ; then
53                echo "thinclient" | pw useradd -n "pxeboot" -h 0 -s /bin/tcsh -d ${PXEWORLD}/mnt/xorg-config -c "pxeboot"
54                chown -R pxeboot:pxeboot ${PXEWORLD}/mnt/xorg-config
55        fi
56
57        # Copy over the default dhcpd.conf file
58        cp ${PROGDIR}/resources/dhcpd/dhcpd.conf /usr/local/etc/dhcpd.conf
59
60        # Ask for the NIC we want to run on
61        while
62        z=1
63        do
64                echo "What NIC do you wish DHCPD to listen on? (I.E. re0)"
65                echo -e "nic) \c"
66                read NIC
67               
68                ifconfig $NIC >/dev/null 2>/dev/null
69                if [ $? -ne 0 -o -z "$NIC" ] ; then
70                        echo "Invalid nic entered, please try again!"
71                        sleep 1
72                else
73                        break
74                fi
75        done
76
77        # Save the rc.conf glue
78        cat /etc/rc.conf | grep "# pc-thinclient" >/dev/null 2>/dev/null
79        if [ "$?" != "0" ] ; then
80                echo "# pc-thinclient configuration
81dhcpd_enable=\"YES\"
82dhcpd_ifaces=\"${NIC}\"
83portmap_enable=\"YES\"
84nfs_server_enable=\"YES\"
85inetd_enable=\"YES\"
86ifconfig_${NIC}=\"192.168.2.2\"" >> /etc/rc.conf
87        fi
88
89        # Add firewall exception
90        if [ -e "/etc/pf.conf" ] ; then
91          cat /etc/pf.conf | grep "pass in on ${NIC} all" >/dev/null 2>/dev/null
92          if [ "$?" != "0" ] ; then
93                # Setup the firewall exclusion for this NIC
94                echo "pass in on ${NIC} all" >> /etc/pf.conf
95          fi
96        fi
97
98        # Add some entries for /etc/exports
99        cat /etc/exports 2>/dev/null | grep "$PXEWORLD" >/dev/null 2>/dev/null
100        if [ "$?" != "0" ] ; then
101                echo "$PXEWORLD -maproot=nobody -ro -network 192.168.2 -mask 255.255.255" >>/etc/exports
102        fi
103
104        # Setup tftp
105        cat /etc/inetd.conf | grep "$PXEWORLD" >/dev/null 2>/dev/null
106        if [ "$?" != "0" ] ; then
107                echo "tftp   dgram   udp     wait    root    /usr/libexec/tftpd      tftpd -l -s ${PXEWORLD}" >> /etc/inetd.conf
108        fi
109
110        # Setup rcpbind entries
111        cat /etc/hosts.allow 2>/dev/null | grep "192.168.2.0" >/dev/null 2>/dev/null
112        if [ "$?" != "0" ] ; then
113                sed  -i '' 's|rpcbind : ALL : deny|rpcbind : 192.168.2.0/255.255.255.0 : allow\
114portmap : 192.168.2.0/255.255.255.0 : allow\
115rpcbind : ALL : deny|' /etc/hosts.allow
116        fi
117
118        # Add a bulk of IPs to /etc/hosts this fixes bugs with RPC timeouts
119        # when mounting NFS
120        grep -q 'thinclient100' /etc/hosts
121        if [ $? -ne 0 ] ; then
122                i="100"
123                while
124                z="1"
125                do
126                        if [ "${i}" = "200" ]; then break; fi
127                        echo "192.168.2.${i}  thinclient${i}" >>/etc/hosts
128                        i="`expr ${i} + 1`"
129                done
130        fi
131
132        # Make sure the NIC is set to the right IP before bringing up dhcpd
133        ifconfig $NIC 192.168.2.2
134
135        # Start the services
136        cmds="/etc/rc.d/nfsd /etc/rc.d/inetd /usr/local/etc/rc.d/isc-dhcpd"
137        for _sC in $cmds
138        do
139                echo -e "Starting ${_sC}...\c"
140                ${_sC} restart >/dev/null 2>/dev/null
141                if [ "$?" != "0" ] ; then
142                        echo -e "FAILED! Please run try running it manually."
143                else
144                        echo -e "OK"
145                fi
146        done
147}
148
149# Check if we need to install custom config
150check_installconfig() {
151        if [ -e "${PXEWORLD}/etc/scripts/tcslogin.sh" ] ; then return ; fi
152
153        # Lets copy over the /etc/scripts directory
154        rm -rf ${PXEWORLD}/etc/scripts >/dev/null 2>/dev/null
155        cp -r ${PROGDIR}/resources/scripts ${PXEWORLD}/etc/scripts
156
157        # Remove a few rc.d things we dont need on clients
158        rm ${PXEWORLD}/etc/rc.d/cron
159        rm ${PXEWORLD}/etc/rc.d/sendmail
160
161        # Lets copy over all the /etc/ files we need
162        cp ${PROGDIR}/resources/etc/fstab ${PXEWORLD}/etc/
163        cp ${PROGDIR}/resources/etc/gettytab ${PXEWORLD}/etc/
164        cp ${PROGDIR}/resources/etc/hosts ${PXEWORLD}/etc/
165        cp ${PROGDIR}/resources/etc/motd ${PXEWORLD}/etc/
166        cp ${PROGDIR}/resources/etc/rc.conf ${PXEWORLD}/etc/
167        cp ${PROGDIR}/resources/etc/ttys ${PXEWORLD}/etc/
168
169        # Copy over rc.d / boot / root files
170        cp ${PROGDIR}/resources/boot/beastie.4th ${PXEWORLD}/boot/
171        cp ${PROGDIR}/resources/root/dot.login ${PXEWORLD}/root/.login
172
173        # Create a few directories used on client
174        mkdir -p ${PXEWORLD}/mnt/xorg-config
175
176        # Create the diskless configuration
177        mkdir -p ${PXEWORLD}/conf/base
178        mkdir -p ${PXEWORLD}/conf/base/etc
179        mkdir -p ${PXEWORLD}/conf/base/var
180        mkdir -p ${PXEWORLD}/conf/base/root
181        echo "10m" > ${PXEWORLD}/conf/base/etc/md_size
182        echo "20m" > ${PXEWORLD}/conf/base/var/md_size
183        echo "30m" > ${PXEWORLD}/conf/base/root/md_size
184        chroot ${PXEWORLD} tar cvf conf/base/etc.cpio.gz --format cpio --gzip etc 2>/dev/null
185        chroot ${PXEWORLD} tar cvf conf/base/var.cpio.gz --exclude var/db/pkg --format cpio --gzip var 2>/dev/null
186        chroot ${PXEWORLD} tar cvf conf/base/root.cpio.gz --format cpio --gzip root 2>/dev/null
187
188        # Fix the ZFS zpool.cache
189        rmdir ${PXEWORLD}/boot/zfs 2>/dev/null
190        ln -fs /tmp ${PXEWORLD}/boot/zfs
191}
192
193# Check if we need to build the world environment
194check_worldports() {
195        if [ -e "${PXEWORLD}/usr/local/bin/xv" ] ; then return ; fi
196
197        if [ ! -d "${PXEWORLD}/usr/ports/x11/xorg" -a ! -d "/usr/ports/x11/xorg" ] ; then
198                exit_err "Missing /usr/ports/x11/xorg, please checkout ports tree to continue"
199        fi
200
201        if [ ! -d "${PXEWORLD}/usr/ports/x11/xorg" ] ; then
202                rm -rf "${PXEWORLD}/usr/ports"
203                echo "Copying /usr/ports -> ${PXEWORLD}/usr/ports"
204                cp -r /usr/ports ${PXEWORLD}/usr/ports
205        fi
206
207        # Building ports inside world
208        mount -t devfs devfs ${PXEWORLD}/dev
209        cp /etc/resolv.conf ${PXEWORLD}/etc/resolv.conf
210        echo "BATCH=yes" >> ${PXEWORLD}/etc/make.conf
211        echo '#!/bin/sh
212
213/etc/rc.d/ldconfig start
214chmod 777 /tmp
215MACHINE=i386 ; export MACHINE
216UNAME_p=i386 ; export UNAME_p
217UNAME_m=i386 ; export UNAME_m
218
219for p in $WORLDPORTS
220do
221        cd /usr/ports/$p
222        make install
223        if [ "$?" != "0" ] ; then
224                exit 1
225        fi
226done
227' > ${PXEWORLD}/.mkports.sh
228
229        chmod 755 ${PXEWORLD}/.mkports.sh
230        chroot ${PXEWORLD} /.mkports.sh
231        if [ "$?" != "0" ] ; then
232                exit_err "Failed building thinclient world ports!"
233        fi
234        rm ${PXEWORLD}/.mkports.sh
235        umount ${PXEWORLD}/dev
236
237}
238
239# Check if we need to build the world environment
240check_world() {
241  if [ -e "${PXEWORLD}/COPYRIGHT" ] ; then return ; fi
242       
243  mkdir -p "${PXEWORLD}"
244  cd "${PXEWORLD}"
245
246  # Default pcbsd.conf file
247  PCBSD_ETCCONF="/usr/local/etc/pcbsd.conf"
248
249  # Set the mirror URL
250  get_mirror
251  MIRRORURL="$VAL"
252
253  # Set the system arch type
254  if [ "$SYSTYPE" = "desktop" ] ; then
255    # If building remote X server, we don't need to run amd64
256    ARCH="i386"
257  else
258    ARCH="`uname -m`"
259  fi
260
261  local dFiles="base.txz doc.txz kernel.txz games.txz"
262  if [ "$ARCH" = "amd64" ] ; then
263     dFiles="$dFiles lib32.txz"
264  fi
265
266  # To fetch the jail environment
267  echo "Fetching FreeBSD environment. This may take a while..."
268  for i in $dFiles
269  do
270    echo "Downloading ${MIRRORURL}/${SYSVER}/${ARCH}/dist/${i} ..."
271   
272    get_file_from_mirrors "/${SYSVER}/${ARCH}/dist/${i}" "$i" "iso"
273    [ $? -ne 0 ] && exit_err "Error while downloading the freebsd world."
274  done
275
276  # Save the archive as our example world environment
277  mkdir -p ${PXEWORLD}/installarchive/
278
279  echo "Extracting FreeBSD environment... This may take a while..."
280  # Extract dist files
281  for i in $dFiles
282  do
283    tar xvpf ${i} 2>/dev/null
284    if [ $? -ne 0 ] ; then exit_err "Failed extracting FreeBSD environment"; fi
285
286    # Save the archive file
287    mv ${i} ${PXEWORLD}/installarchive/
288  done
289
290}
291
292# Function to check if dhcpd is installed
293check_dhcpd() {
294        which dhcpd >/dev/null 2>/dev/null
295        if [ "$?" = "0" ] ; then return; fi
296
297        echo "Installing $DHCPPORT"
298        pkg install -y ${DHCPPORT}
299        if [ "$?" != "0" ] ; then exit_err "Failed installing ${DHCPPORT}"; fi
300}
301
302# Function which checks and sets up the thinclient as an install server
303check_installdirs() {
304        if [ -e "${PXEWORLD}/installscripts/pc-sysinstall.example" ]; then
305           return
306        fi
307        touch ${PXEWORLD}/etc/installserver
308        mkdir ${PXEWORLD}/installscripts
309        cp ${PROGDIR}/resources/scripts/pc-sysinstall.example ${PXEWORLD}/installscripts/pc-sysinstall.example
310        echo "zfs_load=\"YES\"" > ${PXEWORLD}/boot/loader.conf
311        echo "geom_mirror_load=\"YES\"" >> ${PXEWORLD}/boot/loader.conf
312        echo "geom_eli_load=\"YES\"" >> ${PXEWORLD}/boot/loader.conf
313}
314
315do_removal() {
316        if [ -d "${PXEWORLD}" ] ; then
317                echo "Removing ${PXEWORLD}"
318                rm -rf ${PXEWORLD} 2>/dev/null
319                chflags -R noschg ${PXEWORLD} 2>/dev/null
320                rm -rf ${PXEWORLD} 2>/dev/null
321        fi
322}
323
324# Make sure we are root
325if [ `id -u` != "0" ] ; then exit_err "Must be run as root!"; fi
326
327# Check if we are removing the existing thinclient
328if [ "$1" = "-remove" -o "$1" = "remove" ] ;  then
329  do_removal
330  exit 0
331fi
332
333echo "$0 will install the components to convert this system into a thin-client server."
334echo -e "Continue? (Y/N) \c"
335read tmp
336if [ "$tmp" != "Y" -a "$tmp" != "y" ] ; then
337        exit 0
338fi
339
340echo "Do you wish to make this a remote X desktop server or install server?"
341echo -e "(r/i) \c"
342read tmp
343if [ "$tmp" = "I" -o "$tmp" = "i" ] ; then
344   SYSTYPE="install"
345else
346   SYSTYPE="desktop"
347fi
348
349# Start by installing dhcpd
350check_dhcpd
351
352# Start by setting up a new buildworld
353check_world
354
355if [ "$SYSTYPE" = "desktop" ] ; then
356  # Build the ports inside the world environment
357  check_worldports
358else
359  # Setup the installation directories
360  check_installdirs
361fi
362
363# Install the thinclient configuration files
364check_installconfig
365
366# Tweak the base system to enable the thinclient
367check_configsystem
368
369if [ "$SYSTYPE" = "desktop" ] ; then
370  echo ""
371  echo "You will now need to enable remote desktop."
372  echo "This can be done via the PC-BSD Control Panel -> GDM Configuration"
373  echo "or by manually editing /usr/local/etc/gdm/custom.conf"
374else
375  echo ""
376  echo "To perform system installations, place your custom pc-sysinstall scripts in:"
377  echo "/usr/home/thinclient/installscripts"
378  echo ""
379  echo "An example script is provided in the above directory"
380  echo ""
381  echo "For unattended installations, save your pc-sysinstall script as:"
382  echo "/usr/home/thinclient/installscripts/unattended.cfg"
383fi
384
385echo " "
386echo "Your system is now setup to do PXE booting!"
387exit 0
Note: See TracBrowser for help on using the repository browser.