如何在 Perl 脚本中退出 chroot?
在编写旨在完全自动化虚拟机 (Xen pv) 设置的 Perl 脚本时,我遇到了一个可能非常简单的小问题。
使用perl的chroot函数我在来宾文件系统上做我的事情,然后我需要得到回到我最初的真正根源。我到底是怎么做到的?
脚本示例:
`mount $disk_image $mount_point`;
chdir($mount_point);
chroot($mount_point);
#[Do my things...]
#<Exit chroot wanted here>
`umount $mount_point`;
#[Post install things...]
我尝试过退出;但显然会退出整个脚本。
在寻找退出 chroot 的方法时,我发现了许多旨在退出已设置 chroot(权限升级)的脚本。因为我在这里进行了 chroot,所以这些方法不适用。
尝试过一些疯狂的事情,比如:
opendir REAL_ROOT, "/";
chdir($mount_point);
chroot($mount_point);
chdir(*REAL_ROOT);
但不行。
更新 需要考虑的一些要点:
- 我无法将脚本拆分为多个文件。 (愚蠢的原因,但实际上,我不能)
- chroot 部分涉及使用脚本之前(在 chroot 之前)收集的大量数据,强制需要不在 chroot 内运行另一个脚本。
- 使用 open、system 或反引号不好,我需要运行命令并根据输出(不是退出代码,实际输出)做其他事情。
- chroot 之后的步骤取决于 chroot 内部所做的事情,因此我需要拥有在内部、外部定义或更改的所有变量。
- 分叉是可能的,但我不知道正确处理与子进程之间的信息传递的好方法。
While writing a perl script intended to fully automate the setup of virtual machines (Xen pv) I hit a small maybe very simple problem.
Using perl's chroot function I do my things on the guest file system and then I need to get back to my initial real root. How the hell I do that?
Script example:
`mount $disk_image $mount_point`;
chdir($mount_point);
chroot($mount_point);
#[Do my things...]
#<Exit chroot wanted here>
`umount $mount_point`;
#[Post install things...]
I've tried exit; but obviously that exit the whole script.
Searching for a way to exit the chroot I've found a number of scripts who aim to exit an already setup chroot (privilege escalation). Since I do the chroot here theses methods do not aplies.
Tried some crazy things like:
opendir REAL_ROOT, "/";
chdir($mount_point);
chroot($mount_point);
chdir(*REAL_ROOT);
But no go.
UPDATE
Some points to consider:
- I can't split the script in multiple files. (Silly reasons, but really, I can't)
- The chrooted part involve using a lot of data gathered earlier by the script (before the chroot), enforcing the need of not lunching another script inside the chroot.
- Using open, system or backticks is not good, I need to run commands and based on the output (not the exit code, the actual output) do other things.
- Steps after the chroot depends on what was done inside the chroot, hence I need to have all the variables I defined or changed while inside, outside.
- Fork is possible, but I don't know a good way to handle correctly the passing of informations from and to the child.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
chrooted process() 无法通过退出来“unchroot”自身(它只会退出)。
您必须生成一个子进程,它将 chroot。
类似以下内容应该可以解决问题:
它仍然缺乏一些错误检查思想。
The chrooted process() cannot "unchroot" itself by exiting (which would just exit).
You have to spawn a children process, which will chroot.
Something along the lines of the following should do the trick:
It stills lacks of some error checking thought.
您无法撤消进程上的
chroot()
- 这就是系统调用的全部要点。您需要第二个进程(子进程)来在 chroot 环境中完成工作。 Fork,让子进程经历 chroot 并完成其工作并退出,让父进程进行清理工作。
You can't undo a
chroot()
on a process - that's the whole point of the system call.You need a second process (a child process) to do the work in the chrooted environment. Fork, and have the child undergo the
chroot
and do its stuff and exit, leaving the parent to do the cleanup.尝试生成一个执行
chroot
的子进程,例如根据您的需要使用system
或fork
,并等待子进程返回main计划继续。Try spawning a child process that does the
chroot
, e.g. withsystem
orfork
depending on your needs, and waiting for the child to return the main program continues.这看起来很有希望:
使用 PERL 突破 Chroot Jail
This looks like it might be promising:
Breaking Out of a Chroot Jail Using PERL
将原始根目录保存为当前工作目录或文件描述符:
或
Save the original root as the current working directory or as a file descriptor:
OR