Commit c967b232 authored by Nikos Skalkotos's avatar Nikos Skalkotos

Don't losetup the input media if not snapshotting

There is no need to associate a loop device with the input media if we
are not snapshotting the input media. The loop device is only needed in
order to use the media with the device-mapper.
parent 08d71238
......@@ -63,7 +63,7 @@ def create_image(d, media, out, tmp):
try:
# There is no need to snapshot the media if it was created by the Disk
# instance as a temporary object.
device = disk.device if disk.source == '/' else disk.snapshot()
device = disk.file if disk.source == '/' else disk.snapshot()
image = disk.get_image(device)
......
......@@ -72,7 +72,7 @@ class Disk(object):
"""
self._cleanup_jobs = []
self._images = []
self._device = None
self._file = None
self.source = source
self.out = output
self.meta = {}
......@@ -98,15 +98,16 @@ class Disk(object):
"""Create a disk out of a directory"""
if self.source == '/':
bundle = BundleVolume(self.out, self.meta)
image = '%s/%s.diskdump' % (self.tmp, uuid.uuid4().hex)
image = '%s/%s.raw' % (self.tmp, uuid.uuid4().hex)
def check_unlink(path):
"""Unlinks file if exists"""
if os.path.exists(path):
os.unlink(path)
self._add_cleanup(check_unlink, image)
bundle.create_image(image)
return self._losetup(image)
return image
raise FatalError("Using a directory as media source is supported")
def cleanup(self):
......@@ -125,34 +126,36 @@ class Disk(object):
job(*args)
@property
def device(self):
"""Convert the source media into a block device"""
def file(self):
"""Convert the source media into a file"""
if self._device is not None:
return self._device
if self._file is not None:
return self._file
self.out.output("Examining source media `%s' ..." % self.source, False)
mode = os.stat(self.source).st_mode
if stat.S_ISDIR(mode):
self.out.success('looks like a directory')
self._device = self._dir_to_disk()
self._file = self._dir_to_disk()
elif stat.S_ISREG(mode):
self.out.success('looks like an image file')
self._device = self._losetup(self.source)
self._file = self.source
elif not stat.S_ISBLK(mode):
raise FatalError("Invalid media source. Only block devices, "
"regular files and directories are supported.")
else:
self.out.success('looks like a block device')
self._device = self.source
self._file = self.source
return self._device
return self._file
def snapshot(self):
"""Creates a snapshot of the original source media of the Disk
instance.
"""
size = blockdev('--getsz', self.device)
mode = os.stat(self.file).st_mode
device = self.file if stat.S_ISBLK(mode) else self._losetup(self.file)
size = blockdev('--getsz', device)
self.out.output("Snapshotting media source ...", False)
cowfd, cow = tempfile.mkstemp(dir=self.tmp)
os.close(cowfd)
......@@ -167,7 +170,7 @@ class Disk(object):
try:
os.write(tablefd,
"0 %d snapshot %s %s n 8" %
(int(size), self.device, cowdev))
(int(size), device, cowdev))
finally:
os.close(tablefd)
......
......@@ -263,7 +263,7 @@ def image_creator():
try:
# There is no need to snapshot the media if it was created by the Disk
# instance as a temporary object.
device = disk.device if disk.source == '/' else disk.snapshot()
device = disk.file if disk.source == '/' else disk.snapshot()
image = disk.get_image(device, sysprep_params=options.sysprep_params)
if image.is_unsupported() and not options.allow_unsupported:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment