summaryrefslogtreecommitdiffstats
path: root/src/profile/proteanos.sh
blob: fbfce2d025ac0afbf9156024780f896fe322c02c (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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# ProteanOS architecture detection and feeds lists.
#
# Copyright (C) 2013-2016, 2021  Patrick McDermott
#
# This file is part of the ProteanOS Development Kit.
#
# The ProteanOS Development Kit is free software: you can redistribute
# it and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# The ProteanOS Development Kit is distributed in the hope that it
# will be useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with the ProteanOS Development Kit.  If not, see
# <http://www.gnu.org/licenses/>.

_PROF_PROTEANOS_FSTAB_LINUX="\
proc   /proc    proc   defaults
sys    /sys     sysfs  defaults
/dev   /dev     none   bind
devpts /dev/pts devpts noexec,nosuid,gid=5,mode=0620
"
_PROF_PROTEANOS_INSTALL_SERVICE=\
'#!/bin/sh /etc/rc.common

start()
{
	log "Configuring packages"
	printf "disabled\n" >/etc/rc.policy
	opkg install $(opkg list-installed | busybox cut -d " " -f 1)
	printf "enabled\n" >/etc/rc.policy
	rm -f "${SCRIPT}"
}
'

_prof_proteanos_mirror=
_prof_proteanos_suite=
_prof_proteanos_platforms=''
_prof_proteanos_platforms_platform=''
_prof_proteanos_platforms_type=''
_prof_proteanos_platforms_sections=''

_prof_proteanos_platforms_field()
{
	local name="${1}"
	local value="${2}"
	shift 2

	case "${name}" in
		'Platform')
			_prof_proteanos_platforms_platform="${value}"
			;;
		'Type')
			_prof_proteanos_platforms_type="${value}"
			;;
		'Sections')
			_prof_proteanos_platforms_sections="${value}"
			;;
	esac
}

_prof_proteanos_platforms_paragraph()
{
	local platform=
	local archplat=

	platform="${_prof_proteanos_platforms_platform}"
	archplat="$(printf '%s' "${platform}" | tr -c '[a-z0-9/]' '_')"
	eval "_prof_proteanos_archplat_arches_${archplat#*/}=$(: \
		)\"\${_prof_proteanos_archplat_arches_${archplat#*/}-}$(: \
		)\${platform%/*}\""
	archplat="${archplat%/*}__${archplat#*/}"

	_prof_proteanos_platforms="${_prof_proteanos_platforms}${platform} "
	eval "_prof_proteanos_archplat_type_${archplat}=$(: \
		)\"\${_prof_proteanos_platforms_type}\""
	eval "_prof_proteanos_archplat_sections_${archplat}=$(: \
		)\"\${_prof_proteanos_platforms_sections}\""
}

prof_proteanos_detect_root()
{
	local root="${1}"
	shift 1

	if grep "^ID='proteanos'\$" "${root}/etc/os-release" >/dev/null 2>&1
	then
		return 0
	else
		return 1
	fi
}

prof_proteanos_normalize_suite()
{
	local suite="${1}"
	shift 1

	case "${suite}" in
		'')  printf 'rel/trunk\n';;
		*/*) printf '%s\n' "${suite}";;
		*)   printf 'rel/%s\n' "${suite}";;
	esac

	return 0
}

prof_proteanos_detect_arch()
{
	local uname_m=
	local uname_s=
	local arch=

	uname_m="$( (uname -m) 2>/dev/null)" || uname_m='unknown'
	uname_s="$( (uname -s) 2>/dev/null)" || uname_s='unknown'

	case "${uname_m}:${uname_s}" in
		'x86_64:Linux')
			arch='amd64-linux-glibc'
			;;
		'i686:Linux')
			arch='i686-linux-glibc'
			;;
		*)
			arch=''
	esac

	printf '%s\n' "${arch}"
}

prof_proteanos_default_plat()
{
	printf 'dev\n'
}

prof_proteanos_plat_arches()
{
	local plat="${1}"
	shift 1

	plat="$(printf '%s' "${plat}" | tr -c '[a-z0-9]' '_')"
	eval "printf '%s\\n' \${_prof_proteanos_archplat_arches_${plat}}"
}

prof_proteanos_select_mirror()
{
	local mirrors=
	local mirrors_count=
	local mirror_num=

	mirrors="$(${WGET} -q -O - -- \
		'http://files.proteanos.com/pub/proteanos-mirrors-http' \
		'ftp://files.proteanos.com/pub/proteanos-mirrors-ftp'
		)"
	mirrors_count=$(printf '%s\n' "${mirrors}" | wc -l)
	rand
	mirror_num=$((${rand_x} % ${mirrors_count} + 1))

	printf '%s\n' "${mirrors}" | sed -n "${mirror_num}p"
}

prof_proteanos_prepare_install()
{
	local mirror="${1}"
	local suite="${2}"
	shift 2

	_prof_proteanos_mirror="${mirror}"
	_prof_proteanos_suite="${suite}"
	parse_control - _prof_proteanos_platforms_field \
		_prof_proteanos_platforms_paragraph 0<<-EOF
		$(${WGET} -q -O - -- "${mirror}/feeds/${suite}/Platforms")
		EOF
}

prof_proteanos_installer_type()
{
	local arch="${1}"
	local plat="${2}"
	shift 2
	local arch_clean=
	local plat_clean=
	local archplat=
	local installer_type=

	arch_clean="$(printf '%s' "${arch}" | tr -c '[a-z0-9/]' '_')"
	plat_clean="$(printf '%s' "${plat}" | tr -c '[a-z0-9/]' '_')"
	archplat="${arch_clean}__${plat_clean}"
	eval "installer_type=\"\${_prof_proteanos_archplat_type_${archplat}}\""

	printf '%s' "${installer_type}"
}

prof_proteanos_validate_archplat()
{
	local arch="${1}"
	local plat="${2}"
	shift 2
	local archplat=

	case " ${_prof_proteanos_platforms} " in
		*" ${arch}/${plat} "*)
			return 0
			;;
	esac

	return 1
}

prof_proteanos_opkg_state_dir()
{
	printf '/var/lib/opkg'
	return 0
}

prof_proteanos_opkg_conf_file()
{
	printf '/etc/opkg.conf'
	return 0
}

prof_proteanos_feeds()
{
	local arch="${1}"
	local plat="${2}"
	shift 2
	local arch_clean=
	local plat_clean=
	local archplat=
	local sects=
	local manifest=
	local a=
	local p=
	local s=

	arch_clean="$(printf '%s' "${arch}" | tr -c '[a-z0-9/]' '_')"
	plat_clean="$(printf '%s' "${plat}" | tr -c '[a-z0-9/]' '_')"
	archplat="${arch_clean}__${plat_clean}"
	eval "sects=\"\${_prof_proteanos_archplat_sections_${archplat}}\""

	manifest="$(${WGET} -q -O - -- "${_prof_proteanos_mirror}/feeds/$(: \
		)${_prof_proteanos_suite}/Manifest")"

	for a in "${arch}" 'all'; do
		for p in "${plat}" 'all'; do
			for s in ${sects}; do
				case "${LF}${manifest}${LF}" in
					*"${LF}${a}/${p}/${s}${LF}"*) ;;
					*) continue;;
				esac
				printf 'src/gz proteanos_%s_%s_%s_%s_%s.gz ' \
					"${_prof_proteanos_suite%/*}" \
					"${_prof_proteanos_suite#*/}" \
					"${a}" "${p}" "${s}"
				printf '%s/feeds/%s/%s/%s/%s\n' \
					"${_prof_proteanos_mirror}" \
					"${_prof_proteanos_suite}" \
					"${a}" "${p}" "${s}"
			done
		done
	done

	for s in 'base' 'src'; do
		printf 'src/gz proteanos_%s_%s_%s_%s_%s.gz ' \
			"${_prof_proteanos_suite%/*}" \
			"${_prof_proteanos_suite#*/}" \
			'src' 'all' "${s}"
		printf '%s/feeds/%s/%s/%s/%s\n' \
			"${_prof_proteanos_mirror}" \
			"${_prof_proteanos_suite}" \
			'src' 'all' "${s}"
	done
}

prof_proteanos_get_cert_url()
{
	printf '%s/archive.cert' "${_prof_proteanos_mirror}"
	return 0
}

prof_proteanos_get_root_key()
{
	cat <<-EOF
		untrusted comment: ProteanOS archive root key
		RWSG2r6ZwINj8pcScRd4nc9HVZ9zjP5oajMPfTm+dIk67btIsCZaf3SN
		EOF
}

prof_proteanos_dep_fields()
{
	printf 'Depends Pre-Depends'
}

prof_proteanos_include_pkg()
{
	local name="${1}"
	local value="${2}"
	shift 2

	if [ "x${name}" = 'xpackage' ]; then
		case "${value}" in
			'base-files' | 'config-base')
				return 0;;
		esac
	fi

	return 1
}

prof_proteanos_get_fstab()
{
	local arch="${1}"
	local plat="${2}"
	shift 2

	case "${arch}" in
		*-linux-*)
			printf '%s\n' "${_PROF_PROTEANOS_FSTAB_LINUX}"
			;;
	esac
}

prof_proteanos_file_systems_mounted()
{
	local root="${1}"
	local arch="${2}"
	local plat="${3}"
	shift 3

	[ -e "${root}/dev/null" ]
}

prof_proteanos_bind_mount()
{
	local arch="${1}"
	local plat="${2}"
	local olddir="${3}"
	local newdir="${4}"
	shift 4

	case "${arch}" in
		*-linux-*)
			mount -o bind -- "${olddir}" "${newdir}"
			;;
	esac
}

prof_proteanos_bind_umount()
{
	local arch="${1}"
	local plat="${2}"
	local olddir="${3}"
	local newdir="${4}"
	shift 4

	case "${arch}" in
		*-linux-*)
			umount -- "${newdir}"
			;;
	esac
}

prof_proteanos_configure_system_native()
{
	local root="${1}"
	local arch="${2}"
	local plat="${3}"
	shift 3

	printf 'disabled\n' >"${root}/etc/rc.policy"
	opkg_install_all "${root}"
	if [ "x${plat}" = 'xdev' ]; then
		[ -r /etc/resolv.conf ] && cp -- /etc/resolv.conf "${root}/etc"
		[ -r /etc/hostname ] && cp -- /etc/hostname "${root}/etc"
	else
		printf 'enabled\n' >"${root}/etc/rc.policy"
		printf 'proteanos\n' >"${root}/etc/hostname"
	fi
	[ -e "${root}/etc/passwd" ] || printf \
		'root::0:0:root:/root:/bin/sh\n' >"${root}/etc/passwd"
	[ -e "${root}/etc/group" ] || printf \
		'root:x:0:\n' >"${root}/etc/group"
}

prof_proteanos_configure_system_foreign()
{
	local root="${1}"
	local arch="${2}"
	local plat="${3}"
	shift 3

	printf '%s' "${_PROF_PROTEANOS_INSTALL_SERVICE}" \
		>"${root}/etc/rc.d/S00install"
	chmod -- 0755 "${root}/etc/rc.d/S00install"
	printf 'proteanos\n' >"${root}/etc/hostname"
	[ -e "${root}/etc/passwd" ] || printf \
		'root::0:0:root:/root:/bin/sh\n' >"${root}/etc/passwd"
	[ -e "${root}/etc/group" ] || printf \
		'root:x:0:\n' >"${root}/etc/group"
}

prof_proteanos_find_kernel()
{
	local root="${1}"
	local arch="${2}"
	local plat="${3}"
	shift 3
	local kernel=

	case "${arch}" in
		*-linux-*)
			for kernel in vmlinuz; do
				if [ -e "${root}/boot/${kernel}" ]; then
					printf '%s' "/boot/${kernel}"
					return 0
				fi
			done
			;;
	esac
	return 1
}

prof_proteanos_make_initramfs()
{
	local arch="${1}"
	local plat="${2}"
	shift 2
	local init_created=

	case "${arch}" in
		amd64-linux-* | i686-linux-*)
			if ! [ -e init ]; then
				ln -s /sbin/init init
				init_created=true
			else
				init_created=false
			fi
			find . -xdev -a \! -path './boot/*' -a \
				\! -path './prokit/*' | \
				${CPIO} -o -H newc | \
				${XZ} -c --check=crc32 --x86 --lzma2 \
				1>boot/initrd.img
			if ${init_created}; then
				rm -f init
			fi
			;;
	esac

	# Hack to rerun install-lilo:
	session_begin . . true
	session_exec run-parts /etc/kernel/postinst.d
	session_end

	find . -xdev -a \! -path './boot/*' -a \
		\! -path './prokit/*' -a \! -type d | \
		xargs rm -f

	return 0
}

prof_proteanos_register()
{
	register_profile 'proteanos'
}
__init prof_proteanos_register