从软件包到RootFS镜像
定义完软件包后,无需做更多修改,直接 nix run .#rootfs-${target} 即可构建 RootFS。
生成 Docker 镜像
我们利用 pkgs.dockerTools.buildImage 的展平和拷贝属性,将 user/apps 下的软件包,以及 user/sysconfig 中的配置文件(连同它们在 /nix/store 中的依赖闭包)复制到单层镜像中(Docker原理)。 buildImage 的产物是一个可以被 docker load 的未压缩 tar。见 rootfs-tar.nix
{
lib,
pkgs,
nixpkgs,
system,
target,
testOpt,
nightlyRustPlatform,
}:
# 产物是一个未压缩的 docker 镜像 tar,其中的 layer.tar 即 rootfs 内容
let
apps = import ./apps {
inherit
lib
pkgs
nixpkgs
system
target
testOpt
nightlyRustPlatform
;
};
sys-config =
pkgs.runCommand "sysconfig"
{
src = ./sysconfig;
}
''
mkdir -p $out $out/root
cp -r $src/* $out/
'';
# 使用 buildImage 创建 Docker 镜像(单层),copyToRoot 会连同 /nix/store 闭包一起打包
dockerImage = pkgs.dockerTools.buildImage {
name = "busybox-rootfs";
copyToRoot = [
sys-config
]
++ apps;
keepContentsDirlinks = false;
# Create writable runtime directories in the layer, after Nix store
# permission normalization. /var/tmp must remain disk-backed for tests.
extraCommands = ''
mkdir -p var/tmp
chmod 1777 var/tmp
'';
# 仅作为 disk-image.nix 的中间产物,不需要压缩
compressor = "none";
};
in
dockerImage生成磁盘镜像
原理:在 nix 沙箱中取出镜像第一层的 layer.tar,在 fakeroot 下解包,再用 mke2fs -d 直接把目录写入 磁盘镜像中偏移 1MiB 处的分区,并用 sfdisk 写入 MBR 分区表(内核从 /dev/vda1 挂载 rootfs)。 整个过程不需要 root 权限,也不需要 loop 设备。见 user/disk-image.nix
{
lib,
pkgs,
rootfsImage,
rootfsType ? "ext4",
partitionType ? "mbr",
}:
# 在 nix 沙箱内免特权生成带 MBR 分区表的磁盘镜像:
# 单个分区从 1MiB 开始,内核从 /dev/vda1 挂载 rootfs。
# 产物会被 nix 缓存,userland 不变时无需重新生成。
assert lib.assertMsg (
partitionType == "mbr"
) "disk-image.nix: only mbr partition table is supported";
assert lib.assertMsg (lib.elem rootfsType [
"ext4"
"vfat"
]) "disk-image.nix: unsupported rootfsType ${rootfsType}";
let
partOffsetSectors = 2048;
partOffsetBytes = partOffsetSectors * 512;
# 各文件系统的差异:内容大小统计方式、MBR 分区类型、写入命令
# ($ROOT 为 rootfs 目录,$PART_KB 为分区大小)
fs =
if rootfsType == "ext4" then
{
prepare = "";
# 写入时需要 fakeroot 让文件属主为 root
wrap = "fakeroot -i fakeroot.state --";
contentKb = ''$(( $(stat -c %s "$LAYER_TAR") / 1024 ))'';
partType = "83";
mkfs = ''
mke2fs -q -t ext4 -F -d "$ROOT" -E offset=${toString partOffsetBytes} disk.img "''${PART_KB}k"
'';
}
else
{
# vfat 不支持符号链接,mcopy 会解引用复制。先把绝对链接改写到 $ROOT 下,
# 避免解引用到构建机自身的 /nix/store;镜像大小也按解引用后的大小计算
prepare = ''
chmod -R u+w "$ROOT"
find "$ROOT" -type l -lname '/*' | while read -r link; do
ln -sfn "$ROOT$(readlink "$link")" "$link"
done
'';
contentKb = ''$(du -sklL "$ROOT" | cut -f1)'';
# vfat 没有属主概念,不需要 fakeroot
wrap = "";
partType = "c";
mkfs = ''
mkfs.vfat --offset ${toString partOffsetSectors} disk.img "$PART_KB"
mcopy -s -i disk.img@@${toString partOffsetBytes} "$ROOT"/* ::/
'';
};
in
pkgs.runCommand "dragonos-disk-image"
{
nativeBuildInputs = with pkgs; [
gnutar
fakeroot
e2fsprogs
util-linux
dosfstools
mtools
];
}
''
LAYER_TAR=$(tar -tf ${rootfsImage} | grep '/layer.tar$' | head -1)
if [ -z "$LAYER_TAR" ]; then
echo "Error: layer.tar not found in ${rootfsImage}"
exit 1
fi
tar -xf ${rootfsImage} "$LAYER_TAR"
# fakeroot 保证解包与写入文件系统时属主均为 root(-s/-i 在两次调用间保存属主信息)
ROOT=$PWD/root
mkdir "$ROOT"
fakeroot -s fakeroot.state -- tar -xf "$LAYER_TAR" -C "$ROOT"
${fs.prepare}
# 磁盘大小:rootfs 大小 + 1GiB 余量
DISK_KB=$(( ${fs.contentKb} + 1024 * 1024 ))
PART_KB=$(( DISK_KB - ${toString partOffsetBytes} / 1024 ))
truncate -s "''${DISK_KB}K" disk.img
echo 'start=${toString partOffsetSectors}, type=${fs.partType}' | sfdisk -q --label dos disk.img
export ROOT PART_KB
${fs.wrap} sh -euc '
chmod 0755 "$ROOT"
${fs.mkfs}
'
mkdir -p $out
mv disk.img $out/disk.img
''nix build .#checks.x86_64-linux.rootfs-image-x86_64 会对生成的镜像进行校验(分区表、e2fsck、关键文件属主),见 user/check-disk-image.sh。
nix script 的背后
磁盘镜像是一个普通的 nix derivation,会作为构建产物缓存在 /nix/store 中:user/ 没有变化时不会重新生成。 多次修改 user/ 后旧的镜像副本仍会保留,可能占用较多硬盘。 镜像准备选择 AUTO_TEST=dunit 或 AUTO_TEST=syscall 时(包括 make test-dunit 和 make test-syscall),始终恢复干净镜像,并丢弃 guest 内的磁盘修改。
hint: 要想释放磁盘空间,执行 nix store gc
nix run .#rootfs-${target} 运行的脚本(user/default.nix)只做一件事:当 store 中的镜像发生变化时, 把它拷贝为可写的 bin/disk-image-${target}.img,并在 bin/.disk-image-${target}.src 中记录来源。 交互模式下,镜像没有变化时会跳过拷贝,以保留在 DragonOS 中对磁盘所做的修改;-- --force 可强制恢复为干净镜像。
{
lib,
pkgs,
nixpkgs,
system,
target,
buildDir,
testOpt,
nightlyRustPlatform,
rootfsType ? "vfat",
diskPath,
partitionType ? "mbr",
}:
let
rootfsImage = import ./rootfs-tar.nix {
inherit
lib
pkgs
nixpkgs
system
target
testOpt
nightlyRustPlatform
;
};
diskImage = import ./disk-image.nix {
inherit
lib
pkgs
rootfsImage
rootfsType
partitionType
;
};
# 磁盘镜像在 nix store 中构建并缓存;这里只负责在其变化时拷贝出可写副本
buildScript = pkgs.writeShellApplication {
name = "dragonos-rootfs";
runtimeInputs = [
pkgs.coreutils
pkgs.util-linux
];
text = ''
SRC_IMG="${diskImage}/disk.img"
DST_IMG="${diskPath}"
STAMP="${buildDir}/.disk-image-${target}.src"
FORCE=0
if [ "''${1:-}" = "-f" ] || [ "''${1:-}" = "--force" ]; then
FORCE=1
fi
# Test runs require a fresh immutable baseline; interactive runs retain
# guest changes. Match the runner's runtime/configured AUTO_TEST choice.
case "''${AUTO_TEST:-${testOpt.autotest}}" in
dunit|syscall) FORCE=1 ;;
esac
mkdir -p "${buildDir}" "$(dirname "$DST_IMG")"
# Keep the lock file: unlinking it would allow callers to lock different
# inodes. The image and its source stamp form a single transaction.
exec {lock_fd}>"$DST_IMG.lock"
flock -x "$lock_fd"
if [ "$FORCE" = "0" ] && [ -f "$DST_IMG" ] && [ "$(cat "$STAMP" 2>/dev/null)" = "$SRC_IMG" ]; then
echo "==> Disk image unchanged, skip: $DST_IMG"
echo " (use 'nix run .#rootfs-${target} -- --force' to discard changes made inside the guest)"
exit 0
fi
echo "==> Copying disk image to $DST_IMG"
TEMP_IMG=""
TEMP_STAMP=""
trap 'rm -f -- "$TEMP_IMG" "$TEMP_STAMP"' EXIT
TEMP_IMG=$(mktemp "$DST_IMG.tmp.XXXXXX")
TEMP_STAMP=$(mktemp "$STAMP.tmp.XXXXXX")
cp --sparse=always --reflink=auto "$SRC_IMG" "$TEMP_IMG"
chmod u+w "$TEMP_IMG"
printf '%s\n' "$SRC_IMG" > "$TEMP_STAMP"
# Invalidate the old source before replacing its image. A crash between
# the two publications must force a rebuild, including for the old source.
rm -f -- "$STAMP"
mv -f -- "$TEMP_IMG" "$DST_IMG"
# Publish the stamp last, so a failed copy/publication is retried.
mv -f -- "$TEMP_STAMP" "$STAMP"
echo "==> Build complete!"
echo " Disk image: $DST_IMG ($(du -h "$DST_IMG" | cut -f1))"
'';
passthru = { inherit diskImage; };
};
in
buildScript如果你想看看最终的镜像里都有些什么,可以在项目根目录执行 nix build .#packages.x86_64-linux.rootfs-x86_64.diskImage -o bin/result,镜像位于 bin/result/disk.img。