当前位置:首页 > 技术 > LINUX > 正文内容

从零开始构建 RISC-V 架构下的 Debian 根文件镜像

watrt9个月前 (10-24)LINUX3200

主机配置:Ubuntu 18.04

配置基本服务

下载安装依赖

sudo apt install debootstrap qemu qemu-user-static qemu-system qemu-utils qemu-system-misc binfmt-support dpkg-cross debian-ports-archive-keyring --no-install-recommends

生成目标镜像,配置环境

dd if=/dev/zero of=rootfs.img bs=1M count=4096
mkdir rootfs
mkfs.ext4 rootfs.img
sudo mount rootfs.img rootfs

执行下debootstrap

sudo debootstrap --arch=riscv64 --foreign --keyring /usr/share/keyrings/debian-ports-archive-keyring.gpg --include=debian-ports-archive-keyring sid ./rootfs http://deb.debian.org/debian-ports

如果遇到GPG error: E852514F5DF312F6,需要更新下本地钥匙串。

wget http://ftp.cn.debian.org/debian/pool/main/d/debian-ports-archive-keyring/debian-ports-archive-keyring_2022.02.15_all.deb
sudo dpkg -i debian-ports-archive-keyring_2022.02.15_all.deb

基本文件系统就准备好了

编译安装最新版的QEMU

安装依赖。

sudo apt-get install -y pkg-config git libglib2.0-dev libfdt-dev libpixman-1-dev zlib1g-dev

下载,配置QEMU并编译

wget https://download.qemu.org/qemu-7.0.0.tar.xz
tar -xf qemu-7.0.0.tar.xz
cd qemu-7.0.0/
./configure --static --target-list=riscv64-linux-user
make

将QEMU打包成 DEB 并安装

sudo apt-get install checkinstall
sudo checkinstall make install
sudo apt-get install ./*.deb

或直接安装

sudo make install

配置binfmt

/usr/bin/qemu-riscv64-static必须能够在chroot的目的地执行。 已准备好binfmt,riscv64可以运行。

先复制一份

将qemu-riscv64-static放在./riscv/usr/bin/QEMU-riscv 64-static上,如果是为了这次的目的,请使用/usr/bin/qemu-riscv64-static 只要在chroot的前端存在/usr/bin/qemu-riscv64-static二进制文件即可。

sudo cp -rf /usr/bin/qemu-riscv64-static rootfs/usr/bin/

配置binfmt的magic和mask

cat > /tmp/riscv64 <<- EOF
package qemu-user-static
type magic
offset 0
magic \x7f\x45\x4c\x46\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xf3\x00
mask \xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff
interpreter /usr/bin/qemu-riscv64-static
EOF

导入binfmt

sudo update-binfmts --import /tmp/riscv64

检查一下有没有配置成功

ls /proc/sys/fs/binfmt_misc/riscv64
-> /proc/sys/fs/binfmt_misc/riscv64

(如果没有输出,sudo systemctl restart binfmt-support 一下)

进入chroot环境

sudo chroot rootfs/

配置目标rootfs

先让debootstrap把事情干完。

/debootstrap/debootstrap --second-stage

再配置下软件源

cat >/etc/apt/sources.list <<EOF
deb http://ftp.ports.debian.org/debian-ports sid main
deb http://ftp.ports.debian.org/debian-ports unstable main
deb http://ftp.ports.debian.org/debian-ports unreleased main
deb http://ftp.ports.debian.org/debian-ports experimental main
EOF

安装软件包

apt-get update
apt-get install -y util-linux haveged openssh-server systemd kmod initramfs-tools conntrack ebtables ethtool iproute2 iptables mount socat ifupdown iputils-ping vim neofetch sudo chrony pciutils

配置网络

mkdir -p /etc/network
cat >/etc/network/interfaces <<EOF
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
EOF
cat >/etc/resolv.conf <<EOF
nameserver 1.1.1.1
nameserver 8.8.8.8
EOF

配置分区

cat >/etc/fstab <<EOF
LABEL=rootfs/ext4user_xattr,errors=remount-ro01
EOF

配置root密码

echo "root:riscv" | chpasswd

清理缓存

apt-get clean
rm -rf /var/cache/apt/

退出chroot,解除挂载

exit
sudo umount rootfs

输出得到的rootfs.img就是目标根文件。


分享给朋友:

相关文章

mpalqyer 播放命令

mpalqyer 播放命令

mplayer -vo fbdev2 -vf scale=1366:768 film.avi /usr/bin/mplayer -x 800 -y 480 -zoom -framedrop aa.avi...

基于 debootstrap 和 busybox 构建 mini ubuntu

基于 debootstrap 和 busybox 构建 mini ubuntu

最近的工作涉及到服务器自动安装和网络部署操作系统,然后使用 ansible 和 saltsatck 进行配置并安装 openstack 。难点在于服务器的自动安装,由于不单只是通过 PXE 安装服务器,还需要能够安装时进行分区、配置网卡等工作,因此需要在开始安装前,必须先收集服务器的硬件信息。调研了一下目前的开源项目中,提供此类功能的有 tinycorelinux 、 puppet razor-el-mk 可做类似的工作。tinycorelinux 是个很好的工具,整个系统在 PXE 之后在内存...

制作荔枝派Zero开发板(全志V3s) TF/SD卡启动盘

制作荔枝派Zero开发板(全志V3s) TF/SD卡启动盘

0. 前言近几天买了一块荔枝派0开发板,以及官方配的480×272的屏幕。让我记录一下入坑与采坑过程。1. u-boot的编译git clone https://github.com/Lichee-Pi/u-boot -b v3s-current cd u-boot make ARCH=arm LicheePi_Zero_480x272LCD_defconfig make ARCH=arm CROSS...

Linux系统信息查看命令大全

Linux系统信息查看命令大全

最近看了一些Linux命令行的文章,在系统信息查看方面学到不少命令。想起以前写过的一篇其实Linux这样用更简单,发现这些系统信息查看命令也可以总结出一篇小小的东西来了。另外这里还有非常多的命令,可以作为参考。系统# uname -a               # 查看内核/操作系统/CPU信息# head -n 1 /etc/issue   # 查看操作系统版本# cat /proc/cpuinfo  ...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。