From dba1c3aea887a01e2f35c17529693943d1ff4221 Mon Sep 17 00:00:00 2001 From: Himadri Sekhar Basu <41947504+hsbasu@users.noreply.github.com> Date: Mon, 18 Sep 2023 10:38:17 +0530 Subject: [PATCH] Setup mounting and unmounting on rootfs (#6) - Mounts necessary devices like /dev, /proc, /sys etc on chroot directory - Unmounts devices from chroot directory --- src/LinuxIsoCreator/cli.py | 6 +++ src/LinuxIsoCreator/common.py | 77 ++++++++++++++++++++++++++++++++++- 2 files changed, 81 insertions(+), 2 deletions(-) diff --git a/src/LinuxIsoCreator/cli.py b/src/LinuxIsoCreator/cli.py index 5394714..77a7211 100644 --- a/src/LinuxIsoCreator/cli.py +++ b/src/LinuxIsoCreator/cli.py @@ -45,6 +45,12 @@ def start_LinISOtorCli(): if ans.lower() in 'yes': iso_creator.BootstrapRelease() + # mount directories + iso_creator.mount_dirs() + + # unmount directories + iso_creator.unmount_dirs() + ans = input(_("Delete log file %s? ") % LOGFILE) if ans.lower() in 'yes': os.system("rm -f %s" % LOGFILE) diff --git a/src/LinuxIsoCreator/common.py b/src/LinuxIsoCreator/common.py index a9927ed..7cdd28b 100644 --- a/src/LinuxIsoCreator/common.py +++ b/src/LinuxIsoCreator/common.py @@ -134,6 +134,13 @@ def capture_subprocess_output(self, cmd): with open(LOGFILE, 'a+') as logger: subprocess.run(cmd, stdout=logger, stderr=logger) + # def prompt_sudo(self): + # ret = 0 + # if os.geteuid() != 0: + # msg = "[sudo] password for %u:" + # ret = subprocess.check_call("sudo -v -p '%s'" % msg, shell=True) + # return ret + def set_iso_env(self): module_logger.debug(_("Project Name: %s") % self.project_name) module_logger.debug(_("Version: %s") % self.project_version) @@ -185,12 +192,78 @@ def BootstrapRelease(self): self.rootfsdir] try: self.capture_subprocess_output(cmd) - module_logger.debug("# ========================Bootstrapping complete======================== #\n") + module_logger.debug("# ========================Bootstrapping complete======================== #") except: module_logger.debug("Bootstrapping failed.") sys.exit(1) + + def mount_dirs(self): + """ + Mounts or binds directories or filesystems + to rootfs required for logging and installation + of anything using the package desktop-base or grub + """ + mountflag = "Mounting" + + module_logger.debug("\n# ========================Mounting directories======================== #") + # bind /dev to rootfs/dev + mountfs = '/dev' + mountedpath = str(self.rootfsdir + mountfs) + cmd = ['sudo', 'mount', '-o', 'bind', mountfs, mountedpath] + self.run_mount_dirs(cmd, mountflag, mountfs) + + # mount /dev/pts to rootfs/dev/pts + mountfs = 'devpts' + mountedpath = str(self.rootfsdir + '/dev/pts') + cmd = ['sudo', 'mount', 'none', '-t', mountfs, mountedpath] + self.run_mount_dirs(cmd, mountflag, mountfs) + + # mount /proc to rootfs/proc + mountfs = 'proc' + mountedpath = str(self.rootfsdir + '/proc') + cmd = ['sudo', 'mount', 'none', '-t', mountfs, mountedpath] + self.run_mount_dirs(cmd, mountflag, mountfs) + + # mount /sys to rootfs/sys + mountfs = 'sysfs' + mountedpath = str(self.rootfsdir + '/sys') + cmd = ['sudo', 'mount', 'none', '-t', mountfs, mountedpath] + self.run_mount_dirs(cmd, mountflag, mountfs) + module_logger.debug("# ========================Mounting successful======================== #") + + def unmount_dirs(self): + """ + Unmounts directories or filesystems from rootfs. + """ + mountflag = "Unmounting" + + module_logger.debug("\n# ========================Unounting directories======================== #") + # Unmount rootfs/dev/pts + mountedpath = str(self.rootfsdir + '/dev/pts') + cmd = ['sudo', 'umount', mountedpath] + self.run_mount_dirs(cmd, mountflag, mountedpath) + + # Unmount rootfs/dev + mountedpath = str(self.rootfsdir + '/dev') + cmd = ['sudo', 'umount', mountedpath] + self.run_mount_dirs(cmd, mountflag, mountedpath) + + # Unmount rootfs/proc + mountedpath = str(self.rootfsdir + '/proc') + cmd = ['sudo', 'umount', mountedpath] + self.run_mount_dirs(cmd, mountflag, mountedpath) + + # Unmount rootfs/sys + mountedpath = str(self.rootfsdir + '/sys') + cmd = ['sudo', 'umount', mountedpath] + self.run_mount_dirs(cmd, mountflag, mountedpath) + module_logger.debug("# ========================Unounting successful======================== #") + + def run_mount_dirs(self, cmd, mountflag, mountfs=None): + try: + self.capture_subprocess_output(cmd) except: - LOGFILE.write("Bootstrapping failed.") + module_logger.debug("%s %s failed." % (mountflag, mountfs)) sys.exit(1) def setuprootfs(self):