debootstrap 构建 Ubuntu RISC-V Linux 根文件系统
flyfish
主机信息
命令 lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.6 LTS
Release: 20.04
Codename: focal
制作的根文件系统为
RISC-V 64 Ubuntu 22.04 LTS
1 主机安装需要的程序和生成最小 bootstrap rootfs
# 安装需要的程序
sudo apt install debootstrap qemu qemu-user-static binfmt-support dpkg-cross --no-install-recommends
# 生成最小 bootstrap rootfs
sudo debootstrap --arch=riscv64 --foreign jammy ./temp-rootfs http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports
生成文件夹temp-rootfs的内容
2 chroot 和 debootstrap
# chroot 和 debootstrap
sudo chroot temp-rootfs /bin/bash
/debootstrap/debootstrap --second-stage
3 添加 package sources
# 添加 package sources
cat >/etc/apt/sources.list <<EOF
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-updates main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-updates main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-backports main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-backports main restricted universe multiverse
# deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-security main restricted universe multiverse
# # deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-security main restricted universe multiverse
deb http://ports.ubuntu.com/ubuntu-ports/ jammy-security main restricted universe multiverse
# deb-src http://ports.ubuntu.com/ubuntu-ports/ jammy-security main restricted universe multiverse
# 预发布软件源,不建议启用
# deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-proposed main restricted universe multiverse
# # deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-proposed main restricted universe multiverse
EOF
4 安装 essential packages
apt-get update
apt-get install --no-install-recommends -y util-linux haveged openssh-server systemd kmod initramfs-tools conntrack ebtables ethtool iproute2 iptables mount socat ifupdown iputils-ping vim dhcpcd5 neofetch sudo chrony
如果要安装其他包
先挂载,再安装
sudo mount -t proc /proc temp-rootfs/proc
sudo mount -t sysfs /sys temp-rootfs/sys
sudo mount -o bind /dev temp-rootfs/dev
sudo mount -o bind /dev/pts temp-rootfs/dev/pts
apt install rsyslog
apt install language-pack-en-base
apt install language-pack-zh-hans
apt install net-tools
apt install resolvconf
apt install network-manager
apt install usbutils
apt install sysstat
apt install bash-completion
sudo umount /dev/pts/ /dev/ /proc/ /sys
5 基本配置
cat >>/etc/network/interfaces <<EOF
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
EOF
cat >/etc/resolv.conf <<EOF
nameserver 114.114.114.114
nameserver 8.8.8.8
EOF
echo 'riscv-ubuntu2204' > /etc/hostname
echo "127.0.0.1 localhost" > /etc/hosts
echo "127.0.0.1 riscv-ubuntu2204" >> /etc/hosts
#一个大于号是覆盖写,两个大于号是追加写
#添加用户和设置密码
useradd -s '/bin/bash' -m -G adm,sudo flyfish
passwd flyfish
passwd root
sed -i "s/#PermitRootLogin.*/PermitRootLogin yes/g" /etc/ssh/sshd_config
6 文件夹 打包为tar或者gz
sudo tar -cSf rootfs_ubuntu_riscv.tar -C temp-rootfs .
gzip rootfs_ubuntu_riscv.tar
7 制作ext4镜像的完整脚本
echo "making image..."
dd if=/dev/zero of=rootfs_ubuntu_riscv.ext4 bs=1M count=4096
mkfs.ext4 rootfs_ubuntu_riscv.ext4
mkdir -p tmpfs
echo "copy data into rootfs..."
sudo mount -t ext4 rootfs_ubuntu_riscv.ext4 tmpfs/ -o loop
sudo cp -af temp-rootfs/* tmpfs/
sudo umount tmpfs
chmod 777 rootfs_ubuntu_riscv.ext4
以下为可选
8 虚拟机共享文件夹
为了防治物理机被破坏可以将主机放到虚拟机尝试
物理机配置
虚拟机配置
vmware-hgfsclient
sudo vmhgfs-fuse .host:/ /mnt -o nonempty -o allow_other
第一行命令是查看共享有没有成功
第二行命令是将物理机的共享文件夹挂载到虚拟机的/mnt
目录
参考
https://wiki.ubuntu.com/DebootstrapChroot
https://wiki.ubuntu.com/ARM/RootfsFromScratch/
Ubuntu Ports 软件仓库镜像使用帮助