forked from Mirror/pmbootstrap
Hello, there!
This commit is contained in:
parent
bfde354b22
commit
ae950fb9f7
64 changed files with 3923 additions and 0 deletions
72
pmb/chroot/root.py
Normal file
72
pmb/chroot/root.py
Normal file
|
@ -0,0 +1,72 @@
|
|||
"""
|
||||
Copyright 2017 Oliver Smith
|
||||
|
||||
This file is part of pmbootstrap.
|
||||
|
||||
pmbootstrap is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
pmbootstrap is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with pmbootstrap. If not, see <http://www.gnu.org/licenses/>.
|
||||
"""
|
||||
import os
|
||||
import shutil
|
||||
import shlex
|
||||
|
||||
import pmb.config
|
||||
import pmb.chroot
|
||||
import pmb.chroot.binfmt
|
||||
import pmb.helpers.run
|
||||
|
||||
|
||||
def root(args, cmd, suffix="native", working_dir="/", log=True,
|
||||
auto_init=True, return_stdout=False, check=True):
|
||||
"""
|
||||
Run a command inside a chroot as root.
|
||||
|
||||
:param log: When set to true, redirect all output to the logfile
|
||||
:param auto_init: Automatically initialize the chroot
|
||||
"""
|
||||
# Get and verify chroot folder
|
||||
chroot = args.work + "/chroot_" + suffix
|
||||
if not auto_init and not os.path.islink(chroot + "/bin/sh"):
|
||||
raise RuntimeError("Chroot does not exist: " + chroot)
|
||||
|
||||
pmb.chroot.init(args, suffix)
|
||||
|
||||
# Run the args with sudo chroot, and with cleaned environment
|
||||
# variables
|
||||
sh_bin = shutil.which("sh")
|
||||
chroot_bin = shutil.which("chroot")
|
||||
for i in range(len(cmd)):
|
||||
cmd[i] = shlex.quote(cmd[i])
|
||||
|
||||
cmd_inner_shell = ("cd " + shlex.quote(working_dir) + ";" +
|
||||
" ".join(cmd))
|
||||
cmd_full = ["sudo", sh_bin, "-c",
|
||||
"unset $(env | cut -d= -f1);" + # unset all
|
||||
" CHARSET=UTF-8" +
|
||||
" PATH=" + pmb.config.chroot_path +
|
||||
" SHELL=/bin/ash" +
|
||||
" HISTFILE=~/.ash_history" +
|
||||
" " + chroot_bin +
|
||||
" " + chroot +
|
||||
" sh -c " + shlex.quote(cmd_inner_shell)
|
||||
]
|
||||
|
||||
# Generate log message
|
||||
log_message = "(" + suffix + ") % "
|
||||
if working_dir != "/":
|
||||
log_message += "cd " + working_dir + " && "
|
||||
log_message += " ".join(cmd)
|
||||
|
||||
# Run the command
|
||||
return pmb.helpers.run.core(args, cmd_full, log_message, log,
|
||||
return_stdout, check)
|
Loading…
Add table
Add a link
Reference in a new issue