Exemplo n.º 1
0
 def __exit__(self, exc_type, exc_value, tracebk):
     if self.temp_dir:
         umount(self.mount_dir)
         shutil.rmtree(self.temp_dir)
         self.mount_dir = None
         self.temp_dir = None
     execWithRedirect("kpartx", ["-d", "-s", self.disk_img])
Exemplo n.º 2
0
    def execWithRedirect_test(self):
        import logging
        logger = logging.getLogger("pymkefiboot")
        logger.addHandler(logging.NullHandler())
        program_log = logging.getLogger("program")
        program_log.setLevel(logging.INFO)

        tmp_f = tempfile.NamedTemporaryFile(delete=False)
        fh = logging.FileHandler(filename=tmp_f.name, mode="w")
        program_log.addHandler(fh)

        try:
            cmd = [
                "python3", "-c",
                "import sys; print('The Once-ler was here.'); sys.exit(1)"
            ]
            rc = execWithRedirect(cmd[0], cmd[1:])
            self.assertEqual(rc, 1)

            tmp_f.file.close()
            logged_text = open(tmp_f.name, "r").readlines()[-1].strip()
            self.assertEqual(logged_text, "The Once-ler was here.")
        finally:
            os.unlink(tmp_f.name)
            program_log.removeHandler(fh)
Exemplo n.º 3
0
def mkfsimage(fstype,
              rootdir,
              outfile,
              size=None,
              mkfsargs=None,
              mountargs="",
              graft=None):
    '''Generic filesystem image creation function.
    fstype should be a filesystem type - "mkfs.${fstype}" must exist.
    graft should be a dict: {"some/path/in/image": "local/file/or/dir"};
    if the path ends with a '/' it's assumed to be a directory.
    Will raise CalledProcessError if something goes wrong.'''
    mkfsargs = mkfsargs or []
    graft = graft or {}
    preserve = (fstype not in ("msdos", "vfat"))
    if not size:
        size = estimate_size(rootdir, graft, fstype)
    with LoopDev(outfile, size) as loopdev:
        try:
            runcmd(["mkfs.%s" % fstype] + mkfsargs + [loopdev])
        except CalledProcessError as e:
            logger.error("mkfs exited with a non-zero return code: %d",
                         e.returncode)
            logger.error(e.output)
            sys.exit(e.returncode)

        with Mount(loopdev, mountargs) as mnt:
            if rootdir:
                copytree(rootdir, mnt, preserve)
            do_grafts(graft, mnt, preserve)

            # Save information about filesystem usage
            execWithRedirect("df", [mnt])

    # Make absolutely sure that the data has been written
    runcmd(["sync"])
Exemplo n.º 4
0
def dm_detach(dev):
    '''Detach the named devicemapper device. Returns False if dmsetup fails.'''
    dev = dev.replace("/dev/mapper/", "")  # strip prefix, if it's there
    return execWithRedirect("dmsetup", ["remove", dev])
Exemplo n.º 5
0
def loop_detach(loopdev):
    '''Detach the given loop device. Return False on failure.'''
    return (execWithRedirect("losetup", ["--detach", loopdev]) == 0)
Exemplo n.º 6
0
def mksquashfs(rootdir, outfile, compression="default", compressargs=None):
    '''Make a squashfs image containing the given rootdir.'''
    compressargs = compressargs or []
    if compression != "default":
        compressargs = ["-comp", compression] + compressargs
    return execWithRedirect("mksquashfs", [rootdir, outfile] + compressargs)