巴科barco是一個我努力的項目,旨在根據Internet上的其他指南了解有關Linux容器和Linux內核的更多信息。 Linux容器由一組Linux內核功能組成:
namespaces :用於將內核對象分為不同的集合,這些集合可以通過特定的過程樹訪問。有不同類型的namespaces ,例如,使用PID名稱空間來隔離過程樹,而network名稱空間則用於隔離網絡堆棧。seccomp :用於限製過程可以進行的系統調用(通過syscalls處理)capabilities :用於設置UID 0(root)可以執行的限制(通過syscalls處理)cgroups :用於限制一個過程可以使用的資源(例如內存,磁盤I/O,CPU-TME)(通過Cgroupfs處理) barco可用於運行bin/sh .從/目錄作為root (-u 0),帶有以下命令(可選-v ,用於詳細輸出):
$ sudo ./bin/barco -u 0 -m / -c /bin/sh -a . [-v]
22:08:41 INFO ./src/barco.c:96: initializing socket pair...
22:08:41 INFO ./src/barco.c:103: setting socket flags...
22:08:41 INFO ./src/barco.c:112: initializing container stack...
22:08:41 INFO ./src/barco.c:120: initializing container...
22:08:41 INFO ./src/barco.c:131: initializing cgroups...
22:08:41 INFO ./src/cgroups.c:73: setting memory.max to 1G...
22:08:41 INFO ./src/cgroups.c:73: setting cpu.weight to 256...
22:08:41 INFO ./src/cgroups.c:73: setting pids.max to 64...
22:08:41 INFO ./src/cgroups.c:73: setting cgroup.procs to 1458...
22:08:41 INFO ./src/barco.c:139: configuring user namespace...
22:08:41 INFO ./src/barco.c:147: waiting for container to exit...
22:08:41 INFO ./src/container.c:43: # ## BARCONTAINER STARTING - type 'exit' to quit ###
# ls
bin home lib32 media root sys vmlinuz
boot initrd.img lib64 mnt run tmp vmlinuz.old
dev initrd.img.old libx32 opt sbin usr
etc lib lost+found proc srv var
# echo "i am a container"
i am a container
# exit
22:08:55 INFO ./src/barco.c:153: freeing resources...
22:08:55 INFO ./src/barco.c:168: so long and thanks for all the fishbarco需要安裝許多工具和庫來構建項目和開發。
# Install all required tooling and dependencies
$ sudo apt install -y make
$ make setupbarco取決於以下“非標準”庫:
libseccomp :用於設置seccomp過濾器libcap :用於設置容器功能libcuni1 :用於與cunit進行測試barco使用許多基於LLVM-18的工具來開發,覆蓋,格式化,調試和Valgrind來檢查內存洩漏。
隨附的Makefile提供了一些構建barco的目標。可以將變量debug=1設置為在“調試”模式下運行任何目標,該模式使用調試符號構建項目,而無需優化(對於調試器和Valgrind尤其有用)。
# Build barco (executable is in bin/)
# The default target also runs, "make lint" and "make format" to lint and format the code
$ make
# Build barco with debug flags
$ make debug=1barco是使用Visual Studio代碼和GitHub代碼空間開發的。存儲庫包含有效使用這些工具的所有必要的配置文件。 barco依賴於低級Linux功能,因此必須在Linux系統上運行。 github代碼在調整低級容器設置時有時會表現出奇怪的作用:我發現Getutm.App有疑問時可以在Mac上與Debian一起工作。
隨附的Makefile提供了一些對開發有用的目標:
# Run tests
$ make test
# Run linter
$ make lint
# Run formatter
$ make format
# Run valgrind
$ make check
# Clean the build
$ make clean此外,該項目在.vscode/中還包括可用於運行內置調試器的Visual Studio代碼配置(此刻,它是“禁用”的,因為barco應作為root和CodellDB沒有該選項運行)。
該項目的結構如下:
├── .devcontainer configuration for GitHub Codespaces
├── .github configuration GitHub Actions and other GitHub features
├── .vscode configuration for Visual Studio Code
├── bin the executable (created by make)
├── build intermediate build files e.g. * .o (created by make)
├── docs documentation
├── include header files
├── lib third-party libraries
├── scripts scripts for setup and other tasks
├── src C source files
│ ├── barco.c (main) Entry point for the CLI
│ └── * .c
├── tests contains tests
├── .clang-format configuration for the formatter
├── .clang-tidy configuration for the linter
├── .gitignore
├── LICENSE
├── Makefile
└── README.md目前,該項目不包含任何自動化測試或工具來記錄代碼。將來,可能會添加適合自動測試和文檔的合適工具。
barco已在Debian 12上進行了測試,並在6.1.0版中運行Linux內核,並啟用了用戶名稱空間和Cgroupsv2。
barco不處理網絡名稱空間,因此容器無法訪問網絡。可以大致設置網絡,如下所示:
在C中,這通常是通過rtnetlink接口完成的。此外,網絡使用情況可能會受到net_prio CGROUP控制器的限制。
cgroups.c , mount.c , sec.c , user.c中的功能是特定於barco的,應使其更通用用於開發barco一些資源是: