1#!/bin/sh
2#
3# (C) 2001 Luigi Rizzo, Gabriele Cecchetti
4#	<Standard BSD copyright>
5# Revised 2001.04.16
6#
7# $FreeBSD: src/share/examples/diskless/clone_root,v 1.1.2.4 2002/04/07 18:16:18 luigi Exp $
8# $DragonFly: src/share/examples/diskless/clone_root,v 1.3 2006/10/24 17:09:45 swildner Exp $
9#
10# clone root filesystem for diskless root stuff
11#
12# usage
13#	clone_root all		to do a full copy (e.g. bin, sbin...)
14#	clone_root update	to recreate /var (including devices)
15#	clone_root		to copy /conf and password-related files
16#
17# This script assumes that you use a shared readonly root and /usr
18# partition. The script creates a partial clone of the root partition,
19# and puts it into ${DEST} (defaults to /diskless_root ) on the server,
20# where it is read.
21#
22# To run a diskless install you need to do the following:
23#
24# create /conf/default/etc/fstab
25#    this will replace the standard /etc/fstab and should contain
26#    as a minimum the following lines
27#    ${SERVER}:${DEST} /     nfs    ro 0 0
28#    ${SERVER}:/usr    /usr  nfs    ro 0 0
29#    proc              /proc procfs rw 0 0
30#
31# create /conf/default/etc/rc.conf
32#    this will replace the standard rc.conf and should contain
33#    the startup options for the diskless client. Most likely
34#    you will not need to set hostname and ifconfig_* because these
35#    will be already set by the startup code. You will also
36#    probably need to set local_startup="" so that the server's
37#    local startup files will not be used.
38#
39# create a kernel config file in /sys/config/DISKLESS with
40#	options MFS
41#	options BOOTP
42#	options BOOTP_NFSROOT
43#	options BOOTP_COMPAT
44# and do a full build of the kernel.
45# If you use the firewall, remember to default to open or your kernel
46# will not be able to send/receive the bootp packets.
47#
48# On the server:
49# enable NFS server and set /etc/exports as
50#	${DEST}	-ro -maproot=0 -alldirs <list of diskless clients>
51#	/usr -ro -alldirs
52#
53# enable bootpd by uncommenting the bootps line in /etc/inetd.conf
54# and putting at least the following entries in /etc/bootptab:
55#  .default:\
56#	hn:ht=1:vm=rfc1048:\
57#	:sm=255.255.255.0:\
58#	:sa=${SERVER}:\
59#	:gw=${GATEWAY}:\
60#	:rp="${SERVER}:${DEST}":
61#
62#  client1:ha=0123456789ab:tc=.default
63#
64# and make sure that client1 is listed in /etc/hosts
65
66# VARIABLES:
67#	some manual init is needed here.
68# DEST	the diskless_root dir (goes into /etc/bootptab and /etc/exports
69#	on the server)
70# DEVICES	device entries to create in /dev
71DEST=/diskless_root
72DEVICES="all snd1 bktr0"
73
74# you should not touch these vars:
75# SYSDIRS	system directories and mountpoints
76# DIRS		mountpoints (empty dirs)
77# PWFILES	files related to passwords
78# TOCOPY	files and dirs to copy from root partition
79
80SYSDIRS="dev proc root usr var"
81DIRS="cdrom home mnt"
82PWFILES="master.passwd passwd spwd.db pwd.db"
83TOCOPY="bin boot compat etc modules sbin stand sys"
84
85init_diskless_root() {
86	echo "Cleaning old diskless root ($DEST)"
87	cd /
88	rm -rf ${DEST} && echo "Old diskless root removed."
89	echo "Creating $DEST..."
90	mkdir -p $DEST && echo "New diskless root created."
91	echo "+++ Now copy original tree from / ..."
92	ex=""
93	(cd / ; tar -clf - ${TOCOPY} ) | (cd $DEST; tar xvf - )
94	#(cd / ; find -x dev | cpio -o -H newc ) | \
95	#	(cd $DEST; cpio -i -H newc -d )
96	echo "+++ Fixing permissions on some objects"
97	chmod 555 $DEST/sbin/init
98}
99
100update_conf_and_pw() {
101	echo "+++ Copying files in /conf and password files"
102	(cd ${DEST} ; rm -rf conf )
103	(cd / ; tar clf - conf ) | (cd ${DEST}; tar xvf - )
104	mkdir -p ${DEST}/conf/base	# original copy of /etc
105	(cd / ; tar clf - etc ) | (cd ${DEST}/conf/base && tar xvf - )
106	mkdir -p ${DEST}/conf/etc	# used to mount things
107	(cd /etc ; tar cvf - ${PWFILES} ) | (cd ${DEST}/etc ; tar xf - )
108	(cd ${DEST}/conf/base ; find etc | cpio --create -H newc | \
109		gzip > ${DEST}/conf/base/etc.cpio.gz )
110	(cd ${DEST} ; find dev | cpio --create -H newc | \
111		gzip > ${DEST}/conf/dev.cpio.gz )
112}
113
114update() {
115	echo "+++ update: create mountpoints and device entries, kernel"
116	for i in ${SYSDIRS} ${DIRS}
117	do
118	    rm -r -f ${DEST}/$i
119	    mkdir -p ${DEST}/$i && chown root:wheel ${DEST}/$i && echo -n "$i "
120	done
121	echo "."
122	ln -s /var/tmp ${DEST}/tmp
123	echo "+++ Now use MAKEDEV to create devices ${DEVICES}"
124	(cd $DEST/dev ; cp /dev/MAKEDEV . )
125	(cd $DEST/dev ; /dev/MAKEDEV ${DEVICES} )
126	(cd $DEST/dev ; ln -s /dev/sysmouse mouse )
127	echo "+++ Copying kernel from /sys/compile/DISKLESS"
128	cp /sys/compile/DISKLESS/kernel $DEST/kernel
129	echo "."
130}
131
132
133# Main entry point
134case $1 in
135	all)	# clean and reinstall the whole diskless_root
136		init_diskless_root
137		update
138		update_conf_and_pw
139		;;
140
141	update)	# clean and rebuild mountpoints and device entries
142		update
143		update_conf_and_pw
144		;;
145
146	*)	# copy /conf and password files
147		update_conf_and_pw
148		;;
149esac
150exit 0
151### end of file ###
152