Skip to content

Commit 91afb69

Browse files
authored
Merge pull request #4732 from jestabro/simplified-bind-mount
T7836: move bind mount of /config to vyos-1x
2 parents 60a322e + 2d32d32 commit 91afb69

File tree

2 files changed

+38
-26
lines changed

2 files changed

+38
-26
lines changed

src/helpers/vyos-config-encrypt.py

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@
2929
from vyos.tpm import write_tpm_key
3030
from vyos.utils.io import ask_input, ask_yes_no
3131
from vyos.utils.process import cmd
32+
from vyos.defaults import directories
3233

3334
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'
3538
dm_device = '/dev/mapper/vyos_config'
3639

3740
def 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

src/init/vyos-router

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ mount_encrypted_config() {
120120
return 1
121121
fi
122122

123-
mount /dev/mapper/vyos_config /config
124123
mount /dev/mapper/vyos_config $vyatta_sysconfdir/config
125124

126125
echo "Mounted encrypted config volume"
@@ -143,7 +142,6 @@ unmount_encrypted_config() {
143142
return
144143
fi
145144

146-
umount /config
147145
umount $vyatta_sysconfdir/config
148146

149147
cryptsetup close vyos_config
@@ -314,6 +312,17 @@ bind_mount_boot ()
314312
fi
315313
}
316314

315+
bind_mount_slash_config ()
316+
{
317+
if [ -d /opt/vyatta/etc/config ]
318+
then
319+
if [ ! -d /config ] ; then
320+
mkdir /config
321+
fi
322+
mount --bind /opt/vyatta/etc/config /config
323+
fi
324+
}
325+
317326
clear_or_override_config_files ()
318327
{
319328
for conf in snmp/snmpd.conf snmp/snmptrapd.conf snmp/snmp.conf \
@@ -587,6 +596,8 @@ start ()
587596

588597
bind_mount_boot
589598

599+
disabled bind_mount_slash_config || bind_mount_slash_config
600+
590601
disabled configure || load_bootfile || overall_status=1
591602
log_end_msg $?
592603

0 commit comments

Comments
 (0)