29
29
from vyos .tpm import write_tpm_key
30
30
from vyos .utils .io import ask_input , ask_yes_no
31
31
from vyos .utils .process import cmd
32
+ from vyos .defaults import directories
32
33
33
34
persistpath_cmd = '/opt/vyatta/sbin/vyos-persistpath'
34
- mount_paths = ['/config' , '/opt/vyatta/etc/config' ]
35
+ # mount_path is /opt/vyatta/etc/config as of this writing
36
+ mount_path = directories ['config' ]
37
+ mount_path_old = f'{ mount_path } .old'
35
38
dm_device = '/dev/mapper/vyos_config'
36
39
37
40
def is_opened ():
@@ -68,9 +71,8 @@ def load_config(key):
68
71
69
72
cmd (f'cryptsetup -q open { image_path } vyos_config --key-file={ key_file } ' )
70
73
71
- for path in mount_paths :
72
- cmd (f'mount /dev/mapper/vyos_config { path } ' )
73
- cmd (f'chgrp -R vyattacfg { path } ' )
74
+ cmd (f'mount /dev/mapper/vyos_config { mount_path } ' )
75
+ cmd (f'chgrp -R vyattacfg { mount_path } ' )
74
76
75
77
os .unlink (key_file )
76
78
@@ -125,8 +127,8 @@ def encrypt_config(key, recovery_key=None, is_tpm=True):
125
127
with TemporaryDirectory () as d :
126
128
cmd (f'mount /dev/mapper/vyos_config { d } ' )
127
129
128
- # Move /config to encrypted volume
129
- shutil .copytree ('/config' , d , copy_function = shutil .move , dirs_exist_ok = True )
130
+ # Move mount_path to encrypted volume
131
+ shutil .copytree (mount_path , d , copy_function = shutil .move , dirs_exist_ok = True )
130
132
131
133
cmd (f'umount { d } ' )
132
134
@@ -135,9 +137,8 @@ def encrypt_config(key, recovery_key=None, is_tpm=True):
135
137
if recovery_key :
136
138
os .unlink (recovery_key_file )
137
139
138
- for path in mount_paths :
139
- cmd (f'mount /dev/mapper/vyos_config { path } ' )
140
- cmd (f'chgrp vyattacfg { path } ' )
140
+ cmd (f'mount /dev/mapper/vyos_config { mount_path } ' )
141
+ cmd (f'chgrp vyattacfg { mount_path } ' )
141
142
142
143
return True
143
144
@@ -161,23 +162,23 @@ def decrypt_config(key):
161
162
162
163
cmd (f'cryptsetup -q open { image_path } vyos_config --key-file={ key_file } ' )
163
164
164
- # unmount encrypted volume mount points
165
- for path in mount_paths :
166
- if os .path .ismount (path ):
167
- cmd (f'umount { path } ' )
165
+ # unmount encrypted volume mount point
166
+ if os .path .ismount (mount_path ):
167
+ cmd (f'umount { mount_path } ' )
168
168
169
- # If /config is populated, move to /config.old
170
- if len (os .listdir ('/config' )) > 0 :
171
- print ('Moving existing /config folder to /config.old ' )
172
- shutil .move ('/config' , '/config.old' )
169
+ # If /opt/vyatta/etc/ config is populated, move to /opt/vyatta/etc /config.old
170
+ if len (os .listdir (mount_path )) > 0 :
171
+ print (f 'Moving existing { mount_path } folder to { mount_path_old } ' )
172
+ shutil .move (mount_path , mount_path_old )
173
173
174
- # Temporarily mount encrypted volume and migrate files to /config on rootfs
174
+ # Temporarily mount encrypted volume and migrate files to
175
+ # /opt/vyatta/etc/config on rootfs
175
176
with TemporaryDirectory () as d :
176
177
cmd (f'mount /dev/mapper/vyos_config { d } ' )
177
178
178
- # Move encrypted volume to /config
179
- shutil .copytree (d , '/config' , copy_function = shutil .move , dirs_exist_ok = True )
180
- cmd (f'chgrp -R vyattacfg /config ' )
179
+ # Move encrypted volume to /opt/vyatta/etc/ config
180
+ shutil .copytree (d , mount_path , copy_function = shutil .move , dirs_exist_ok = True )
181
+ cmd (f'chgrp -R vyattacfg { mount_path } ' )
181
182
182
183
cmd (f'umount { d } ' )
183
184
@@ -235,7 +236,7 @@ def decrypt_config(key):
235
236
if args .enable and not tpm_exists :
236
237
print ('WARNING: VyOS will boot into a default config when encrypted without a TPM' )
237
238
print ('You will need to manually login with default credentials and use "encryption load"' )
238
- print ('to mount the encrypted volume and use "load /config /config.boot"' )
239
+ print (f 'to mount the encrypted volume and use "load { mount_path } /config.boot"' )
239
240
240
241
if not ask_yes_no ('Are you sure you want to proceed?' ):
241
242
sys .exit (0 )
@@ -256,12 +257,12 @@ def decrypt_config(key):
256
257
decrypt_config (key or recovery_key )
257
258
258
259
print ('Encrypted config volume has been disabled' )
259
- print ('Contents have been migrated to /config on rootfs' )
260
+ print (f 'Contents have been migrated to { mount_path } on rootfs' )
260
261
elif args .load :
261
262
load_config (key or recovery_key )
262
263
263
264
print ('Encrypted config volume has been mounted' )
264
- print ('Use "load /config /config.boot" to load configuration' )
265
+ print (f 'Use "load { mount_path } /config.boot" to load configuration' )
265
266
elif args .enable and tpm_exists :
266
267
encrypt_config (key , recovery_key )
267
268
0 commit comments