Skip to content

Commit 0b88ade

Browse files
committed
tpm: T7726: Copy encrypted volume when adding system images
1 parent b115b74 commit 0b88ade

File tree

1 file changed

+44
-15
lines changed

1 file changed

+44
-15
lines changed

src/op_mode/image_installer.py

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from os import readlink
2727
from os import getpid
2828
from os import getppid
29+
from os import sync
2930
from json import loads
3031
from json import dumps
3132
from typing import Union
@@ -1083,30 +1084,60 @@ def add_image(image_path: str, vrf: str = None, username: str = '',
10831084
# find target directory
10841085
root_dir: str = disk.find_persistence()
10851086

1087+
cmdline_options = []
1088+
10861089
# a config dir. It is the deepest one, so the comand will
10871090
# create all the rest in a single step
10881091
target_config_dir: str = f'{root_dir}/boot/{image_name}/rw/opt/vyatta/etc/config/'
10891092
# copy config
10901093
if no_prompt or migrate_config():
1091-
print('Copying configuration directory')
1092-
# copytree preserves perms but not ownership:
1093-
Path(target_config_dir).mkdir(parents=True)
1094-
chown(target_config_dir, group='vyattacfg')
1095-
chmod_2775(target_config_dir)
1096-
copytree('/opt/vyatta/etc/config/', target_config_dir, symlinks=True,
1097-
copy_function=copy_preserve_owner, dirs_exist_ok=True)
1098-
1099-
# Record information from which image we upgraded to the new one.
1100-
# This can be used for a future automatic rollback into the old image.
1101-
tmp = {'previous_image' : image.get_running_image()}
1102-
write_file(f'{target_config_dir}/first_boot', dumps(tmp))
1103-
1094+
if Path('/dev/mapper/vyos_config').exists():
1095+
print('Copying encrypted configuration volume')
1096+
1097+
# Record information from which image we upgraded to the new one.
1098+
# This can be used for a future automatic rollback into the old image.
1099+
#
1100+
# For encrypted config, we need to copy, sync filesystems and remove from current image
1101+
tmp = {'previous_image' : image.get_running_image()}
1102+
write_file('/opt/vyatta/etc/config/first_boot', dumps(tmp))
1103+
sync()
1104+
1105+
# Copy encrypteed volumes
1106+
current_name = image.get_running_image()
1107+
current_config_path = f'{root_dir}/luks/{current_name}'
1108+
target_config_path = f'{root_dir}/luks/{image_name}'
1109+
copy(current_config_path, target_config_path)
1110+
1111+
# Now remove from current image
1112+
Path('/opt/vyatta/etc/config/first_boot').unlink()
1113+
1114+
cmdline_options = get_cli_kernel_options(
1115+
f'/opt/vyatta/etc/config/config.boot')
1116+
else:
1117+
print('Copying configuration directory')
1118+
# copytree preserves perms but not ownership:
1119+
Path(target_config_dir).mkdir(parents=True)
1120+
chown(target_config_dir, group='vyattacfg')
1121+
chmod_2775(target_config_dir)
1122+
copytree('/opt/vyatta/etc/config/', target_config_dir, symlinks=True,
1123+
copy_function=copy_preserve_owner, dirs_exist_ok=True)
1124+
1125+
# Record information from which image we upgraded to the new one.
1126+
# This can be used for a future automatic rollback into the old image.
1127+
tmp = {'previous_image' : image.get_running_image()}
1128+
write_file(f'{target_config_dir}/first_boot', dumps(tmp))
1129+
1130+
cmdline_options = get_cli_kernel_options(
1131+
f'{target_config_dir}/config.boot')
11041132
else:
11051133
Path(target_config_dir).mkdir(parents=True)
11061134
chown(target_config_dir, group='vyattacfg')
11071135
chmod_2775(target_config_dir)
11081136
Path(f'{target_config_dir}/.vyatta_config').touch()
11091137

1138+
cmdline_options = get_cli_kernel_options(
1139+
f'{target_config_dir}/config.boot')
1140+
11101141
target_ssh_dir: str = f'{root_dir}/boot/{image_name}/rw/etc/ssh/'
11111142
if no_prompt or copy_ssh_host_keys():
11121143
print('Copying SSH host keys')
@@ -1132,8 +1163,6 @@ def add_image(image_path: str, vrf: str = None, username: str = '',
11321163
if set_as_default:
11331164
grub.set_default(image_name, root_dir)
11341165

1135-
cmdline_options = get_cli_kernel_options(
1136-
f'{target_config_dir}/config.boot')
11371166
grub_util.update_kernel_cmdline_options(' '.join(cmdline_options),
11381167
root_dir=root_dir,
11391168
version=image_name)

0 commit comments

Comments
 (0)