summaryrefslogtreecommitdiff
path: root/src/chroot-tools/librechroot
blob: 7f113f5d31e149f334344db70b3443b38f633879 (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
#!/bin/bash -euE
# librechroot

# Copyright 2010 Nicolás Reynolds
# Copyright 2011 Joshua Haase
# Copyright 2012 Luke Shumaker
#
# 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/>.

. /usr/share/libretools/conf.sh
load_conf_libretools_chroot

. "$(which libremessages)"
. /usr/share/devtools/makechrootpkg.sh

cleanup=(':');
cleanup() {
	for cmd in "${cleanup[@]}"; do
		$cmd
	done
}

cmd=${0##*/}
usage() {
	echo "Usage: $cmd [OPTIONS] "
	echo 'Interacts with a chroot.'
	echo ''
	echo 'This command will make the following configuration changes in'
	echo 'the chroot:'
	echo "  - overwrite \`/etc/libretools.d/chroot.conf'"
	echo "  - overwrite \`/etc/pacman.d/mirrorlist'"
	echo "  - set \`CacheDir' in \`/etc/pacman.conf'"
	echo ''
	echo 'Options:'
	echo ' Settings:'
	echo "  -n <CHROOT>   Use this chroot instead of \`$CHROOT'"
	echo "  -l <COPY>     Use this chroot copy instead \`$CHROOTCOPY'"
	echo '  -N            Disable networking in the chroot'
	echo ''
	echo ' Modes: (the last mode given will be used)'
	echo '  -C            Clean /repo in the chroot'
	echo '  -c            Clean the packages installed in the chroot'
	echo '  -I <FILE>     Install the package FILE into the chroot'
	echo '  -i <PKG>      Install the package PKG from repos into the chroot'
	echo '  -m            Make sure the chroot exists; do nothing else'
	echo '  -r <CMD>      Run CMD in the chroot'
	echo "  -s            Sync the copy with the \`root' copy"
	echo '  -u            Update the chroot'
	echo '  -h            Show this message'
}

main() {
	CHROOTCOPY=$LIBREUSER
	[[ $CHROOTCOPY != root ]] || CHROOTCOPY=copy

	local mode=enter
	local archroot_args=(-f)
	local ARG=''
	while getopts 'n:l:NCcI:i:mr:suh' arg; do
		case $arg in
			n) CHROOT=$OPTARG;;
			l) CHROOTCOPY=$OPTARG;;
			N) archroot_args+=(-N);;

			C) mode=clean_repo;;
			c) mode=clean_pacman;;
			I) mode=install_file; ARG=$OPTARG;;
			i) mode=install_pkg; ARG=$OPTARG;;
			m) mode=noop;;
			r) mode=run; ARG=$OPTARG;;
			s) mode=sync;;
			u) mode=update;;

			h) usage; exit 0;;
			*) usage; exit 1;;
		esac
	done
	shift $(($OPTIND - 1))
	if [[ $# > 0 ]]; then
		usage
		exit 1
	fi

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

	########################################################################

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

	# Keep this lock as long as we are running
	# Note that '9' is the same FD number as in (mk)archroot
	lock_open_write 9 "$copydir" \
		"Waiting for existing lock on \`$copydir' to be released"

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

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

	mkdir -p $copydir/etc/libretools.d
	{
		if [[ -n ${CHROOTEXTRAPKG[@]:-} ]]; then
			printf 'CHROOTEXTRAPKG=('
			printf "'%s' " "${CHROOTEXTRAPKG[@]}"
			printf ')\n'
		else
			printf 'CHROOTEXTRAPKG=()\n'
		fi
	} > $copydir/etc/libretools.d/chroot.conf

	########################################################################

	trap cleanup EXIT
	case "$mode" in
		clean_repo)
			rm -rf "${copydir}/repo/*"
			bsdtar -czf "${copydir}/repo/repo.db.tar.gz" -T /dev/null
			ln -s "repo.db.tar.gz" "${copydir}/repo/repo.db"
			;;
		clean_pacman)
			cleanup+=("rm -f $copydir/clean $copydir/chrootexec")
			cp -a "$(which chcleanup)"  "${copydir}/clean"
			echo '#!/bin/bash'        > "${copydir}/chrootexec"
			echo 'mkdir /build'      >> "${copydir}/chrootexec"
			echo 'cd /build; /clean' >> "${copydir}/chrootexec"
			chmod 755 "${copydir}/chrootexec"
			archroot "${archroot_args[@]}" "${copydir}" -r /chrootexec
			;;
		install_file)
			cleanup+=("rm $copydir/${ARG##*/}")
			cp "$ARG" "$copydir/${ARG##*/}"
			archroot "${archroot_args[@]}" "${copydir}" -r "pacman -U /${ARG##*/} --noconfirm"
			;;
		install_pkg) archroot "${archroot_args[@]}" "${copydir}" -i $ARG ;;
		noop) :;;
		run)         archroot "${archroot_args[@]}" "${copydir}" -r "$ARG" ;;
		sync) chroot_sync;;
		update)      archroot "${archroot_args[@]}" "${copydir}" -u ;;
		enter)       archroot "${archroot_args[@]}" "${copydir}" -r bash ;;
	esac
}

main "$@"