Archives 3 月 2025

以 deepin 为例,手把手教你如何自定义系统镜像 | 内附视频

图片

你是否想过自己动手打造一个专属的操作系统镜像?无论是为极客项目定制开发环境,还是让老旧设备焕发新生,系统镜像定制都是极客玩家的必备技能。

那么如何针对自己手中的设备,为其定制一个专属的系统镜像呢?

感谢社区用户@空木蓮華 的投稿及贡献。本文将以 deepin 深度操作系统以及树莓派设备为例,演示树莓派镜像的制作、烧录及运行,与你分享自定义系统镜像的实用思路与宝贵经验。

1

基础工具链安装

在开始制作镜像前,需先安装基础工具链。可通过以下命令安装必要软件包:

sudo apt update -ysudo apt-get install -y qemu-user-static binfmt-support mmdebstrap arch-test usrmerge usr-is-merged fdisk dosfstoolssudo systemctl restart systemd-binfmt  # 重启 binfmt 服务加载ARM支持

其中,qemu-user-static 与 binfmt-support 用于实现异架构模拟,让 X86 主机可直接执行 ARM 程序。

2

根文件系统构建

使用 mmdebstrap 创建基础系统,命令如下:

mkdir -p rootfssudo mmdebstrap     --hook-dir=/usr/share/mmdebstrap/hooks/merged-usr     --include="ca-certificates,locales,sudo,apt,adduser,polkitd,systemd,network-manager,dbus-daemon,apt-utils,bash-completion,curl,vim,bash,deepin-keyring,init,ssh,net-tools,iputils-ping,lshw,iproute2,iptables,procps,wpasupplicant,dmidecode,ntpsec-ntpdate,linux-firmware"     --architectures=arm64     beige     rootfs     "deb https://community-packages.deepin.com/beige/ beige main commercial community"     "deb https://proposed-packages.deepin.com/beige-testing/ unstable/25 main commercial community"
该命令指定创建 ARM64 架构的根文件系统,包含系统工具、网络工具、调试工具等必要软件包。

3

磁盘镜像制作

3.1 创建空白镜像并分区

使用以下命令创建空白镜像并进行分区:

dd if=/dev/zero of=deepin-raspberrypi.img bs=1M count=4096sudo fdisk deepin-raspberrypi.img << EOFnp1
+300Mtcnp2
wEOF

上述命令创建了一个 4GB 的空白镜像文件,并将其分为两个分区:300MB 的 FAT32 格式启动分区和剩余空间的 Linux 分区。

3.2 格式化与挂载分区

通过 losetup 将镜像文件绑定到回环设备,然后分别对两个分区进行格式化和挂载操作:

LOOP=$(sudo losetup -Pf --show deepin-raspberrypi.img)sudo mkfs.fat -F32 "${LOOP}p1"sudo dosfslabel "${LOOP}p1" bootfssudo mkfs.ext4 "${LOOP}p2"sudo e2label "${LOOP}p2" rootfs
mkdir tmpsudo mount "${LOOP}p2" tmpsudo cp -a rootfs/* tmpsudo mkdir tmp/boot/firmwaresudo mount "${LOOP}p1" tmp/boot/firmware
sudo mount --bind /dev tmp/devsudo mount -t proc chproc tmp/procsudo mount -t sysfs chsys tmp/syssudo mount -t tmpfs -o "size=99%" tmpfs tmp/tmpsudo mount -t tmpfs -o "size=99%" tmpfs tmp/var/tmpsudo mount --bind /etc/resolv.conf tmp/etc/resolv.confsudo mount -t devpts devpts tmp/dev/pts

4

树莓派硬件适配

4.1 安装固件与内核

从树莓派官方仓库[1]下载树莓派预编译固件,并拷贝至相应目录。

git clone --depth=1 https://github.com/raspberrypi/firmware.gitsudo cp -r firmware/boot/* tmp/boot/firmware

4.2 配置 config.txt

配置config.txt文件[2],以启用音频、自动加载覆盖层、设置 64 位模式等。

sudo tee tmp/boot/firmware/config.txt <<EOF  # For more options and information see  # http://rptl.io/configtxt  # Some settings may impact device functionality. See link above for details
  # Uncomment some or all of these to enable the optional hardware interfaces  #dtparam=i2c_arm=on  #dtparam=i2s=on  #dtparam=spi=on
  # Enable audio (loads snd_bcm2835)  dtparam=audio=on
  # Additional overlays and parameters are documented  # /boot/firmware/overlays/README
  # Automatically load overlays for detected cameras  camera_auto_detect=1
  # Automatically load overlays for detected DSI displays  display_auto_detect=1
  # Automatically load initramfs files, if found  auto_initramfs=1
  # Enable DRM VC4 V3D driver  dtoverlay=vc4-kms-v3d  max_framebuffers=2
  # Don't have the firmware create an initial video= setting in cmdline.txt.  # Use the kernel's default instead.  disable_fw_kms_setup=1
  # Run in 64-bit mode  arm_64bit=1
  # Disable compensation for displays with overscan  disable_overscan=1
  # Run as fast as firmware / board allows  arm_boost=1
  [cm4]  # Enable host mode on the 2711 built-in XHCI USB controller.  # This line should be removed if the legacy DWC2 controller is required  # (e.g. for USB device mode) or if USB support is not required.  otg_mode=1
  [cm5]  dtoverlay=dwc2,dr_mode=host
  [all]  EOF

4.3 添加树莓派官方源

mkdir -p tmp/etc/apt/sources.list.d# 添加树莓派的源,一些树莓派提供的工具,需要从这个软件源下载echo "deb [trusted=yes] http://archive.raspberrypi.org/debian/ bookworm main" | sudo tee tmp/etc/apt/sources.list.d/raspberrypi.list

4.4 安装额外的软件包、内核

# raspi-config 会依赖到 libfmt9,deepin 源里没 已经升级到 libfmt10,从 Debian 下载 deb 包curl -L http://ftp.cn.debian.org/debian/pool/main/f/fmtlib/libfmt9_9.1.0+ds1-2_arm64.deb -o tmp/tmp/libfmt9.debcurl -L http://ftp.cn.debian.org/debian/pool/main/d/device-tree-compiler/libfdt1_1.6.1-4+b1_arm64.deb -o tmp/tmp/libfdt1.deb

4.5 进入 chroot,并安装配置工具及内核

sudo chroot tmp bash
(chroot) apt update -y && apt install -y /tmp/libfmt9.deb /tmp/libfdt1.deb
(chroot) apt install -y raspi-config raspberrypi-sys-mods firmware-brcm80211 raspi-firmware bluez-firmware
(chroot) apt install -y     linux-image-rpi-v8     linux-image-rpi-2712     linux-headers-rpi-v8     linux-headers-rpi-2712

5

配置启动参数

设置 cmdline.txt 内核参数,指定控制台、根文件系统等信息。

# 配置 cmdline.txt 内核参数(chroot) echo "console=serial0,115200 console=tty1 root=LABEL=rootfs rootfstype=ext4 fsck.repair=yes rootwait quiet init=/usr/lib/raspberrypi-sys-mods/firstboot splash plymouth.ignore-serial-consoles" | tee /boot/firmware/cmdline.txt

6

编辑分区表

配置 /etc/fstab 文件,确保系统启动时正确挂载分区。

(chroot) tee /etc/fstab << EOFproc          /proc           proc    defaults          0       0LABEL=bootfs  /boot/firmware  vfat    defaults          0       2LABEL=rootfs  /               ext4    defaults,rw,errors=remount-ro,x-systemd.growfs  0       1EOF

7

系统个性化配置

创建用户,并配置本地化设置,包括语言、时区等。

# 创建用户并设置密码(chroot) useradd -m deepin && usermod -aG sudo deepin(chroot) echo 'deepin:deepin' | chpasswd(chroot) chsh -s /bin/bash deepin
# 取消注释(chroot) sed -i -E 's/#[[:space:]]*(en_US.UTF-8[[:space:]]+UTF-8)/1/g' /etc/locale.gen(chroot) sed -i -E 's/#[[:space:]]*(zh_CN.UTF-8[[:space:]]+UTF-8)/1/g' /etc/locale.gen# 生成语言设置(chroot) locale-gen
# 设置中文(chroot) tee /etc/locale.conf << EOFLANG=zh_CN.UTF-8LANGUAGE=zh_CNEOF
# 设置本地上海时区(chroot) ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

8

清理与压缩

在完成上述配置后,可以清理软件包缓存并卸载挂载点。

# 清理软件包缓存(chroot) apt clean && rm -rf /var/cache/apt/archives/*(chroot) exit
# 卸载挂载点sudo umount -l tmp
# 强制文件系统检查sudo e2fsck -f "${LOOP}p2"
# 解除回环设备sudo losetup -D $LOOP

9

烧录镜像

此步骤建议使用树莓派官方烧录工具,根据系统提示进行操作,将制作好的镜像烧录到树莓派的存储设备上。

详细步骤可参考:《deepin 25树莓派通用镜像上线》

10

安装桌面环境

若需要安装桌面环境,可在系统启动后通过 APT 安装,或直接在树莓派设备上构建含桌面的完整镜像。

export DEBIAN_FRONTEND=noninteractivesudo apt update# DDE 桌面环境相关的包sudo apt install deepin-desktop-environment-{base,cli,core,extras}# 这里安装完系统lightdm是被禁用自启动,允许自启动重启sudo systemctl enable lightdm# 玲珑环境sudo apt install deepin-desktop-environment-ll# UOS AI 、火狐浏览器sudo apt install uos-ai firefox
# 如果需要使用 treeland 窗管sudo apt install treeland ddm# 禁用 lightdm 自启动,允许 ddm 自启动sudo systemctl disable lightdm && sudo systemctl enable ddm# 停止 lightdm,启动 ddmsudo systemctl stop lightdm && sudo systemctl enable ddm

11

制作镜像脚本

可通过克隆 GitHub 仓库获取制作镜像的脚本,方便批量制作或自动化操作。

git clone --depth=1 https://github.com/deepin-community/deepin-raspberrypi.git
# 构建不包含桌面环境的镜像cd deepin-raspberrypi./build.sh
# 构建包含桌面环境的镜像,需要在树莓派上构建cd deepin-raspberrypi./build.sh desktop

图片

以上便是在树莓派上定制 deepin 系统镜像的全过程,本教程至此结束。教程中所涉及命令的详细解读可点击「阅读原文」查看。

通过以上讲解,相信大家已经了解了系统磁盘镜像的制作过程。这里以 deepin 和树莓派为例,其他架构的系统定制(如 X86_64、LoongArch64、RISC-V64 等架构的系统)也可以参考这一思路。

再次感谢@空木蓮華的贡献及分享!若您在实践过程中遇到了任何问题,或者对我们的教程有任何建议,欢迎随时在deepin 社区论坛与我们交流。

相关阅读:

[1] 树莓派官方仓库
https://github.com/raspberrypi/firmware/tree/master/boot
[2] config.txt

https://www.raspberrypi.com/documentation/computers/config_txt.html

往期精选  |  Selection in the past