【问题标题】:Writing files into chroot environment将文件写入 chroot 环境
【发布时间】:2011-02-02 06:27:47
【问题描述】:

我正在尝试将数据写入 chroot 环境中的文件。由于我是非 root 用户,因此我与 chroot 通信的唯一方法是使用 schroot 命令。

目前我正在使用以下技巧来写入数据。

$ schroot -c chroot_session -r -d /tmp -- bash -c "echo \"$text\" > file.txt"

但是我敢肯定,如果文本中有一些特殊字符、引号等,这会给我带来很多痛苦。那么有什么更好的方法将 $text 发送到 chroot。很可能我将通过 python 脚本使用上述命令。有没有更简单的方法?

【问题讨论】:

    标签: python linux shell chroot


    【解决方案1】:

    有点骇人听闻,但是……

    c = ConfigParser.RawConfigParser()
    c.readfp(open(os.path.join('/var/lib/schroot/session', chroot_session), 'r'))
    chroot_basedir = c.get(chroot_session, 'mount-location')
    with open(os.path.join(chroot_basedir, '/tmp/file.txt'), 'w') as fp:
        fp.write(text)
    

    好的,所以除了schroot之外,特权不会让你通过任何方式进入吧?

    p = subprocess.Popen(['schroot', '-c', name, '-r', 'tee', '/tmp/file.txt'],
                         stdin=subprocess.PIPE,
                         stdout=open('/dev/null', 'w'),
                         stderr=sys.stderr)
    p.stdin.write(text)
    p.stdin.close()
    rc = p.wait()
    assert rc == 0
    

    【讨论】:

    • 我想到了。但我是非特权用户。我无法写信给 /var/lib/schroot/sessions :(。我是否从您的回答中遗漏了什么?
    • 超级答案。这正是我一直在寻找的,某种管道技术。非常感谢。
    【解决方案2】:

    可以使用python将$text写入文件(python有写入权限),
    然后将该文件复制到 file.txt

    【讨论】:

    • 如何将文件从父环境复制到 Chroot?我是非特权用户,这就是使用 schroot 的重点。
    猜你喜欢
    • 2012-07-10
    • 1970-01-01
    • 2012-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多