summaryrefslogtreecommitdiff
path: root/eclass/calculate.eclass
blob: 98815f06527eeb796cedf92e7b2163550cad3255 (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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# Copyright 1999-2008 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header:

# @ECLASS: calculate.eclass
# @MAINTAINER:
# support@calculate.ru
# @AUTHOR:
# Author: Mir Calculate
# @BLURB: Share functions for calculate-utils and calculate-sources
# @DESCRIPTION:
# This eclass use for calculate-utils and calculate-sources ebuilds

inherit eutils linux-info versionator

EXPORT_FUNCTIONS pkg_postinst

# @FUNCTION: last_arg
# @USAGE: last_arg manyarguments
# @DESCRIPTION:
# print last argument
last_arg() {
	shift $(( $# - 1 ))
	echo $1
}

# @FUNCTION: rm_link_with_file
# @USAGE: rm_link_with_file filename
# @DESCRIPTION:
# delete the file, and if it is symbolic then delete the file,
# which point out a link
rm_link_with_file() {
	[[ -L $1 ]] && rm -f `readlink -f $1`
	rm -f $1
}

# @FUNCTION: cp_link_with_file
# @USAGE: cp_link_with_file filename suffix
# @DESCRIPTION:
# copy the file with the same name with the suffix, and if it is a symbolic
# link, then also copy the target file with the addition of the suffix
cp_link_with_file() {
	if [[ -L $1 ]]
	then
		rm -f `readlink -f ${1}`$2
		cp -aH ${1} `readlink -f ${1}`$2
		ln -sf `readlink ${1}`${2} ${1}${2}
	else
		rm -f ${1}${2}
		cp -aH ${1} ${1}${2}
	fi
}

# @FUNCTION: make_old_file
# @USAGE: make_old_file filename 
# @DESCRIPTION:
# wear out a file, copy the file, adding its name suffix ". old"
# support symbolic link
make_old_file() {
	if [[ -e $1 ]]
	then
		rm_link_with_file $1.old
		cp_link_with_file $1 .old
	fi
	rm_link_with_file $1
}

# @FUNCTION: update_file
# @USAGE: wear_out_file filename link
# @DESCRIPTION:
# update a file from filename-installed, and make old file if need
update_file() {
	# if newest file is absent
	[[ -e $1-installed ]] || return 1
	# link and filename exist
	if [[ `readlink -f $2` == `readlink -f $1` ]]
	then
		make_old_file $2
	else
		# rename link to link.old
		mv $2 $2.old &>/dev/null
		# make old filename
		make_old_file $1
		# fix link pointed to previous filename
		find -lname "$1" -exec ln -sf $1.old {} \;
	fi
	# make link to filename
	ln -sf `basename $1` $2
	# rename installed
	mv $1-installed $1
}

# @FUNCTION: detect_linux_shortname
# @USAGE: 
# @DESCRIPTION:
# Detect calculate linux shortname by /etc/make.profile
detect_linux_shortname() {
	local makeprofile=$(readlink ${ROOT}/etc/make.profile)
	local profile=
	local system=
	local shortname=
	while [[ $profile != "calculate" && $profile != "." ]]
	do
		shortname=$system
		system=$profile
		profile=$(basename $makeprofile)
		makeprofile=$(dirname $makeprofile)
	done
	if [[ $profile == "calculate" ]]
	then
		echo $shortname
	else
		echo "gentoo"
	fi
}

# @FUNCTION: calculate_update_kernel
# @USAGE: [kernelname] [kernelversion] [destination]
# @DESCRIPTION:
# Make symbolic link to vmlinuz, preserve old vmlinuz
# Copy initramfs to initrd and initrd-install
calculate_update_kernel() {
	kversion=$1
	dir=$2

	# update vmlinuz
	update_file ${dir}/vmlinuz-${kversion} ${dir}/vmlinuz
	# update initrd
	update_file ${dir}/initramfs-${kversion} ${dir}/initrd
	# update initrd-install
	update_file ${dir}/initramfs-${kversion}-install ${dir}/initrd-install
	# update System.map
	update_file ${dir}/System.map-${kversion} ${dir}/System.map
	# update config-{KV_FULL}
	make_old_file ${dir}/config-${kversion}
	mv ${dir}/config-${kversion}-installed ${dir}/config-${kversion}

	ebegin "Trying to optimize initramfs"
	( which calculate &>/dev/null && calculate --initrd ) && eend 0 || eend 1
	if [[ "$(md5sum ${ROOT}/boot/initrd | awk '{print $1}')" == \
		"$(md5sum ${ROOT}/boot/initrd-install | awk '{print $1}')" ]]
	then
		ewarn
		ewarn "Perform command after reboot for optimization initramfs:"
		ewarn "  calculate --initrd"
	fi
}

is_broken_link() {
	fname=$1
	[[ -n $( file $fname | grep "broken symbolic link" ) ]] &&
		return 0 ||	return 1
}

# @FUNCTION: calculate_restore_kernel
# @USAGE: [destination]
# @DESCRIPTION:
# Restore vmlinux.old and initrd.old in destination
calculate_restore_kernel() {
	dir=$1

	# restore vmlinuz
	is_broken_link ${dir}/vmlinuz && [ -f ${dir}/vmlinuz.old ] &&
		mv ${dir}/vmlinuz.old ${dir}/vmlinuz

	# resotre initrd
	is_broken_link ${dir}/initrd && [ -f ${dir}/initrd.old ] &&
		mv ${dir}/initrd.old ${dir}/initrd

	# restore System.map
	is_broken_link ${dir}/System.map && [ -f ${dir}/System.map.old ] &&
		mv ${dir}/System.map.old ${dir}/System.map
}

TMP_INITRAMFS=${T}/initramfs
SPLASH_DESCRIPTOR=/etc/splash/tty1/1024x768.cfg

# @FUNCTION: calculate_rm_modules_dir
# @USAGE: [CONTENTS]
# @DESCRIPTION:
# Remove installed files from lib/modules specified by CONTENTS file.
# For work need specify and create SLOT_T directory for .alreadydel flag file,
# which determined was or not file removing.
calculate_rm_modules_dir() {
	PKG_CONTENTS=$1
	[[ -f ${SLOT_T}/.alreadydel ]] && return 0 || 
		touch ${SLOT_T}/.alreadydel &>/dev/null
	addwrite "/lib/modules"
	DIRRM=$( sed -rn '/^dir.*lib\/modules/ s/^\S+\s+(\S+)\s*.*$/\1/p' \
		${PKG_CONTENTS} | sort -r)
	FILERM=$( sed -rn '/^(obj|sym).*lib\/modules/ s/^\S+\s+(\S+)\s+.*$/\1/p' ${PKG_CONTENTS} )
	if [[ -n ${FILERM} ]]
	then
		for f in ${FILERM}
		do
			rm -f $f 
		done
	fi
	if [[ -n ${DIRRM} ]]
	then
		for f in ${DIRRM}
		do
			rmdir $f &>/dev/null
		done
	fi
}

initramfs_unpack() {
	mkdir -p ${TMP_INITRAMFS}
	cd ${TMP_INITRAMFS}
	# select arch
	UNPACKER="gzip"
	lzma --force -t $1 &>/dev/null && UNPACKER="lzma"
	# unpack initramfs
	${UNPACKER} -dc $1 | cpio -di &>/dev/null
	return $?
}

initramfs_change_spalsh() {
	if [ -f ${ROOT}${SPLASH_DESCRIPTOR} ]
	then
		# get silentpic param
		SILENTPIC=$( sed -nr '/^silentpic/ s/^[^=]+=(.*)$/\1/p' \
			${ROOT}${SPLASH_DESCRIPTOR} )
		# get pic param
		PIC=$( sed -nr '/^pic/ s/^[^=]+=(.*)$/\1/p' \
			${ROOT}${SPLASH_DESCRIPTOR} )
		if [ -f ${ROOT}${SILENTPIC} ] && [ -f ${ROOT}${PIC} ]
		then
			cp ${ROOT}${SPLASH_DESCRIPTOR} \
					${TMP_INITRAMFS}${SPLASH_DESCRIPTOR} &&
				mkdir -p ${TMP_INITRAMFS}${SILENTPIC%$(basename $SILENTPIC)} &&
				cp ${ROOT}${SILENTPIC} ${TMP_INITRAMFS}${SILENTPIC} &&
				mkdir -p ${TMP_INITRAMFS}${PIC%$(basename $PIC)} &&
				cp ${ROOT}$PIC ${TMP_INITRAMFS}${PIC}
			return $?
		fi
	else
		return 1
	fi
}

initramfs_pack() {
	# pack new initramfs
	cd ${TMP_INITRAMFS}
	find * | cpio -o --quiet -H newc | gzip -9 >$1
}

# @FUNCTION: calculate_update_splash
# @USAGE: [initramfsfile]
# @DESCRIPTION:
# Install into initramfs splash data, which descripted by
# /etc/splash/tty1/1024x768.cfg
calculate_update_splash() {
	local initrdfile=$1
	for theme_name in calculate tty1
	do
		if which splash_geninitramfs &>/dev/null && \
			[[ -e /etc/splash/$theme_name ]]
		then
			ebegin "Update splash screen for $1"
			if [[ -L $initrdfile ]]
			then
				initrdfile=$(readlink -f $initrdfile)
			fi
			if [[ -f $initrdfile ]]
			then
				splash_geninitramfs -a $initrdfile $theme_name &>/dev/null
				eend $?
			else
				eend 1
			fi
			return 0
		fi 
	done
}

# @FUNCTION: calculate_set_kernelversion
# @USAGE: KERNEL_DIR
# @DESCRIPTION:
# Change version in Makefile of kernel sources on version specified by
# variables KV_MAJOR KV_MINOR KV_PATCH KV_TYPE
calculate_set_kernelversion() {
	KERNEL_DIR=$1
	sed -ri "s/^VERSION = .*$/VERSION = $KV_MAJOR/" \
		${KERNEL_DIR}/Makefile
	sed -ri "s/^PATCHLEVEL = .*$/PATCHLEVEL = $KV_MINOR/" \
		${KERNEL_DIR}/Makefile
	sed -ri "s/^SUBLEVEL = .*$/SUBLEVEL = $KV_PATCH/" \
		${KERNEL_DIR}/Makefile
	sed -ri "s/^EXTRAVERSION = .*$/EXTRAVERSION = $KV_TYPE/" \
		${KERNEL_DIR}/Makefile
}

# @FUNCTION: calculate_update_modules
# @USAGE:
# @DESCRIPTION:
# It calls the update-modules utility. Get from linux-mod.
calculate_update_modules() {
	if [ -x /sbin/update-modules ] && \
		grep -v -e "^#" -e "^$" "${D}"/etc/modules.d/* >/dev/null 2>&1; then
		ebegin "Updating modules.conf"
		/sbin/update-modules
		eend $?
	elif [ -x /sbin/update-modules ] && \
		grep -v -e "^#" -e "^$" "${D}"/etc/modules.d/* >/dev/null 2>&1; then
		ebegin "Updating modules.conf"
		/sbin/update-modules
		eend $?
	fi
}

# @FUNCTION: calculate_update_depmod
# @USAGE:
# @DESCRIPTION:
# It updates the modules.dep file for the current kernel.
# Get from linux-mod.
calculate_update_depmod() {
	# if we haven't determined the version yet, we need too.
	get_version;

	ebegin "Updating module dependencies for ${KV_FULL}"
	if [ -r "${KV_OUT_DIR}"/System.map ]
	then
		depmod -ae -F "${KV_OUT_DIR}"/System.map -b "${ROOT}" -r ${KV_FULL}
		eend $?
	else
		ewarn
		ewarn "${KV_OUT_DIR}/System.map not found."
		ewarn "You must manually update the kernel module dependencies using depmod."
		eend 1
		ewarn
	fi
}

# @FUNCTION: calculate_clean_firmwares
# @USAGE:
# @DESCRIPTION:
# Workaround kernel issue with collising
# firmwares across different kernel versions
calculate_clean_firmwares() {
	for fwfile in `find "${ROOT}/tmp/firmware" -type f`; do
		sysfile="${ROOT}/lib/${fwfile/${ROOT}/tmp/}"
		if [ -f "${sysfile}" ]; then
			#ewarn "Removing duplicated: ${sysfile}"
			rm ${sysfile} || die "failed to remove ${sysfile}"
		fi
	done
}

# @FUNCTION: get_value
# @USAGE:
# @DESCRIPTION:
# Get the value of the variable from stdio.
get_value() {
	sed -rn "s/^(.*\s+)?+$1=\"?([^\" ]*)\"?(\s+.*|$)/\2/p"
}

# @FUNCTION: set_value
# @USAGE:
# @DESCRIPTION:
# Set the value to the variable in the file
set_value() {
	local var=$1
	local value=$2
	local filename=$3
	[[ -n $( get_value $var < $filename ) ]] &&
		sed -ri "s/^$var=(\"?[^\"]*\"?)$/$var=$value/" $filename ||
		echo "$var=$value" >>$filename
}

# @VARIABLE: CALCULATE_INI
# @DESCRIPTION:
# Fullpath to calculate.ini
CALCULATE_INI=${ROOT}/etc/calculate/calculate.ini
# @VARIABLE: LINUXVER
# @DESCRIPTION:
# The version of current operation system.
LINUXVER=
# @VARIABLE: ROOTDEV
# @DESCRIPTION:
# Boot device.
ROOTDEV=

# @FUNCTION: change_issue
# @USAGE:
# @DESCRIPTION:
# Change version in /etc/issue
change_issue() {
	sed -ri "s/${LINUXVER}/${PV}/" ${ROOT}/etc/issue
}

# @FUNCTION: change_grub
# @USAGE:
# @DESCRIPTION:
# Change version for grub
change_grub() {
	sed -ri "/^title/ {:f;N;s/\nkernel/&/;tc;bf;:c;s|root=${ROOTDEV}|&|;Te;s/ ${LINUXVER} / $PV /;:e}" /boot/grub/grub.conf
}

# @FUNCTION: calculate_initvars
# @USAGE:
# @DESCRIPTION:
# Init LINUXVER,ROOTDEV
calculate_initvars() {
	makeProfile=/etc/make.profile
	if [[ -f ${CALCULATE_INI} ]]
	then
		LINUXVER=$( get_value linuxver < ${CALCULATE_INI} )
	else
		LINUXVER=10
		if [[ $(readlink $makeProfile) =~ \
			.*(calculate/(server|desktop)/([^/]+)/).* ]]
		then
			CALCULATEDISTRO=$(echo ${BASH_REMATCH[3]} | tr [:upper:] [:lower:])
			metaPkgFile=/var/db/pkg/app-misc
			if [[ "$(ls $metaPkgFile | grep ${CALCULATEDISTRO}-meta)" =~ \
				${CALCULATEDISTRO}-meta-((.*?)-r[0-9]+|(.*?)) ]]
			then
				if [[ -n ${BASH_REMATCH[2]} ]]
				then
					LINUXVER=${BASH_REMATCH[2]}
				else
					LINUXVER=${BASH_REMATCH[3]}
				fi
			fi
		fi
	fi
	ROOTDEV=$( get_value root < ${ROOT}/proc/cmdline )
}

# @FUNCTION: calculate_change_version
# @USAGE:
# @DESCRIPTION:
# Change the version of the system in calculate.ini,issue,grub.conf
calculate_change_version() {
	calculate_initvars
	if [[ -n "${LINUXVER}" ]] && ! version_is_at_least ${PV} ${LINUXVER}
	then
		ebegin "Change version of operation system"
			change_issue &&
			change_grub
		eend $?
	fi
}

# @FUNCTION: get_last_filename
# @USAGE:
# @DESCRIPTION:
# Get latest regular file by name
get_last_filename() {
	findfiles=$(ls -d $1/$2*{-[a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9],[CDGXS]}$3 2>/dev/null)
	if [[ -n $findfiles ]]
	then
		for line in $findfiles
		do
			if [[ $(LANG=C stat -c %F $line) == "regular file" ]]
			then
				echo $(stat -c %Y $line) $line
			fi
		done | sort | tail -1 | awk '{print $2}'
	fi
}

# @FUNCTION: calculate_get_current_initrd
# @USAGE: suffix
# @DESCRIPTION:
# Get current initrd or initrd with suffix
calculate_get_current_initrd() {
	calculate_initvars
	local suffix=$1
	if [[ -f /boot/grub/grub.conf ]]
	then
		filename=$(sed -nr "/^title/{            #find title in grub.conf
		:readnextline;N;                         #read next line
		s/\ninitrd/&/;                           #if pattern not contents initrd 
		Treadnextline;                           #goto read next line
		s|root=${ROOTDEV}.*initrd.*${suffix}|&|; #if menuitem not for this system
		Tskipmenuitem;                           #then skip menuitem
		s|^.*initrd (.*)$|\1|p;                  #display initramfs
		q;
		:skipmenuitem;
		d;
		}" /boot/grub/grub.conf)
		if [[ -z $filename ]]
		then
			get_last_filename /boot initr ${suffix}
		else
			echo $filename
		fi
	else
		get_last_filename /boot initr ${suffix}
	fi
}

calculate_pkg_postinst() {
	case "${PN}" in 
		cld-themes|cmc-themes|cds-themes|cls-themes|cldg-themes|cldx-themes|splash-themes-calculate)
			local initrdfile=$(calculate_get_current_initrd )
			local initrdinstallfile=$(calculate_get_current_initrd -install)
			[[ -f ${ROOT}${initrdfile} ]] &&
				calculate_update_splash ${ROOT}${initrdfile}
			[[ -f ${ROOT}${initrdinstallfile} &&
				"${ROOT}${initrdinstallfile}" != "${ROOT}${initrdfile}" ]] &&
				calculate_update_splash ${ROOT}${initrdinstallfile}
			;;
	esac
}

# @FUNCTION: calculate_update_ver (/boot vmlinuz
# @USAGE: boot_dir file_basename version source_file [suffix]
# @DESCRIPTION:
# Create backups of older versions before installing
calculate_update_ver() {
  local dir=$1
  local fn=$2
  local ver=$3
  local src=$4
  local suffix="$5"
  if [ -f "$dir/$fn-$ver$suffix" ] ; then
    mv "$dir/$fn-$ver$suffix" "$dir/$fn-$ver$suffix.old"
  fi

  cat "$src" > "$dir/$fn-$ver$suffix"
}

# @FUNCTION: calculate_fix_lib_modules_contents
# @USAGE: 
# @DESCRIPTION:
# Unlink /lib/modules files from CONTENTS
calculate_fix_lib_modules_contents() {
    local vardb=/var/db/pkg
    local content="${vardb}/${CATEGORY}/${PF}/CONTENTS"
    sed -i '/ \/lib\/modules/d' $content
}