Big update

This commit is contained in:
Fierelier 2023-07-05 04:30:47 +02:00
parent 904134f1ec
commit 11258f3b22
7 changed files with 126 additions and 51 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
chroots/

View File

@ -1,44 +0,0 @@
#!/usr/bin/env python3
import sys
import os
import subprocess
p = os.path.join
mounts = [
"proc",
"sys",
"dev",
p("dev","pts"),
"tmp",
"run"
]
def call(cmd,*args,**kwargs):
proc = subprocess.Popen(cmd,*args,**kwargs)
rtn = proc.wait()
if rtn != 0: raise subprocess.CalledProcessError(rtn,cmd)
return proc
def callRtn(cmd,*args,**kwargs):
proc = subprocess.Popen(cmd,*args,**kwargs)
return proc.wait()
def mountChroot(d):
for mount in mounts:
if callRtn(["mountpoint","-q",p(d,mount)]) == 0: continue
call(["mount","-o","x-gvfs-hide","--bind",os.path.sep + mount,p(d,mount)])
def umountChroot(d):
for mount in reversed(mounts):
if callRtn(["mountpoint","-q",p(d,mount)]) != 0: continue
call(["umount","-lR",p(d,mount)])
def chroot(d,cmd):
mountChroot(d)
call(["chroot",d] + cmd)
if sys.argv[1] == "-u":
umountChroot(sys.argv[2])
sys.exit(0)
chroot(sys.argv[1],sys.argv[2:])

89
mfchroot Executable file
View File

@ -0,0 +1,89 @@
#!/usr/bin/env python3
import sys
import os
import subprocess
p = os.path.join
pUp = os.path.dirname
s = False
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
s = os.path.realpath(sys.executable)
else:
s = os.path.realpath(__file__)
sd = pUp(s)
def runCode(st,name,glb = False):
if glb == False: glb = {}
code = compile(st,name,"exec")
exec(code,glb)
def runFile(path,glb = False):
return runCode(open(path,encoding="utf-8").read(),path,glb)
def getScriptFiles(paths):
sfiles = {}
for path in paths:
for root,dirs,files in os.walk(path):
for file in sorted(files):
ffile = p(root,file)
lfile = ffile.replace(root + os.path.sep,"",1)
if not lfile in sfiles: sfiles[lfile] = ffile
break
return sfiles
def runStage(d,stage,env = False):
if env == False: env = {}
env["dir"] = d
env["stage"] = stage
glb = {}
glb["_g"] = globals()
glb["_chroot"] = env
sdirs = []
sdir = p(d,"opt","mfchroot","scripts",stage)
if os.path.isdir(sdir): sdirs.append(sdir)
sdir = p(sd,"scripts",stage)
if os.path.isdir(sdir): sdirs.append(sdir)
sfiles = getScriptFiles(sdirs)
for sfile in sorted(sfiles.keys()):
runFile(sfiles[sfile],glb)
def call(cmd,*args,**kwargs):
proc = subprocess.Popen(cmd,*args,**kwargs)
rtn = proc.wait()
if rtn != 0: raise subprocess.CalledProcessError(rtn,cmd)
return proc
def callRtn(cmd,*args,**kwargs):
proc = subprocess.Popen(cmd,*args,**kwargs)
return proc.wait()
def mountBind(start,end):
if callRtn(["mountpoint","-q",end]) == 0: return
if not os.path.isdir(end): os.makedirs(end)
call(["mount","-o","x-gvfs-hide","--bind",start,end])
def umount(end):
if callRtn(["mountpoint","-q",end]) != 0: return
call(["umount","-lR",end])
def main():
if sys.argv[1] == "-u":
runStage(sys.argv[2],"all")
runStage(sys.argv[2],"umount")
return
if sys.argv[1] == "-m":
runStage(sys.argv[2],"all")
runStage(sys.argv[2],"mount")
return
runStage(sys.argv[1],"mount")
env = {}
env["args"] = sys.argv[2:]
runStage(sys.argv[1],"chroot",env)
return
main()

View File

@ -1,15 +1,16 @@
Syntax: ./chroot.py <flags> <dir> [command] [arg 1] [arg 2] ...
Syntax: ./mfchroot [flags] <dir> [command] [arg 1] [arg 2] ...
Chroots into a directory, and keeps it mounted.
Chroots into a directory, and optionally sets up additional things.
Flags:
* -m: Mount the chroot, but don't actually chroot into it.
* -u: Unmount the chroot, useful for copying, moving, deleting the chroot.
---
Scripts are sourced from '$scriptdir/scripts/*', and then '$chroot/opt/me.fier.chroot/*'
Scripts are sourced from '$scriptdir/scripts/*' and '$chroot/opt/mfchroot/scripts/*'. If the scripts have the same name in the second folder, it overrides scripts in the first.
Scripts (Draft, TODO!):
* premount/*.py: Ran before directories are mounted/unmounted.
* mount/*.py: Ran after mounted.
* umount/*.py: Ran after unmounting.
Scripts:
* mount/: Mount the chroot.
* chroot/: Chroot.
* umount/: Unmount the chroot.

View File

@ -0,0 +1,8 @@
import os
import shutil
chrootexe = shutil.which("chroot")
os.execv(chrootexe,[
chrootexe,
_chroot["dir"]
] + _chroot["args"]
)

10
scripts/mount/50_main.py Normal file
View File

@ -0,0 +1,10 @@
import os
mounts = [
"proc",
"sys",
"dev",
os.path.join("dev","pts")
]
for mount in mounts:
_g["mountBind"](os.path.sep + mount,os.path.join(_chroot["dir"],mount))

10
scripts/umount/50_main.py Normal file
View File

@ -0,0 +1,10 @@
import os
mounts = [
"proc",
"sys",
"dev",
os.path.join("dev","pts")
]
for mount in reversed(mounts):
_g["umount"](os.path.join(_chroot["dir"],mount))