在开发的过程中,我需要往某个容器的rootfs里面加一个mkfs.ext4命令。由于容器rootfs基于的base已经不知道是啥了(古早的东西),导致我直接下载安装的rpm包装不上(动态链接库版本问题)。因此打算静态编译一个mkfs.ext4命令来解决。

操作步骤

1. 克隆源代码

首先,从GitHub上克隆e2fsprogs源代码:

git clone https://github.com/tytso/e2fsprogs

2. 切换到特定版本

切换到v1.45.6版本,这是一个稳定版本:

cd e2fsprogs
git checkout v1.45.6

3. 创建构建目录

在源代码目录下创建一个构建目录,并进入该目录:

mkdir build && cd build

4. 创建sysroot目录

在构建目录下创建一个sysroot目录,用于存放编译后的文件:

mkdir sysroot

5. 配置编译选项

使用configure命令配置编译选项,这里使用musl-gcc编译器,并添加-static选项以生成静态链接的可执行文件:

../configure CC=musl-gcc LDFLAGS=-static --prefix=$(pwd)/sysroot

6. 编译

使用make命令进行编译,这里使用-j 32选项以加速编译过程:

make -j 32

7. 安装

编译完成后,使用make install命令将编译好的文件安装到sysroot目录:

make install

8. 查看安装结果

sysroot目录下,我们可以看到以下文件:

sysroot/
├── bin
   ├── chattr
   ├── lsattr
   └── uuidgen
├── etc
   └── mke2fs.conf
├── lib
   ├── e2fsprogs
   └── e2initrd_helper
├── sbin
   ├── badblocks
   ├── blkid
   ├── debugfs
   ├── dumpe2fs
   ├── e2freefrag
   ├── e2fsck
   ├── e2image
   ├── e2label
   ├── e2mmpstatus
   ├── e2undo
   ├── e4crypt
   ├── e4defrag
   ├── filefrag
   ├── findfs
   ├── fsck
   ├── fsck.ext2
   ├── fsck.ext3
   ├── fsck.ext4
   ├── logsave
   ├── mke2fs
   ├── mkfs.ext2
   ├── mkfs.ext3
   ├── mkfs.ext4
   ├── mklost+found
   ├── resize2fs
   ├── tune2fs
   └── uuidd

9. 验证

为了验证mkfs.ext4命令是否为静态链接,可以使用以下命令:

readelf -h sysroot/sbin/mkfs.ext4

输出结果如下:

ELF Header:
  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF64
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           Advanced Micro Devices X86-64
  Version:                           0x1
  Entry point address:               0x4028c0
  Start of program headers:          64 (bytes into file)
  Start of section headers:          2921504 (bytes into file)
  Flags:                             0x0
  Size of this header:               64 (bytes)
  Size of program headers:           56 (bytes)
  Number of program headers:         5
  Size of section headers:           64 (bytes)
  Number of section headers:         23
  Section header string table index: 22

从输出结果的 ` Type: EXEC (Executable file)` 可以看出,mkfs.ext4是一个静态链接的可执行文件。

10. 使用

接着就可以使用这个程序来格式化文件系统了。

欢迎关注我的公众号“灯珑”,让我们一起了解更多的事物~

你也可能喜欢

发表评论