~sircmpwn/builds.sr.ht

builds.sr.ht/images/control -rwxr-xr-x 3.9 KiB
c2e5cf82Simon Ser contrib/crontab: add freebsd/13.x job 4 days ago
                                                                                
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/sh -eu
self=$(readlink -f $0)
self=$(dirname "$self")

if [ -f /etc/image-control.conf ]
then
	. /etc/image-control.conf
fi

base=$1
cmd=$2
shift 2

if [ ! -d $self/$base ]
then
	echo "Unknown base image $base"
	exit 1
fi

ssh_opts="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
guestport=22

guest_ssh() {
	ssh $ssh_opts "$@"
}

cpu_opts() {
	if [ "$(uname -m)" = "$1" ] && [ -e /dev/kvm ]
	then
		printf "%s" "-cpu host -enable-kvm"
		return
	fi
	case "$1" in
		aarch64)
			printf "%s" "-M virt -cpu cortex-a53"
			;;
		x86_64)
			printf "%s" "-cpu qemu64"
			;;
		ppc64le)
			printf "%s" "-cpu power9 -machine pseries"
			;;
		*)
			echo "Unsupported architecture $arch" >&2
			exit 1
			;;
	esac
}

_docker_boot() {
	docker run -d \
		-v "$self/$base":/base:ro \
		--mount type=tmpfs,destination=/var/tmp \
		--device /dev/kvm \
		--cpus=2 \
		-p 127.0.0.1:$port:$port \
		--rm \
		--name "builds_job_${BUILD_JOB_ID:-unknown_$(date +"%s")}" \
		qemu /bin/${qemu:-qemu-system-$arch} \
		-m ${MEMORY:-4096} \
		-smp cpus=2 \
		-net nic,model=virtio -net user,hostfwd=tcp::$port-:$guestport \
		-display none \
		-device virtio-rng-pci \
		-device virtio-balloon \
		-drive file="$wd/$arch/root.img.qcow2",media=disk,snapshot=on,${driveopts:-if=virtio} \
		"$@" > /tmp/docker-$port.id
}

_qemu_boot() {
	${qemu:-qemu-system-$arch} \
		-pidfile /tmp/qemu-$port.id \
		-m ${MEMORY:-4096} \
		-smp cpus=2 \
		-net nic,model=virtio -net user,hostfwd=tcp:127.0.0.1:$port-:$guestport \
		-display none \
		-device virtio-rng-pci \
		-device virtio-balloon \
		-drive file="$wd/$arch/root.img.qcow2",media=disk,snapshot=on,${driveopts:-if=virtio} \
		"$@" &
}

_qemu_chroot_boot() {
	qemu-chroot \
		-p /tmp/qemu-$port.id \
		-b "$self/$base":/base \
		-b /var/tmp:/var/tmp \
		/bin/${qemu:-qemu-system-$arch} \
		-m ${MEMORY:-4096} \
		-smp cpus=2 \
		-net nic,model=virtio -net user,hostfwd=tcp:127.0.0.1:$port-:$guestport \
		-display none \
		-device virtio-rng-pci \
		-device virtio-balloon \
		-drive file="$wd/$arch/root.img.qcow2",media=disk,snapshot=on,${driveopts:-if=virtio} \
		"$@" &
}

_boot() {
	if [ "$means" = "docker" ]
	then
		_docker_boot "$@"
	elif [ "$means" = "qemu" ]
	then
		_qemu_boot "$@"
	elif [ "$means" = "qemu-chroot" ]
	then
		_qemu_chroot_boot "$@"
	fi
}

cmd_boot() {
	arch=$1
	shift

	if [ "$arch" = "default" ]
	then
		arch="$default_arch"
	fi
	if [ ! -e "$self/$base/$arch/root.img.qcow2" ]
	then
		printf "Unknown image $base\n"
		exit 1
	fi

	port=$1
	if [ "$#" -gt 1 ]
	then
		means=$2
	else
		means="${default_means:-docker}"
	fi

	if [ "$means" = "docker" ]
	then
		wd="/base"
	elif [ "$means" = "qemu" ]
	then
		wd="$self/$base"
	elif [ "$means" = "qemu-chroot" ]
	then
		wd="/base"
	else
		printf "Unknown boot mode %s\n" "$means" >2
		exit 1
	fi

	boot
}

_wait_boot() {
	port=$1
	attempts=0
	echo "Waiting for VM to come up..."
	while ! guest_ssh -p "$port" build@localhost true 2>&1 >/dev/null
	do
		sleep 5
		attempts=$((attempts + 1))
		if [ "$attempts" -eq 20 ]
		then
			echo "Giving up."
			cmd_cleanup "$port"
			exit 1
		fi
		echo "Attempt $attempts..."
	done
}

cmd_cleanup() {
	port=$1
	# Power off
	if [ "$#" -eq 1 ]
	then
		if [ -e /tmp/docker-$port.id ]
		then
			cid=$(cat /tmp/docker-$port.id)
			guest_ssh -p $port build@localhost $poweroff_cmd || true
			sleep 2
			docker kill $cid && sleep 2 || true
			rm /tmp/docker-$port.id
		fi
		if [ -e /tmp/qemu-$port.id ]
		then
			cid=$(cat /tmp/qemu-$port.id)
			guest_ssh -p $port build@localhost $poweroff_cmd || true
			sleep 2
			kill $cid || true
			rm -f /tmp/qemu-$port.id
		fi
	fi
}

if ! [ -e "$self/$base/functions" ]
then
	printf "%s" "Unknown base image '$base'" >&2
	exit 1
fi

. $self/$base/functions

case "$cmd" in
	boot)
		cmd_boot "$@"
		;;
	cleanup)
		cmd_cleanup "$@"
		;;
	sanity-check)
		sanity_check "$@"
		;;
	install)
		install "$@"
		;;
	add-repo)
		add_repository "$@"
		;;
	ssh)
		port=$1
		shift
		guest_ssh -p "$port" build@localhost "$@"
		;;
	*)
		echo "Unknown command $cmd"
		;;
esac