xref: /dflybsd-src/share/examples/diskless/clone_root (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
186d7f5d3SJohn Marino#!/bin/sh
286d7f5d3SJohn Marino#
386d7f5d3SJohn Marino# (C) 2001 Luigi Rizzo, Gabriele Cecchetti
486d7f5d3SJohn Marino#	<Standard BSD copyright>
586d7f5d3SJohn Marino# Revised 2001.04.16
686d7f5d3SJohn Marino#
786d7f5d3SJohn Marino# $FreeBSD: src/share/examples/diskless/clone_root,v 1.1.2.4 2002/04/07 18:16:18 luigi Exp $
886d7f5d3SJohn Marino# $DragonFly: src/share/examples/diskless/clone_root,v 1.3 2006/10/24 17:09:45 swildner Exp $
986d7f5d3SJohn Marino#
1086d7f5d3SJohn Marino# clone root filesystem for diskless root stuff
1186d7f5d3SJohn Marino#
1286d7f5d3SJohn Marino# usage
1386d7f5d3SJohn Marino#	clone_root all		to do a full copy (e.g. bin, sbin...)
1486d7f5d3SJohn Marino#	clone_root update	to recreate /var (including devices)
1586d7f5d3SJohn Marino#	clone_root		to copy /conf and password-related files
1686d7f5d3SJohn Marino#
1786d7f5d3SJohn Marino# This script assumes that you use a shared readonly root and /usr
1886d7f5d3SJohn Marino# partition. The script creates a partial clone of the root partition,
1986d7f5d3SJohn Marino# and puts it into ${DEST} (defaults to /diskless_root ) on the server,
2086d7f5d3SJohn Marino# where it is read.
2186d7f5d3SJohn Marino#
2286d7f5d3SJohn Marino# To run a diskless install you need to do the following:
2386d7f5d3SJohn Marino#
2486d7f5d3SJohn Marino# create /conf/default/etc/fstab
2586d7f5d3SJohn Marino#    this will replace the standard /etc/fstab and should contain
2686d7f5d3SJohn Marino#    as a minimum the following lines
2786d7f5d3SJohn Marino#    ${SERVER}:${DEST} /     nfs    ro 0 0
2886d7f5d3SJohn Marino#    ${SERVER}:/usr    /usr  nfs    ro 0 0
2986d7f5d3SJohn Marino#    proc              /proc procfs rw 0 0
3086d7f5d3SJohn Marino#
3186d7f5d3SJohn Marino# create /conf/default/etc/rc.conf
3286d7f5d3SJohn Marino#    this will replace the standard rc.conf and should contain
3386d7f5d3SJohn Marino#    the startup options for the diskless client. Most likely
3486d7f5d3SJohn Marino#    you will not need to set hostname and ifconfig_* because these
3586d7f5d3SJohn Marino#    will be already set by the startup code. You will also
3686d7f5d3SJohn Marino#    probably need to set local_startup="" so that the server's
3786d7f5d3SJohn Marino#    local startup files will not be used.
3886d7f5d3SJohn Marino#
3986d7f5d3SJohn Marino# create a kernel config file in /sys/config/DISKLESS with
4086d7f5d3SJohn Marino#	options MFS
4186d7f5d3SJohn Marino#	options BOOTP
4286d7f5d3SJohn Marino#	options BOOTP_NFSROOT
4386d7f5d3SJohn Marino#	options BOOTP_COMPAT
4486d7f5d3SJohn Marino# and do a full build of the kernel.
4586d7f5d3SJohn Marino# If you use the firewall, remember to default to open or your kernel
4686d7f5d3SJohn Marino# will not be able to send/receive the bootp packets.
4786d7f5d3SJohn Marino#
4886d7f5d3SJohn Marino# On the server:
4986d7f5d3SJohn Marino# enable NFS server and set /etc/exports as
5086d7f5d3SJohn Marino#	${DEST}	-ro -maproot=0 -alldirs <list of diskless clients>
5186d7f5d3SJohn Marino#	/usr -ro -alldirs
5286d7f5d3SJohn Marino#
5386d7f5d3SJohn Marino# enable bootpd by uncommenting the bootps line in /etc/inetd.conf
5486d7f5d3SJohn Marino# and putting at least the following entries in /etc/bootptab:
5586d7f5d3SJohn Marino#  .default:\
5686d7f5d3SJohn Marino#	hn:ht=1:vm=rfc1048:\
5786d7f5d3SJohn Marino#	:sm=255.255.255.0:\
5886d7f5d3SJohn Marino#	:sa=${SERVER}:\
5986d7f5d3SJohn Marino#	:gw=${GATEWAY}:\
6086d7f5d3SJohn Marino#	:rp="${SERVER}:${DEST}":
6186d7f5d3SJohn Marino#
6286d7f5d3SJohn Marino#  client1:ha=0123456789ab:tc=.default
6386d7f5d3SJohn Marino#
6486d7f5d3SJohn Marino# and make sure that client1 is listed in /etc/hosts
6586d7f5d3SJohn Marino
6686d7f5d3SJohn Marino# VARIABLES:
6786d7f5d3SJohn Marino#	some manual init is needed here.
6886d7f5d3SJohn Marino# DEST	the diskless_root dir (goes into /etc/bootptab and /etc/exports
6986d7f5d3SJohn Marino#	on the server)
7086d7f5d3SJohn Marino# DEVICES	device entries to create in /dev
7186d7f5d3SJohn MarinoDEST=/diskless_root
7286d7f5d3SJohn MarinoDEVICES="all snd1 bktr0"
7386d7f5d3SJohn Marino
7486d7f5d3SJohn Marino# you should not touch these vars:
7586d7f5d3SJohn Marino# SYSDIRS	system directories and mountpoints
7686d7f5d3SJohn Marino# DIRS		mountpoints (empty dirs)
7786d7f5d3SJohn Marino# PWFILES	files related to passwords
7886d7f5d3SJohn Marino# TOCOPY	files and dirs to copy from root partition
7986d7f5d3SJohn Marino
8086d7f5d3SJohn MarinoSYSDIRS="dev proc root usr var"
8186d7f5d3SJohn MarinoDIRS="cdrom home mnt"
8286d7f5d3SJohn MarinoPWFILES="master.passwd passwd spwd.db pwd.db"
8386d7f5d3SJohn MarinoTOCOPY="bin boot compat etc modules sbin stand sys"
8486d7f5d3SJohn Marino
8586d7f5d3SJohn Marinoinit_diskless_root() {
8686d7f5d3SJohn Marino	echo "Cleaning old diskless root ($DEST)"
8786d7f5d3SJohn Marino	cd /
8886d7f5d3SJohn Marino	rm -rf ${DEST} && echo "Old diskless root removed."
8986d7f5d3SJohn Marino	echo "Creating $DEST..."
9086d7f5d3SJohn Marino	mkdir -p $DEST && echo "New diskless root created."
9186d7f5d3SJohn Marino	echo "+++ Now copy original tree from / ..."
9286d7f5d3SJohn Marino	ex=""
9386d7f5d3SJohn Marino	(cd / ; tar -clf - ${TOCOPY} ) | (cd $DEST; tar xvf - )
9486d7f5d3SJohn Marino	#(cd / ; find -x dev | cpio -o -H newc ) | \
9586d7f5d3SJohn Marino	#	(cd $DEST; cpio -i -H newc -d )
9686d7f5d3SJohn Marino	echo "+++ Fixing permissions on some objects"
9786d7f5d3SJohn Marino	chmod 555 $DEST/sbin/init
9886d7f5d3SJohn Marino}
9986d7f5d3SJohn Marino
10086d7f5d3SJohn Marinoupdate_conf_and_pw() {
10186d7f5d3SJohn Marino	echo "+++ Copying files in /conf and password files"
10286d7f5d3SJohn Marino	(cd ${DEST} ; rm -rf conf )
10386d7f5d3SJohn Marino	(cd / ; tar clf - conf ) | (cd ${DEST}; tar xvf - )
10486d7f5d3SJohn Marino	mkdir -p ${DEST}/conf/base	# original copy of /etc
10586d7f5d3SJohn Marino	(cd / ; tar clf - etc ) | (cd ${DEST}/conf/base && tar xvf - )
10686d7f5d3SJohn Marino	mkdir -p ${DEST}/conf/etc	# used to mount things
10786d7f5d3SJohn Marino	(cd /etc ; tar cvf - ${PWFILES} ) | (cd ${DEST}/etc ; tar xf - )
10886d7f5d3SJohn Marino	(cd ${DEST}/conf/base ; find etc | cpio --create -H newc | \
10986d7f5d3SJohn Marino		gzip > ${DEST}/conf/base/etc.cpio.gz )
11086d7f5d3SJohn Marino	(cd ${DEST} ; find dev | cpio --create -H newc | \
11186d7f5d3SJohn Marino		gzip > ${DEST}/conf/dev.cpio.gz )
11286d7f5d3SJohn Marino}
11386d7f5d3SJohn Marino
11486d7f5d3SJohn Marinoupdate() {
11586d7f5d3SJohn Marino	echo "+++ update: create mountpoints and device entries, kernel"
11686d7f5d3SJohn Marino	for i in ${SYSDIRS} ${DIRS}
11786d7f5d3SJohn Marino	do
11886d7f5d3SJohn Marino	    rm -r -f ${DEST}/$i
11986d7f5d3SJohn Marino	    mkdir -p ${DEST}/$i && chown root:wheel ${DEST}/$i && echo -n "$i "
12086d7f5d3SJohn Marino	done
12186d7f5d3SJohn Marino	echo "."
12286d7f5d3SJohn Marino	ln -s /var/tmp ${DEST}/tmp
12386d7f5d3SJohn Marino	echo "+++ Now use MAKEDEV to create devices ${DEVICES}"
12486d7f5d3SJohn Marino	(cd $DEST/dev ; cp /dev/MAKEDEV . )
12586d7f5d3SJohn Marino	(cd $DEST/dev ; /dev/MAKEDEV ${DEVICES} )
12686d7f5d3SJohn Marino	(cd $DEST/dev ; ln -s /dev/sysmouse mouse )
12786d7f5d3SJohn Marino	echo "+++ Copying kernel from /sys/compile/DISKLESS"
12886d7f5d3SJohn Marino	cp /sys/compile/DISKLESS/kernel $DEST/kernel
12986d7f5d3SJohn Marino	echo "."
13086d7f5d3SJohn Marino}
13186d7f5d3SJohn Marino
13286d7f5d3SJohn Marino
13386d7f5d3SJohn Marino# Main entry point
13486d7f5d3SJohn Marinocase $1 in
13586d7f5d3SJohn Marino	all)	# clean and reinstall the whole diskless_root
13686d7f5d3SJohn Marino		init_diskless_root
13786d7f5d3SJohn Marino		update
13886d7f5d3SJohn Marino		update_conf_and_pw
13986d7f5d3SJohn Marino		;;
14086d7f5d3SJohn Marino
14186d7f5d3SJohn Marino	update)	# clean and rebuild mountpoints and device entries
14286d7f5d3SJohn Marino		update
14386d7f5d3SJohn Marino		update_conf_and_pw
14486d7f5d3SJohn Marino		;;
14586d7f5d3SJohn Marino
14686d7f5d3SJohn Marino	*)	# copy /conf and password files
14786d7f5d3SJohn Marino		update_conf_and_pw
14886d7f5d3SJohn Marino		;;
14986d7f5d3SJohn Marinoesac
15086d7f5d3SJohn Marinoexit 0
15186d7f5d3SJohn Marino### end of file ###
152