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