summaryrefslogtreecommitdiff
path: root/mkarchroot.in
blob: d95f6da63b039252206e24c00d029d87516fc987 (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
#!/bin/bash
# This program 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; version 2 of the License.
#
# This program 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.

m4_include(lib/common.sh)

CHROOT_VERSION='v2'

FORCE='n'
RUN=''
NOCOPY='n'
NONETWORK='n'

working_dir=''

APPNAME=$(basename "${0}")

# usage: usage <exitvalue>
usage() {
	echo "Usage: ${APPNAME} [options] working-dir [action]"
	echo ' options:'
	echo '    -f            Force overwrite of files in the working-dir'
	echo '    -C <file>     Location of a pacman config file'
	echo '    -M <file>     Location of a makepkg config file'
	echo '    -n            Do not copy config files into the chroot'
	echo '    -c <dir>      Set pacman cache'
	echo '    -N            Disable networking in the chroot'
	echo ' actions:'
	echo '    -i <pkg-list> Install "pkg-list" in the chroot.'
	echo '                  Creates the chroot if necessary, workding-dir must exist'
	echo '    -r <cmd>      Run "cmd" within the context of the chroot'
	echo '    -u            Update the chroot via pacman'
	echo '    -h            Print this message'

	exit ${1-1}
}

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

while getopts 'fC:M:nc:Nr:uh' arg; do
	case "${arg}" in
		f) FORCE='y' ;;
		C) pac_conf="$OPTARG" ;;
		M) makepkg_conf="$OPTARG" ;;
		n) NOCOPY='y' ;;
		c) cache_dir="$OPTARG" ;;
		N) NONETWORK='y' ;;

		r) action="-$arg"; action_arg="$OPTARG" ;;
		u|h) action="-$arg" ;;

		*) error "invalid argument '${arg}'"; usage ;;
	esac
done

shift $(($OPTIND - 1))

if [[ -n $action ]]; then
	case $# in
		0) error 'You must specify a directory.'; usage ;;
		1)
			args=("$1" "$action")
			[[ -n $action_arg ]] && args+=("$action_arg")
			set -- "${args[@]}"
			unset args action action_arg
			;;
		*) error 'Extra arguments.'; usage ;;
	esac
else
	if (( $# < 2 )); then
		error 'You must specify a directory and an action.'
		usage
	fi
fi

working_dir="$(readlink -f "${1}")"
shift 1
[[ -z $working_dir ]] && die 'Please specify a working directory.'

action=$1
shift 1
case "$action" in
	-i) PKGS=("$@") ;;
	-r) RUN="$*" ;;
	-u)
		(( $# > 0 )) && { error 'Extra arguments.'; usage; }
		RUN='/bin/sh -c "pacman -Syu --noconfirm && (pacman -Qqu >/dev/null && pacman -Su --noconfirm || exit 0)"'
		;;
	-h) usage 0 ;;
	-*) error "invalid argument '${action#-}'"; usage ;;
	*) PKGS=("$action" "$@") ;; # for compatability with mkarchroot
esac
unset action

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

if (( $EUID != 0 )); then
	die 'This script must be run as root.'
fi

if [[ -z $cache_dir ]]; then
	cache_dirs=($(pacman -v $cache_conf 2>&1 | grep '^Cache Dirs:' | sed 's/Cache Dirs:\s*//g'))
else
	cache_dirs=(${cache_dir})
fi

host_mirror=$(pacman -Sddp extra/devtools 2>/dev/null | sed -E 's#(.*/)extra/os/.*#\1$repo/os/$arch#')
if echo "${host_mirror}" | grep -q 'file://'; then
	host_mirror_path=$(echo "${host_mirror}" | sed -E 's#file://(/.*)/\$repo/os/\$arch#\1#g')
fi

# {{{ functions
build_mount_args() {
	local p
	declare -g mount_args=()

	if [[ -n $host_mirror_path ]]; then
		printf -v p '%q' "$host_mirror_path"
		mount_args+=(--bind-ro="$p")
	fi

	printf -v p '%q' "${cache_dirs[0]}"
	mount_args+=(--bind="$p")

	for cache_dir in ${cache_dirs[@]:1}; do
		printf -v p '%q' "$cache_dir"
		mount_args+=(--bind-ro="$p")
	done
}

copy_hostconf () {
	cp -a /etc/pacman.d/gnupg "${working_dir}/etc/pacman.d"
	echo "Server = ${host_mirror}" > ${working_dir}/etc/pacman.d/mirrorlist

	if [[ -n $pac_conf && $NOCOPY = 'n' ]]; then
		cp ${pac_conf} ${working_dir}/etc/pacman.conf
	fi

	if [[ -n $makepkg_conf && $NOCOPY = 'n' ]]; then
		cp ${makepkg_conf} ${working_dir}/etc/makepkg.conf
	fi

	sed -r "s|^#?\\s*CacheDir.+|CacheDir = $(echo -n ${cache_dirs[@]})|g" -i ${working_dir}/etc/pacman.conf
}

chroot_lock () {
	lock_open_write 9 "${working_dir}" "Locking chroot"
}

chroot_run() {
	local dir=$1
	shift
	local nspawn_args=(-D "$dir" "${mount_args[@]}")
	if [[ $NONETWORK = y ]]; then
		nspawn_args+=(--private-network)
	fi
	eval systemd-nspawn "${nspawn_args[@]}" -- "${@}" 2>/dev/null
}

# }}}

umask 0022
if [[ -n $RUN ]]; then
	# run chroot {{{
	#Sanity check
	if [[ ! -f "${working_dir}/.arch-chroot" ]]; then
		die "'${working_dir}' does not appear to be a Arch chroot."
	elif [[ $(cat "${working_dir}/.arch-chroot") != ${CHROOT_VERSION} ]]; then
		die "'${working_dir}' is not compatible with ${APPNAME} version ${CHROOT_VERSION}. Please rebuild."
	fi

	chroot_lock
	build_mount_args
	copy_hostconf

	chroot_run "${working_dir}" ${RUN}

	# }}}
else
	# {{{ build chroot
	if [[ -e $working_dir && $FORCE = 'n' ]]; then
		die "Working directory '${working_dir}' already exists - try using -f"
	fi

	if { type -P btrfs && btrfs subvolume create "${working_dir}"; } &>/dev/null; then
		chmod 0755 "${working_dir}"
	fi

	chroot_lock

	pacargs=("${cache_dirs[@]/#/--cachedir=}")
	if [[ -n $pac_conf ]]; then
		pacargs+=("--config=${pac_conf}")
	fi

	if (( $# != 0 )); then
		if [[ $FORCE = 'y' ]]; then
			pacargs+=("--force")
		fi
		if ! pacstrap -GMcd "${working_dir}" "${pacargs[@]}" "${PKGS[@]}"; then
			die 'Failed to install all packages'
		fi
	fi

	if [[ -d "${working_dir}/lib/modules" ]]; then
		chroot_run "${working_dir}" ldconfig
	fi

	if [[ -e "${working_dir}/etc/locale.gen" ]]; then
		sed -i 's@^#\(en_US\|de_DE\)\(\.UTF-8\)@\1\2@' "${working_dir}/etc/locale.gen"
		chroot_run "${working_dir}" locale-gen
	fi
	echo 'LANG=C' > "${working_dir}/etc/locale.conf"

	copy_hostconf

	echo "${CHROOT_VERSION}" > "${working_dir}/.arch-chroot"
	# }}}
fi