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
相关推荐
0+1115 小时前
Linux --从 poll 到 epoll多路转接
linux·服务器·1024程序员节
青瓦梦滋6 小时前
WebSocket协议
linux·服务器·网络·c++·websocket·网络协议·http
2601_949847759 小时前
bcrypt.js 跨端三方库仓颉适配全流程
linux·运维·ubuntu
m0_639906949 小时前
Linux 操作系统入门:目录结构、服务排查常用命令、Shell 脚本基础
linux·运维·后端
水饺编程11 小时前
第2章:Linux 基础,exit 命令
linux·c语言·汇编
木子欢儿11 小时前
Debian 13 + vsftpd 搭建FTP服务
linux·服务器·前端·网络·debian
GeW12 小时前
新质生产力落地实践:企业级Linux迁移与数据库国产化
linux
雯宝12 小时前
usb_serial_generic_register
linux
疯狂的Meng13 小时前
我把第一周的 Linux 命令整理成了自己的手册(附 7 个我踩的坑)
linux
wt952713 小时前
VMware 虚拟机静态 IP 配置
linux