ubuntu开发系统镜像构建

ubuntu开发系统镜像构建

  • 开发环境
  • 开发容器配置
  • [准备 rootfs](#准备 rootfs)
  • [编译配置 kernel、modules、initrd](#编译配置 kernel、modules、initrd)
  • [制作 initrd](#制作 initrd)
  • 准备磁盘镜像
  • [Qemu 启动并调试](#Qemu 启动并调试)

文章介绍了构建现代操作系统发行版的方法,在 Docker 中启动 ubuntu 作为编译调试环境,在此环境中编译 kernel、initrd、rootfs,rootfs 采用 ubuntu 发行版,最后使用 Qemu 通过默认 UEFI 引导系统镜像启动,具备调试功能,能够使用 kvm 加速。

开发环境

  • 开发容器:ubuntu 25.10 container
  • kernel:6.19.0
  • rootfs ubuntu:25.10
  • 物理机:Arch Linux(kernel 6.18.8)

开发容器配置

bash 复制代码
docker pull ubuntu:25.10

# 为了能够在容器中使用 kvm 加速,需要映射文件 /dev/kvm,同时也需要赋予特权
docker run --name kernel-dev -it --privileged \
    -v /dev:/dev \
    -v $(pwd):/workspace \
    --net=host \
    ubuntu:25.10 /bin/bash

[docker]
apt update
apt install -y build-essential git bc bison flex libssl-dev libelf-dev \
    qemu-system-x86 qemu-utils ovmf \
    dosfstools cpio kmod debootstrap parted dosfstools grub-efi-amd64-bin grub-common binutils ovmf

准备 rootfs

shell 复制代码
debootstrap --extractor=ar --merged-usr --arch=amd64 --verbose questing /workspace/rootfs http://mirrors.aliyun.com/ubuntu/
mount --bind /dev /workspace/rootfs/dev
mount --bind /proc /workspace/rootfs/proc
mount --bind /sys /workspace/rootfs/sys
mount --bind /dev/pts /workspace/rootfs/dev/pts
chroot /workspace/rootfs apt update
chroot /workspace/rootfs apt install -y sudo bash vim ssh initramfs-tools
umount -l /workspace/rootfs/dev/pts
umount -l /workspace/rootfs/dev
umount -l /workspace/rootfs/proc
umount -l /workspace/rootfs/sys

编译配置 kernel、modules、initrd

shell 复制代码
git clone --depth 1 -b v6.19 https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
cd linux
make x86_64_defconfig

# 确保以下配置为模块 (M) 或开启调试 (Y)
scripts/config --module CONFIG_VIRTIO_BLK \
               --module CONFIG_VIRTIO_PCI \
               --module CONFIG_EXT4_FS \
               --enable DEBUG_INFO \
               --disable RANDOMIZE_BASE
#  编译内核和模块
make -j$(nproc) bzImage modules
# 安装 kernel modules headers
mkdir -p /workspace/rootfs
make INSTALL_MOD_PATH=/workspace/rootfs modules_install
make INSTALL_HDR_PATH=/workspace/rootfs/usr headers_install

制作 initrd

shell 复制代码
chroot /workspace/rootfs
mkinitramfs -o /boot/initrd.img-6.19.0 6.19.0

准备磁盘镜像

shell 复制代码
qemu-img create -f raw /workspace/kernel-6.19-ubuntu-25.10.img 4G

parted /workspace/kernel-6.19-ubuntu-25.10.img --script mklabel gpt
parted /workspace/kernel-6.19-ubuntu-25.10.img --script mkpart ESP fat32 1MiB 513MiB
parted /workspace/kernel-6.19-ubuntu-25.10.img --script set 1 boot on
parted /workspace/kernel-6.19-ubuntu-25.10.img --script mkpart root ext4 513MiB 100%

# 挂载分区前,需要映射 loop device
LOOP=$(losetup -Pf --show /workspace/kernel-6.19-ubuntu-25.10.img)
mkfs.vfat ${LOOP}p1        # EFI 分区
mkfs.ext4 ${LOOP}p2        # 根分区

# 挂载分区并复制文件
mkdir -p /mnt/root
mount ${LOOP}p2 /mnt/root
mkdir -p /mnt/root/boot/efi
mount ${LOOP}p1 /mnt/root/boot/efi

# 把内核、initrd、模块、rootfs 都放到根分区:
mkdir -p /mnt/root/boot
cp /workspace/linux/arch/x86/boot/bzImage /mnt/root/boot/vmlinuz
# cp /workspace/initramfs.img /mnt/root/initrd.img
cp -r /workspace/rootfs/* /mnt/root/

# 安装grub
grub-install --target=x86_64-efi \
             --boot-directory=/mnt/root/boot \
             --efi-directory=/mnt/root/boot/efi \
             --removable \
             --recheck

mkdir -p /mnt/root/boot/grub
cat <<EOF > /mnt/root/boot/grub/grub.cfg
menuentry "Ubuntu 25.10 (Kernel 6.19)" {
    # uuid 用 lsblk -f 查看 ${LOOP}p2
    search --no-floppy --fs-uuid 87284236-265e-4a5c-a9bf-0e9e4d049a34 --set=root
    linux /boot/vmlinuz root=/dev/vda2 rw console=ttyS0
    initrd /boot/initrd.img-6.19.0
}
EOF

# 检查 EFI GRUB 文件是否正确,如果没有则创建
cat /mnt/root/boot/efi/grub.cfg
# 正确内容 如下
insmod part_gpt
insmod ext2

search --no-floppy --fs-uuid 87284236-265e-4a5c-a9bf-0e9e4d049a34 --set=root
set prefix=($root)/boot/grub
configfile $prefix/grub.cfg

# 卸载
umount /mnt/root/boot/efi /mnt/root
losetup -d $LOOP

Qemu 启动并调试

shell 复制代码
qemu-system-x86_64 \
    -enable-kvm \
    -m 4G \
    -cpu host \
    -drive if=pflash,format=raw,readonly=on,file=/workspace/OVMF_CODE_4M.fd \
    -drive if=pflash,format=raw,file=/workspace/OVMF_VARS_4M.fd \
    -drive file=/workspace/kernel-6.19-ubuntu-25.10.img,format=raw,if=virtio \
    -smp 4 \
    -net nic -net user,hostfwd=tcp::2222-:22 \
    -nographic  \
    -s -S


gdb ./vmlinux
(gdb) target remote :1234
(gdb) hbreak start_kernel # 设置内核入口断点,KVM 加速会导致硬件断点和软件断点冲突
(gdb) c
相关推荐
xlp666hub15 小时前
Leetcode第五题:用C++解决盛最多水的容器问题
linux·c++·leetcode
张宏23617 小时前
原子操作 (基于Linux 应用层 C 语言)
linux
kymjs张涛1 天前
OpenClaw 学习小组:初识
android·linux·人工智能
程序设计实验室1 天前
经历分享,发现挖矿木马后,服务器快速备份与重装(腾讯云平台)
linux
Miku161 天前
OpenClaw-Linux+飞书官方Plugin安装指南
linux·人工智能·agent
Miku161 天前
OpenClaw 接入 QQ Bot 完整实践指南
linux·人工智能·agent
Yogurt_cry2 天前
[树莓派4B] 闲置近10年的爱普生 L310 打印机爆改无线打印机
linux·物联网·树莓派
Johny_Zhao3 天前
OpenClaw中级到高级教程
linux·人工智能·信息安全·kubernetes·云计算·yum源·系统运维·openclaw
Sheffield3 天前
Docker的跨主机服务与其对应的优缺点
linux·网络协议·docker
Sheffield4 天前
Alpine是什么,为什么是Docker首选?
linux·docker·容器