aboutsummaryrefslogtreecommitdiff
blob: a24b77f91334130a760d169af8c61bbf1532bb30 (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
# 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"

DEPEND=""
RDEPEND="net-nds/ldap-auth
	sys-boot/syslinux
	net-dns/dnsmasq
	net-misc/sipcalc
	net-fs/nfs-utils
	sys-cluster/c3"

CONFPATH="/etc/gentoo"
CONFIG_FILE="cluster.conf"

inherit confutils

get_ip_config()
{
	CLUSTER_ETH_IP="$(sipcalc ${CLUSTER_ETH} | egrep 'Host address\W*-' | cut -d- -f2)"
	RANGE_START="$(sipcalc ${CLUSTER_ETH} | egrep 'Host address\W*-' | cut -d- -f2)"
	IPSTART="${RANGE_START%.*}.$(( ${RANGE_START##*.} +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)"
	# We have to clean up the vars from spaces:
	CLUSTER_ETH_IP=${CLUSTER_ETH_IP// }
	RANGE_START=${RANGE_START// }
	IPSTART=${IPSTART// }
	IPSTOP=${IPSTOP// }
	SUBNET=${SUBNET// }
	NETADDR=${NETADDR// }
}

dnsmasq_conf()
{
	cat > ${ROOT}/etc/dnsmasq.conf <<-EOF	

	# Public interface
	except-interface=$PUBETH

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

	# Some netiquette:
	domain-needed
	bogus-priv
	
	# 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

	enable-tftp
	tftp-root=${TFTPROOT}

	### 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

	# We are the authoritative DHCP server for this network
	dhcp-authoritative

	# address and root path of NFS server
	dhcp-option=17,$CLUSTER_ETH_IP:${NFSROOT}
	
	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

	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 nonfs4 ; 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 nonfs4 ; 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/\/} -ro,$NFSMOUNTOPTS $CLUSTER_ETH_Ir:${I}" >> ${NFSROOT}/etc/autofs/auto.nfs
	done

}

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_install()
{
	dodir ${CONFPATH}
	insinto ${CONFPATH}
	doins ${FILESDIR}/${CONFIG_FILE}

	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
	pxe_conf
	nfs_root_conf
	
	for I in dnsmasq netmount nfs
	do
		rc-update add $I default
		/etc/init.d/$I start
	done
}