Beispiel #1
0
    def test_create_configdrive_vfat(self):
        CONF.set_override('config_drive_format', 'vfat')
        imagefile = None
        try:
            self.mox.StubOutWithMock(utils, 'mkfs')
            self.mox.StubOutWithMock(utils, 'execute')
            self.mox.StubOutWithMock(utils, 'trycmd')

            utils.mkfs('vfat', mox.IgnoreArg(),
                       label='config-2').AndReturn(None)
            utils.trycmd('mount',
                         '-o',
                         mox.IgnoreArg(),
                         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()

            with configdrive.ConfigDriveBuilder(FakeInstanceMD()) as c:
                (fd, imagefile) = tempfile.mkstemp(prefix='cd_vfat_')
                os.close(fd)
                c.make_drive(imagefile)

            # 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:
                fileutils.delete_if_exists(imagefile)
Beispiel #2
0
    def test_create_configdrive_vfat(self):
        imagefile = None
        try:
            self.mox.StubOutWithMock(utils, 'mkfs')
            self.mox.StubOutWithMock(utils, 'execute')
            self.mox.StubOutWithMock(utils, 'trycmd')

            utils.mkfs('vfat', mox.IgnoreArg(),
                       label='config-2').AndReturn(None)
            utils.trycmd('mount', '-o', mox.IgnoreArg(), 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()

            with configdrive.ConfigDriveBuilder() as c:
                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)

            # 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)
    def _make_vfat(self, path, tmpdir):
        # NOTE(mikal): This is a little horrible, but I couldn't find an
        # equivalent to genisoimage for vfat filesystems.
        with open(path, 'wb') as f:
            f.truncate(CONFIGDRIVESIZE_BYTES)

        utils.mkfs('vfat', path, label='config-2')

        with utils.tempdir() as mountdir:
            mounted = False
            try:
                _, err = nova.privsep.fs.mount(
                    None, path, mountdir,
                    ['-o',
                     'loop,uid=%d,gid=%d' % (os.getuid(), os.getgid())])
                if err:
                    raise exception.ConfigDriveMountFailed(operation='mount',
                                                           error=err)
                mounted = True

                # NOTE(mikal): I can't just use shutils.copytree here,
                # because the destination directory already
                # exists. This is annoying.
                for ent in os.listdir(tmpdir):
                    shutil.copytree(os.path.join(tmpdir, ent),
                                    os.path.join(mountdir, ent))

            finally:
                if mounted:
                    nova.privsep.fs.umount(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.
        with open(path, 'wb') as f:
            f.truncate(CONFIGDRIVESIZE_BYTES)

        utils.mkfs('vfat', path, label='config-2')

        mounted = False
        try:
            mountdir = tempfile.mkdtemp(dir=CONF.config_drive_tempdir,
                                        prefix='cd_mnt_')
            _out, err = utils.trycmd('mount', '-o',
                                     'loop,uid=%d,gid=%d' % (os.getuid(),
                                                             os.getgid()),
                                     path, mountdir,
                                     run_as_root=True)
            if err:
                raise exception.ConfigDriveMountFailed(operation='mount',
                                                       error=err)
            mounted = True

            # 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)
Beispiel #5
0
    def _make_vfat(self, path, tmpdir):
        # NOTE(mikal): This is a little horrible, but I couldn't find an
        # equivalent to genisoimage for vfat filesystems.
        with open(path, "wb") as f:
            f.truncate(CONFIGDRIVESIZE_BYTES)

        utils.mkfs("vfat", path, label="config-2")

        with utils.tempdir() as mountdir:
            mounted = False
            try:
                _, err = utils.trycmd(
                    "mount", "-o", "loop,uid=%d,gid=%d" % (os.getuid(), os.getgid()), path, mountdir, run_as_root=True
                )
                if err:
                    raise exception.ConfigDriveMountFailed(operation="mount", error=err)
                mounted = True

                # NOTE(mikal): I can't just use shutils.copytree here,
                # because the destination directory already
                # exists. This is annoying.
                for ent in os.listdir(tmpdir):
                    shutil.copytree(os.path.join(tmpdir, ent), os.path.join(mountdir, ent))

            finally:
                if mounted:
                    utils.execute("umount", mountdir, run_as_root=True)
Beispiel #6
0
    def test_create_configdrive_vfat(self):
        CONF.set_override('config_drive_format', 'vfat')
        imagefile = None
        try:
            self.mox.StubOutWithMock(utils, 'mkfs')
            self.mox.StubOutWithMock(utils, 'execute')
            self.mox.StubOutWithMock(utils, 'trycmd')

            utils.mkfs('vfat', mox.IgnoreArg(),
                       label='config-2').AndReturn(None)
            utils.trycmd('mount', '-o', mox.IgnoreArg(), 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()

            with configdrive.ConfigDriveBuilder(FakeInstanceMD()) as c:
                (fd, imagefile) = tempfile.mkstemp(prefix='cd_vfat_')
                os.close(fd)
                c.make_drive(imagefile)

            # 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:
                fileutils.delete_if_exists(imagefile)
Beispiel #7
0
    def _make_vfat(self, path):
        # NOTE(mikal): This is a little horrible, but I couldn't find an
        # equivalent to genisoimage for vfat filesystems.
        with open(path, 'w') as f:
            f.truncate(CONFIGDRIVESIZE_BYTES)

        utils.mkfs('vfat', path, label='config-2')

        mounted = False
        try:
            mountdir = tempfile.mkdtemp(dir=CONF.config_drive_tempdir,
                                        prefix='cd_mnt_')
            _out, err = utils.trycmd('mount',
                                     '-o',
                                     'loop,uid=%d,gid=%d' %
                                     (os.getuid(), os.getgid()),
                                     path,
                                     mountdir,
                                     run_as_root=True)
            if err:
                raise exception.ConfigDriveMountFailed(operation='mount',
                                                       error=err)
            mounted = True

            # 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)
Beispiel #8
0
    def test_mkfs(self):
        self.mox.StubOutWithMock(utils, "execute")
        utils.execute("mkfs", "-t", "ext4", "-F", "/my/block/dev")
        utils.execute("mkswap", "/my/swap/block/dev")
        self.mox.ReplayAll()

        utils.mkfs("ext4", "/my/block/dev")
        utils.mkfs("swap", "/my/swap/block/dev")
Beispiel #9
0
    def test_mkfs(self):
        self.mox.StubOutWithMock(utils, 'execute')
        utils.execute('mkfs', '-t', 'ext4', '-F', '/my/block/dev')
        utils.execute('mkswap', '/my/swap/block/dev')
        self.mox.ReplayAll()

        utils.mkfs('ext4', '/my/block/dev')
        utils.mkfs('swap', '/my/swap/block/dev')
Beispiel #10
0
def mkfs(os_type, fs_label, target, run_as_root=True):
    """Format a file or block device using
       a user provided command for each os type.
       If user has not provided any configuration,
       format type will be used according to a
       default_ephemeral_format configuration
       or a system defaults.
    """

    mkfs_command = (_MKFS_COMMAND.get(os_type, _DEFAULT_MKFS_COMMAND) or "") % {"fs_label": fs_label, "target": target}
    if mkfs_command:
        utils.execute(*mkfs_command.split(), run_as_root=run_as_root)
    else:
        default_fs = CONF.default_ephemeral_format
        if not default_fs:
            default_fs = _DEFAULT_FS_BY_OSTYPE.get(os_type, "ext3")
        utils.mkfs(default_fs, target, fs_label, run_as_root=run_as_root)
Beispiel #11
0
def mkfs(os_type, fs_label, target, run_as_root=True):
    """Format a file or block device using
       a user provided command for each os type.
       If user has not provided any configuration,
       format type will be used according to a
       default_ephemeral_format configuration
       or a system defaults.
    """

    mkfs_command = (_MKFS_COMMAND.get(os_type, _DEFAULT_MKFS_COMMAND) or
                    '') % {'fs_label': fs_label, 'target': target}
    if mkfs_command:
        utils.execute(*mkfs_command.split(), run_as_root=run_as_root)
    else:
        default_fs = CONF.default_ephemeral_format
        if not default_fs:
            default_fs = _DEFAULT_FS_BY_OSTYPE.get(os_type, 'ext3')
        utils.mkfs(default_fs, target, fs_label, run_as_root=run_as_root)
Beispiel #12
0
def mkfs(os_type, fs_label, target, run_as_root=True, specified_fs=None):
    """Format a file or block device using
       a user provided command for each os type.
       If user has not provided any configuration,
       format type will be used according to a
       default_ephemeral_format configuration
       or a system defaults.
    """

    mkfs_command = (_MKFS_COMMAND.get(os_type, _DEFAULT_MKFS_COMMAND) or
                    '') % {'fs_label': fs_label, 'target': target}
    if mkfs_command:
        utils.execute(*mkfs_command.split(), run_as_root=run_as_root)
    else:
        if not specified_fs:
            specified_fs = CONF.default_ephemeral_format
            if not specified_fs:
                specified_fs = _DEFAULT_FS_BY_OSTYPE.get(os_type,
                                                         _DEFAULT_FILE_SYSTEM)

        utils.mkfs(specified_fs, target, fs_label, run_as_root=run_as_root)
Beispiel #13
0
    def test_create_configdrive_vfat(self):
        imagefile = None
        try:
            self.mox.StubOutWithMock(utils, 'mkfs')
            self.mox.StubOutWithMock(utils, 'execute')
            self.mox.StubOutWithMock(utils, 'trycmd')

            utils.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()

            with configdrive.config_drive_helper() as c:
                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)

            # 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)
Beispiel #14
0
    def test_mkfs_with_label(self):
        self.mox.StubOutWithMock(utils, 'execute')
        utils.execute('mkfs',
                      '-t',
                      'ext4',
                      '-F',
                      '-L',
                      'ext4-vol',
                      '/my/block/dev',
                      run_as_root=False)
        utils.execute('mkfs',
                      '-t',
                      'msdos',
                      '-n',
                      'msdos-vol',
                      '/my/msdos/block/dev',
                      run_as_root=False)
        utils.execute('mkswap',
                      '-L',
                      'swap-vol',
                      '/my/swap/block/dev',
                      run_as_root=False)
        self.mox.ReplayAll()

        utils.mkfs('ext4', '/my/block/dev', 'ext4-vol')
        utils.mkfs('msdos', '/my/msdos/block/dev', 'msdos-vol')
        utils.mkfs('swap', '/my/swap/block/dev', 'swap-vol')
Beispiel #15
0
    def test_mkfs(self):
        self.mox.StubOutWithMock(utils, 'execute')
        utils.execute('mkfs', '-t', 'ext4', '-F', '/my/block/dev')
        utils.execute('mkfs', '-t', 'msdos', '/my/msdos/block/dev')
        utils.execute('mkswap', '/my/swap/block/dev')
        self.mox.ReplayAll()

        utils.mkfs('ext4', '/my/block/dev')
        utils.mkfs('msdos', '/my/msdos/block/dev')
        utils.mkfs('swap', '/my/swap/block/dev')
Beispiel #16
0
    def test_mkfs_with_label(self):
        self.mox.StubOutWithMock(utils, "execute")
        utils.execute("mkfs", "-t", "ext4", "-F", "-L", "ext4-vol", "/my/block/dev", run_as_root=False)
        utils.execute("mkfs", "-t", "msdos", "-n", "msdos-vol", "/my/msdos/block/dev", run_as_root=False)
        utils.execute("mkswap", "-L", "swap-vol", "/my/swap/block/dev", run_as_root=False)
        self.mox.ReplayAll()

        utils.mkfs("ext4", "/my/block/dev", "ext4-vol")
        utils.mkfs("msdos", "/my/msdos/block/dev", "msdos-vol")
        utils.mkfs("swap", "/my/swap/block/dev", "swap-vol")
Beispiel #17
0
    def test_mkfs_with_label(self):
        self.mox.StubOutWithMock(utils, 'execute')
        utils.execute('mkfs', '-t', 'ext4', '-F',
                      '-L', 'ext4-vol', '/my/block/dev')
        utils.execute('mkfs', '-t', 'msdos',
                      '-n', 'msdos-vol', '/my/msdos/block/dev')
        utils.execute('mkswap', '-L', 'swap-vol', '/my/swap/block/dev')
        self.mox.ReplayAll()

        utils.mkfs('ext4', '/my/block/dev', 'ext4-vol')
        utils.mkfs('msdos', '/my/msdos/block/dev', 'msdos-vol')
        utils.mkfs('swap', '/my/swap/block/dev', 'swap-vol')
Beispiel #18
0
    def test_mkfs(self):
        self.mox.StubOutWithMock(utils, 'execute')
        utils.execute('mkfs', '-t', 'ext4', '-F', '/my/block/dev',
                      run_as_root=False)
        utils.execute('mkfs', '-t', 'msdos', '/my/msdos/block/dev',
                      run_as_root=False)
        utils.execute('mkswap', '/my/swap/block/dev',
                      run_as_root=False)
        self.mox.ReplayAll()

        utils.mkfs('ext4', '/my/block/dev')
        utils.mkfs('msdos', '/my/msdos/block/dev')
        utils.mkfs('swap', '/my/swap/block/dev')
 def test_mkfs_ext4_withlabel(self, mock_execute):
     utils.mkfs("ext4", "/my/block/dev", "ext4-vol")
     mock_execute.assert_called_once_with(
         "mkfs", "-t", "ext4", "-F", "-L", "ext4-vol", "/my/block/dev", run_as_root=False
     )
 def test_mkfs_msdos_withlabel(self, mock_execute):
     utils.mkfs("msdos", "/my/msdos/block/dev", "msdos-vol")
     mock_execute.assert_called_once_with(
         "mkfs", "-t", "msdos", "-n", "msdos-vol", "/my/msdos/block/dev", run_as_root=False
     )
 def test_mkfs_swap_withlabel(self, mock_execute):
     utils.mkfs("swap", "/my/swap/block/dev", "swap-vol")
     mock_execute.assert_called_once_with("mkswap", "-L", "swap-vol", "/my/swap/block/dev", run_as_root=False)