问题
在FreeBSD jail里使用linux兼容创建了ubuntu环境,但是没有apt命令,该怎么办呢? jail的介绍:FreeBSD jail 容器手册_freebsd虚拟化-CSDN博客
系统是这样进去的:
jexec focal chroot /compat/ubuntu /bin/bash
这个问题很少碰到,大约只有两种情况会碰到:1 FreeBSD jail虚拟容器里装ubuntu系统 2 非常精简的ubuntu以至于把apt都精简掉了。
先上解决方法
因为对ubuntu不熟,所有走了弯路,注意以下几点可以少踩坑:
1 创建ubuntu jail的时候,注意就选好版本
普通用户(非FreeBSD jail用户)可以略过这一步,不过话说回来,如果不做ubuntu jail,普通的ubuntu用户也不会碰到没有apt的情况。
比如想用focal,命令里面模板就用ubuntu_focal
cbsd jcreate jname=ubu1 jprofile=ubuntu_focal allow_raw_sockets=1
cbsd jcreate jname=ubu2 jprofile=ubuntu_jammy allow_raw_sockets=1
2 启动jail之后,直接进入/var/cache/apt/archivers目录安装所有deb包
使用cbsd jstart ubu1 启动jail,然后使用cbsd jlogin ubu1 进入jail内部
然后再用chroot /compat/ubuntu /bin/bash 进入ubuntu系统,
然后在/var/cache/apt/archivers目录安装所有deb包
dpkg -i *.deb
这样就不用一个一个安装deb包了,当然如果能精确控制最好,但是对大多数项目不需要这一点半点的空间,即使对单片机,也不在乎这区区几十几百M的空间了
3 修复dpkg和apt
修复dpkg
dpkg --configure -a
修复apt
apt --fix-broken install
最后apt update一下,完活拉!
apt update
踩坑详细过程
经过复杂的操作,安装好FreeBSD虚拟容器jail里的Ubuntu系统,但是发现没有apt,这样啥也干不了!
咨询大模型:
如果没有gpt,我如何dpkg 安装apt_1.9.3_amd64.deb这个包
如果你的系统中没有gpt命令,你可以尝试使用以下方法来安装apt_1.9.3_amd64.deb这个包:
- 使用dpkg命令:即使没有gpt,你仍然可以使用dpkg命令来安装.deb包。你可以在终端中运行以下命令来安装apt_1.9.3_amd64.deb包:
sudo dpkg -i apt_1.9.3_amd64.deb这将尝试安装apt_1.9.3_amd64.deb包,但请注意,如果有其他依赖文件缺失,你可能需要手动安装这些依赖文件。希望这个回答对你有所帮助!如果你还有其他问题,请随时提问。
因为用习惯了apt,dpkg好像好多年没有用过了,所以是help+大模型+baidu一起开干!
从网上下载apt的包安装(后来发现虚拟容器里自带)
一开始是从网上下载了apt包,而且是下了个很新的
wget http://www.ftp.ne.jp/Linux/packages/ubuntu/archive/pool/main/a/apt/apt_2.9.4_amd64.deb
因为当时容器里没有apt,当然也没有wget,所以是跑到ubuntu容器外面下载的,然后到ubuntu里面使用dpkg -i安装
dpkg -i apt_2.9.4_amd64.deb
结果发现依赖太多,不可能缺的库都到网上下载吧? 这简直是不可能完成的任务。
本地找到deb安装apt
查看debootstrap jammy /compat/ubuntu时候的log信息,发现本地有apt的包。
于是进入jammy ubuntu,发现apt的deb包放在这个目录:/var/cache/apt/archives
里面有apt的安装包,于是安装,并根据提示安装需要的包:
dpkg -i apt_2.4.5_amd64.deb
dpkg -i libapt-pkg6.0_2.4.5_amd64.deb
dpkg -i libstdc++6_12-20220319-1ubuntu1_amd64.deb
dpkg -i libxxhash0_0.8.1-1_amd64.deb dpkg -i libapt-pkg6.0_2.4.5_amd64.deb
dpkg不如apt好用,安装的时候一直提示缺其它依赖,缺哪个就再装哪个。尽管这个目录下有所有需要的包,但一个一个装还是很让人讨厌。
最终走了一圈弯路后,终于知道了一次安装所有包的命令:
dpkg -i *.deb
这样一次性安装好了所有的包,解决了安装apt之后的包依赖问题。
详细描述期间走的弯路
解决apt依赖库的问题
使用dpkg -i apt_2.4.5_amd64.deb 安装apt的时候,碰到了依赖问题,本身执行apt命令的时候,它会报错自己的依赖库,但是它是顺序报错,也就是补上一个依赖库,再报下一个依赖库,所以非常麻烦非常慢。幸运的是后来找到了用一条命令来一次性找到所有的依赖,即使用dpkg -I apt_2.0.2_amd64.deb |grep Depends命令来找依赖:
/var/cache/apt/archives# dpkg -I apt_2.0.2_amd64.deb |grep Depends
Depends: adduser, gpgv | gpgv2 | gpgv1, libapt-pkg6.0 (>= 2.0.2), ubuntu-keyring, libc6 (>= 2.15), libgcc-s1 (>= 3.0), libgnutls30 (>= 3.6.12), libseccomp2 (>= 2.4.2), libstdc++6 (>= 9), libsystemd0
在系统的/var/cache/apt/archivers目录,这些依赖库的deb文件都在。
I have no name!@focal:/var/cache/apt/archives# ls add*
adduser_3.118ubuntu2_all.deb
I have no name!@focal:/var/cache/apt/archives# ls gpgv_2.2.19-3ubuntu2_amd64.deb
gpgv_2.2.19-3ubuntu2_amd64.deb
I have no name!@focal:/var/cache/apt/archives# ls liba*
libacl1_2.2.53-6_amd64.deb libatm1_1%3a2.5.1-4_amd64.deb
libapparmor1_2.13.3-7ubuntu5_amd64.deb libattr1_1%3a2.4.48-5_amd64.deb
libapt-pkg6.0_2.0.2_amd64.deb libaudit-common_1%3a2.8.5-2ubuntu6_all.deb
libargon2-1_0~20171227-0.2_amd64.deb libaudit1_1%3a2.8.5-2ubuntu6_amd64.deb
I have no name!@focal:/var/cache/apt/archives# ls libc*
libc-bin_2.31-0ubuntu9_amd64.deb libcap2_1%3a2.32-1_amd64.deb
libc6_2.31-0ubuntu9_amd64.deb libcom-err2_1.45.5-2ubuntu1_amd64.deb
libcap-ng0_0.7.9-2.1build1_amd64.deb libcrypt1_1%3a4.4.10-10ubuntu4_amd64.deb
libcap2-bin_1%3a2.32-1_amd64.deb libcryptsetup12_2%3a2.2.2-3ubuntu2_amd64.deb
I have no name!@focal:/var/cache/apt/archives# ls libgcc*
libgcc-s1_10-20200411-0ubuntu1_amd64.deb
I have no name!@focal:/var/cache/apt/archives# ls libgnu*
libgnutls30_3.6.13-2ubuntu1_amd64.deb
I have no name!@focal:/var/cache/apt/archives# ls libsec*
libseccomp2_2.4.3-1ubuntu1_amd64.deb
I have no name!@focal:/var/cache/apt/archives# ls libsys*
libsystemd0_245.4-4ubuntu3_amd64.deb
于是使用dpkg -i 安装这些库:
dpkg -i adduser_3.118ubuntu2_all.deb gpgv_2.2.19-3ubuntu2_amd64.deb libapt-pkg6.0_2.0.2_amd64.deb libc6_2.31-0ubuntu9_amd64.deb libgcc-s1_10-20200411-0ubuntu1_amd64.deb
dpkg -i libgnutls30_3.6.13-2ubuntu1_amd64.deb libseccomp2_2.4.3-1ubuntu1_amd64.deb libsystemd0_245.4-4ubuntu3_amd64.deb
dpkg -i 安装的时候又显示一大堆东西,而且也报依赖库问题。也就是依赖的库还依赖其它库....这样简直没法完成这个任务....
怒了,不管三七二十一,使用dpkg -i *.deb安装所有包:
dpkg -i *.deb
安装完所有的包之后,apt update总算可以执行了,但是有报错,apt search和apt install 也无法执行。
解决apt search和apt install 无法执行的问题
执行apt --fix-broken install 修复,结果没成功。那就执行dpkg的修复:
dpkg --configure -a
按照提示选择即可。修复完成后,再执行apt的修复:
apt --fix-broken install
期间会问到一些问题,比如
执行完毕,apt就算修复好了,这时候查找和安装wget试试,成功!
apt search wget
Sorting... Done
Full Text Search... Done
devscripts/focal 2.20.2ubuntu2 amd64
scripts to make the life of a Debian Package maintainer easier
wget/focal 1.20.3-1ubuntu1 amd64
retrieves files from the web
I have no name!@focal:/var/cache/apt/archives# apt install wget
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
libpsl5 publicsuffix
总结
ubuntu没有apt,这对FreeBSD用户来说可是废了老劲了,因为FreeBSD的jail里面再精简,也有pkg这个包管理软件。退一万步,jail里面没有pkg,也可以在host主机使用jexec 来安装,如jail序号为1,那么是 jexec 1 pkg install xxpkg ,如下面jail的序号是24,主机名是focal:
root@fbhost:~ # jexec 24 pkg search wget
pkg: No SRV record found for the repo 'FreeBSD'
wget-1.24.5 Retrieve files from the Net via HTTP(S) and FTP
wget2-2.1.0_1 File and recursive website downloader
wgetpaste-2.34 Paste to several pastebin services via bash script
root@fbhost:~ # jexec focal pkg search wget
pkg: No SRV record found for the repo 'FreeBSD'
wget-1.24.5 Retrieve files from the Net via HTTP(S) and FTP
wget2-2.1.0_1 File and recursive website downloader
wgetpaste-2.34 Paste to several pastebin services via bash script
FreeBSD本身不能创建ubuntu jail虚拟机(jail比虚拟机更轻量极) ,而是在FreeBSD jail虚拟机里使用了linux虚拟,这样两层虚拟操作,导致无法使用jexec 原生的命令来安装ubuntu的包,因为官方根本就没提供啊! 官方没有,jail里也没有,用户可不就不知所措了。
多亏有文心和chatgpt,反复询问这哥俩,终于弄明白了dpkg -i xx.deb的解决方法,最终跌跌撞撞的装好了apt,这样就算把ubuntu jail跑通了!
顺便,ubuntu jail里默认没有装好apt,刚开始很不理解,后来就想明白了,因为jail本身就是轻量极,用户未必需要apt那一套系统,这样就把是否安装apt选择权给了用户,可以节省系统资源。
避坑指南
因为对ubuntu不熟,所有走了弯路,注意以下几点可以少踩坑:
1 创建ubuntu jail的时候,注意选好版本
比如想用ubuntu focal,命令里面模板就用ubuntu_focal
cbsd jcreate jname=ubu1 jprofile=ubuntu_focal allow_raw_sockets=1
cbsd jcreate jname=ubu2 jprofile=ubuntu_jammy allow_raw_sockets=1
2 启动jail之后,直接进入/var/cache/apt/archivers目录安装所有deb包
dpkg -i *.deb
这样就不用一个一个安装deb包了,当然如果能精确控制最好,但是对大多数项目不需要节省这一点半点的空间,即使对单片机,也不在乎这区区几十几百M的空间了。
3 修复dpkg和apt
修复dpkg
dpkg --configure -a
修复apt
apt --fix-broken install
最后update一下,完活拉!
apt update
调试
在csdn写了命令之后cp到终端执行报错
dpkg -i libgnutls30_3.6.13-2ubuntu1_amd64.deb libseccomp2_2.4.3-1ubuntu1_amd64.deb libsystemd0_245.4-4ubuntu3_amd64.deb
dpkg: error: unknown option -�
Type dpkg --help for help about installing and deinstalling packages [*];
Use 'apt' or 'aptitude' for user-friendly package management;
Type dpkg -Dhelp for a list of dpkg debug flag values;
Type dpkg --force-help for a list of forcing options;
Type dpkg-deb --help for help about manipulating *.deb files;
重复命令,将每个空格重写,问题解决。
所以命令还是要写在代码里,写在外面可能因为字符的原因而出错,尤其是命令较长,有多个空格的情况下。
apt update 有个warning
apt update
Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease
Reading package lists... Done
Building dependency tree... Done
All packages are up to date.
W: No sandbox user '_apt' on the system, can not drop privileges
apt update之后无法search 和install安装
apt search wget之后直接Segmentation fault (core dumped)
apt install wget之后报一堆依赖depends问题
apt install wget
Reading package lists... Done
Building dependency tree... Done
You might want to run 'apt --fix-broken install' to correct these.
The following packages have unmet dependencies:
adduser : Depends: debconf (>= 0.5) but it is not going to be installed or
debconf-2.0
apt : Depends: libsystemd0 but it is not going to be installed
ca-certificates : Depends: debconf (>= 0.5) but it is not going to be installed or
debconf-2.0
console-setup-linux : Depends: keyboard-configuration (= 1.194ubuntu3) but it is not going to be installed
dbus : Depends: libsystemd0 but it is not going to be installed
debconf-i18n : Depends: debconf (= 1.5.73) but it is not going to be installed
Depends: liblocale-gettext-perl but it is not going to be installed
init-system-helpers : Depends: perl-base (>= 5.20.1-3) but it is not going to be installed
iproute2 : Depends: debconf (>= 0.5) but it is not going to be installed or
debconf-2.0
isc-dhcp-client : Depends: debianutils (>= 2.8.2) but it is not going to be installed
isc-dhcp-common : Depends: debianutils (>= 2.8.2) but it is not going to be installed
kbd : Depends: console-setup but it is not going to be installed or
console-setup-mini but it is not installable
libapt-pkg6.0 : Depends: libsystemd0 (>= 221) but it is not going to be installed
按照提示执行
apt --fix-broken install
执行apt --fix-broken install 修复的失败
I have no name!@focal:/var/cache/apt/archives# apt --fix-broken install
Reading package lists... Done
Building dependency tree
Reading state information... Done
Correcting dependencies... Done
The following additional packages will be installed:
console-setup coreutils debianutils e2fsprogs init keyboard-configuration
liblocale-gettext-perl mount python3 python3-minimal python3.8-minimal systemd systemd-sysv
Suggested packages:
gpart parted fuse2fs e2fsck-static nfs-common python3-doc python3-tk python3-venv
binfmt-support systemd-container policykit-1
Recommended packages:
e2fsprogs-l10n
The following NEW packages will be installed:
console-setup coreutils debianutils e2fsprogs init keyboard-configuration
liblocale-gettext-perl mount python3 python3-minimal python3.8-minimal systemd systemd-sysv
0 upgraded, 13 newly installed, 0 to remove and 0 not upgraded.
111 not fully installed or removed.
Need to get 0 B/8064 kB of archives.
After this operation, 32.5 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
dpkg-preconfigure: unable to re-open stdin: No such file or directory
E: Can not write log (Is /dev/pts mounted?) - posix_openpt (2: No such file or directory)
Setting up apt (2.0.2) ...
/etc/kernel/postinst.d/apt-auto-removal: 21: awk: not found
/etc/kernel/postinst.d/apt-auto-removal: 30: awk: not found
dpkg: error processing package apt (--configure):
installed apt package post-installation script subprocess returned error exit status 127
Errors were encountered while processing:
apt
E: Sub-process /usr/bin/dpkg returned an error code (1)
于是决定使用dpkg --configure -a 重新配置
dpkg --configure -a
使用dpkg --configure -a 重新配置,里面好像设置了城市?
然后执行apt 修复
apt --fix-broken install
会有菜单,选择英文,控制台等。
最后apt update一下,完活!
apt update