~sircmpwn/builds.sr.ht

builds.sr.ht/images/debian/genimg -rwxr-xr-x 3.8 KiB
0467110eDrew DeVault qemu: upgrade to 7.0.0 6 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
#!/bin/sh -eux
echo "$release" >/dev/null # fail on -u if release unset
arch=${1:-amd64}
case $arch in
	amd64)
		iface=ens3
		qarch=x86_64
		kpkg=linux-image-amd64
		;;
	arm64)
		iface=enp0s1
		qarch=aarch64
		kpkg=linux-image-arm64
		;;
	ppc64el)
		iface=enp0s0
		qarch=ppc64le
		kpkg=linux-image-powerpc64le
		;;
	*)
		echo "unsupported architecture $arch"
		exit 1
		;;
esac

cleanup() {
	# The order here is important if you don't want to hose your mounts
	cat /mnt/debootstrap/debootstrap.log || true
	umount /mnt/dev/pts 2>/dev/null || true
	umount /mnt/dev/shm 2>/dev/null || true
	umount /mnt/dev 2>/dev/null || true
	umount /mnt/proc 2>/dev/null || true
	umount /mnt/run 2>/dev/null || true
	umount /mnt/sys 2>/dev/null || true
	umount /mnt/boot 2>/dev/null || true 
	umount /mnt 2>/dev/null || true 
	qemu-nbd --disconnect /dev/nbd0 || true
}

mkdir -p "$arch"

qemu-img create -f qcow2 $arch/root.img.qcow2 24G
modprobe nbd max_part=16
qemu-nbd --connect=/dev/nbd0 $arch/root.img.qcow2
trap cleanup EXIT

if [ "$arch" = "amd64" ]
then
	dd if=/usr/lib/syslinux/mbr/mbr.bin of=/dev/nbd0 bs=1 count=440
fi

sfdisk --no-reread /dev/nbd0 <<EOF
1M,100M,L,*
,2048M,S
,,L
EOF

mkfs.ext4 /dev/nbd0p1
mkswap /dev/nbd0p2
mkfs.ext4 /dev/nbd0p3

mount /dev/nbd0p3 /mnt
mkdir /mnt/boot
mount /dev/nbd0p1 /mnt/boot

if [ "$arch" = "amd64" ]
then
	debootstrap --include=gnupg2 --arch=$arch $release /mnt
else
	../qemu-debootstrap --include=gnupg2 --arch=$arch $release /mnt
fi

mount --bind /dev /mnt/dev
mount --bind /dev/pts /mnt/dev/pts
mount --bind /dev/shm /mnt/dev/shm
mount --bind /proc /mnt/proc
mount --bind /run /mnt/run
mount --bind /sys /mnt/sys

if [ "$arch" != "amd64" ]
then
	cp /usr/bin/qemu-$qarch-static /mnt/usr/bin
fi

run_root() {
	chroot /mnt /usr/bin/env \
		PATH=/sbin:/usr/sbin:/bin:/usr/bin \
		sh -c "$*"
}

echo 'nameserver 8.8.8.8' >/mnt/etc/resolv.conf
echo 'nameserver 8.8.4.4' >>/mnt/etc/resolv.conf
cat > /mnt/etc/network/interfaces <<EOF
auto lo
iface lo inet loopback

auto $iface
iface $iface inet static
	hostname build
	address 10.0.2.15
	netmask 255.255.255.0
	gateway 10.0.2.2
EOF
echo build > /mnt/etc/hostname
cat > /mnt/etc/hosts <<EOF 
127.0.0.1 localhost
127.0.0.1 build
EOF

echo 'APT::Install-Recommends "False";' > /mnt/etc/apt/apt.conf.d/60recommends

run_root apt-key update
run_root apt-get update
run_root apt-get -y install locales
run_root apt-get -y install $kpkg
run_root apt-get -y install build-essential git mercurial ssh sudo \
	gnupg dirmngr ca-certificates apt-transport-https curl dbus \
	systemd-timesyncd

run_root ln -sf /usr/share/zoneinfo/UTC /etc/localtime
run_root systemctl enable systemd-timesyncd.service

run_root useradd -mG sudo build
run_root passwd -d build
echo '%sudo ALL=(ALL) NOPASSWD: ALL' >> /mnt/etc/sudoers

echo "PermitEmptyPasswords yes" >> /mnt/etc/ssh/sshd_config
echo ssh >> /mnt/etc/securetty
run_root systemctl enable ssh

# Prevent docker from mucking up networking
mkdir -p /mnt/etc/docker
cat >/mnt/etc/docker/daemon.json <<EOF
{
	"bip": "172.18.0.1/16"
}
EOF

run_root update-initramfs -u

linuxver=$(ls /mnt/boot | grep vmlinuz | cut -d- -f2-)

cat >>/mnt/etc/fstab <<EOF
/dev/vda1 /boot ext4 rw,relatime,data=ordered 0 0
/dev/vda2 swap swap defaults 0 0
/dev/vda3 / ext4 rw,relatime,data=ordered 0 0
EOF

cat >/mnt/home/build/.gitconfig <<EOF
[user]
  name = builds.sr.ht
  email = builds@sr.ht
EOF
chown build:build /mnt/home/build/.gitconfig

# Boot setup
case "$arch" in 
	amd64)
		run_root apt-get -y install extlinux
		extlinux -i /mnt/boot

		cat >/mnt/boot/extlinux.conf <<-EOF
		default debian
		label debian
			linux vmlinuz-$linuxver
			initrd initrd.img-$linuxver
			append root=/dev/vda3 rw quiet
		EOF
		;;
	arm64)
		cp /mnt/boot/vmlinuz-* $arch/vmlinuz
		cp /mnt/boot/initrd.img-* $arch/initrd.img
		;;
	ppc64el)
		cp /mnt/boot/vmlinux-* $arch/vmlinux
		cp /mnt/boot/initrd.img-* $arch/initrd.img
		;;
esac

sync