summaryrefslogtreecommitdiff
path: root/src/chroot-tools/librechroot
blob: 07360d78c636ada78dfb05ed1b23b5f26f22f4ee (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
#!/bin/bash
# LibreChRoot
# Enters a chroot

# Copyright 2010 Nicolás Reynolds
# Copyright 2011 Joshua Haase
# Copyright 2012 Luke Shumaker

# ---------- GNU General Public License 3 ----------

# This file is part of Parabola.

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

# Parabola 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 Parabola. If not, see <http://www.gnu.org/licenses/>.

. /etc/libretools.conf

cmd=${0##*/}

lock_open_write() {
	local fd=$1
	local path=$2
	local msg=$3

	# Only reopen the FD if it wasn't handed to us
	if [[ $(readlink -f /dev/fd/$fd) != "${path}.lock" ]]; then
		eval "exec $fd>${path}.lock"
	fi

	if ! flock -n $fd; then
		stat_busy "$msg"
		flock $fd
		stat_done
	fi
}

lock_open_read() {
	local fd=$1
	local path=$2
	local msg=$3

	# Only reopen the FD if it wasn't handed to us
	if [[ $(readlink -f /dev/fd/$fd) != "${path}.lock" ]]; then
		eval "exec $fd>${path}.lock"
	fi

	if ! flock -sn $fd; then
		stat_busy "$msg"
		flock -s $fd
		stat_done
	fi
}

lock_close() {
	local fd=$1
	eval "exec $fd>&-"
}

clean_pacman() {
	msg "Cleaning chroot with pacman: ${copydir}"

	cp -a "$(dirname $0)/chcleanup"   "${copydir}/clean"
	mkdir -p "$copydir/build"
	mkarchroot -r "cd /build; /clean" "${copydir}"

	#mkarchroot "${copydir}" base base-devel sudo "${CHROOTEXTRAPKG[@]}"
}

clean_repo() {
	msg "Cleaning repo for chroot: ${copydir}"

	if [ -d  "${copydir}/repo" ]; then
		find "${copydir}/repo/" -mindepth 1 -delete
	else
		mkdir -p "${copydir}/repo"
	fi
	bsdtar -czf "${copydir}/repo/repo.db.tar.gz" -T /dev/null
	ln -s "repo.db.tar.gz" "${copydir}/repo/repo.db"
}

##
# This function is almost verbatim from makechrootpkg
##
sync() {
	if [[ $CHROOTCOPY = root ]]; then
		error "Cannot sync the root copy with itself"
		exit 1
	fi

	# Note that '9' is the same FD number as in mkarchroot
	lock_open_write 9 "$copydir" "Locking chroot copy '$copy'"
	lock_open_read  8 "$rootdir" "Locking clean chroot"

	stat_busy 'Creating clean working copy'
	local use_rsync=false
	if type -P btrfs >/dev/null; then
		[[ -d $copydir ]] && btrfs subvolume delete "$copydir" &>/dev/null
		btrfs subvolume snapshot "$chrootdir/root" "$copydir" &>/dev/null ||
			use_rsync=true
	else
		use_rsync=true
	fi

	if $use_rsync; then
		mkdir -p "$copydir"
		rsync -a --delete -q -W -x "$CHROOTDIR/$CHROOT/root/" "$copydir"
	fi
	stat_done

	lock_close 8
	lock_close 9
}

update() {
	msg "Updating chroot: ${copydir}"
	mkarchroot -u "${copydir}"
}

enter() {
	msg "Entering chroot: ${copydir}"
	mkarchroot -r "bash" "${copydir}"
}

usage() {
	echo "Usage: $cmd [OPTIONS] [CHROOT]"
	echo 'Interacts with a chroot.'
	echo ''
	echo "The default CHROOT is \`${CHROOT}'."
	echo ''
	echo 'Options:'
	echo ' Settings:'
	echo " -d <CHROOTDIR>  Use this dir instead of \`${CHROOTDIR}'"
	echo ' -l <copy>       Use this as the chroot copy instead of basing it'
	echo '                 on the username'
	echo ' Modes:'
	echo ' -h              Show this message'
	echo ' -c              Clean the chroot using pacman'
	echo " -C              Clean the chroot by syncing it with 'root' copy"
	echo ' -r              Clean /repo in the chroot'
	echo ' -u              Update the chroot'
}

main() {
	# The logic for setting CHROOTCOPY is mirred from makechrootpkg
	CHROOTCOPY=$USER
	[[ -n $SUDO_USER ]] && CHROOTCOPY=$SUDO_USER
	[[ -z $CHROOTCOPY || $CHROOTCOPY = root ]] && CHROOTCOPY=copy

	local mode=enter
	while getopts 'hrcud:l:' arg; do
		case $arg in
			d) CHROOTDIR=$OPTARG;;
			l) CHROOTCOPY=$OPTARG;;

			c) mode=clean_pacman;;
			C) mode=sync;;
			r) mode=clean_repo;;
			u) mode=update;;

			h) usage; exit 0;;
			*) usage; exit 1;;
		esac
	done
	shift $(($OPTIND - 1))
	case $# in
		0) :;;
		1) CHROOT="$1";;
		*) usage; exit 1;;
	esac

	# not local
	rootdir="${CHROOTDIR}/${CHROOT}/root"
	copydir="${CHROOTDIR}/${CHROOT}/${CHROOTCOPY}"

	if (( EUID )); then
		error "This script must be run as root."
		exit 1
	fi

	if [[ ! -d $rootdir ]]; then
		libremkchroot -d "$CHROOTDIR" "$CHROOT"
	fi

	if [[ ! -d $copydir ]] && [[ $mode != sync ]]; then
		sync
	fi

	case "$mode" in
		clean_pacman) clean_pacman; exit $?;;
		clean_repo) clean_repo; exit $?;;
		update) update; exit $?;;
		enter) enter; exit $?;;
	esac
}

main "$@"