Commit c617c67f authored by Pierre Schmitz's avatar Pierre Schmitz
Browse files

mkarchroot: Use systemd's nspawn if available

* If we are running systemd use nspawn instead of our own chroot setup
* Use pacstrap to setup our chroot environment
* Make sure the common trap is still called
* Bind resolve.conf, timezone and lcoaltime from the host if nspawn is not used
* Run ldconfig within the chroot
parent 7228cc00
Loading
Loading
Loading
Loading
+83 −59
Original line number Diff line number Diff line
@@ -79,6 +79,9 @@ fi

# {{{ functions
chroot_mount() {
	trap 'trap_chroot_umount' EXIT INT QUIT TERM HUP

	if (( ! have_nspawn )); then
		[[ -e "${working_dir}/sys" ]] || mkdir "${working_dir}/sys"
		mount -o bind /sys "${working_dir}/sys"
		mount -o remount,ro,bind "${working_dir}/sys"
@@ -115,6 +118,13 @@ chroot_mount() {
		[[ -e "${working_dir}/run" ]] || mkdir "${working_dir}/run"
		mount -t tmpfs tmpfs "${working_dir}/run" -o mode=0755,nodev,nosuid,strictatime,size=64M

		for host_config in resolv.conf timezone localtime; do
			[[ -e "${working_dir}/etc/${host_config}" ]] || touch "${working_dir}/etc/${host_config}"
			mount -o bind /etc/${host_config} "${working_dir}/etc/${host_config}"
			mount -o remount,ro,bind "${working_dir}/etc/${host_config}"
		done
	fi

	if [[ -n $host_mirror_path ]]; then
		[[ -e "${working_dir}/${host_mirror_path}" ]] || mkdir -p "${working_dir}/${host_mirror_path}"
		mount -o bind "${host_mirror_path}" "${working_dir}/${host_mirror_path}"
@@ -132,12 +142,9 @@ chroot_mount() {
			cache_dir_first=false
		fi
	done

	trap 'chroot_umount' EXIT INT QUIT TERM HUP
}

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

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

chroot_umount () {
	trap - EXIT INT QUIT TERM HUP
trap_chroot_umount () {
	trap 'trap_exit' EXIT INT QUIT TERM HUP

	for cache_dir in ${cache_dirs[@]}; do
		umount "${working_dir}/${cache_dir}"
	done
	[[ -n $host_mirror_path ]] && umount "${working_dir}/${host_mirror_path}"

	if (( ! have_nspawn )); then
		for host_config in resolv.conf timezone localtime; do
			umount "${working_dir}/etc/${host_config}"
		done
		umount "${working_dir}/proc/sys"
		umount "${working_dir}/proc"
		umount "${working_dir}/sys"
@@ -161,10 +178,7 @@ chroot_umount () {
		umount "${working_dir}/dev/shm"
		umount "${working_dir}/dev"
		umount "${working_dir}/run"
	for cache_dir in ${cache_dirs[@]}; do
		umount "${working_dir}/${cache_dir}"
	done
	[[ -n $host_mirror_path ]] && umount "${working_dir}/${host_mirror_path}"
	fi
}

chroot_lock () {
@@ -180,8 +194,24 @@ chroot_lock () {
		stat_done
	fi
}

chroot_run() {
	local dir=$1
	shift
	if (( have_nspawn)); then
		eval systemd-nspawn -D "${dir}" -- ${@} 2>/dev/null
	else
		eval unshare -mui -- chroot "${dir}" ${@}
	fi
}

# }}}

# use systemd-nspawn if we have it available and systemd is running
if type -P systemd-nspawn >/dev/null && mountpoint -q /sys/fs/cgroup/systemd; then
	have_nspawn=1
fi

umask 0022
if [[ -n $RUN ]]; then
	# run chroot {{{
@@ -196,7 +226,7 @@ if [[ -n $RUN ]]; then
	chroot_mount
	copy_hostconf

	eval unshare -mui -- chroot "${working_dir}" ${RUN}
	chroot_run "${working_dir}" ${RUN}

	# }}}
else
@@ -209,37 +239,31 @@ else
		chmod 0755 "${working_dir}"
	fi

	mkdir -p "${working_dir}/var/lib/pacman/sync"
	mkdir -p "${working_dir}/etc/"

	chroot_lock
	chroot_mount

	pacargs="--noconfirm --root=${working_dir} ${cache_dirs[@]/#/--cachedir=}"
	pacargs="${cache_dirs[@]/#/--cachedir=}"
	if [[ -n $pac_conf ]]; then
		pacargs="$pacargs --config=${pac_conf}"
	fi

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

	if [[ -d "${working_dir}/lib/modules" ]]; then
		ldconfig -r "${working_dir}"
		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 "${working_dir}" /usr/sbin/locale-gen
		chroot_run "${working_dir}" locale-gen
	fi
	echo 'UTC' > "${working_dir}/etc/timezone"
	ln -s /usr/share/zoneinfo/UTC "${working_dir}/etc/localtime"
	echo 'LANG=C' > "${working_dir}/etc/locale.conf"

	copy_hostconf