在编写用于完全自动化虚拟机设置的perl脚本(Xen pv)时,我遇到了一个很小的问题.
使用perl的chroot function我在来宾文件系统上做我的事情然后我需要回到我最初的真正根.我到底怎么回事?
脚本示例:
`mount $disk_image $mount_point`;
chdir($mount_point);
chroot($mount_point);
#[Do my things...]
#
`umount $mount_point`;
#[Post install things...]
我试过退出;但显然退出整个脚本.
正在寻找一种退出chroot的方法我发现了许多旨在退出已经设置好的chroot的脚本(权限提升).由于我在这里做chroot这些方法不适用.
试过一些疯狂的事情:
opendir REAL_ROOT, "/";
chdir($mount_point);
chroot($mount_point);
chdir(*REAL_ROOT);
但是没有去.
UPDATE
有些要考虑的要点:
>我无法在多个文件中拆分脚本. (愚蠢的原因,但真的,我不能)
> chrooted部分涉及使用脚本先前收集的大量数据(在chroot之前),强制不需要在chroot中运行另一个脚本.
>使用open,system或backticks并不好,我需要运行命令并根据输出(不是退出代码,实际输出)做其他事情.
> chroot之后的步骤取决于chroot内部的操作,因此我需要在内部,外部具有我定义或更改的所有变量.
>叉子是可能的,但我不知道正确处理来自孩子的信息传递的好方法.
解决方法:
chrooted process()不能通过退出(退出)退出“unchroot”.
你必须生成一个子进程,它将chroot.
下面的内容应该可以解决问题:
if (fork())
{
# parent
wait;
}
else
{
# children
chroot("/path/to/somewhere/");
# do some Perl stuff inside the chroot...
exit;
}
# The parent can continue it's stuff after his chrooted children did some others stuff...
它仍然缺乏一些错误检查思想.
标签:linux,perl,chroot
来源: https://codeday.me/bug/20190713/1444662.html