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