Skip to content

Commit

Permalink
Setup mounting and unmounting on rootfs (mamolinux#6)
Browse files Browse the repository at this point in the history
- Mounts necessary devices like /dev,
  /proc, /sys etc on chroot directory
- Unmounts devices from chroot directory
  • Loading branch information
hsbasu authored Sep 18, 2023
1 parent da0378c commit dba1c3a
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/LinuxIsoCreator/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
77 changes: 75 additions & 2 deletions src/LinuxIsoCreator/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit dba1c3a

Please sign in to comment.