-
Notifications
You must be signed in to change notification settings - Fork 6
/
rpixen.sh
executable file
·409 lines (327 loc) · 12 KB
/
rpixen.sh
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#!/bin/bash
# SPDX-License-Identifier: MIT
# Copyright (c) 2019, DornerWorks, Ltd.
# Author: Stewart Hildebrand
#
# Copyright (c) 2020, MERA
# Author: Leonid Lazarev
PROXY_CFG="";
DNS_SERVER="8.8.8.8";
ARCH_CFG="arm64";
IMAGE_SIZE=2048;
# build type guest or host
BUILD_TYPE="host";
helpFunction()
{
echo ""
echo "Usage: $0 [-p proxy] [-d dns server] [-s image size]"
echo " -p http proxy to be used in format site.domain.com"
echo " -d dns server"
echo " -s image size in MB. Default 2048"
echo " -h help"
exit 1 # Exit script after printing help
}
optstring="p:d:s:h"
while getopts $optstring opt
do
case $opt in
p ) PROXY_CFG="$OPTARG" ;;
d ) DNS_SERVER="$OPTARG" ;;
s ) IMAGE_SIZE="$OPTARG" ;;
h | *) helpFunction ;; # Print helpFunction in case parameter is non-existent
esac
done
WRKDIR=$(pwd)/
SCRIPTDIR=$(cd $(dirname $0) && pwd)/
USERNAME=pi
PASSWORD=123
SALT=dw
HASHED_PASSWORD=$(perl -e "print crypt(\"${PASSWORD}\",\"${SALT}\");")
HOSTNAME=ubuntu
VARIANT=dom0
BUILD_ARCH=$ARCH_CFG
sudo apt install device-tree-compiler tftpd-hpa flex bison qemu-utils kpartx git curl qemu-user-static binfmt-support parted bc libncurses5-dev libssl-dev pkg-config python acpica-tools u-boot-tools
source ${SCRIPTDIR}toolchain-aarch64-linux-gnu.sh
DTBFILE=bcm2711-rpi-4-b.dtb
# Clone firmaware source
if [ ! -d firmware ]; then
git clone --depth 1 https://github.com/raspberrypi/firmware.git
fi
if [ ! -d xen ]; then
git clone git://xenbits.xen.org/xen.git
cd xen
git checkout RELEASE-4.13.0
git am ${SCRIPTDIR}patches/xen/0001-XEN-on-RPi4-1GB-lmitation-workaround-XEN-tries-to-al.patch
cd ${WRKDIR}
fi
if [ ! -d linux ]; then
git clone --depth 1 --branch rpi-4.19.y https://github.com/raspberrypi/linux.git linux
cd linux
git am ${SCRIPTDIR}patches/linux/*.patch
cd ${WRKDIR}
fi
# Clone u-boot and build
if [ ! -d u-boot ]; then
git clone https://github.com/u-boot/u-boot.git
fi
#compile uboot for rpi4
if [ ! -s ${WRKDIR}u-boot/u-boot.bin ]; then
cd u-boot
if [ "${BUILD_ARCH}" == "arm64" ]; then
make CROSS_COMPILE=aarch64-linux-gnu- rpi_arm64_defconfig
make CROSS_COMPILE=aarch64-linux-gnu- -j $(nproc)
fi
cd ${WRKDIR}
fi
# Build xen
if [ ! -s ${WRKDIR}xen/xen/xen ]; then
cd ${WRKDIR}xen
if [ ! -s xen/.config ]; then
echo "CONFIG_DEBUG=y" > xen/arch/arm/configs/arm64_defconfig
echo "CONFIG_SCHED_ARINC653=y" >> xen/arch/arm/configs/arm64_defconfig
make -C xen XEN_TARGET_ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- CONFIG_EARLY_PRINTK=8250,0xfe215040,2 defconfig
fi
make XEN_TARGET_ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- CONFIG_EARLY_PRINTK=8250,0xfe215040,2 dist-xen -j $(nproc)
cd ${WRKDIR}
fi
# Build Linux
cd ${WRKDIR}linux
if [ "${BUILD_ARCH}" == "arm64" ]; then
if [ ! -s ${WRKDIR}linux/.build-arm64/.config ]; then
# utilize kernel/configs/xen.config fragment
make O=.build-arm64 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- bcm2711_defconfig xen.config
fi
make O=.build-arm64 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -j $(nproc) broadcom/${DTBFILE}
if [ ! -s ${WRKDIR}linux/.build-arm64/arch/arm64/boot/Image ]; then
echo "Building kernel. This takes a while. To monitor progress, open a new terminal and use \"tail -f buildoutput.log\""
make O=.build-arm64 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -j $(nproc) > ${WRKDIR}buildoutput.log 2> ${WRKDIR}buildoutput2.log
fi
fi
cd ${WRKDIR}
if [ ! -d bootfiles ]; then
mkdir bootfiles
fi
cp ${WRKDIR}firmware/boot/fixup4*.dat ${WRKDIR}firmware/boot/start4*.elf bootfiles/
if [ "${BUILD_ARCH}" == "arm64" ]; then
cp ${WRKDIR}linux/.build-arm64/arch/arm64/boot/dts/broadcom/${DTBFILE} bootfiles/
fi
#prepare u-boot script
cd boot_scripts
mkimage -A arm -T script -C none -n "RPI4 Boot script" -d "boot.cmd" boot.scr
cd ${WRKDIR}
#add the command line parameters for dom0 kernel loading
cat > bootfiles/cmdline.txt <<EOF
console=hvc0 clk_ignore_unused root=/dev/mmcblk0p2 rootwait
EOF
# https://www.raspberrypi.org/documentation/configuration/config-txt/boot.md
# We could use the kernel option to overwrite the default kernel name
# Load u-boot.bin instead of kernel
#
# Due to limitation for Arm64 mode: https://github.com/raspberrypi/linux/issues/3093
# USB works only if the total_mem is 3G or less
#
cat > bootfiles/config.txt <<EOF
kernel=u-boot.bin
arm_64bit=1
device_tree=${DTBFILE}
total_mem=3072
enable_gic=1
#disable_overscan=1
# Enable audio (loads snd_bcm2835)
dtparam=audio=on
[pi4]
max_framebuffers=2
[all]
enable_jtag_gpio=1
enable_uart=1
init_uart_baud=115200
EOF
# Copy u-boot script to boot
cp ${WRKDIR}boot_scripts/boot.scr bootfiles/
# Copy u-boot binary to boot
cp ${WRKDIR}u-boot/u-boot.bin bootfiles/
# Copy xen to the boot partition
cp ${WRKDIR}xen/xen/xen bootfiles/
# Copy kernel to boot partion
if [ "${BUILD_ARCH}" == "arm64" ]; then
cp ${WRKDIR}linux/.build-arm64/arch/arm64/boot/Image bootfiles/
fi
#copy rpi bootfiles to boot partion
if [ -d /media/${USER}/boot/ ]; then
cp bootfiles/* /media/${USER}/boot/
fi
sync
MNTRAMDISK=/mnt/dom0_ramdisk/
MNTROOTFS=/mnt/dom0_rpi-arm64-rootfs/
MNTBOOT=${MNTROOTFS}boot/
IMGFILE=${MNTRAMDISK}rpixen.img
ROOTFS=${VARIANT}-ubuntu-base-18.04.3-base-${BUILD_ARCH}-prepped.tar.gz
if [ ! -s ${ROOTFS} ]; then
./ubuntu-base-prep.sh ${ROOTFS} ${MNTRAMDISK} ${BUILD_ARCH} ${DNS_SERVER} ${PROXY_CFG}
fi
unmountstuff () {
sudo umount ${MNTROOTFS}proc || true
sudo umount ${MNTROOTFS}dev/pts || true
sudo umount ${MNTROOTFS}dev || true
sudo umount ${MNTROOTFS}sys || true
sudo umount ${MNTROOTFS}tmp || true
sudo umount ${MNTBOOT} || true
sudo umount ${MNTROOTFS} || true
}
mountstuff () {
sudo mkdir -p ${MNTROOTFS}
if ! mount | grep ${LOOPDEVROOTFS}; then
sudo mount ${LOOPDEVROOTFS} ${MNTROOTFS}
fi
sudo mkdir -p ${MNTBOOT}
sudo mount ${LOOPDEVBOOT} ${MNTBOOT}
sudo mount -o bind /proc ${MNTROOTFS}proc
sudo mount -o bind /dev ${MNTROOTFS}dev
sudo mount -o bind /dev/pts ${MNTROOTFS}dev/pts
sudo mount -o bind /sys ${MNTROOTFS}sys
sudo mount -o bind /tmp ${MNTROOTFS}tmp
}
finish () {
cd ${WRKDIR}
sudo sync
unmountstuff
sudo kpartx -dvs ${IMGFILE} || true
sudo rmdir ${MNTROOTFS} || true
mv ${IMGFILE} . || true
sudo umount ${MNTRAMDISK} || true
sudo rmdir ${MNTRAMDISK} || true
}
trap finish EXIT
sudo mkdir -p ${MNTRAMDISK}
sudo mount -t tmpfs -o size=3g tmpfs ${MNTRAMDISK}
qemu-img create ${IMGFILE} ${IMAGE_SIZE}M
/sbin/parted ${IMGFILE} --script -- mklabel msdos
/sbin/parted ${IMGFILE} --script -- mkpart primary fat32 2048s 264191s
/sbin/parted ${IMGFILE} --script -- mkpart primary ext4 264192s -1s
LOOPDEVS=$(sudo kpartx -avs ${IMGFILE} | awk '{print $3}')
LOOPDEVBOOT=/dev/mapper/$(echo ${LOOPDEVS} | awk '{print $1}')
LOOPDEVROOTFS=/dev/mapper/$(echo ${LOOPDEVS} | awk '{print $2}')
sudo mkfs.vfat ${LOOPDEVBOOT}
sudo mkfs.ext4 ${LOOPDEVROOTFS}
sudo fatlabel ${LOOPDEVBOOT} boot
sudo e2label ${LOOPDEVROOTFS} RpiUbuntu
sudo mkdir -p ${MNTROOTFS}
sudo mount ${LOOPDEVROOTFS} ${MNTROOTFS}
sudo tar -C ${MNTROOTFS} -xf ${ROOTFS}
mountstuff
sudo cp -r bootfiles/* ${MNTBOOT}
cd ${WRKDIR}linux
if [ "${BUILD_ARCH}" == "arm64" ]; then
sudo --preserve-env PATH=${PATH} make O=.build-arm64 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- INSTALL_MOD_PATH=${MNTROOTFS} modules_install > ${WRKDIR}modules_install.log
fi
cd ${WRKDIR}
# Build Xen tools
if [ "${BUILD_ARCH}" == "arm64" ]; then
CROSS_PREFIX=aarch64-linux-gnu
XEN_ARCH=arm64
fi
# Change the shared library symlinks to relative instead of absolute so they play nice with cross-compiling
sudo chroot ${MNTROOTFS} symlinks -c /usr/lib/${CROSS_PREFIX}/
cd ${WRKDIR}xen
# TODO: --with-xenstored=oxenstored
# Ask the native compiler what system include directories it searches through.
SYSINCDIRS=$(echo $(sudo chroot ${MNTROOTFS} bash -c "echo | gcc -E -Wp,-v -o /dev/null - 2>&1" | grep "^ " | sed "s|^ /| -isystem${MNTROOTFS}|"))
SYSINCDIRSCXX=$(echo $(sudo chroot ${MNTROOTFS} bash -c "echo | g++ -x c++ -E -Wp,-v -o /dev/null - 2>&1" | grep "^ " | sed "s|^ /| -isystem${MNTROOTFS}|"))
CC="${CROSS_PREFIX}-gcc --sysroot=${MNTROOTFS} -nostdinc ${SYSINCDIRS} -B${MNTROOTFS}lib/${CROSS_PREFIX} -B${MNTROOTFS}usr/lib/${CROSS_PREFIX}"
CXX="${CROSS_PREFIX}-g++ --sysroot=${MNTROOTFS} -nostdinc ${SYSINCDIRSCXX} -B${MNTROOTFS}lib/${CROSS_PREFIX} -B${MNTROOTFS}usr/lib/${CROSS_PREFIX}"
LDFLAGS="-Wl,-rpath-link=${MNTROOTFS}lib/${CROSS_PREFIX} -Wl,-rpath-link=${MNTROOTFS}usr/lib/${CROSS_PREFIX}"
PKG_CONFIG=pkg-config \
PKG_CONFIG_LIBDIR=${MNTROOTFS}usr/lib/${CROSS_PREFIX}/pkgconfig:${MNTROOTFS}usr/share/pkgconfig \
PKG_CONFIG_SYSROOT_DIR=${MNTROOTFS} \
LDFLAGS="${LDFLAGS}" \
./configure \
PYTHON_PREFIX_ARG=--install-layout=deb \
--enable-systemd \
--disable-xen \
--enable-tools \
--disable-docs \
--disable-stubdom \
--prefix=/usr \
--with-xenstored=xenstored \
--build=x86_64-linux-gnu \
--host=${CROSS_PREFIX} \
CC="${CC}" \
CXX="${CXX}"
PKG_CONFIG=pkg-config \
PKG_CONFIG_LIBDIR=${MNTROOTFS}usr/lib/${CROSS_PREFIX}/pkgconfig:${MNTROOTFS}usr/share/pkgconfig \
PKG_CONFIG_SYSROOT_DIR=${MNTROOTFS} \
LDFLAGS="${LDFLAGS}" \
make dist-tools \
CROSS_COMPILE=${CROSS_PREFIX}- XEN_TARGET_ARCH=${XEN_ARCH} \
CC="${CC}" \
CXX="${CXX}" \
-j $(nproc)
sudo --preserve-env PATH=${PATH} \
PKG_CONFIG=pkg-config \
PKG_CONFIG_LIBDIR=${MNTROOTFS}usr/lib/${CROSS_PREFIX}/pkgconfig:${MNTROOTFS}usr/share/pkgconfig \
PKG_CONFIG_SYSROOT_DIR=${MNTROOTFS} \
LDFLAGS="${LDFLAGS}" \
make install-tools \
CROSS_COMPILE=${CROSS_PREFIX}- XEN_TARGET_ARCH=${XEN_ARCH} \
CC="${CC}" \
CXX="${CXX}" \
DESTDIR=${MNTROOTFS}
sudo chroot ${MNTROOTFS} systemctl enable xen-qemu-dom0-disk-backend.service
sudo chroot ${MNTROOTFS} systemctl enable xen-init-dom0.service
sudo chroot ${MNTROOTFS} systemctl enable xenconsoled.service
sudo chroot ${MNTROOTFS} systemctl enable xendomains.service
sudo chroot ${MNTROOTFS} systemctl enable xen-watchdog.service
cd ${WRKDIR}
# It seems like the xen tools configure script selects a few too many of these backend driver modules, so we override it with a simpler list.
# /usr/lib/modules-load.d/xen.conf
sudo bash -c "cat > ${MNTROOTFS}usr/lib/modules-load.d/xen.conf" <<EOF
xen-evtchn
xen-gntdev
xen-gntalloc
xen-blkback
xen-netback
EOF
# /etc/hostname
sudo bash -c "echo ${HOSTNAME} > ${MNTROOTFS}etc/hostname"
# /etc/hosts
sudo bash -c "cat > ${MNTROOTFS}etc/hosts" <<EOF
127.0.0.1 localhost
127.0.1.1 ${HOSTNAME}
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
EOF
# /etc/fstab
sudo bash -c "cat > ${MNTROOTFS}etc/fstab" <<EOF
proc /proc proc defaults 0 0
/dev/mmcblk0p1 /boot vfat defaults 0 2
/dev/mmcblk0p2 / ext4 defaults,noatime 0 1
EOF
# /etc/network/interfaces.d/eth0
sudo bash -c "cat > ${MNTROOTFS}etc/network/interfaces.d/eth0" <<EOF
auto eth0
iface eth0 inet manual
EOF
sudo chmod 0644 ${MNTROOTFS}etc/network/interfaces.d/eth0
# /etc/network/interfaces.d/xenbr0
sudo bash -c "cat > ${MNTROOTFS}etc/network/interfaces.d/xenbr0" <<EOF
auto xenbr0
iface xenbr0 inet dhcp
bridge_ports eth0
EOF
sudo chmod 0644 ${MNTROOTFS}etc/network/interfaces.d/xenbr0
# Don't wait forever and a day for the network to come online
if [ -s ${MNTROOTFS}lib/systemd/system/networking.service ]; then
sudo sed -i -e "s/TimeoutStartSec=5min/TimeoutStartSec=15sec/" ${MNTROOTFS}lib/systemd/system/networking.service
fi
if [ -s ${MNTROOTFS}lib/systemd/system/ifup@.service ]; then
sudo bash -c "echo \"TimeoutStopSec=15s\" >> ${MNTROOTFS}lib/systemd/system/ifup@.service"
fi
# User account setup
sudo chroot ${MNTROOTFS} useradd -s /bin/bash -G adm,sudo -l -m -p ${HASHED_PASSWORD} ${USERNAME}
# Password-less sudo
sudo chroot ${MNTROOTFS} /bin/bash -euxc "echo \"${USERNAME} ALL=(ALL) NOPASSWD:ALL\" > /etc/sudoers.d/90-${USERNAME}-user"
df -h | grep -e "Filesystem" -e "/dev/mapper/loop"