summarylogtreecommitdiffstats
path: root/arch.build
blob: 53f937ca78f3110b93568c6a97ddd008fd20d628 (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
#!/bin/bash

set -e

arch=$1
shift
repo=$@

case $arch in
	x86_64) cpu=qemu64 ;;
	i686) cpu=qemu32 ;;
	*) exit 1 ;;
esac

img=/var/lib/buildstuff/$arch.qcow2
vport=$((32768+$RANDOM))
vhost="
	-o ConnectTimeout=3
	-o StrictHostKeyChecking=no
	-o UserKnownHostsFile=/dev/null
	-o Port=$vport user@localhost
"

start_vm() {
	qemu-system-x86_64 \
		-cpu $cpu -smp 4 -m 4G \
		-accel kvm:tcg,thread=multi \
		-device virtio-net,netdev=vlan \
		-netdev user,id=vlan,hostfwd=::$vport-:22 \
		-drive file=$1 &
	while sleep 1; do
		ssh $vhost 'curl -o /dev/null -s vesath.org' &&
		break
	done
	cat /etc/pacman.d/mirrorlist |
	ssh $vhost 'sudo dd of=/etc/pacman.d/mirrorlist'
}

stop_vm() {
	ssh $vhost 'sudo poweroff' || true
	wait
	rm -f /tmp/$vport
}

trap_err() {
	trap - ERR
	trap - INT
	cat <<EOF

Investigate the error with \`ssh -p $vport user@localhost\`
then press enter here to terminate the virtual machine.
EOF
	read x
	stop_vm
}
trap trap_err ERR
trap trap_err INT
set -E


[[ -e $img ]] || {
	sudo arch.install $arch $img
	sudo chown "$USER" $img
}

(( $(date +%s) - $(stat -c%Y $img) > 86400 )) && {
	start_vm $img
	ssh $vhost 'sudo pacman --noconfirm -Syu'
	ssh $vhost 'sudo pacman --noconfirm -Su'
	ssh $vhost 'sudo paccache --remove --keep 0'
	stop_vm
}

qemu-img create -f qcow2 -o backing_file=$img /tmp/$vport 8G

start_vm /tmp/$vport
for r in $repo; do
	echo "sudo sed '/\[core\]/i [$r]\nInclude = /etc/pacman.d/mirrorlist\n' -i /etc/pacman.conf"
done | ssh $vhost
ssh $vhost 'sudo pacman --noconfirm -Syu'
ssh $vhost 'sudo pacman --noconfirm -Su'
ssh $vhost 'sudo install -o user -g user -d /build'
sftp $vhost <<<"mkdir .gnupg"
sftp $vhost <<<"put $HOME/.makepkg.conf"
sftp $vhost <<<"put $HOME/.gnupg/pubring.kbx .gnupg/pubring.kbx"
sftp $vhost <<<'put -r . /build' # fake chroot build for db-update
ssh $vhost 'rm -fr /build/{src,pkg}' # fix source repo relocation
ssh $vhost 'cd /build; makepkg --log --noconfirm --syncdeps'
sftp $vhost <<<'get /build/*.pkg.tar.xz'
sftp $vhost <<<'get /build/*.log'
stop_vm