主页 > 开源代码  > 

我的docker随笔46:在x86平台构建龙芯镜像

我的docker随笔46:在x86平台构建龙芯镜像

本文介绍在x86服务器上构建龙芯平台的docker镜像。

前言

去年11月,在龙芯机器上安装了docker工具,并开始尝试研究如何构建龙芯的文件系统。断断续续搞了2个月后,有点结果出来了。前面有文章介绍了如何用debootstrap构建龙芯编译运行环境,本文将进一步,将构建好的文件做成镜像,方便使用。

环境及依赖文件

本文使用的软硬件说明如下。

x86服务器一台,CentOS7系统。安装docker 20.10.21版本。已配置好qemu。

依赖文件如下。

前文构建好的运行环境,压缩包文件为loong-runfs-gcc-8.3.0-20241202.tar.gz。最新构建的编译环境,压缩包文件为loong-buildfs-20250213.tar.gz。 docker镜像制作

经研究,除在龙芯机子上构建docker镜像外,还可以在其它平台上构建,再使用qemu-loongarch64-static即可运行。如此一来,运行简单的程序就可以不再依赖龙芯硬件机器了(当然,复杂的还得上硬件)。本节介绍如何制作。

制作压缩包

将前面文章构建好的rootfs做成压缩包,以当前日期打包:

cd loong-buildfs time tar zcf ../loong-buildfs-$(date "+%Y%m%d").tar.gz .

本次得到的文件为loong-buildfs-20250213.tar.gz,制作压缩包耗时约8分钟。

构建镜像

由于压缩包是完整的文件系统,因此用FROM scratch,即从头开始构建,不需要依赖其它的基础镜像了。在与上述压缩包同一目录添加Dockerfile文件,如下

# 定义当前日期变量 export TODAY=$(date +%Y%m%d) cat << EOF > Dockerfile FROM scratch ADD loong-buildfs-$TODAY.tar.gz / LABEL version="1.0" LABEL release="Loongnix GNU/Linux 20 (DaoXiangHu)" LABEL maintainer="Late Lee" LABEL buildtime="2025-02-13 23:46" CMD ["/bin/bash"] EOF

构建镜像:

docker build -t registry -shenzhen.aliyuncs /hxr/buildloong:$TODAY . # 顺便打上latest标签 docker tag registry -shenzhen.aliyuncs /hxr/buildloong:$TODAY registry -shenzhen.aliyuncs /hxr/buildloong

查看镜像:

# docker images | grep loong registry -shenzhen.aliyuncs /hxr/buildloong 20250213 91cbd3e0f843 31 minutes ago 2.7GB registry -shenzhen.aliyuncs /hxr/buildloong latest 91cbd3e0f843 31 minutes ago 2.7GB 运行测试

在x86上先注册qemu-loongarch64-static:

echo ":qemu-loongarch64-static:M::\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x02\x01:\xff\xff\xff\xff\xff\xfe\xfe\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:/usr/bin/qemu-loongarch64-static:" > /proc/sys/fs/binfmt_misc/register

再启动容器:

docker run -itd --rm --name buildloong -v /etc/localtime:/etc/localtime -v /home/:/home -v /root:/root -v /usr/bin/qemu-loongarch64-static:/usr/bin/qemu-loongarch64-static registry -shenzhen.aliyuncs /hxr/buildloong bash

进入容器,查看内核版本:

# docker run -itd --rm --name buildloong -v /etc/localtime:/etc/localtime -v /home/:/home -v /root:/root -v /usr/bin/qemu-loongarch64-static:/usr/bin/qemu-loongarch64-static registry -shenzhen.aliyuncs /hxr/buildloong bash a5de8b323cf3541c788b3c446e05a21cfced5990df6e381f3363ed2c63edf46e [root@localhost tools]# docker exec -it buildloong bash root@a5de8b323cf3:/# uname -a Linux a5de8b323cf3 5.16.0 #1 SMP Mon Oct 19 16:18:59 UTC 2020 loongarch64 loongarch64 loongarch64 GNU/Linux

说明:输出loongarch64表示已是龙芯架构了。由于本次运行的服务器系统版本不是很新,因此内核时间是2020年。

下面看一些文件属性:

root@a5de8b323cf3:/# file /bin/ls /bin/ls: ELF 64-bit LSB executable, LoongArch-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld.so.1, for GNU/Linux 4.15.0, BuildID[sha1]=8fa6f44877798e250e12ef4493b0dc9e329f6893, stripped root@a5de8b323cf3:/# file /bin/qemu-loongarch64-static /bin/qemu-loongarch64-static: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), statically linked, for GNU/Linux 3.2.0, BuildID[sha1]=e776c991b0409cd7bc955a7d6d0616411ca7040b, with debug_info, not stripped

说明:ls文件为LoongArch-64架构。/bin/qemu-loongarch64-static 为x86-64架构,这属正常现象。

退出容器,看一下镜像信息:

# docker inspect registry -shenzhen.aliyuncs /hxr/buildloong [ { "Id": "sha256:91cbd3e0f843c8133135396f3df772949178387be2292c8faa7dfe9304d5d62c", "RepoTags": [ "registry -shenzhen.aliyuncs /hxr/buildloong:20250213", "registry -shenzhen.aliyuncs /hxr/buildloong:latest" ], "RepoDigests": [], "ContainerConfig": { "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "/bin/sh", "-c", "#(nop) ", "CMD [\"/bin/bash\"]" ], "Image": "sha256:c59803d555c9476750c87e012ad396986145ce27266409eb8a4d22c04bb3e961", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": { "buildtime": "2025-02-13 23:46", "maintainer": "Late Lee", "release": "Loongnix GNU/Linux 20 (DaoXiangHu)", "version": "1.0" } }, "DockerVersion": "20.10.21", "Author": "", "Config": { "Hostname": "", "Domainname": "", "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "/bin/bash" ], ... }, "Architecture": "amd64", "Os": "linux", ... "RootFS": { "Type": "layers", "Layers": [ "sha256:d5e53cf29937d8df0c0945e4f191bb09889c13d10b23a988138d5941a82d54f2" ] }, "Metadata": { "LastTagTime": "2025-02-13T23:50:24.163418785+08:00" } } ]

输出信息能与构建信息对应,符合预期。

维护思路

由于用debootstrap已经能生成了可跨平台运行的rootfs,如要更新软件或库,使用chroot切换,安装即可。接着将rootfs做成压缩包。再制作成镜像。最后提交远程镜像仓库备用。

另一种制作维护方法

笔者构建的镜像,包括编译环境、运行环境两种。前者体积较大,用于编译场景,后者用于运行测试场景。此处以运行环境为例给出另一种制作维护方法。

运行环境构建Dockerfile内容如下:

FROM scratch ADD loong-runfs-gcc-8.3.0-20241202.tar.gz / LABEL version="1.0" LABEL release="Loongnix GNU/Linux 20 (DaoXiangHu)" LABEL maintainer="Late Lee" LABEL mark="just for loongarch64 running env, not build env." CMD ["/bin/bash"]

构建命令:

time docker build -t registry -shenzhen.aliyuncs /hxr/loongrun:20241202 .

同样,在x86平台上运行容器。

docker run -itd --rm --name loongrun -v /etc/localtime:/etc/localtime -v /home/:/home -v /root:/root -v /usr/bin/qemu-loongarch64-static:/usr/bin/qemu-loongarch64-static registry -shenzhen.aliyuncs /hxr/loongrun:20241202 bash

如果发现缺少库,则在容器安装好库或软件后,使用docker commit将容器保存为镜像,再提交镜像仓库,如此一来,只针对镜像这一种类型文件操作,无须再用chroot切换环境了。

保存镜像示例命令:

docker commit -m "<这是注释>" <容器名> <镜像名>:<标签名>

如:

docker commit -m "<这是注释>" loongrun registry -shenzhen.aliyuncs /hxr/loongrun:20241202 小结

通过本文的实践,可以成功在非龙芯机器上构建并运行龙芯docker镜像。

标签:

我的docker随笔46:在x86平台构建龙芯镜像由讯客互联开源代码栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“我的docker随笔46:在x86平台构建龙芯镜像