当前位置:   article > 正文

FreeBSD通过CBSD管理低资源容器jail来安装Ubuntu子系统实践

FreeBSD通过CBSD管理低资源容器jail来安装Ubuntu子系统实践

简介

FreeBSD、CBSD、Jail和Ubuntu,四者的组合方案可以说是强强联合,极具性价比和竞争力!同时安装简单方便,整体方案非常先进。

CBSD是为FreeBSD jail子系统、bhyve、QEMU/NVMM和Xen编写的管理层。该项目定位为一个综合解决方案的单一集成工具,用于使用预定义的软件集以最少的配置快速构建和部署计算机虚拟环境。

FreeBSD jail系统容器本身是FreeBSD子系统的低资源解决方案,但是因为FreeBSD支持Linux模拟,就使得jail容器也支持了Linux,比如可以在jail里建立Ubuntu子系统。整个解决方案非常丝滑,简单方便。

一、安装cbsd

pkg安装

pkg install cbsd

CBSD初始化


如果是使用zfs系统,需要先执行:

/sbin/zfs create -o mountpoint=/usr/jails -o atime=off zroot/jails


然后进行初始化即可:

env workdir=/usr/jails /usr/local/cbsd/sudoexec/initenv

具体安装可以参考官网:CBSD — FreeBSD Jail and Bhyve Management Tools 

还有这篇文档: jail管理器CBSD实践@FreeBSD-CSDN博客

二、使用CBSD菜单方式安装Ubuntu子系统

创建Ubuntu子系统

使用命令

cbsd jconstruct-tui 

进入菜单后,配置profile选择ubuntu_jammy

名字jname 自定义,比如ubjammy

其它使用默认即可,如下图:

然后选择“2 GO”,按回车,很快就创建成功了:

  1. Please wait: this will take a while...
  2. Applying custom skel system dir template from: /usr/local/cbsd/share/Linux-jail-ubuntu-jammy-system-skel
  3. customskel dir specified but not found: /usr/local/cbsd/share/Linux-jail-ubuntu-jammy-skel
  4. To edit VM properties use: cbsd jconfig jname=ubjammy
  5. To start VM use: cbsd jstart ubjammy
  6. To stop VM use: cbsd jstop ubjammy
  7. To remove VM use: cbsd jremove ubjammy
  8. For attach VM console use: cbsd jlogin ubjammy
  9. Creating ubjammy complete: Enjoy!
  10. jcreate done in 9 seconds

可以看到6秒创建好了。使用cbsd jls可以看到还是关机状态,于是使用cbsd jstart ubjammy命令开机:

  1. cbsd jstart ubjammy
  2. Check environment script: 00.check_distribution.sh
  3. populate jails data from: /usr/jails/basejail/base_amd64_amd64_jammy ...
  4. Applying custom skel dir template from: /usr/jails/basejail/base_amd64_amd64_jammy
  5. Default NIC automatically selected: web
  6. set resource limit: [ ]
  7. jail renice: 1
  8. Starting jail: ubjammy, parallel timeout=5
  9. ubjammy: created
  10. CBSD setup: jail ipfw counters num: 107/108
  11. jstart done in 6 seconds

开机速度也很快,只需要6秒就开机成功!

登录Ubuntu子系统

这里要注意,使用常规登录方法是不行的

  1. cbsd jlogin ubjammy
  2. Welcome to Ubuntu 22.04.4 LTS (GNU/Linux 5.15.0 x86_64)
  3. * Documentation: https://help.ubuntu.com
  4. * Management: https://landscape.canonical.com
  5. * Support: https://ubuntu.com/pro
  6. This system has been minimized by removing packages and content that are
  7. not required on a system that users do not log into.
  8. To restore this content, you can run the 'unminimize' command.
  9. The programs included with the Ubuntu system are free software;
  10. the exact distribution terms for each program are described in the
  11. individual files in /usr/share/doc/*/copyright.
  12. Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
  13. applicable law.
  14. Cannot execute /bin/bash: No such file or directory

常规登录会报错没有/bin/bash文件,而导致无法登到虚拟子系统。这时候要用jail原装命令“jexec ubjammy sh”登录:

  1. root@fbhost:~ # jexec ubjammy sh
  2. # uname -a
  3. Linux ubjammy.my.domain 5.15.0 FreeBSD 14.1-RELEASE releng/14.1-n267679-10e31f0946d8 GENERIC x86_64 x86_64 x86_64 GNU/Linux

登录之后,通过使用uname命令,可以看到这是一台5.15.0内核版本Linux系统,到这里安装就算完成了

装好apt管理软件

装好内核后,并不能干什么事情,因为我们习惯了在Ubuntu下使用apt来管理软件,所以要

首先更新apt

apt update

然后就可以越快的玩耍拉!

三、使用CBSD命令方式安装Ubuntu子系统

可以直接用一条命令创建Linux子系统,但是如果需要Ubuntu子系统,则需要创建好之后再到里面安装Ubuntu虚拟子系统。

先用命令创建Linux虚拟子系统

使用命令创建:

cbsd jcreate jname=ubu jprofile=ubuntu_focal allow_raw_sockets=1

 一会儿就创建好了。注意,如果想后面装哪个版本的Ubuntu,就要在jprofile这里调用哪个版本的配置文件(怀疑如果不一致后面库会发生不一致的问题,实际上这里应该影响后面网上下载base.txz库的链接)。所以这里的例子是Ubuntu的focal版本。

  1. cbsd jcreate jname=ubu jprofile=ubuntu_focal allow_raw_sockets=1
  2. Please wait: this will take a while...
  3. Applying skel dir template from: /usr/jails/share/FreeBSD-jail-skel
  4. To edit VM properties use: cbsd jconfig jname=ubu
  5. To start VM use: cbsd jstart ubu
  6. To stop VM use: cbsd jstop ubu
  7. To remove VM use: cbsd jremove ubu
  8. For attach VM console use: cbsd jlogin ubu
  9. Creating ubu complete: Enjoy!
  10. /usr/jails/jails/ubu/etc already mounted
  11. /usr/jails/jails/ubu/root already mounted
  12. /usr/jails/jails/ubu/tmp already mounted
  13. /usr/jails/jails/ubu/home already mounted
  14. /usr/jails/jails/ubu/usr/local already mounted
  15. /usr/jails/jails/ubu/compat already mounted
  16. /usr/jails/jails/ubu/var already mounted
  17. jcreate done in 1 minutes and 9 seconds

启动子系统

  1. cbsd jstart ubu
  2. Default NIC automatically selected: web
  3. set resource limit: [ ]
  4. jail renice: 1
  5. Starting jail: ubu, parallel timeout=5
  6. ubu: created
  7. ELF ldconfig path: /lib /usr/lib /usr/lib/compat /usr/local/lib /usr/local/lib/compat/pkg /usr/local/lib/compat/pkg
  8. 32-bit compatibility ldconfig path: /usr/lib32 /usr/lib32
  9. Updating motd:.
  10. Creating and/or trimming log files.
  11. Clearing /tmp (X related).
  12. Updating /var/run/os-release done.
  13. Starting syslogd.
  14. Starting cron.
  15. Fri Jun 14 14:17:01 CST 2024
  16. CBSD setup: jail ipfw counters num: 99/100
  17. jstart done in 5 seconds

登录Linux子系统

与进入菜单方式安装的Ubuntu子系统不同,命令方式建立的子系统,既可以使用jexec指令进入,也可以用常规的cbsd指令进入。

使用jexec 指令直接进入Linux子系统

  1. jexec ubu chroot /compat/linux /bin/bash
  2. bash-4.2# uname -a
  3. Linux ubu.my.domain 5.15.0 FreeBSD 14.1-RELEASE releng/14.1-n267679-10e31f0946d8 GENERIC x86_64 x86_64 x86_64 GNU/Linux

使用cbsd命令需要两步

首先使用cbsd进入FreeBSD虚拟子系统

cbsd jlogin ubu

这时候系统是FreeBSD的。进入FreeBSD虚拟子系统后,再执行chroot进入Linux子系统

chroot /compat/linux/ /bin/bash

这样就进入了Linux子系统,但是如果想用Ubuntu,还需要手工安装Ubuntu子系统。

手工安装Ubuntu子系统

在FreeBSD子系统里面,安装Ubuntu子系统

首先安装debootstrap

pkg install debootstrap

 然后使用debootstrap安装Ubuntu

debootstrap focal /compat/ubuntu

安装好后

执行chroot进入Ubuntu子系统

  1. chroot /compat/ubuntu /bin/bash
  2. groups: cannot find name for group ID 0
  3. groups: cannot find name for group ID 5
  4. I have no name!@ubu:/# uname -a
  5. Linux ubu.my.domain 5.15.0 FreeBSD 14.1-RELEASE releng/14.1-n267679-10e31f0946d8 GENERIC x86_64 x86_64 x86_64 GNU/Linux

好了,Ubuntu子系统就安装好了。

登录ubuntu子系统

要使用这个子系统,可以直接在宿主FreeBSD主机使用命令进入:

jexec ubu chroot /compat/ubuntu /bin/bash

检查一下,确实是Ubuntu linux

  1. root@ubu:/# uname -a
  2. Linux ubu.my.domain 5.15.0 FreeBSD 14.1-RELEASE releng/14.1-n267679-10e31f0946d8 GENERIC x86_64 x86_64 x86_64 GNU/Linux

这样不需要再进入FreeBSD子系统周转,管理起来特别简单方便。当然可以采用先登录FreeBSD子系统,再进入Ubuntu子系统的方法:

  1. root@fbhost:~ # cbsd jlogin ubu
  2. Last login: Sun Jun 16 19:21:31 on pts/1
  3. FreeBSD 14.1-RELEASE (GENERIC) releng/14.1-n267679-10e31f0946d8
  4. ubu:/root@[19:33] # chroot /compat/ubuntu/ /bin/bash
  5. root@ubu:/# uname -a
  6. Linux ubu.my.domain 5.15.0 FreeBSD 14.1-RELEASE releng/14.1-n267679-10e31f0946d8 GENERIC x86_64 x86_64 x86_64 GNU/Linux

由于jail是特别轻量级的容器,所以Ubuntu子系统竟然连apt也没有装.....(其实这里是误会了,正常安装的话都是有apt的,我没有安装出来,是因为使用的base.txz里面没装apt,啥都没装)

如果没有apt,那么要装apt也是一个费脑筋的活。

四、安装Ubuntu子系统下的apt管理软件

可以参考这个文档:FreeBSD jail虚拟容器里装ubuntu系统没有apt命令怎么办?-CSDN博客

一般大家不会碰到我这个问题,我是因为使用的base.txz里面没装apt,啥都没装导致的。

不过万一碰到了没有apt的情况,可以使用如下方法安装: 

首先进入Ubuntu子系统

jexec ubu chroot /compat/ubuntu /bin/bash

  1. root@fbhost:~ # cbsd jlogin ubu
  2. Last login: Sun Jun 16 19:21:31 on pts/1
  3. FreeBSD 14.1-RELEASE (GENERIC) releng/14.1-n267679-10e31f0946d8
  4. ubu:/root@[19:33] # chroot /compat/ubuntu/ /bin/bash
  5. root@ubu:/# uname -a
  6. Linux ubu.my.domain 5.15.0 FreeBSD 14.1-RELEASE releng/14.1-n267679-10e31f0946d8 GENERIC x86_64 x86_64 x86_64 GNU/Linux

直接进入/var/cache/apt/archivers目录dpkg安装所有deb包

进入目录

cd /var/cache/apt/archives

然后使用dpkg -i 命令安装所有deb包

dpkg -i *.deb

这样就不用一个一个安装deb包了,当然如果能精确控制最好,但是对大多数项目不需要这一点半点的空间,即使对单片机,也不在乎这区区几十几百M的空间了 

3 修复dpkg和apt

修复dpkg 

dpkg --configure -a

修复apt

apt --fix-broken install

最后apt update一下,完活拉!

apt update

五、总结

FreeBSD、CBSD、Jail和Ubuntu,四者的组合方案可以说是强强联合,极具性价比和竞争力!同时安装简单方便,整体方案非常先进。

但是第一次实践的时候,估计会走一些弯路,因为说明书实在太简单了,忽略了很多细节,这导致新手会踩好几个坑。好消息就是坑被我踩过了,大家可以愉快的实践拉!

调试

apt update 报错

  1. apt update
  2. Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease
  3. Hit:2 http://security.ubuntu.com/ubuntu jammy-security InRelease
  4. Hit:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease
  5. Hit:4 http://archive.ubuntu.com/ubuntu jammy-backports InRelease
  6. E: Dynamic MMap ran out of room. Please increase the size of APT::Cache-Start. Current value: 25165824. (man 5 apt.conf)
  7. Reading package lists... Error!
  8. E: Dynamic MMap ran out of room. Please increase the size of APT::Cache-Start. Current value: 25165824. (man 5 apt.conf)
  9. E: Error occurred while processing libsurgescript0.5.4.4 (NewVersion1)
  10. E: Problem with MergeList /var/lib/apt/lists/archive.ubuntu.com_ubuntu_dists_jammy_universe_binary-amd64_Packages
  11. E: The package lists or status file could not be parsed or opened.

尝试更新dpkg 

dpkg --configure -a

没反应

尝试修复apt

  1. apt --fix-broken install
  2. E: Dynamic MMap ran out of room. Please increase the size of APT::Cache-Start. Current value: 25165824. (man 5 apt.conf)
  3. Reading package lists... Error!
  4. E: Dynamic MMap ran out of room. Please increase the size of APT::Cache-Start. Current value: 25165824. (man 5 apt.conf)
  5. E: Error occurred while processing libsurgescript0.5.4.4 (NewVersion1)
  6. E: Problem with MergeList /var/lib/apt/lists/archive.ubuntu.com_ubuntu_dists_jammy_universe_binary-amd64_Packages
  7. E: The package lists or status file could not be parsed or opened.

这好象是因为空间不够造成的。

先搁置。

有时候因为中文空格或拼写错误导致命令会报错

比如

  1. root@ubu:/var/cache/apt/archives# cd /var/cache/apt/archivers
  2. bash: cd: /var/cache/apt/archivers: No such file or directory

这是拼写错误。

有时候是因为中文空格,这时候把命令中间的空格在终端重新输入一下即可。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/741162
推荐阅读
相关标签
  

闽ICP备14008679号