schroot的安装与使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014633283/article/details/73823484

什么是schroot

schroot使得用户可以在不同的chroot下执行交互命令

Schroot allows users to execute commands or interactive shells in different ——debian wiki


schroot和chroot的不同


  • chroot只能被root用户使用,schroot可以被普通用户使用
  • schrootchroot的基础上还提供了permissions checking, environmental setup(如filesystem mount)等

A chroot may be used directly as root by running chroot(8), but normal users are not able to use this command. schroot allows access to chroots for normal users using the same mechanism, but with permissions checking and allowing additional automated setup of the chroot environment, such as mounting additional filesystems and other configuration tasks. This automated setup is done through the action of setup scripts which may be customised and extended to perform any actions required. ——askubuntu


schroot安装和使用

安装

schroot的安装很简单,直接参考官方文档即可,此处省略

使用

schroot的使用分为三步:
Step1 : 下载目标系统的基础文件系统
Step2 : 配置挂载目录
Step3 : 切换到目标系统
以下将分别对这三步进行具体介绍,并在最后介绍一点小技巧。

Step 1: 下载目标系统的基础文件系统

按照官方文档中的说明,在下载基础文件系统时,需要利用release指定目标系统的发行版本,利用--arch指定架构,相应地会在/var/lib/schroot/chroots/路径下生成相应目录。

Step 2: 配置挂载目录

在不配置挂载目录的情况下,切换后的schroot运行环境将是全新的文件系统环境,无法感知并访问host主机中的文件。为了能够访问host主机中的文件,需要将host中的目录挂载到schroot环境中,具体操作如下:

$ sudo vim /etc/schroot/sbuild/fstab # 打开fstab配置文件
# 将特定目录挂载到schroot环境中
# 以普通用户的home目录为例,将以下条目写入fstab中并保存
/home/<username>                 /home/<username>  none  rw,bind  0  0

Step 3: 切换到目标系统

参考官方文档,此处省略。

小技巧

这个小技巧在官方文档中有所涉及,但讲述得不是很清楚,这里单独拿出来讲解。

需求

基础文件系统往往缺乏各种软件包。举例来说,连最基本的sudo都无法执行。在此情况下,用户需要执行以下操作:

$ su #切换到root用户
# apt-get install sudo #安装sudo软件包
$ exit #切换为普通用户
$ sudo apt-get install ... #继续安装其他软件包

花了很长时间终于将所需的软件包安装完毕了,完成部分工作后可能需要先退出当前的schroot环境(可能是因为下班了,甚至突然死机了( ´▽`))。
当利用mk-sbuild命令再次进入该schroot环境时,你会发现刚刚装的所有软件包都没有了,需要重新安装一遍。

方法

此处的小技巧正是用来解决上述问题的。通过在切换到目标文件时,指定source标签,即可将所安装的软件保存到基础文件系统中,具体的操作如下所示:

$ sudo schroot -c source:xenial-i386 #以xenial-i386为例
#以下安装软件包按照普通流程
...

后面每次进入该schroot环境,这些软件包都是安装好了的。


结束语

schroot常用于模拟运行环境,或进行交叉编译。如在64-bit的系统中编译生成32-bit的可执行应用程序,askubuntu中就涉及到相关的问答。

猜你喜欢

转载自blog.csdn.net/u014633283/article/details/73823484