summaryrefslogtreecommitdiff
path: root/bin/buildrc
blob: 237f0b228a6ab5096123a2ad4358d86ef8b02ce3 (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
#!/bin/bash

_exec_locked() {
	local tmp_dir="/var/tmp"
	local lvm_snapshot_lock_file="/.entropy_locks/vg_chroots-lv_chroots-snapshot.lock"
	local snapshot_lock_file="${tmp_dir}/.emerge.snapshot.lock"
	local matter_lock_file="${tmp_dir}/.matter_resource.lock"

	if [ ! -d "${tmp_dir}" ]; then
		echo "${tmp_dir} is missing, creating it..." >&2
		mkdir -p "${tmp_dir}"
	fi

	flock -x -n "${matter_lock_file}" true
	rc=${?}
	if [ "${rc}" != "0" ]; then
		echo >&2
		echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" >&2
		echo "Matter or the Backup Script is running on this chroot" >&2
		echo "Please be patient, it will eventually terminate..." >&2
		echo "Snapshot lock file = ${snapshot_lock_file}" >&2
		echo "Matter lock file = ${matter_lock_file}" >&2
		echo >&2
		echo "This ${1} instance will terminate NOW" >&2
		echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" >&2
		echo >&2
		return 1
	else
		# acquire snapshot read lock in non-blocking, shared mode
		(
			flock -s -n 9 || {
				echo "Hello there," >&2;
				echo "a chroot snapshot is in progress..." >&2;
				echo "Try again later." >&2;
				exit 1;
			}

			flock -s --timeout=120 10
			if [ "${?}" != "0" ]; then
				echo >&2
				echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" >&2
				echo "The LVM lock of the Backup Script is being held for 2 minutes" >&2
				echo "This should not happen. Please contact stefan.cristian@rogentos.ro and" >&2
				echo "stop any activity NOW. However, if you're in an emergency" >&2
				echo "Just rm ${lvm_snapshot_lock_file} and continue." >&2
				echo "Please note that this will cause inconsistent backups." >&2
				echo >&2
				echo "This instance will terminate NOW" >&2
				echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" >&2
				echo >&2
				exit 1
			fi

			"${@}"
			exit ${?}

		) 9> "${snapshot_lock_file}" 10>"${lvm_snapshot_lock_file}"
		return ${?}
	fi
}

emerge() {
	_exec_locked "/usr/bin/emerge" --quiet-build=y --oneshot "${@}"
	return ${?}
}

eit() {
	_exec_locked "/usr/bin/eit" "${@}"
	return ${?}
}

# Alias configuration
alias cosmos=/particles/cosmos/cosmos