aboutsummaryrefslogtreecommitdiff
blob: e09e0c6fc6785c363026315439d94a9ef31b4701 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# Copyright 1999-2008 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

DESCRIPTION="This is a meta ebuild that pulls in the packages and some config scripts/files for building the Gentoo Clustering Master node"
HOMEPAGE="http://git.overlays.gentoo.org/gitweb/"
SRC_URI=""

LICENSE="GPL-2"
SLOT="0"
KEYWORDS="amd64 ~x86"
IUSE="autofs nonfsv4 beep pbs"

DEPEND=""
RDEPEND="net-nds/ldap-auth
	sys-boot/syslinux
	net-dns/dnsmasq
	net-misc/sipcalc
	net-fs/nfs-utils
	sys-cluster/c3
	net-dns/openresolv
	beep? (app-misc/beep)
	pbs? (sys-cluster/torque)"

CONFPATH="/etc/gentoo"
CONFIG_FILE="cluster.conf"
MODULESPATH="/usr/libexec/clustering/"
DNSMASQSCRIPT="/usr/sbin/node-manager"

inherit confutils

get_ip_config()
{
	CLUSTER_ETH_IP="$(sipcalc ${CLUSTER_ETH} | egrep 'Host address\W*-' | cut -d- -f2)"
	IPSTART="${CLUSTER_ETH_IP%.*}.$(( ${CLUSTER_ETH_IP##*.} +1 ))"
	IPSTOP="   $(sipcalc ${CLUSTER_ETH} | egrep 'Usable range\W*-'    | cut -d- -f3)"
	SUBNET="   $(sipcalc ${CLUSTER_ETH} | egrep 'Network mask\W*-'    | cut -d- -f2)"
	NETADDR="  $(sipcalc ${CLUSTER_ETH} | egrep 'Network address'     | cut -d- -f2)"
	NODECOUNT="$(sipcalc ${CLUSTER_ETH} | egrep 'Addresses in network'| cut -d- -f2)"

	# We have to clean up the vars from spaces:
	CLUSTER_ETH_IP=${CLUSTER_ETH_IP// }
	IPSTART=${IPSTART// }
	IPSTOP=${IPSTOP// }
	SUBNET=${SUBNET// }
	NETADDR=${NETADDR// }
	NODECOUNT=$(($NODECOUNT-2)) # removing server host + broadcast
}

dnsmasq_conf()
{
# The following (commneted) would be much cleaner but this can't work in the livecd environment since the portage tree doesn't exist
# and the template file is thus non-existant (${FILESDIR}/...) .
#	cp ${FILESDIR}/dnsmasq-2.4-conf ${ROOT}/etc/dnsmasq.conf
#
#	for I in PUBETH LOCALDOMAINNAME TFTPROOT IPSTART IPSTOP SUBNET CLUSTER_ETH_IP PXELINUX HOSTNAME NFSROOT NODECOUNT DNSMASQSCRIPT
#	do
#		sed -i -e"s:%%$I:${!I}:g" ${ROOT}/etc/dnsmasq.conf
#	done
#	cp 

	cat > ${ROOT}/etc/dnsmasq.conf <<-EOF	
	# For the sake of non-duplication of documentation, please view the dnsmasq manpage for a description of these options.
	# Public interface
	except-interface=$PUBETH
	
	# We are the authoritative DHCP server for this network
	dhcp-authoritative

	# Set up the local-only domain
	local=/${LOCALDOMAINNAME}/127.0.0.1
	domain=$LOCALDOMAINNAME
	expand-hosts

	# We need the following to retain dhcp served resolution info:
	resolv-file=/etc/dnsmasq-resolv.conf

	dhcp-script=$DNSMASQSCRIPT

	# Some netiquette:
	domain-needed
	bogus-priv
	
	enable-tftp
	tftp-root=${TFTPROOT}

	# Send RFC-3397 DNS domain search DHCP option. WARNING: Your DHCP client
	# probably doesn't support this...... (option 119)
	dhcp-option=119,$LOCALDOMAINNAME
	# Domain DNS name
	dhcp-option=15,$LOCALDOMAINNAME

	# set the master node as the NTP server
	dhcp-option=option:ntp-server,0.0.0.0

	### Node definitions, this could be in a separate file
	# Turn on the DHCP server, pass out addresses from $IPSTART to $IPSTOP
	# with subnet mask of $SUBNET with a 12-hour lease
	dhcp-range=nodes,$IPSTART,$IPSTOP,$SUBNET,12h

	# Override the default route supplied by dnsmasq, which assumes the
	# router is the same machine as the one running dnsmasq.
	dhcp-option=3,$CLUSTER_ETH_IP

	# This tells the clients the hostname and IP of the TFTP server
	dhcp-boot=${PXELINUX},$(hostname),$CLUSTER_ETH_IP

	# address and root path of NFS server
	dhcp-option=17,$CLUSTER_ETH_IP:${NFSROOT}
	
	# The default is 150, which is quite low if one wants a cluster with more than 150 nodes.
	dhcp-lease-max=$NODECOUNT

	EOF
}

pxe_conf()
{
	PROFNAME="$(uname -m)_node"
	BOOTPATH="${NFSROOT##$TFTPROOT}"
	BOOTPATH="${BOOTPATH#/}/boot"

	mkdir -p ${TFTPROOT}/pxelinux.cfg

	cat > ${TFTPROOT}/pxelinux.cfg/default <<-EOF
	#prompt 1
	#timeout 50
	#say Press F1 for boot profiles, default is $PROFNAME in 5 seconds...
	F1 BootProfiles
	default $PROFNAME

	label $PROFNAME
	  kernel ${BOOTPATH}/kernel
	#important: AuFS only supports NFSv3
	  append ip=dhcp nfsroot=${CLUSTER_ETH_IP}:${NFSROOT},hard,intr,nfsvers=3 init=/boot/stateless.sh unionmod=aufs rwdev=tmpfs

	label local
	  localboot 0
	EOF

	cat > ${TFTPROOT}/BootProfiles <<-EOF
	
	Type $PROFNAME for regular bootup (default after 5 seconds)
	Type local to boot on the local HDD
	EOF

	ln -s "${ROOT}/usr/lib/syslinux/pxelinux.0" "${TFTPROOT}/pxelinux.0"
}

nfs_root_conf()
{
	[ -d ${NFSROOT} ] || die "${NFSROOT} is missing! Either create your own of find one ;)"
	if [ ! -f ${NFSROOT}/boot/kernel ]; then
		ewarn "Couldn't find ${NFSROOT}/boot/kernel !"
		ewarn "This is what we built the config files to expect."
		ewarn "You might have to perform"
	fi

	echo "${NFSROOT} $NETADDR/$SUBNET(ro,$NFSEPORTOPTS,fsid=0)" > ${ROOT}/etc/exports

	# Yes, this could be looped on RO and RW...but that wouldn't be more redable
	for I in $ROEXPORTS
	do
		if use nonfsv4 ; then
			echo "${I} $NETADDR/$SUBNET(ro,$NFSEPORTOPTS)" >> ${ROOT}/etc/exports
		else
			mkdir -p ${NFSROOT}/${I}
			mount -o bind ${I} ${NFSROOT}/${I}
		fi
		use autofs && echo "${I/\/} -ro,$NFSMOUNTOPTS $CLUSTER_ETH_Ir:${I}" >> ${NFSROOT}/etc/autofs/auto.nfs
	done

	for I in $RWEXPORTS
	do
		if use nonfsv4 ; then
			echo "${I} $NETADDR/$SUBNET(rw,$NFSEPORTOPTS)" >> ${ROOT}/etc/exports
		else
			mkdir -p ${NFSROOT}/${I}
			mount -o bind ${I} ${NFSROOT}/${I}
		fi
		use autofs && echo "${I/\/} -rw,$NFSMOUNTOPTS $CLUSTER_ETH_Ir:${I}" >> ${NFSROOT}/etc/autofs/auto.nfs
	done

}

resolv_conf()
{
	cat >> /etc/resolvconf/resolv.conf.d/base <<-EOF
	search $LOCALDOMAINNAME
	nameserver 127.0.0.1
	EOF
}

parse_config()
{
	[[ -f ${1} ]] || die "${1} missing!!! re-emerge ${PF}"
	. ${1}
	[[ $CONFIG_OK != "yes" ]] && die "You need to edit ${CONFIG_FILE} and set CONFIG_OK=\"yes\""
}

pkg_setup() {
	if ! built_with_use net-dns/dnsmasq tftp ; then
		die "net-dns/dnsmasq must be built with USE=tftp to be used with ${P}!"
	fi
	use autofs && die "We're not there yet ;)"
}

src_unpack(){
	mkdir -p ${S}

	cp ${FILESDIR}/node-manager ${S}/node-manager || die "Som ting Wong"
	sed -e "s:%%MODULESPATH:$MODULESPATH:" -i ${S}/node-manager 
	if use beep ; then
		sed -e "s:%%BEEPS:true:" -i ${S}/node-manager
	else
		sed -e "s:%%BEEPS:false:" -i ${S}/node-manager
	fi

	if use pbs ; then
		cp ${FILESDIR}/torque-* ${S}/
		sed -e "s:%%CONFPATH/%%CONFIG_FILE:$CONFPATH/$CONFIG_FILE:" -i ${S}/torque-add
	fi

}

src_install(){
	dodir   ${CONFPATH}
	insinto ${CONFPATH}
	doins   ${FILESDIR}/${CONFIG_FILE}
	
	dosbin ${S}/node-manager

	# add and delnode are derived from the dnsmasq add/del/old directives
	# passed onto the node-manager script...don't change arbritrarily
	dodir   ${MODULESPATH}
	exeinto ${MODULESPATH}
	doexe   ${FILESDIR}/c3-* 
	use pbs && doexe ${S}/torque-* 

	elog "You need to edit ${CONFPATH}/${CONFIG_FILE} to your likings"
	elog "Once you are done, set CONFIG_OK=\"yes\" in there and"
	elog "call emerge --config =${CATEGORY}/${PF}"
}

pkg_config()
{
	parse_config "${CONFPATH}/${CONFIG_FILE}"
	get_ip_config
	dnsmasq_conf
	resolv_conf
	pxe_conf
	nfs_root_conf
	
	use pbs && TORQUED="pbs_server pbs_sched" 
	for I in dnsmasq netmount nfs $TORQUED
	do
		rc-update add $I default
		/etc/init.d/$I start
	done
	
	elog "Since we just changed a resolvconf file, "
	elog "it is required that your main NIC interface be restarted"
	elog "or at least a renewal of dhcp be triggered."
	elog 'ie: with dhcpcd one calls `dhcpcd -n eth0`'
	elog "You will then want to restart dnsmasq."
}