-
Notifications
You must be signed in to change notification settings - Fork 6
/
build_qemu.sh
153 lines (122 loc) · 4.23 KB
/
build_qemu.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
#!/bin/bash
build_kernel() {
if [ ! -d $build_dir/linux ]; then
git clone --depth=1 https://github.com/torvalds/linux.git -b v6.6
fi
cd linux
if [ -f arch/riscv/configs/linux-qemu-current_defconfig ]; then
rm arch/riscv/configs/linux-qemu-current_defconfig
fi
cp $build_dir/config/linux-qemu-current.config arch/riscv/configs/linux-qemu-current_defconfig
make ARCH=riscv CROSS_COMPILE=riscv64-linux-gnu- linux-qemu-current_defconfig
make ARCH=riscv CROSS_COMPILE=riscv64-linux-gnu- -j$(nproc)
make ARCH=riscv CROSS_COMPILE=riscv64-linux-gnu- modules_install INSTALL_MOD_PATH=kmod
cd $build_dir
cp -rfp linux/kmod/lib/modules/* rootfs/lib/modules
}
build_u-boot() {
if [ ! -d $build_dir/u-boot ]; then
git clone --depth=1 https://github.com/u-boot/u-boot.git -b v2022.04
fi
cd u-boot
make ARCH=riscv CROSS_COMPILE=riscv64-linux-gnu- qemu-riscv64_smode_defconfig
make ARCH=riscv CROSS_COMPILE=riscv64-linux-gnu- -j$(nproc)
cp u-boot-nodtb.bin $build_dir/firmware
cd $build_dir
}
build_opensbi() {
if [ ! -d $build_dir/opensbi ]; then
git clone --depth=1 https://github.com/riscv-software-src/opensbi.git -b v1.3
fi
cd opensbi
make PLATFORM=generic PLATFORM_RISCV_XLEN=64 CROSS_COMPILE=riscv64-linux-gnu- -j$(nproc)
cp build/platform/generic/firmware/fw_jump.bin $build_dir/firmware
cd $build_dir
}
mk_img() {
cd $build_dir
device=""
LOSETUP_D_IMG
UMOUNT_ALL
size=`du -sh --block-size=1MiB ${build_dir}/rootfs | cut -f 1 | xargs`
size=$(($size+720))
losetup -D
img_file=${build_dir}/rootfs.img
dd if=/dev/zero of=${img_file} bs=1MiB count=$size status=progress && sync
parted ${img_file} mklabel gpt mkpart primary fat32 32768s 524287s
parted ${img_file} mkpart primary ext4 524288s 100%
device=`losetup -f --show -P ${img_file}`
trap 'LOSETUP_D_IMG' EXIT
kpartx -va ${device}
loopX=${device##*\/}
partprobe ${device}
sdbootp=/dev/mapper/${loopX}p1
sdrootp=/dev/mapper/${loopX}p2
mkfs.vfat -n fedora-boot ${sdbootp}
mkfs.ext4 -L fedora-root ${sdrootp}
mkdir -p ${root_mnt} ${boot_mnt}
mount ${sdbootp} ${boot_mnt}
mount ${sdrootp} ${root_mnt}
if [ -d $boot_mnt/extlinux ]; then
rm -rf $boot_mnt/extlinux
fi
mkdir -p $boot_mnt/extlinux
line=$(blkid | grep $sdrootp)
uuid=${line#*UUID=\"}
uuid=${uuid%%\"*}
echo "label Fedora
kernel /Image
initrd /initrd.img
append console=ttyS0,115200 root=UUID=${uuid} rootfstype=ext4 rootwait rw earlycon loglevel=7 init=/lib/systemd/systemd" \
> $boot_mnt/extlinux/extlinux.conf
cp $build_dir/firmware/fw_jump.bin $boot_mnt
cp $build_dir/linux/arch/riscv/boot/Image $boot_mnt
echo "LABEL=fedora-root / ext4 defaults,noatime 0 0" > ${build_dir}/rootfs/etc/fstab
echo "LABEL=fedora-boot /boot vfat defaults,noatime 0 0" >> ${build_dir}/rootfs/etc/fstab
cp -rfp ${build_dir}/rootfs/boot/* $boot_mnt
rm -rf ${build_dir}/rootfs/boot/*
rsync -avHAXq ${build_dir}/rootfs/* ${root_mnt}
sync
sleep 10
umount $sdrootp
umount $sdbootp
LOSETUP_D_IMG
UMOUNT_ALL
losetup -D
kpartx -d ${img_file}
}
comp_img() {
if [ ! -f $build_dir/rootfs.img ]; then
echo "rootfs file build failed!"
exit 2
fi
if [ -d $build_dir/tar_dir ]; then
rm -rf $build_dir/tar_dir
fi
mkdir tar_dir
mv $build_dir/firmware/fw_jump.bin $build_dir/tar_dir
mv $build_dir/firmware/u-boot-nodtb.bin $build_dir/tar_dir
cp $build_dir/config/run-qemu-rv64.sh $build_dir/tar_dir
mv $build_dir/rootfs.img $build_dir/tar_dir
cd tar_dir && tar -zcvf ../Fedora-38-Minimal-QEMU-riscv64.tar.gz .
cd $build_dir
if [ ! -f $build_dir/Fedora-38-Minimal-QEMU-riscv64.tar.gz ]; then
echo "image file build failed!"
exit 2
fi
sha256sum Fedora-38-Minimal-QEMU-riscv64.tar.gz >> Fedora-38-Minimal-QEMU-riscv64.tar.gz.sha256
}
build_dir=$(pwd)
boot_mnt=${build_dir}/boot_tmp
root_mnt=${build_dir}/root_tmp
rootfs_dir=${build_dir}/rootfs
source scripts/common.sh
source scripts/fedora_rootfs.sh
install_reqpkg
get_riscv_system
UMOUNT_ALL
build_kernel
build_u-boot
build_opensbi
mk_img
comp_img