def _make_vfat(self, path): # NOTE(mikal): This is a little horrible, but I couldn't find an # equivalent to genisoimage for vfat filesystems. vfat images are # always 64mb. with open(path, "w") as f: f.truncate(64 * 1024 * 1024) virtutils.mkfs("vfat", path, label="config-2") mounted = False try: mountdir = tempfile.mkdtemp(dir=FLAGS.config_drive_tempdir, prefix="cd_mnt_") _out, err = utils.trycmd("mount", "-o", "loop", path, mountdir, run_as_root=True) if err: raise exception.ConfigDriveMountFailed(operation="mount", error=err) mounted = True _out, err = utils.trycmd("chown", "%s.%s" % (os.getuid(), os.getgid()), mountdir, run_as_root=True) if err: raise exception.ConfigDriveMountFailed(operation="chown", error=err) # NOTE(mikal): I can't just use shutils.copytree here, because the # destination directory already exists. This is annoying. for ent in os.listdir(self.tempdir): shutil.copytree(os.path.join(self.tempdir, ent), os.path.join(mountdir, ent)) finally: if mounted: utils.execute("umount", mountdir, run_as_root=True) shutil.rmtree(mountdir)
def _make_vfat(self, path): # NOTE(mikal): This is a little horrible, but I couldn't find an # equivalent to genisoimage for vfat filesystems. vfat images are # always 64mb. with open(path, 'w') as f: f.truncate(64 * 1024 * 1024) virtutils.mkfs('vfat', path, label='config-2') mounted = False try: mountdir = tempfile.mkdtemp(dir=FLAGS.config_drive_tempdir, prefix='cd_mnt_') _out, err = utils.trycmd('mount', '-o', 'loop', path, mountdir, run_as_root=True) if err: raise exception.ConfigDriveMountFailed(operation='mount', error=err) mounted = True _out, err = utils.trycmd('chown', '%s.%s' % (os.getuid(), os.getgid()), mountdir, run_as_root=True) if err: raise exception.ConfigDriveMountFailed(operation='chown', error=err) # NOTE(mikal): I can't just use shutils.copytree here, because the # destination directory already exists. This is annoying. for ent in os.listdir(self.tempdir): shutil.copytree(os.path.join(self.tempdir, ent), os.path.join(mountdir, ent)) finally: if mounted: utils.execute('umount', mountdir, run_as_root=True) shutil.rmtree(mountdir)
def test_create_configdrive_vfat(self): imagefile = None try: self.mox.StubOutWithMock(virtutils, 'mkfs') self.mox.StubOutWithMock(utils, 'execute') self.mox.StubOutWithMock(utils, 'trycmd') virtutils.mkfs('vfat', mox.IgnoreArg(), label='config-2').AndReturn(None) utils.trycmd('mount', '-o', 'loop', mox.IgnoreArg(), mox.IgnoreArg(), run_as_root=True).AndReturn((None, None)) utils.trycmd('chown', mox.IgnoreArg(), mox.IgnoreArg(), run_as_root=True).AndReturn((None, None)) utils.execute('umount', mox.IgnoreArg(), run_as_root=True).AndReturn(None) self.mox.ReplayAll() c = configdrive.ConfigDriveBuilder() c._add_file('this/is/a/path/hello', 'This is some content') (fd, imagefile) = tempfile.mkstemp(prefix='cd_vfat_') os.close(fd) c._make_vfat(imagefile) c.cleanup() # Check cleanup self.assertFalse(os.path.exists(c.tempdir)) # NOTE(mikal): we can't check for a VFAT output here because the # filesystem creation stuff has been mocked out because it # requires root permissions finally: if imagefile: utils.delete_if_exists(imagefile)