Merge "Merging two mkfs commands."

This commit is contained in:
Jenkins 2013-10-23 07:42:13 +00:00 committed by Gerrit Code Review
commit 6768a5b67f
4 changed files with 34 additions and 23 deletions

View File

@ -1871,9 +1871,7 @@
# mkfs commands for ephemeral device. The format is
# <os_type>=<mkfs command> (multi valued)
#virt_mkfs=default=mkfs.ext3 -L %(fs_label)s -F %(target)s
#virt_mkfs=linux=mkfs.ext3 -L %(fs_label)s -F %(target)s
#virt_mkfs=windows=mkfs.ntfs --force --fast --label %(fs_label)s %(target)s
#virt_mkfs=
# Attempt to resize the filesystem by accessing the image over
# a block device. This is done by the host and may not be

View File

@ -572,9 +572,12 @@ class MkfsTestCase(test.NoDBTestCase):
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')
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')
@ -584,10 +587,12 @@ class MkfsTestCase(test.NoDBTestCase):
def test_mkfs_with_label(self):
self.mox.StubOutWithMock(utils, 'execute')
utils.execute('mkfs', '-t', 'ext4', '-F',
'-L', 'ext4-vol', '/my/block/dev')
'-L', 'ext4-vol', '/my/block/dev', run_as_root=False)
utils.execute('mkfs', '-t', 'msdos',
'-n', 'msdos-vol', '/my/msdos/block/dev')
utils.execute('mkswap', '-L', 'swap-vol', '/my/swap/block/dev')
'-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')

View File

@ -841,7 +841,7 @@ class UndoManager(object):
self._rollback()
def mkfs(fs, path, label=None):
def mkfs(fs, path, label=None, run_as_root=False):
"""Format a file or block device
:param fs: Filesystem type (examples include 'swap', 'ext3', 'ext4'
@ -854,7 +854,7 @@ def mkfs(fs, path, label=None):
else:
args = ['mkfs', '-t', fs]
#add -F to force no interactive execute on non-block device.
if fs in ('ext3', 'ext4'):
if fs in ('ext3', 'ext4', 'ntfs'):
args.extend(['-F'])
if label:
if fs in ('msdos', 'vfat'):
@ -863,7 +863,7 @@ def mkfs(fs, path, label=None):
label_opt = '-L'
args.extend([label_opt, label])
args.append(path)
execute(*args)
execute(*args, run_as_root=run_as_root)
def last_bytes(file_like_object, num):

View File

@ -63,14 +63,7 @@ disk_opts = [
# escape such commas.
#
cfg.MultiStrOpt('virt_mkfs',
default=[
'default=mkfs.ext3 -L %(fs_label)s -F %(target)s',
'linux=mkfs.ext3 -L %(fs_label)s -F %(target)s',
'windows=mkfs.ntfs'
' --force --fast --label %(fs_label)s %(target)s',
# NOTE(yamahata): vfat case
#'windows=mkfs.vfat -n %(fs_label)s %(target)s',
],
default=[],
help='mkfs commands for ephemeral device. '
'The format is <os_type>=<mkfs command>'),
@ -85,10 +78,12 @@ disk_opts = [
CONF = cfg.CONF
CONF.register_opts(disk_opts)
CONF.import_opt('default_ephemeral_format', 'nova.virt.driver')
_MKFS_COMMAND = {}
_DEFAULT_MKFS_COMMAND = None
_DEFAULT_FS_BY_OSTYPE = {'linux': 'ext3',
'windows': 'ntfs'}
for s in CONF.virt_mkfs:
# NOTE(yamahata): mkfs command may includes '=' for its options.
@ -100,11 +95,24 @@ for s in CONF.virt_mkfs:
_DEFAULT_MKFS_COMMAND = mkfs_command
def mkfs(os_type, fs_label, target):
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=True)
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)
def resize2fs(image, check_exit_code=False, run_as_root=False):