2022-05-23 21:10:31 +08:00
|
|
|
#!/usr/bin/bash
|
|
|
|
#
|
2022-09-11 23:25:47 +08:00
|
|
|
# SPDX-FileCopyrightText: 2022 Celeste Liu <CoelacanthusHex@gmail.com>
|
2022-05-23 21:10:31 +08:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
# shellcheck disable=1091
|
|
|
|
. /usr/share/makepkg/util.sh
|
|
|
|
colorize
|
|
|
|
|
2022-09-11 23:25:47 +08:00
|
|
|
verbose=0
|
|
|
|
use_fixed_password=0
|
|
|
|
varbose_arg=
|
|
|
|
|
|
|
|
show_help() {
|
|
|
|
cat << EOF
|
|
|
|
Usage: ${0##*/} [-hvf] [-p PASSWORD] [-r ROOTFS] [FILENAME]
|
|
|
|
Create Arch RISC-V qemu image.
|
|
|
|
|
|
|
|
FILENAME generated image file name
|
|
|
|
default: 'archriscv-$(date --rfc-3339=date).qcow2'
|
|
|
|
|
|
|
|
-h display this help and exit
|
|
|
|
-f use fixed password instead of using systemd-firstboot to ask
|
|
|
|
-p PASSWORD set root password to PASSWORD instead of passwd in rootfs
|
|
|
|
-r ROOTFS specify rootfs file name
|
|
|
|
-v verbose mode
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
parse-args() {
|
|
|
|
local OPTIND=1
|
|
|
|
while getopts 'hvfr:p:' opt; do
|
|
|
|
case $opt in
|
|
|
|
h)
|
|
|
|
show_help
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
v) verbose=$((verbose+1))
|
|
|
|
varbose_arg="--verbose"
|
|
|
|
;;
|
|
|
|
f)
|
|
|
|
use_fixed_password=1
|
|
|
|
;;
|
|
|
|
p) password=$OPTARG
|
|
|
|
;;
|
|
|
|
r) rootfs=${OPTARG:-archriscv-$(date --rfc-3339=date).tar.zst}
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
show_help >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
shift "$(( OPTIND-1 ))"
|
|
|
|
filename=${1:-archriscv-$(date --rfc-3339=date).qcow2}
|
|
|
|
}
|
|
|
|
|
2022-05-23 23:27:56 +08:00
|
|
|
toggle-systemd-firstboot() {
|
|
|
|
msg2 "Toggle systemd-firstboot..."
|
2022-09-11 23:25:47 +08:00
|
|
|
sudo rm -f qcow2/etc/{machine-id,hostname,shadow}
|
2022-07-28 15:15:43 +08:00
|
|
|
sudo mkdir -p qcow2/etc/systemd/system/systemd-firstboot.service.d
|
2022-07-28 15:07:13 +08:00
|
|
|
cat << EOF | sudo tee qcow2/etc/systemd/system/systemd-firstboot.service.d/install.conf
|
2022-05-23 23:27:56 +08:00
|
|
|
[Service]
|
|
|
|
ExecStart=
|
2022-05-24 17:38:06 +08:00
|
|
|
ExecStart=/usr/bin/systemd-firstboot --prompt --force
|
2022-05-23 23:27:56 +08:00
|
|
|
|
|
|
|
[Install]
|
|
|
|
WantedBy=sysinit.target
|
|
|
|
EOF
|
|
|
|
sudo arch-chroot qcow2 systemctl enable systemd-firstboot.service
|
|
|
|
}
|
|
|
|
|
|
|
|
use-fixed-password() {
|
2022-09-11 23:25:47 +08:00
|
|
|
msg2 "Using fixed password... $password"
|
|
|
|
[[ -n $password ]] && sudo usermod --root $(realpath ./qcow2) --password $(openssl passwd -6 "$password") root
|
2022-05-23 23:27:56 +08:00
|
|
|
}
|
|
|
|
|
2022-09-11 23:25:47 +08:00
|
|
|
parse-args "$@"
|
|
|
|
|
2022-05-23 21:10:31 +08:00
|
|
|
msg "Building u-boot..."
|
|
|
|
|
2022-07-28 21:30:04 +08:00
|
|
|
[[ -d u-boot ]] || git clone https://github.com/u-boot/u-boot.git
|
2022-05-23 21:10:31 +08:00
|
|
|
pushd u-boot
|
2022-08-29 18:53:51 +08:00
|
|
|
git checkout master
|
|
|
|
git checkout master -- ':(top)'
|
2022-07-28 21:30:04 +08:00
|
|
|
git pull --rebase
|
2022-07-28 15:27:52 +08:00
|
|
|
git checkout v2022.07
|
2022-05-23 21:10:31 +08:00
|
|
|
msg2 "Apply binutils 2.38 compitible patch"
|
|
|
|
git apply ../0001-riscv-fix-compitible-with-binutils-2.38.patch
|
2022-08-29 18:50:37 +08:00
|
|
|
msg2 "Apply 'virtio: pci: fix bug of virtio_pci_map_capability' patch"
|
|
|
|
git apply ../0002-virtio-pci-fix-bug-of-virtio_pci_map_capability.patch
|
2022-05-23 21:10:31 +08:00
|
|
|
make \
|
|
|
|
CROSS_COMPILE=riscv64-linux-gnu- \
|
|
|
|
qemu-riscv64_smode_defconfig
|
|
|
|
make CROSS_COMPILE=riscv64-linux-gnu-
|
|
|
|
popd
|
|
|
|
|
|
|
|
msg "Building OpenSBI..."
|
|
|
|
|
2022-07-28 21:30:04 +08:00
|
|
|
[[ -d opensbi ]] || git clone https://github.com/riscv-software-src/opensbi
|
2022-05-23 21:10:31 +08:00
|
|
|
pushd opensbi
|
2022-08-29 18:53:51 +08:00
|
|
|
git checkout master
|
|
|
|
git checkout master -- ':(top)'
|
2022-07-28 21:30:04 +08:00
|
|
|
git pull --rebase
|
2022-07-28 16:22:43 +08:00
|
|
|
git checkout v1.0
|
|
|
|
msg2 "Apply binutils 2.38 compitible patch"
|
|
|
|
git cherry-pick -n 5d53b55aa77ffeefd4012445dfa6ad3535e1ff2c
|
2022-05-23 21:10:31 +08:00
|
|
|
|
|
|
|
make \
|
|
|
|
CROSS_COMPILE=riscv64-linux-gnu- \
|
2022-05-23 23:27:56 +08:00
|
|
|
PLATFORM=generic \
|
2022-05-23 21:10:31 +08:00
|
|
|
FW_PAYLOAD_PATH=../u-boot/u-boot.bin
|
|
|
|
popd
|
|
|
|
|
2022-09-11 23:25:47 +08:00
|
|
|
cp ./opensbi/build/platform/generic/firmware/fw_payload.bin opensbi_fw_payload.bin
|
|
|
|
|
2022-05-23 21:10:31 +08:00
|
|
|
msg "Create image file..."
|
2022-09-11 23:25:47 +08:00
|
|
|
qemu-img create -f qcow2 "$filename" 10G
|
2022-05-23 21:10:31 +08:00
|
|
|
sudo modprobe nbd max_part=16
|
2022-09-11 23:25:47 +08:00
|
|
|
sudo qemu-nbd -c /dev/nbd0 "$filename"
|
2022-05-23 21:10:31 +08:00
|
|
|
|
|
|
|
sudo sfdisk /dev/nbd0 <<EOF
|
|
|
|
label: dos
|
|
|
|
label-id: 0x17527589
|
|
|
|
device: /dev/nbd0
|
|
|
|
unit: sectors
|
|
|
|
|
|
|
|
/dev/nbd0p1 : start= 2048, type=83, bootable
|
|
|
|
EOF
|
|
|
|
|
|
|
|
sudo mkfs.ext4 /dev/nbd0p1
|
|
|
|
sudo e2label /dev/nbd0p1 rootfs
|
|
|
|
|
2022-07-27 22:31:08 +08:00
|
|
|
sudo mkdir -p qcow2
|
2022-05-23 21:10:31 +08:00
|
|
|
sudo mount /dev/nbd0p1 qcow2
|
2022-07-27 22:29:02 +08:00
|
|
|
sudo chown root:root qcow2
|
2022-05-23 21:10:31 +08:00
|
|
|
|
2022-07-27 19:55:25 +08:00
|
|
|
msg "Extract rootfs..."
|
|
|
|
|
|
|
|
pushd qcow2
|
2022-09-11 23:25:47 +08:00
|
|
|
sudo bsdtar $varbose_arg -pxf "../$rootfs"
|
2022-07-27 19:55:25 +08:00
|
|
|
popd
|
|
|
|
|
2022-05-23 21:10:31 +08:00
|
|
|
msg "Install kernel package..."
|
|
|
|
|
2022-07-27 20:45:02 +08:00
|
|
|
sudo arch-chroot qcow2 pacman \
|
2022-05-23 21:10:31 +08:00
|
|
|
--noconfirm \
|
2022-08-25 07:30:52 +03:00
|
|
|
-Syu linux linux-firmware
|
2022-05-23 21:10:31 +08:00
|
|
|
|
2022-07-28 15:02:41 +08:00
|
|
|
sudo mkdir -p qcow2/boot/extlinux
|
|
|
|
cat << EOF | sudo tee qcow2/boot/extlinux/extlinux.conf
|
2022-05-23 21:10:31 +08:00
|
|
|
menu title Arch RISC-V QEMU Boot
|
|
|
|
timeout 100
|
|
|
|
default linux
|
|
|
|
|
|
|
|
label linux
|
|
|
|
menu label Linux linux
|
|
|
|
kernel /boot/vmlinuz-linux
|
2022-08-03 07:10:27 +03:00
|
|
|
initrd /boot/initramfs-linux-fallback.img
|
2022-05-23 21:10:31 +08:00
|
|
|
append earlyprintk rw root=/dev/vda1 rootwait rootfstype=ext4 LANG=en_US.UTF-8 console=ttyS0
|
|
|
|
EOF
|
|
|
|
|
|
|
|
msg "Clean up..."
|
|
|
|
msg2 "Clean up pacman package cache..."
|
|
|
|
yes y | sudo pacman \
|
2022-07-27 19:55:25 +08:00
|
|
|
--sysroot ./qcow2 \
|
2022-05-23 21:10:31 +08:00
|
|
|
--sync --clean --clean
|
|
|
|
|
2022-09-11 23:25:47 +08:00
|
|
|
(( use_fixed_password==0 )) && toggle-systemd-firstboot || use-fixed-password
|
2022-05-23 21:10:31 +08:00
|
|
|
|
|
|
|
msg2 "Unmount..."
|
|
|
|
sudo umount qcow2
|
|
|
|
sudo qemu-nbd -d /dev/nbd0
|