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
相关推荐
刘瑜澄28 分钟前
[邪修方法]ubuntu 25 wayland窗口协议下使用utools
linux·运维·ubuntu·wayland·utools
魔都吴所谓37 分钟前
【Linux】Ubuntu22.04 Docker+四大数据库(挂载本地)一键安装脚本
linux·数据库·docker
木下~learning41 分钟前
初学Linux之设备树的使用| RK3399上实操
linux·设备树·rk3568·rk3399·平台总线和外设总线
Yupureki42 分钟前
《Linux系统编程》19.线程同步与互斥
java·linux·服务器·c语言·开发语言·数据结构·c++
又来敲代码了43 分钟前
Zrlog博客的系统部署
java·linux·运维·mysql·apache·tornado
feng_you_ying_li1 小时前
linux开发工具的介绍(5)
linux·运维·centos
Lugas Luo1 小时前
Kernel 5.10 SD卡专属探测、上电与注册流程分析 (Detect -> Power Up -> Add)
linux·嵌入式硬件
艾莉丝努力练剑1 小时前
【Linux信号】Linux进程信号(下):可重入函数、Volatile关键字、SIGCHLD信号
linux·运维·服务器·c++·人工智能·后端·学习
si莉亚1 小时前
2026.3.31成功安装Ubuntu22.04+ROS2记录
linux·c++·开源
RrEeSsEeTt1 小时前
【HackTheBox】- Monteverde 靶机学习
linux·网络安全·渗透测试·kali·红队·hackthebox·ad域