summaryrefslogtreecommitdiffstats
path: root/mltk
blob: d705df509d23284d4848a7ae181fa47027077cf0 (plain) (blame)
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/bin/bash
# -----------------------------------------------------------------------------
# 
# Copyright (c) 2011 - OpenSLX GmbH
#
# This program is free software distributed under the GPL version 2.
# See http://openslx.org/COPYING
#
# If you have any feedback please consult http://openslx.org/feedback and
# send your suggestions, praise, or complaints to feedback@openslx.org
#
# General information about OpenSLX can be found at http://openslx.org/
# -----------------------------------------------------------------------------
#				
#				Mini-Linux Toolkit
#				
# -----------------------------------------------------------------------------

SELF="$(readlink -f $0)"
ROOT_DIR="$(dirname "${SELF}")"
MLTK_PID="$$"

# Make apt-get install non-interactive when it comes to postinstall questions
# ie. kdm asking which dm you want to set as default
export DEBIAN_FRONTEND=noninteractive

qnd_exit() {
	unset_quiet
	kill "$MLTK_PID"
	[ $# -ge 1 ] && kill "$1"
	exit 1
}

# source all helper functions files that are found under helper/*.inc
for HELPER in $ROOT_DIR/helper/*.inc; do
	. "$HELPER" && continue
	unset_quiet
	echo "Could not source $HELPER"
	qnd_exit
done

banner () {
	echo -e "\033[38;5;202m\t           __   __   __    "
	echo -e "\033[38;5;202m\t.--------.|  | |  |_|  |--."
	echo -e "\033[38;5;208m\t|        ||  |_|   _|    < "
	echo -e "\033[38;5;214m\t|__|__|__||____|____|__|__|"
	echo -e "\033[38;5;214m\t "
	echo -e "\033[38;5;220m\t                     ** OpenSLX Project // 2013 **"
	echo -e "\033[38;5;226m\t                        http://lab.openslx.org/"
	echo -e "\033[0m"
}

print_usage() {
	echo -e ""
	echo -e "Toolkit for creating preboot mini-linux for OpenSLX NG (mltk)"
	echo -e "Usage: $(basename ${SELF}) remote <target> [-d] [-c [module]*] [-b [module]*] [-p profile]"
	echo -e "       $(basename ${SELF}) server <remotehost> [-s] [<target>] [-e stage31|stage32|addons] [-d] [-c]"
	echo -e ""
	echo -e "  Mode:"
	echo -e "     server \t server mode: packs stage3.1, stage3.2 or addons as initramfs/squashfs."
	echo -e "     remote \t remote mode: builds a minimal rootfs based on the activated modules in <target>"
	echo -e ""
	echo -e "  Mode options:"
	echo -e "     -b            remote: build module(s)"
	echo -e "     -e type       server: export target as stage31, stage32 or addons"
	echo -e "     -s            server: sync 'builds' directory of remote host"
	echo -e "     -c            remote: clean module(s). / server: clean target in remote_builds/ and corresponding files under boot/"
	echo -e "     -d            activates debug output for current target/build"
	echo -e "     -p profile    remote: build all modules from given profile"
	echo -e ""
	echo -e "  In mode 'remote', you can pass names of specific modules to clean/build."
	echo -e "  Otherwise, all modules will be built/cleaned."
	echo -e ""
	echo -e "  Examples:"
	echo -e "     remote stage31 -c -b   				(clean all modules and build all linked modules in remote/targets/stage31 to remote/builds/stage31)"
	echo -e "     remote stage32 -c -b rootfs_stage31 sshd   	(clean all modules, build base, policykit and sshd in remote/builds/stage32)"
	echo -e "     remote stage32 -c base sshd -b sshd ldm -d   	(clean base and sshd, build sshd and ldm, be verbose)"
	echo -e "     server 1.2.3.4 -s					(sync all builds from remote system 1.2.3.4)"
	echo -e "     server 1.2.3.4 stage32 -e stage32			(pack stage32 as squashfs+initramfs from remote system 1.2.3.4)"
	echo -e "     server 1.2.3.4 stage31 -c				(clean stage31 build under server/remote_builds and initramfs under server/boot)"
	echo -e "     server 1.2.3.4 addons-eexam -e addons		(pack eexam-addons as squashfs)"
	echo -e ""
	echo -e "  Existing targets for remote are:"
	echo -e "    $(echo $(ls ${ROOT_DIR}/remote/targets 2>/dev/null || echo "No targets for remote found."))"
	echo -e "  Existing targets for server are:"
	echo -e "<TODO: Scan mirrored remote machines and the targets they contain>"
	echo -e "*** Use 'local' as the remotehost if you're running the server part on the same machine as the remote part ***"
	echo -e ""
}


check_devtools() {
	# Checking for needed development tools, compilers etc.
	# Required: m4 make gcc g++ binutils
	DEVTOOLS="gcc c++ g++ make m4 strip git depmod patch mksquashfs pkg-config"		# 'strip' as marker for binutils
	for i in $DEVTOOLS; do
		which "$i" 2>/dev/null 1>&2 || { echo "Essential development tool $i not found - exiting."; exit 1; }
	done
}

initial_checks() {
	if [ "x$(whoami)" != "xroot" ]; then
		perror "ERROR: You need to have root rights to install packages."
		exit 1
	else
		banner
	fi
	pinfo "Arch triplet of this machine: $ARCH_TRIPLET"

	# source the 2 central scripts:
	# setup_target
	#	- builds all modules activated under the target directory
	#	  (basic targets are 'stage31' and 'stage32')
	# export_target
	#	- pack given target as a cpio or squashfs depending on
	#	  the export type:
	#	  	stage31 (cpio archive of the complete stage31-filesystem)
	#		stage32 (cpio archive containing the squashfs'ed stage32-filesystem)
	REMOTE_SETUP_TARGET="${ROOT_DIR}/remote/setup_target"
	SERVER_EXPORT_TARGET="${ROOT_DIR}/server/export_target"

	[ ! -e "${REMOTE_SETUP_TARGET}" ] && perror "Missing script remote/setup_target. Exiting."
	[ ! -e "${SERVER_EXPORT_TARGET}" ] && perror "Missing script server/export_target. Exiting."
}

read_params() {
	local MODE=""
	local SUBMODE=""
	# mltk knows two modes in which to run:
	#	- remote: functions to be run on the reference system
	#		  from which to build the mini-linux
	#	- server: functions for rsyncing the generated stage31/stage32
	# 		  to a server, exporting the stage31-/stage32-filesystems
	case "$1" in
		server)
			MODE="SERVER"
			[ "$#" -lt "2" ] && perror "Missing argument to server-mode flag 'server' (expecting remote host)"
			REMOTE_IP="$2"
			shift
			;;
		remote)
			MODE="REMOTE"
			;;
		*)
			pwarning "Unknown mode: $1"
			print_usage
			exit 1
			;;
	esac
	shift

	# A target represents a set of modules to be build for a specific stage.
	# i.e. a very basic stage31 target should includes:
	#	- busybox
	#	- kernel
	#	- rootfs-stage31
	# a basic stage32 target could add:
	#	- systemd
	#	- dbus
	#	- pam
	# for a GUI, you could add:
	#	- xorg
	#	- kdm
	# 	- plymouth
	TARGET=""
	if [[ $1 != "-"* ]]; then
		TARGET=$1
		shift
	elif [[ $1 != "-s" && $1 != "-n" && $1 != "-k" ]]; then
		perror "A target is required. None given."
	fi

	# handle rest of arguments
	while [ "$#" -gt "0" ]; do
		local PARAM="$1"
		shift

		# options to current target
		if [[ "$PARAM" == "-"* ]]; then
			case "$PARAM" in
				-k)
					[ "$MODE" != "SERVER" ] && perror "-k can only be used in server mode"
					# NOTE: check for validity of config is done in 'inital_checks' of server/export_target
					SERVER_CONFIG_TYPE="$1"
					SERVER_CONFIG="1"
					shift
					continue
					;;
				-s)
					SERVER_SYNC="1"
					continue
					;;
				-c)
					SUBMODE="CLEAN"
					;;
				-b)
					[ "$MODE" != "REMOTE" ] && perror "-b can only be used in remote mode"
					SUBMODE="BUILD"
					;;
				-d)
					eval ${MODE}_DEBUG="1"
					continue
					;;
				-p)
					[ "$#" -lt "1" ] && perror "Missing argument to -p"
					[ "$MODE" != "REMOTE" ] && perror "-p can only be used in remote mode"
					. "remote/profiles/${1}.profile" || perror "Profile '$1' not found in remote/profiles/"
					REMOTE_BUILD="1"
					REMOTE_LIST_BUILD="$REMOTE_LIST_BUILD $MODULES"
					unset MODULES
					shift
					continue
					;;
				-e)	
					[ "$#" -lt "1" ] && perror "Missing argument to -e"
					[[ "stage31|stage32|addons" != *"$1"* ]] && perror "Wrong type specified. Muste be either 'stage31', 'stage32' or 'addons'" 
					SERVER_EXPORT="1"
					SERVER_EXPORT_TYPE="$1"
					shift
					continue
					;;
				-n)
					if [ "$MODE" == "REMOTE" ]; then
						[ "x" != "x$1" ] && perror "'-n' accepts no parameters. Given: $1"
						REMOTE_EXPORT="1"
					fi
					continue
					;;
				-a)
					if [ "$MODE" == "REMOTE" ]; then
						[ "x" != "x$1" ] && perror "'-a' accepts no parameters. Given: $1"
						REMOTE_AUTOMATIC_BUILD="1"
					fi
					continue
					;;
				*)
					pwarning "Unknown flag to target: $PARAM"
					print_usage
					exit 1
					;;
			esac
			eval ${MODE}_${SUBMODE}="1"
			continue
		fi

		# module name
		[[ $MODE != REMOTE ]] && pwarning "You cannot specify module names in server mode." && print_usage && exit 1
		[[ $SUBMODE != CLEAN && $SUBMODE != BUILD ]] && pwarning "Module name given in remote mode, but no action specified (eg. build)" && print_usage && exit 1
		eval "${MODE}_LIST_${SUBMODE}=\"\$${MODE}_LIST_${SUBMODE} \$PARAM\""
	done
	
	# exit if no command
	
	[[ $SERVER_CLEAN == 0 && $SERVER_EXPORT == 0 && $REMOTE_CLEAN == 0 && $REMOTE_BUILD == 0 && $SERVER_SYNC == 0 && $REMOTE_EXPORT == 0 && $SERVER_CONFIG == 0 ]] && print_usage && exit 1
}

run() {
	if [[ $REMOTE_CLEAN == 1 || $REMOTE_BUILD == 1 || $REMOTE_EXPORT == 1 ]]; then
		[[ $REMOTE_DEBUG == 1 ]] && unset_quiet || set_quiet
		. "${REMOTE_SETUP_TARGET}" || perror "Cannot source ${REMOTE_SETUP_TARGET}"
	        [[ $REMOTE_CLEAN == 1 ]] && clean_modules $TARGET $REMOTE_LIST_CLEAN
	        [[ $REMOTE_BUILD == 1 ]] && generate_target $TARGET $REMOTE_LIST_BUILD
		[[ $REMOTE_EXPORT == 1 ]] && export_builds
	fi
	if [[ $SERVER_CLEAN == 1 || $SERVER_EXPORT == 1 || $SERVER_SYNC == 1 || $SERVER_CONFIG == 1 ]]; then
		[[ $SERVER_DEBUG == 1 ]] && unset_quiet || set_quiet
		. "${SERVER_EXPORT_TARGET}" || perror "Cannot source ${SERVER_EXPORT_TARGET}"
		[[ $SERVER_SYNC == 1 ]] && sync_remote
	        [[ $SERVER_CLEAN == 1 ]] && clean_target $TARGET
	        [[ $SERVER_EXPORT == 1 ]] && export_target $TARGET $SERVER_EXPORT_TYPE
		[[ $SERVER_CONFIG == 1 ]] && generate_config $SERVER_CONFIG_TYPE
	fi
}

SERVER_DEBUG="0"
SERVER_EXPORT="0"
SERVER_CLEAN="0"
SERVER_EXPORT_TYPE=""
SERVER_SYNC="0"
SERVER_CONFIG="0"
REMOTE_DEBUG="0"
REMOTE_CLEAN="0"
REMOTE_BUILD="0"
REMOTE_LIST_CLEAN=""
REMOTE_LIST_BUILD=""
REMOTE_AUTOMATIC_BUILD=0

initial_checks
read_params $@

check_devtools

run