Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting size for non-existing LUKS devices #848

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions blivet/devices/luks.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from ..storage_log import log_method_call
from ..size import Size
from ..tasks import availability
from ..errors import DeviceError

import logging
log = logging.getLogger("blivet")
Expand Down Expand Up @@ -73,8 +74,7 @@ def slave(self):
return self.parents[0].parents[0]
return self.parents[0]

@property
def size(self):
def _get_size(self):
if not self.exists:
size = self.slave.size - crypto.LUKS_METADATA_SIZE
elif self.resizable and self.target_size != Size(0):
Expand All @@ -83,6 +83,17 @@ def size(self):
size = self.current_size
return size

def _set_size(self, newsize):
if not self.exists and not self.slave.exists:
self.slave.size = newsize + crypto.LUKS_METADATA_SIZE

# just run the StorageDevice._set_size to make sure we are in the format limits
super(LUKSDevice, self)._set_size(newsize - crypto.LUKS_METADATA_SIZE)
else:
raise DeviceError("Cannot set size for an existing LUKS device")

size = property(_get_size, _set_size)

@property
def _has_integrity(self):
return self.parents[0].type == "integrity/dm-crypt"
Expand Down
19 changes: 18 additions & 1 deletion tests/devices_test/size_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

import unittest

from blivet.devices import StorageDevice
from blivet.devicelibs import crypto
from blivet.devices import StorageDevice, LUKSDevice
from blivet import errors
from blivet.formats import get_format
from blivet.size import Size
Expand Down Expand Up @@ -100,3 +101,19 @@ def test_size_getter(self):
dev.target_size = Size(0)
self.assertEqual(dev.size, initial_size)
self.assertEqual(dev.size, dev.current_size)


class LUKSDeviceSizeTest(StorageDeviceSizeTest):

def _get_device(self, *args, **kwargs):
exists = kwargs.get("exists", False)
slave = StorageDevice(*args, size=kwargs["size"] + crypto.LUKS_METADATA_SIZE, exists=exists)
return LUKSDevice(*args, **kwargs, parents=[slave])

def test_size_getter(self):
initial_size = Size("10 GiB")
dev = self._get_device('sizetest', size=initial_size)

# for LUKS size depends on the backing device size
self.assertEqual(dev.size, initial_size)
self.assertEqual(dev.slave.size, initial_size + crypto.LUKS_METADATA_SIZE)