如何退出perl脚本中的chroot?

时间:2011-09-30 16:26:38

标签: linux perl chroot

在编写用于完全自动化虚拟机设置的perl脚本(Xen pv)时,我遇到了一个很小的问题。

使用perl的chroot function我在来宾文件系统上做我的事情然后我需要回到我最初的真实根。我到底怎么做?

脚本示例:

`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);

但是没有。

更新 需要考虑的一些要点:

  • 我无法将脚本拆分为多个文件。 (愚蠢的理由,但实际上,我不能)
  • chrooted部分涉及使用脚本先前收集的大量数据(在chroot之前),强制不需要在chroot中运行另一个脚本。
  • 使用open,system或backticks不好,我需要运行命令并根据输出(不是退出代码,实际输出)做其他事情。
  • chroot之后的步骤取决于chroot内部的操作,因此我需要在内部,外部拥有我定义或更改的所有变量。
  • 叉子是可能的,但我不知道如何正确处理从孩子那里传递信息的好方法。

5 个答案:

答案 0 :(得分:9)

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...

仍然缺乏一些错误检查思路。

答案 1 :(得分:4)

您无法在进程上撤消chroot() - 这就是系统调用的全部内容。

您需要第二个流程(子流程)才能在chrooted环境中完成工作。叉子,让孩子经历chroot并做其事并离开,让父母进行清理。

答案 2 :(得分:2)

尝试生成执行chroot的子进程,例如根据您的需要使用systemfork,并等待孩子继续返回主程序。

答案 3 :(得分:2)

这看起来很有希望:

Breaking Out of a Chroot Jail Using PERL

答案 4 :(得分:0)

将原始根保存为当前工作目录或文件描述符:

  "error": {
    "code": "MailboxNotHostedInExchangeOnline",
    "message": "Mailbox is hosted by an on-premises or non-Exchange server, which is not supported.",
    "innerError": {
      "request-id": "REDACTED",
      "date": ""
    }
  }
}```

Is this a relatively new addition to the API? If so, does anyone have ideas on how to circumvent this?

OR

chdir "/";
chroot "/mnt";
# Do something
chroot ".";