2929from vyos .tpm import write_tpm_key
3030from vyos .utils .io import ask_input , ask_yes_no
3131from vyos .utils .process import cmd
32+ from vyos .defaults import directories
3233
3334persistpath_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'
3538dm_device = '/dev/mapper/vyos_config'
3639
3740def is_opened ():
@@ -68,9 +71,8 @@ def load_config(key):
6871
6972 cmd (f'cryptsetup -q open { image_path } vyos_config --key-file={ key_file } ' )
7073
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 } ' )
7476
7577 os .unlink (key_file )
7678
@@ -125,8 +127,8 @@ def encrypt_config(key, recovery_key=None, is_tpm=True):
125127 with TemporaryDirectory () as d :
126128 cmd (f'mount /dev/mapper/vyos_config { d } ' )
127129
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 )
130132
131133 cmd (f'umount { d } ' )
132134
@@ -135,9 +137,8 @@ def encrypt_config(key, recovery_key=None, is_tpm=True):
135137 if recovery_key :
136138 os .unlink (recovery_key_file )
137139
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 } ' )
141142
142143 return True
143144
@@ -161,23 +162,23 @@ def decrypt_config(key):
161162
162163 cmd (f'cryptsetup -q open { image_path } vyos_config --key-file={ key_file } ' )
163164
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 } ' )
168168
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 )
173173
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
175176 with TemporaryDirectory () as d :
176177 cmd (f'mount /dev/mapper/vyos_config { d } ' )
177178
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 } ' )
181182
182183 cmd (f'umount { d } ' )
183184
@@ -235,7 +236,7 @@ def decrypt_config(key):
235236 if args .enable and not tpm_exists :
236237 print ('WARNING: VyOS will boot into a default config when encrypted without a TPM' )
237238 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"' )
239240
240241 if not ask_yes_no ('Are you sure you want to proceed?' ):
241242 sys .exit (0 )
@@ -256,12 +257,12 @@ def decrypt_config(key):
256257 decrypt_config (key or recovery_key )
257258
258259 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' )
260261 elif args .load :
261262 load_config (key or recovery_key )
262263
263264 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' )
265266 elif args .enable and tpm_exists :
266267 encrypt_config (key , recovery_key )
267268
0 commit comments