[elbe-devel] [PATCH v1 3/8] Add save excursion in ChRootFilesystem context
dion at linutronix.de
dion at linutronix.de
Wed May 22 15:33:43 CEST 2019
From: Olivier Dion <dion at linutronix.de>
Instead of manually copying/moving/deleting files around,
the '__enter__' method of 'ChRootFilesystem' should save the
excursion into 'self.excursion'.
The excursion can be hardcoded directly at the top of the
'__enter__' method. Depending on the filesystem object,
other files can be add to the excursion as well dynamically.
To put a file into the excursion, 2 variables must be
provide and 1 optional.
* 'path' - file on the *host* to copy to the RFS.
* 'restore' - if to restore 'path' in the RFS on '__exit__'
* 'dst' (optional) - Copy 'path' to 'dst'
If 'dst' is not provided, then 'path' is copy to the same
place in the RFS.
Uppon exiting the 'ChrootFilesystem' context with '__exit__'
the excursion is restored according to the policies in it.
Finally, the excursion is deleted so it's reset in the next
context.
Examples
========
````````````````````````````````````````````````````````````
self.excursion.append({
"path":"/usr/bin",
"restore":True
})
````````````````````````````````````````````````````````````
On '__enter__'
* Move "/rfs/usr/bin" to "/rfs/usr/bin.orig", even if it's a
broken link
* Copy "/usr/bin" to "/rfs/usr/bin"
On '__exit__'
* Remove "/rfs/usr/bin"
* Move "/rfs/usr/bin.orig" to "/rfs/usr/bin"
````````````````````````````````````````````````````````````
ui = "/usr/share/elbe/qemu-elbe/elbe"
self.excursion.append({
"path":ui,
"to":"/usr/bin",
"restore":False
})
````````````````````````````````````````````````````````````
On '__enter__'
* Move "/usr/share/elbe/qemu-elbe/elbe" to "/rfs/usr/bin/elbe"
On '__exit__'
* Remove "/rfs/usr/bin/elbe"
Signed-off-by: Olivier Dion <dion at linutronix.de>
---
elbepack/efilesystem.py | 73 ++++++++++++++++++++++++++-----------------------
1 file changed, 39 insertions(+), 34 deletions(-)
diff --git a/elbepack/efilesystem.py b/elbepack/efilesystem.py
index 1f6428e0..3422b6fc 100644
--- a/elbepack/efilesystem.py
+++ b/elbepack/efilesystem.py
@@ -179,32 +179,44 @@ class ChRootFilesystem(ElbeFilesystem):
os.close(self.cwd)
def __enter__(self):
+ self.excursion = [
+ {"path":"/etc/resolv.conf", "restore":True},
+ {"path":"/etc/apt/apt.conf", "restore":True},
+ {"path":"/usr/sbin/policy-rc.d", "restore":False}
+ ]
+
if self.interpreter:
if not self.exists("usr/bin"):
+ if self.islink("usr/bin"):
+ self.excursion.append({
+ "path":"/usr/bin",
+ "restore":True
+ })
self.mkdir("usr/bin")
ui = "/usr/share/elbe/qemu-elbe/" + self.interpreter
if not os.path.exists(ui):
ui = "/usr/bin/" + self.interpreter
- os.system('cp %s %s' % (ui, self.fname("usr/bin")))
-
- if self.exists("/etc/resolv.conf"):
- os.system('mv %s %s' % (self.fname("etc/resolv.conf"),
- self.fname("etc/resolv.conf.orig")))
- os.system('cp %s %s' % ("/etc/resolv.conf",
- self.fname("etc/resolv.conf")))
- if self.exists("/etc/apt/apt.conf"):
- os.system('cp %s %s' % (self.fname("/etc/apt/apt.conf"),
- self.fname("/etc/apt/apt.conf.orig")))
- if os.path.exists("/etc/apt/apt.conf"):
- os.system('cp %s %s' % ("/etc/apt/apt.conf",
- self.fname("/etc/apt/")))
+ self.excursion.append({
+ "path":ui,
+ "dst": "/usr/bin",
+ "restore":False
+ })
+
+ for tmp in self.excursion:
+ origin = tmp["path"]
+ if self.lexists(origin) and tmp["restore"] is True:
+ save_to = "%s.orig" % origin
+ os.system('mv %s %s' % (self.fname(origin), self.fname(save_to)))
+ if os.path.exists(origin):
+ dst = origin
+ if "dst" in tmp:
+ dst = tmp["dst"]
+ os.system('cp %s %s' % (origin, self.fname(dst)))
self.mkdir_p("usr/sbin")
- self.write_file("usr/sbin/policy-rc.d",
- 0o755, "#!/bin/sh\nexit 101\n")
-
+ self.write_file("usr/sbin/policy-rc.d", 0o755, "#!/bin/sh\nexit 101\n")
self.mount()
return self
@@ -212,25 +224,18 @@ class ChRootFilesystem(ElbeFilesystem):
if self.inchroot:
self.leave_chroot()
self.umount()
- if self.interpreter:
- os.system('rm -f %s' %
- os.path.join(self.path, "usr/bin/" + self.interpreter))
-
- os.system('rm -f %s' % (self.fname("etc/resolv.conf")))
-
- if self.exists("/etc/resolv.conf.orig"):
- os.system('mv %s %s' % (self.fname("etc/resolv.conf.orig"),
- self.fname("etc/resolv.conf")))
-
- if self.exists("/etc/apt/apt.conf"):
- os.system('rm -f %s' % (self.fname("etc/apt/apt.conf")))
-
- if self.exists("/etc/apt/apt.conf.orig"):
- os.system('mv %s %s' % (self.fname("etc/apt/apt.conf.orig"),
- self.fname("etc/apt/apt.conf")))
- if self.exists("/usr/sbin/policy-rc.d"):
- os.system('rm -f %s' % (self.fname("usr/sbin/policy-rc.d")))
+ for tmp in self.excursion:
+ origin = tmp["path"]
+ saved_to = "%s.orig" % origin
+ if self.lexists(origin):
+ flags = "-f"
+ if self.isdir(origin):
+ flags += "r"
+ os.system('rm %s %s' % (flags, self.fname(origin)))
+ if tmp["restore"] is True and self.lexists(saved_to):
+ os.system('mv %s %s' % (self.fname(saved_to), self.fname(origin)))
+ del self.excursion
def mount(self):
if self.path == '/':
--
2.11.0
More information about the elbe-devel
mailing list