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

9.1-release9.2-releasereleng/10.0releng/10.0.1
Last change on this file since 1620346 was 1620346, checked in by Kris Moore <kris@…>, 12 months ago

Initial import of PC-BSD /current/ SVN repo

  • 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="net/isc-dhcp42-server"
37PXEWORLD="/usr/home/thinclient"
38WORLDPORTS="x11/xorg graphics/xv"
39export WORLDPORTS
40SYSVER="`uname -r`"
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}
189
190# Check if we need to build the world environment
191check_worldports() {
192        if [ -e "${PXEWORLD}/usr/local/bin/xv" ] ; then return ; fi
193
194        if [ ! -d "${PXEWORLD}/usr/ports/x11/xorg" -a ! -d "/usr/ports/x11/xorg" ] ; then
195                exit_err "Missing /usr/ports/x11/xorg, please checkout ports tree to continue"
196        fi
197
198        if [ ! -d "${PXEWORLD}/usr/ports/x11/xorg" ] ; then
199                rm -rf "${PXEWORLD}/usr/ports"
200                echo "Copying /usr/ports -> ${PXEWORLD}/usr/ports"
201                cp -r /usr/ports ${PXEWORLD}/usr/ports
202        fi
203
204        # Building ports inside world
205        mount -t devfs devfs ${PXEWORLD}/dev
206        cp /etc/resolv.conf ${PXEWORLD}/etc/resolv.conf
207        echo "BATCH=yes" >> ${PXEWORLD}/etc/make.conf
208        echo '#!/bin/sh
209
210/etc/rc.d/ldconfig start
211chmod 777 /tmp
212MACHINE=i386 ; export MACHINE
213UNAME_p=i386 ; export UNAME_p
214UNAME_m=i386 ; export UNAME_m
215
216for p in $WORLDPORTS
217do
218        cd /usr/ports/$p
219        make install
220        if [ "$?" != "0" ] ; then
221                exit 1
222        fi
223done
224' > ${PXEWORLD}/.mkports.sh
225
226        chmod 755 ${PXEWORLD}/.mkports.sh
227        chroot ${PXEWORLD} /.mkports.sh
228        if [ "$?" != "0" ] ; then
229                exit_err "Failed building thinclient world ports!"
230        fi
231        rm ${PXEWORLD}/.mkports.sh
232        umount ${PXEWORLD}/dev
233
234}
235
236# Check if we need to build the world environment
237check_world() {
238  if [ -e "${PXEWORLD}/COPYRIGHT" ] ; then return ; fi
239       
240  mkdir -p "${PXEWORLD}"
241  cd "${PXEWORLD}"
242
243  # Default pcbsd.conf file
244  PCBSD_ETCCONF="/usr/local/etc/pcbsd.conf"
245
246  # Set the mirror URL
247  get_mirror
248  MIRRORURL="$VAL"
249
250  # Set the system arch type
251  if [ "$SYSTYPE" = "desktop" ] ; then
252    # If building remote X server, we don't need to run amd64
253    ARCH="i386"
254  else
255    ARCH="`uname -m`"
256  fi
257
258  local dFiles="base.txz doc.txz kernel.txz games.txz"
259  if [ "$ARCH" = "amd64" ] ; then
260     dFiles="$dFiles lib32.txz"
261  fi
262
263  # To fetch the jail environment
264  echo "Fetching FreeBSD environment. This may take a while..."
265  for i in $dFiles
266  do
267    echo "Downloading ${MIRRORURL}/${SYSVER}/${ARCH}/dist/${i} ..."
268   
269    get_file_from_mirrors "/${SYSVER}/${ARCH}/dist/${i}" "$i"
270    [ $? -ne 0 ] && exit_err "Error while downloading the freebsd world."
271  done
272
273  # Save the archive as our example world environment
274  mkdir -p ${PXEWORLD}/installarchive/
275
276  echo "Extracting FreeBSD environment... This may take a while..."
277  # Extract dist files
278  for i in $dFiles
279  do
280    tar xvpf ${i} 2>/dev/null
281    if [ $? -ne 0 ] ; then exit_err "Failed extracting FreeBSD environment"; fi
282
283    # Save the archive file
284    mv ${i} ${PXEWORLD}/installarchive/
285  done
286
287}
288
289# Function to check if dhcpd is installed
290check_dhcpd() {
291        which dhcpd >/dev/null 2>/dev/null
292        if [ "$?" = "0" ] ; then return; fi
293
294        if [ ! -d "/usr/ports/$DHCPPORT" ] ; then
295                exit_err "Missing /usr/ports/$DHCPPORT, please fetch the FreeBSD ports tree or install dhcp-server."
296        fi
297
298        echo "Installing $DHCPPORT"
299        cd /usr/ports/${DHCPPORT}
300        make install
301        if [ "$?" != "0" ] ; then exit_err "Failed installing ${DHCPPORT}"; fi
302}
303
304# Function which checks and sets up the thinclient as an install server
305check_installdirs() {
306        if [ -e "${PXEWORLD}/installscripts/pc-sysinstall.example" ]; then
307           return
308        fi
309        touch ${PXEWORLD}/etc/installserver
310        mkdir ${PXEWORLD}/installscripts
311        cp ${PROGDIR}/resources/scripts/pc-sysinstall.example ${PXEWORLD}/installscripts/pc-sysinstall.example
312        echo "zfs_load=\"YES\"" > ${PXEWORLD}/boot/loader.conf
313        echo "geom_mirror_load=\"YES\"" >> ${PXEWORLD}/boot/loader.conf
314        echo "geom_eli_load=\"YES\"" >> ${PXEWORLD}/boot/loader.conf
315}
316
317do_removal() {
318        if [ -d "${PXEWORLD}" ] ; then
319                echo "Removing ${PXEWORLD}"
320                rm -rf ${PXEWORLD} 2>/dev/null
321                chflags -R noschg ${PXEWORLD} 2>/dev/null
322                rm -rf ${PXEWORLD} 2>/dev/null
323        fi
324}
325
326# Make sure we are root
327if [ `id -u` != "0" ] ; then exit_err "Must be run as root!"; fi
328
329# Check if we are removing the existing thinclient
330if [ "$1" = "-remove" -o "$1" = "remove" ] ;  then
331  do_removal
332  exit 0
333fi
334
335echo "$0 will install the components to convert this system into a thin-client server."
336echo -e "Continue? (Y/N) \c"
337read tmp
338if [ "$tmp" != "Y" -a "$tmp" != "y" ] ; then
339        exit 0
340fi
341
342echo "Do you wish to make this a remote X desktop server or install server?"
343echo -e "(r/i) \c"
344read tmp
345if [ "$tmp" = "I" -o "$tmp" = "i" ] ; then
346   SYSTYPE="install"
347else
348   SYSTYPE="desktop"
349fi
350
351# Start by installing dhcpd
352check_dhcpd
353
354# Start by setting up a new buildworld
355check_world
356
357if [ "$SYSTYPE" = "desktop" ] ; then
358  # Build the ports inside the world environment
359  check_worldports
360else
361  # Setup the installation directories
362  check_installdirs
363fi
364
365# Install the thinclient configuration files
366check_installconfig
367
368# Tweak the base system to enable the thinclient
369check_configsystem
370
371if [ "$SYSTYPE" = "desktop" ] ; then
372  echo ""
373  echo "You will now need to enable remote desktop."
374  echo "This can be done via the PC-BSD Control Panel -> GDM Configuration"
375  echo "or by manually editing /usr/local/etc/gdm/custom.conf"
376else
377  echo ""
378  echo "To perform system installations, place your custom pc-sysinstall scripts in:"
379  echo "/usr/home/thinclient/installscripts"
380  echo ""
381  echo "An example script is provided in the above directory"
382  echo ""
383  echo "For unattended installations, save your pc-sysinstall script as:"
384  echo "/usr/home/thinclient/installscripts/unattended.cfg"
385fi
386
387echo " "
388echo "Your system is now setup to do PXE booting!"
389exit 0
Note: See TracBrowser for help on using the repository browser.