in VMEncryption/main/handle.py [0:0]
def encrypt_inplace_with_seperate_header_file(passphrase_file,
device_item,
disk_util,
bek_util,
status_prefix='',
ongoing_item_config=None):
"""
if ongoing_item_config is not None, then this is a resume case.
"""
logger.log("encrypt_inplace_with_seperate_header_file")
current_phase = CommonVariables.EncryptionPhaseEncryptDevice
if ongoing_item_config is None:
ongoing_item_config = OnGoingItemConfig(encryption_environment=encryption_environment,
logger=logger)
mapper_name = str(uuid.uuid4())
ongoing_item_config.current_block_size = CommonVariables.default_block_size
ongoing_item_config.current_slice_index = 0
ongoing_item_config.device_size = device_item.size
ongoing_item_config.file_system = device_item.file_system
ongoing_item_config.mapper_name = mapper_name
ongoing_item_config.mount_point = device_item.mount_point
# TODO improve this.
if os.path.exists(os.path.join('/dev/', device_item.name)):
ongoing_item_config.original_dev_name_path = os.path.join('/dev/', device_item.name)
else:
ongoing_item_config.original_dev_name_path = os.path.join('/dev/mapper/', device_item.name)
ongoing_item_config.original_dev_path = os.path.join('/dev/disk/by-uuid', device_item.uuid)
luks_header_file_path = disk_util.create_luks_header(mapper_name=mapper_name)
if luks_header_file_path is None:
logger.log(msg="create header file failed", level=CommonVariables.ErrorLevel)
return current_phase
else:
ongoing_item_config.luks_header_file_path = luks_header_file_path
ongoing_item_config.phase = CommonVariables.EncryptionPhaseEncryptDevice
ongoing_item_config.commit()
else:
logger.log(msg="ongoing item config is not none, this is resuming: {0}".format(ongoing_item_config),
level=CommonVariables.WarningLevel)
current_phase = ongoing_item_config.get_phase()
while current_phase != CommonVariables.EncryptionPhaseDone:
if current_phase == CommonVariables.EncryptionPhaseEncryptDevice:
try:
mapper_name = ongoing_item_config.get_mapper_name()
original_dev_path = ongoing_item_config.get_original_dev_path()
luks_header_file_path = ongoing_item_config.get_header_file_path()
toggle_se_linux_for_centos7(True)
encrypt_result = disk_util.encrypt_disk(dev_path=original_dev_path,
passphrase_file=passphrase_file,
mapper_name=mapper_name,
header_file=luks_header_file_path)
if encrypt_result != CommonVariables.process_success:
logger.log(msg="the encrypton for {0} failed".format(original_dev_path),
level=CommonVariables.ErrorLevel)
return current_phase
else:
ongoing_item_config.phase = CommonVariables.EncryptionPhaseCopyData
ongoing_item_config.commit()
current_phase = CommonVariables.EncryptionPhaseCopyData
finally:
toggle_se_linux_for_centos7(False)
elif current_phase == CommonVariables.EncryptionPhaseCopyData:
try:
mapper_name = ongoing_item_config.get_mapper_name()
original_dev_path = ongoing_item_config.get_original_dev_path()
luks_header_file_path = ongoing_item_config.get_header_file_path()
toggle_se_linux_for_centos7(True)
device_mapper_path = os.path.join("/dev/mapper", mapper_name)
if not os.path.exists(device_mapper_path):
open_result = disk_util.luks_open(passphrase_file=passphrase_file,
dev_path=original_dev_path,
mapper_name=mapper_name,
header_file=luks_header_file_path,
uses_cleartext_key=False)
if open_result != CommonVariables.process_success:
logger.log(msg="the luks open for {0} failed.".format(original_dev_path),
level=CommonVariables.ErrorLevel)
return current_phase
else:
logger.log(msg="the device mapper path existed, so skip the luks open.",
level=CommonVariables.InfoLevel)
device_size = ongoing_item_config.get_device_size()
current_slice_index = ongoing_item_config.get_current_slice_index()
if current_slice_index is None:
ongoing_item_config.current_slice_index = 0
ongoing_item_config.current_source_path = original_dev_path
ongoing_item_config.current_destination = device_mapper_path
ongoing_item_config.current_total_copy_size = device_size
ongoing_item_config.from_end = True
ongoing_item_config.commit()
copy_result = disk_util.copy(ongoing_item_config=ongoing_item_config, status_prefix=status_prefix)
if copy_result != CommonVariables.success:
error_message = "the copying result is {0} so skip the mounting".format(copy_result)
logger.log(msg=(error_message), level=CommonVariables.ErrorLevel)
return current_phase
else:
crypt_item_to_update = CryptItem()
crypt_item_to_update.mapper_name = mapper_name
original_dev_name_path = ongoing_item_config.get_original_dev_name_path()
crypt_item_to_update.dev_path = disk_util.get_persistent_path_by_sdx_path(original_dev_name_path)
crypt_item_to_update.luks_header_path = luks_header_file_path
crypt_item_to_update.file_system = ongoing_item_config.get_file_system()
crypt_item_to_update.uses_cleartext_key = False
crypt_item_to_update.current_luks_slot = 0
# if the original mountpoint is empty, then leave
# it as None
mount_point = ongoing_item_config.get_mount_point()
if mount_point is None or mount_point == "":
crypt_item_to_update.mount_point = "None"
else:
crypt_item_to_update.mount_point = mount_point
update_crypt_item_result = disk_util.add_crypt_item(crypt_item_to_update, passphrase_file)
if not update_crypt_item_result:
logger.log(msg="update crypt item failed", level=CommonVariables.ErrorLevel)
if crypt_item_to_update.mount_point != "None":
disk_util.mount_filesystem(device_mapper_path, mount_point)
else:
logger.log("the crypt_item_to_update.mount_point is None, so we do not mount it.")
if mount_point:
logger.log(msg="removing entry for unencrypted drive from fstab",
level=CommonVariables.InfoLevel)
disk_util.modify_fstab_entry_encrypt(mount_point, os.path.join(CommonVariables.dev_mapper_root, mapper_name))
else:
logger.log(msg=original_dev_name_path + " is not defined in fstab, no need to update",
level=CommonVariables.InfoLevel)
current_phase = CommonVariables.EncryptionPhaseDone
ongoing_item_config.phase = current_phase
ongoing_item_config.commit()
ongoing_item_config.clear_config()
return current_phase
finally:
toggle_se_linux_for_centos7(False)