-
Notifications
You must be signed in to change notification settings - Fork 10
/
mkimg
executable file
·234 lines (197 loc) · 5.82 KB
/
mkimg
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
#!/usr/bin/bash
#
# SPDX-FileCopyrightText: 2022 Celeste Liu <CoelacanthusHex@gmail.com>
# SPDX-License-Identifier: GPL-3.0-or-later
# shellcheck disable=1091
. /usr/share/makepkg/util.sh
colorize
verbose=0
use_fixed_password=0
build_firmware=1
varbose_arg=
rootfs="archriscv-$(date --rfc-3339=date).tar.zst"
fstype=ext4
kernel=linux
kernel_suffix=""
cmdline=""
uboot_version=v2023.10
opensbi_version=v1.3.1
show_help() {
cat << EOF
Usage: ${0##*/} [-hvfd] [-p PASSWORD] [-r ROOTFS] [-t FSTYPE] [FILENAME]
Create Arch RISC-V distro image.
FILENAME generated image file name
default: 'archriscv-$(date --rfc-3339=date).qcow2'
unless the extension is qcow2, implies raw disk format
-h display this help and exit
-f use fixed password instead of using systemd-firstboot to ask
-d only build the disk image and omit building OpenSBI/U-Boot
-p PASSWORD set root password to PASSWORD instead of passwd in rootfs
-r ROOTFS specify rootfs file name
-t FSTYPE specify rootfs filesystem type (default: ext4)
-k KERNEL specify kernel package name (default: linux)
-c CMDLINE append CMDLINE to kernel command line
-v verbose mode
EOF
}
parse-args() {
local OPTIND=1
while getopts 'hvfdr:t:p:k:c:' opt; do
case $opt in
h) show_help
exit 0
;;
v) verbose=$((verbose+1))
varbose_arg="--verbose"
;;
f) use_fixed_password=1
;;
d) build_firmware=0
;;
p) password=$OPTARG
;;
r) rootfs=$OPTARG
;;
t) fstype=$OPTARG
;;
k) kernel=$OPTARG
;;
c) cmdline=$OPTARG
;;
*) show_help >&2
exit 1
;;
esac
done
shift "$(( OPTIND-1 ))"
filename=${1:-archriscv-$(date --rfc-3339=date).qcow2}
}
toggle-systemd-firstboot() {
msg2 "Toggle systemd-firstboot..."
sudo rm -f mnt/etc/{machine-id,hostname,shadow}
# Mask Arch filesystem's factory to avoid the removed shadow file being copied over
sudo ln -s /dev/null mnt/etc/tmpfiles.d/arch.conf
}
use-fixed-password() {
msg2 "Using fixed password... $password"
[[ -n $password ]] && sudo usermod --root $(realpath ./mnt) --password $(openssl passwd -6 "$password") root
}
parse-args "$@"
if [ $build_firmware == 1 ]
then
msg "Building U-Boot..."
if [[ -d u-boot ]]; then
pushd u-boot
git checkout HEAD -- ':(top)'
git fetch origin
git checkout $uboot_version
popd
else
git clone --filter=blob:none -b $uboot_version https://github.com/u-boot/u-boot.git
fi
pushd u-boot
make \
CROSS_COMPILE=riscv64-linux-gnu- \
qemu-riscv64_smode_defconfig
./scripts/config \
-e CMD_BTRFS -e FS_BTRFS
make \
CROSS_COMPILE=riscv64-linux-gnu- \
olddefconfig
make CROSS_COMPILE=riscv64-linux-gnu-
popd
msg "Building OpenSBI..."
if [[ -d opensbi ]]; then
pushd opensbi
git checkout HEAD -- ':(top)'
git fetch origin
git checkout $opensbi_version
popd
else
git clone --filter=blob:none -b $opensbi_version https://github.com/riscv-software-src/opensbi
fi
pushd opensbi
# If user set it, OpenSBI will failed to build.
unset PYTHONSAFEPATH
make \
CROSS_COMPILE=riscv64-linux-gnu- \
PLATFORM=generic \
FW_PAYLOAD_PATH=../u-boot/u-boot.bin
popd
cp ./opensbi/build/platform/generic/firmware/fw_payload.bin opensbi_fw_payload.bin
fi
if [[ $filename == *.qcow2 ]]
then
msg "Create QCOW2 image..."
qemu-img create -f qcow2 "$filename" 10G
sudo modprobe nbd max_part=16 || exit 1
# Possible NBD device collision?
sudo qemu-nbd -c /dev/nbd0 "$filename"
loopdev=/dev/nbd0
else
msg "Create raw image..."
fallocate "$filename" -l 10G
loopdev=$(sudo losetup --show -P -f "$filename")
fi
msg "Partitioning..."
# FIXME: otherwise NBD device is not ready
sleep 1s
sudo parted "$loopdev" mklabel gpt mkpart "" "$fstype" 0% 100%
sudo partprobe "$loopdev"
partdev="$loopdev"p1
sudo mkfs.$fstype -L rootfs "$partdev"
sudo mkdir -p mnt
sudo mount "$partdev" mnt
sudo chown root:root mnt
uuid=$(sudo findmnt mnt -o UUID -n)
msg "Extract rootfs..."
pushd mnt
sudo bsdtar $varbose_arg -kpxf "../$rootfs"
popd
msg "Install kernel package..."
if [[ $kernel != linux ]]
then
kernel_suffix="${kernel#linux}"
echo -e "[unsupported]\nInclude = /etc/pacman.d/mirrorlist" | sudo tee -a mnt/etc/pacman.conf
fi
sudo systemd-nspawn -D mnt pacman \
--noconfirm --needed \
-Syu $kernel linux-firmware
sudo mkdir -p mnt/boot/extlinux
cat << EOF | sudo tee mnt/boot/extlinux/extlinux.conf
menu title Arch RISC-V Boot Menu
timeout 100
default linux-fallback
label linux
menu label Linux linux
kernel /boot/vmlinuz-linux$kernel_suffix
initrd /boot/initramfs-linux$kernel_suffix.img
append earlyprintk rw root=UUID=$uuid rootwait console=ttyS0,115200 $cmdline
label linux-fallback
menu label Linux linux (fallback initramfs)
kernel /boot/vmlinuz-linux$kernel_suffix
initrd /boot/initramfs-linux$kernel_suffix-fallback.img
append earlyprintk rw root=UUID=$uuid rootwait console=ttyS0,115200 $cmdline
EOF
cat << EOF | sudo tee mnt/etc/systemd/network/default.network
[Match]
Name=en*
[Network]
DHCP=yes
EOF
sudo systemd-nspawn -D mnt systemctl enable systemd-networkd.service
msg "Clean up..."
msg2 "Clean up pacman package cache..."
yes y | sudo pacman \
--sysroot ./mnt \
--sync --clean --clean
(( use_fixed_password==0 )) && toggle-systemd-firstboot || use-fixed-password
msg2 "Unmount and sync..."
sudo umount mnt
if [[ $filename == *.qcow2 ]]
then
sudo qemu-nbd -d "$loopdev"
else
sudo losetup -d "$loopdev"
fi
sudo sync