目录
1 引言
2 构建工具安装
3 发行版本构建工具选择
3.1 debootstrap
3.1.1 参数说明
3.1.2 应用
3.2 multistrap
3.2.1 参数说明
3.2.2 应用
4 注意事项
参考
1 引言
debootstrap和multistrap是debian/ubuntu下的一个工具,用来构建一套基本的系统(根文件系统),生成的目录符合Linux文件系统标准(FHS)。
两者之间的最大区别是:若软件源只有一个,则选择使用debootstrap,若有多个软件源则使用multistrap。比如我们不仅使用ubuntu的官方源,而且还会使用本地的仓库源,则选择使用multistrap;本文会对两个工具的使用做详细解释。
2 构建工具安装
# sudo apt-get install binfmt-support qemu qemu-user-static debootstrap multibootstrap
- qemu ---- 用于模拟 arm 环境
3 发行版本构建工具
3.1 debootstrap
debootstrap 通过读取配置文件从MIRROR引导一个发行版代号(codename)为SUITE(例如bionic——对应ubuntu18.04,focal——对应ubuntu20.04)的ubuntu指定系统安装到TARGET目录。
[MIRROR] 可以是http:///URL, 或是file:///URL, 或是ssh:///URL。(注意都是’///’)
debootstrap可以在没有安装盘的情况下在一个已启动的系统上部署一个ubuntu系统并运行在chroot环境。如此可以支持一个完全体(或最小化)ubuntu系统用以测试(比如OS安装程序调试)。
3.1.1 参数说明
第一次运行:
debootstrap [OPTION…] SUITE TARGET [MIRROR [SCRIPT]]
第二次运行
debootstrap [OPTION…] –second-stage
参数
–arch=ARCH
将目标系统架构设置为ARCH(当dpkg未安装时使用)。亦可见 –foreign
–include=alpha,beta
使用逗号隔开的将会被下载并提取(安装到TARGET目录)的软件包名的列表
–exclude=alpha,beta
使用逗哥隔开的将会被卸载移除的软件包列表(注意可能卸载必要的包)
–components=alpha,beta
使用软件源指定的部件(例如main/restricted)中的软件包
–variant=minbase|buildd|fakechroot|scratchbox
指定引导脚本集。目前支持的脚本集有(默认不指定脚本集):
minbase: 只包含必要的包和apt;
buildd: 包含编译工具包
fakechroot: 包含不用root权限的包
scratchbox: 包含scratchbox(交叉编译工具链)相关包
–keyring=KEYRING
根据Release文件下载签名并和KEYRING对比。默认不检查签名。
–foreign
仅做初始化的解包。当目标架构和宿主机架构不一致时需要指定此选项。需要配合使用–second-stage。
–second-stage
完成整个引导过程。一般不需要其他参数
3.1.2 应用
接下来我们将举例并进行解释,如:
debootstrap --variant=minbase --include=${DEBOOTSTRAP_LIST// /,} ${PACKAGE_LIST_EXCLUDE:+ --exclude=${PACKAGE_LIST_EXCLUDE// /,}} \
--arch=$DISTRIB_ARCH --components=${DEBOOTSTRAP_COMPONENTS} --foreign $DISTRIB_RELEASE $ROOTFS_TEMP/ $apt_mirror
从上述命令中可知:
–variant=minbase:指定构建基础的ubuntu发行版本
--include=${DEBOOTSTRAP_LIST// /,}:DEBOOTSTRAP_LIST为需要安装的包列表
${PACKAGE_LIST_EXCLUDE:+ --exclude=${PACKAGE_LIST_EXCLUDE// /,}}:PACKAGE_LIST_EXCLUDE为不需要安装的包列表
--arch=$DISTRIB_ARCH:DISTRIB_ARCH为构建的目标架构,针对arm架构可以选择:arm64, armhf
--components=${DEBOOTSTRAP_COMPONENTS}:DEBOOTSTRAP_COMPONENTS代表仓库中的:main restricted universe multiverse
--foreign $DISTRIB_RELEASE:DISTRIB_RELEASE为发行版本类型,如bionic focal
$ROOTFS_TEMP:构建文件的生成地址
$apt_mirror:拉取包的源地址,如国内源:https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/
因为在宿主是 x86 架构,不能安装 arm 架构的软件。需要依靠 qemu-arm-static 来模拟成 arm 环境进行安装。
复制 qemu-arm-static 到刚构建的基本系统中
32位系统
sudo cp /usr/bin/qemu-arm-static $ROOTFS_TEMP/usr/bin
64位系统
sudo cp /usr/bin/qemu-aarch64-static $ROOTFS_TEMP/usr/bin
初始化文件系统,会把一个系统的基础包等全部初始化
sudo DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true LC_ALL=C LANGUAGE=C LANG=C chroot $ROOTFS_TEMP debootstrap/debootstrap --second-stage
上述命令中, DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true 是为了设置非图形化界面,使用命令行形式交互。
LC_ALL=C LANGUAGE=C LANG=C 设置语言环境
chroot $ROOTFS_TEMP debootstrap/debootstrap --second-stage 进入到rootfs中,执行 debootstrap/debootstrap --second-stage命令
终端上会最后打印 I: Base system installed successfully.,说明根文件系统已经定制成功。
3.2 multistrap
multistrap是一种工具与debootstrap有类似的使用方式,但是提供了完全不同的应用方法,可以合并不同存储库来自动创建完整,可引导的根文件系统。
使用格式
multistrap [-a arch] -d [dir] -f config_file
3.2.1 参数说明
multistrap支持很多参数,具体见:https://manpages.debian.org/testing/multistrap/multistrap.1.en.html
这里我们只列出常用参数:
-a|--arch - architecture of the packages to put into the multistrap. (构建版本的架构)
-d|--dir - directory into which the bootstrap will be installed. (构建输出目录)
-f|--file - configuration file for multistrap [required](multistrap配置文件)
3.2.2 应用
下面展示如何配置从两个源获取数据来构建ubuntu rootfs,关键是配置:Main MainUpdates
[General]
# arch and directory can be specified on the command line.
#arch=arm64
#directory=/home/work/glp/ubuntu/unisoc-config/config/dist-release/test
# same as --tidy-up option if set to true
cleanup=true
# retain the sources outside the rootfs for distribution
# specify a directory to which all the .debs can be moved.
# or override with the --source-dir option.
# retainsources=/path/to/dep/cache
# same as --no-auth option if set to true
# keyring packages listed in each debootstrap will
# still be installed.
noauth=true
# extract all downloaded archives
unpack=true
# the order of sections is no longer important.
# debootstrap determines which repository is used to
# calculate the list of Priority: required packagesdebootstrap=Bionic BionicUpdates
debootstrap=Main MainUpdates
# aptsources is a list of sections to be listed
# in the /etc/apt/sources.list.d/multistrap.sources.list
# of the target.
aptsources=Main
# Any packages installed from Bionic
[Main]
packages=ubuntu-minimal udev vim-tiny whiptail init base-files gpg expect debconf-utils
source=https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/
keyring=ubuntu-keyring
suite=focal
components=main restricted universe multiverse
omitdebsrc=false
# Any packages installed from Bionic-updates
[MainUpdates]
packages=private-pac1 private-pac2 private-pac3
source=http://192.168.2.110/
suite=focal
components=main restricted universe multiverse
omitdebsrc=false
执行构建命令
multistrap --no-auth -a arm64 -d $ROOTFS_TEMP/ -f $FILE
其中,$FILE配置文件的内容如上所示,增加--no-auth,允许使用未经身份验证的存储库。
构建成功之后,安装debootstrap的方法,复制 qemu-arm-static 到刚构建的基本系统中。
同理要进行deb包的配置,chroot $ROOTFS_TEMP 之后执行:
export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true
export LC_ALL=C LANGUAGE=C LANG=C
dpkg --configure -a
注意:根据multistrap应用网站提示:
For "dpkg" to work, /proc and /sysfs must be mounted (or mountable), /dev/pts is also recommended.
在执行 dpkg --configure -a 之前,请挂载:/proc,/sys,/dev/pts
在使用chroot之前,执行:
sudo mount -t proc /proc rootfs/proc
sudo mount -t sysfs /sys rootfs/sys
sudo mount -o bind /dev rootfs/dev
sudo mount -o bind /dev/pts rootfs/dev/pts
至此,指定ubuntu发行版本的构建已经完成。
4 注意事项
1)如果某些包出现如下报错:
No diversion 'diversion of /bin/sh by dash', none removed.
This should never be reached
dpkg: error processing dash (--configure):
subprocess installed post-installation script returned error exit status 1
Errors were encountered while processing:
dash
解决办法,chroot进入$ROOTFS_TEMP后,删除系统中该包的信息
rm -rf /var/lib/dpkg/info/dash.*
2)The following signaturescouldn't be verified because the public key is not available:NO_PUBKEY *********
解决办法:允许使用未签名的仓库
将:$config_str .= " -o Acquire::AllowInsecureRepositories=true"; 写入到320行
vim /usr/sbin/multistrap
316$config_str = '';
317 $config_str .= " -o Apt::Architecture=" . shellescape($arch);
318 $config_str .= " -o Dir::Etc::TrustedParts=" . shellescape("${dir}${etcdir}trusted.gpg.d");
319 $config_str .= " -o Dir::Etc::Trusted=" . shellescape("${dir}${etcdir}trusted.gpg");
320 $config_str .= " -o Acquire::AllowInsecureRepositories=true";
321 $config_str .= " -o Apt::Get::AllowUnauthenticated=true"
322 if (defined $noauth);
323 $config_str .= " -o Apt::Get::Download-Only=true";
324 $config_str .= " -o Apt::Install-Recommends=false"
参考
http://www.vjiot.net/typecho/index.php/archives/82/
https://manpages.debian.org/testing/multistrap/multistrap.1.en.html