2011-02-02 60 views
0

我試圖將數據寫入chroot環境中的文件。由於我是非root用戶,所以他們只能使用schroot命令與chroot進行通信。將文件寫入chroot環境

目前我正在使用以下技巧來編寫數據。

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

但我敢肯定,這會給我很多痛苦,如果文本有一些特殊的字符,報價等於是什麼$發送文本的chroot的更好的方法。很可能我會通過python腳本使用上述命令。有一個更簡單的方法嗎?

回答

1

均田的hackish,但...

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 
+0

我想到了。但我是非特權用戶。我無法寫入/ var/lib/schroot/sessions :(我從你的答案中遺漏了什麼? – Neo 2011-02-02 06:47:26

0

你可以使用Python $文本寫入到文件(Python有寫權),
然後將該文件複製到FILE.TXT

+0

如何將文件從父級環境複製到Chroot?我是非特權用戶,這是使用schroot的全部要點。 – Neo 2011-02-02 06:55:12