2 Star 1 Fork 1

michael / qemu-loongarch64-static

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
在x86上使用debootstrap来制作debianLoongArchl)系统.md 5.73 KB
一键复制 编辑 原始数据 按行查看 历史
michael 提交于 2021-12-24 22:51 . init version

在x86上使用debootstrap来制作debian(LoongArch)系统

系统环境:ubuntu 18.04 x64

使用debootstrap制作根文件系统会分成两个阶段。第一阶段是,使用debootstrap命令来下载软件包。 第二阶段是安装软件包。

  1. 安装debootstap 等相关工具 $ sudo apt install binfmt-support qemu qemu-user-static debootstrap

  2. 使用debootstrap 下载软件包

    第一阶段

    # cd /usr/share/debootstrap/scripts
    # ln -s sid DaoXiangHu-testing
     
    # cd ~
    # mkdir iso
    # debootstrap --no-check-gpg --variant=minbase --components=main,non-free,contrib --arch=loongarch64 --foreign DaoXiangHu-testing iso http://pkg.loongnix.cn/loongnix/
    
    I: Retrieving InRelease 
    I: Retrieving Packages 
    I: Validating Packages 
    I: Retrieving Packages 
    I: Validating Packages 
    I: Retrieving Packages 
    I: Validating Packages 
    I: Resolving dependencies of required packages...
    I: Resolving dependencies of base packages...
    I: Found additional required dependencies: debconf fdisk libacl1 libattr1 libaudit-common libaudit1 libblkid1 libbz2-1.0 libc6 libcap-ng0 libcom-err2 libdb5.3 libdebconfclient0 libext2fs2 libfdisk1 libgcc1 libgcrypt20 libgpg-error0 liblz4-1 liblzma5 libmount1 libncursesw6 libpam0g libpcre3 libselinux1 libsemanage-common libsemanage1 libsepol1 libsmartcols1 libss2 libsystemd0 libtinfo6 libudev1 libuuid1 
    I: Found additional base dependencies: adduser debian-archive-keyring gpgv libapt-pkg5.0 libffi7 libgmp10 libgnutls30 libhogweed4 libidn2-0 libnettle6 libp11-kit0 libstdc++6 libtasn1-6 libunistring2 libzstd1 
    I: Checking component main on http://pkg.loongnix.cn/loongnix...
    I: Retrieving adduser 3.118
    I: Validating adduser 3.118
    I: Retrieving apt 1.8.2
    .....
    I: Extracting sensible-utils...
    I: Extracting sysvinit-utils...
    I: Extracting tar...
    I: Extracting tzdata...
    I: Extracting util-linux...
    I: Extracting zlib1g...
    
    --arch:指定要制作文件系统的处理器体系结构,比如loongarch64
    testing:指定版本
    iso:本地目录,最后制作好的文件系统会在此目录。
    --foreign:只执行引导的初始解包阶段,仅仅下载和解压。
    http://pkg.loongnix.cn/loongnix/:龙芯的镜像源地址

    第二阶段

    在第一阶段下载完最小系统所需的软件包之后,通过chroot 使用debootstrap命令来对这些进行软件包的安装和配置。其中命令参数--second-stage表示执行第二阶段的安装。因为不能识别loongarch64,所以会报错。。。

    # chroot iso debootstrap/debootstrap --second-stage
    chroot: failed to run command ‘debootstrap/debootstrap’: Exec format error
  3. 因为主机跑在x86架构上,而我们要制作的文件系统是龙芯架构的,因此可以使用qemu-loongarch64-static来模拟成loongarch64的执行环境。

    # cp ./qemu-loongarch64-static iso/usr/bin/

    接下来就是在Binfmt注册LoongArch可执行文件的信息了,使用如下命令:

    # cd /proc/sys/fs/binfmt_misc
    # cat status 
    enabled
    
    # echo ":qemu-loongarch64: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
    
    # ls
    llvm-6.0-runtime.binfmt  python2.7  python3.6  qemu-loongarch64-static  register  status
    
    # cat qemu-loongarch64-static 
    enabled
    interpreter /usr/bin/qemu-loongarch64-static
    flags: 
    offset 0
    magic 7f454c4602010100000000000000000002000201
    mask fffffffffffefe00fffffffffffffffffeffffff
    

    以上命令注册了LoongArch可执行文件“头信息”,并指定符合条件的文件会使用/usr/bin/qemu-loongarch64-static"命令来执行,所以这个"/usr/bin/qemu-loongarch64-static"命令必须真实有效。

    上面通过命令手动注册的方式重启系统后就失效了,想要每次系统重启仍然有效可以在/var/lib/binfmts创建一个qemu-loongarch64的文件。内容如下:

    # cat /var/lib/binfmts/qemu-loongarch64 
    qemu-user-static
    magic
    0
    \x7f\x45\x4c\x46\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
    
    yes
    
    

    注册完了信息之后继续debootstrap的第二阶段

    # chroot iso debootstrap/debootstrap --second-stage
    I: Keyring file not available at /usr/share/keyrings/debian-archive-keyring.gpg; switching to https mirror https://deb.debian.org/debian
    I: Installing core packages...
    I: Unpacking required packages...
    I: Unpacking base-files...
    I: Unpacking base-passwd...
    I: Unpacking bash...
    I: Unpacking bsdutils...
    ....
    
    I: Configuring libgnutls30:loongarch64...
    I: Configuring libapt-pkg5.0:loongarch64...
    I: Configuring apt...
    I: Configuring libc-bin...
    I: Base system installed successfully.

    显示"I:Base system installed successfully."这句话,说明第二阶段已经完成。接下来就可以愉快的使用刚刚制作系统了。

  4. 通过chroot 进入刚制作的文件系统

    # chroot iso
    root@ubuntu:/# uname -a
    Linux ubuntu 5.16.0 #90~18.04.1-Ubuntu SMP Tue Jul 13 19:40:02 UTC 2021 loongarch64 loongarch64 loongarch64 GNU/Linux
    
    root@ubuntu:/# cat /etc/issue
    Loongnix GNU/Linux 20 Release 1 \n \l
    
    root@ubuntu:/# cat /etc/debian_version 
    10.4

    从上面的信息可以看到运行在loongarch64 架构了

  5. 参考

    https://github.com/sunhaiyong1978/CLFS-for-LoongArch

1
https://gitee.com/michael0066/qemu-loongarch64-static.git
git@gitee.com:michael0066/qemu-loongarch64-static.git
michael0066
qemu-loongarch64-static
qemu-loongarch64-static
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891