aboutsummaryrefslogtreecommitdiff
path: root/debian-container/README
blob: 0328defe44543089508220de79a890298d412972 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
Debian 容器

这个工具使用 Linux 的 mount namespace、PID namespace 和 network namespace,创建
一个最小 Debian 实验环境。

用法:

第一次创建基础软件包 tarball:

sudo ./debian-container.sh base-create \
  --output ./debian-stable.tar.gz \
  --suite stable \
  --arch amd64 \
  --include sudo,vim-tiny,curl

默认同时生成:

debian-stable.tar.gz.manifest

查看基础 tarball 信息:

sudo ./debian-container.sh base-info ./debian-stable.tar.gz

从 tarball 创建容器:

sudo ./debian-container.sh init test-1 --tarball ./debian-stable.tar.gz
sudo ./debian-container.sh init test-2 --tarball ./debian-stable.tar.gz
sudo ./debian-container.sh init test-3 --size 8G --tarball ./debian-stable.tar.gz

使用 profile 创建容器:

profile 文件内容:

network=none
mount=/home/richard/project:/workspace:rw
mount=/home/richard/data:/data:ro

sudo ./debian-container.sh init lab --profile ./lab.profile --tarball ./debian-stable.tar.gz
sudo ./debian-container.sh enter lab

扩容容器:

sudo ./debian-container.sh resize test-3 --size 12G

进入容器:

sudo ./debian-container.sh enter test-1
sudo ./debian-container.sh enter test-1 --network host
sudo ./debian-container.sh enter test-1 --network nat
sudo ./debian-container.sh enter test-1 --mount /home/richard/project:/workspace:rw
sudo ./debian-container.sh enter test-1 --mount /home/richard/data:/data:ro

在容器中执行命令:

sudo ./debian-container.sh exec test-1 -- uname -a
sudo ./debian-container.sh exec test-1 --network host -- apt-get update
sudo ./debian-container.sh exec test-1 --network nat -- apt-get update
sudo ./debian-container.sh exec test-1 --mount /home/richard/project:/workspace:rw -- make -C /workspace

列出和查看容器:

sudo ./debian-container.sh list
sudo ./debian-container.sh status test-1

删除容器:

sudo ./debian-container.sh delete test-1

直接联网初始化容器:

sudo ./debian-container.sh init test-3

默认情况下,实例数据位于当前目录的 instances/ 下:

instances/test-1/rootfs.ext4
instances/test-1/root/
instances/lab/config

默认基础 tarball:

debian-base.tar.gz

可以通过环境变量修改路径:

sudo env \
  DEBIAN_INSTANCES=/path/to/instances \
  ./debian-container.sh enter

可配置的环境变量:

DEBIAN_INSTANCES 实例目录
DEBIAN_SIZE     新实例镜像大小,默认 3G
DEBIAN_LOCK_FILE 锁文件路径
DEBIAN_TARBALL  默认基础 tarball 路径
DEBIAN_SUITE    Debian 版本,默认 stable
DEBIAN_ARCH     目标架构
DEBIAN_MIRROR   Debian 镜像地址
DEBIAN_INCLUDE  额外软件包,使用逗号分隔
DEBIAN_EXCLUDE  要排除的软件包,使用逗号分隔
DEBIAN_NETWORK  默认网络模式,none、host 或 nat
DEBIAN_NAT_BRIDGE  NAT 网桥名称,默认 rutils0
DEBIAN_NAT_NETWORK NAT 子网,默认 10.88.0.0/24
DEBIAN_NAT_GATEWAY NAT 网关地址,默认 10.88.0.1
DEBIAN_NAT_ADDRESS NAT 容器地址,默认 10.88.0.2
DEBIAN_NAT_DNS NAT 模式 DNS,默认 1.1.1.1

挂载参数格式:

HOST:CONTAINER[:ro|rw]

目前只支持目录挂载,默认模式为 rw。

实例 profile 保存在 instances/NAME/config,支持 network 和 mount 配置项。
enter 或 exec 的命令行选项会覆盖 profile 中的网络模式,并追加挂载项。

脚本实现按职责拆分在 lib/ 目录中,入口脚本只负责加载模块和分发命令。