Skip to content

Commit dcd1a5f

Browse files
committed
Rename LVM volumes to something more reasonable
We still convert them to the old broken names for (internal) backwards compatibility.
1 parent 7b1b267 commit dcd1a5f

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

qubes/storage/lvm.py

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import logging
2222
import os
2323
import subprocess
24+
import re
2425

2526
import time
2627

@@ -30,6 +31,7 @@
3031
import qubes.storage
3132
import qubes.utils
3233

34+
_suffix_re = re.compile(r'-(?:private|root|volatile)(?:-snap|-import|-[0-9]+-back)\Z')
3335

3436
def check_lvm_version():
3537
#Check if lvm is very very old, like in Travis-CI
@@ -158,9 +160,10 @@ def get_volume(self, vid):
158160

159161
def list_volumes(self):
160162
''' Return a list of volumes managed by this pool '''
161-
volumes = []
162-
for vid, vol_info in size_cache.items():
163-
if not vid.startswith(self.volume_group + '/'):
163+
volumes = set()
164+
prefix = self.volume_group + '/vm-'
165+
for vid, vol_info in size_cache.items(prefix):
166+
if not vid.startswith():
164167
continue
165168
if vol_info['pool_lv'] != self.thin_pool:
166169
continue
@@ -216,7 +219,7 @@ def usage_details(self):
216219
'vg_name,pool_lv,name,lv_size,data_percent,lv_attr,origin,lv_metadata_size,'
217220
'metadata_percent', '--units', 'b', '--separator', ';']
218221

219-
def _parse_lvm_cache(lvm_output):
222+
def _parse_lvm_cache(lvm_output, replace=True):
220223
result = {}
221224

222225
for line in lvm_output.splitlines():
@@ -225,6 +228,8 @@ def _parse_lvm_cache(lvm_output):
225228
origin, metadata_size, metadata_percent = line.split(';', 8)
226229
if '' in [pool_name, name, size, usage_percent]:
227230
continue
231+
if replace:
232+
name = name.replace('+', '-').replace('.', '-')
228233
name = pool_name + "/" + name
229234
size = int(size[:-1]) # Remove 'B' suffix
230235
usage = int(size / 100 * float(usage_percent))
@@ -241,20 +246,33 @@ def _parse_lvm_cache(lvm_output):
241246

242247
def init_cache(log=logging.getLogger('qubes.storage.lvm')):
243248
cmd = _init_cache_cmd
244-
if os.getuid() != 0:
245-
cmd = ['sudo'] + cmd
246-
environ = os.environ.copy()
247-
environ['LC_ALL'] = 'C.utf8'
248-
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
249-
close_fds=True, env=environ)
250-
out, err = p.communicate()
251-
return_code = p.returncode
252-
if return_code == 0 and err:
253-
log.warning(err)
254-
elif return_code != 0:
255-
raise qubes.storage.StoragePoolException(err)
256-
257-
return _parse_lvm_cache(out)
249+
def process_cmd(cmd):
250+
if os.getuid() != 0:
251+
cmd = ['sudo'] + cmd
252+
environ = os.environ.copy()
253+
environ['LC_ALL'] = 'C.utf8'
254+
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
255+
close_fds=True, env=environ)
256+
out, err = p.communicate()
257+
return_code = p.returncode
258+
if return_code:
259+
raise qubes.storage.StoragePoolException(err)
260+
elif err:
261+
log.warning(err)
262+
return out
263+
264+
res = _parse_lvm_cache(process_cmd(cmd), False)
265+
for name, _ in res:
266+
pool, name = name.split('/')
267+
if not name.startswith('vm-'):
268+
continue
269+
match_res = _suffix_re.search(name, 3)
270+
if match_res:
271+
match_offset = match_res.span()[0]
272+
suffix = name[match_offset + 1:]
273+
qube_name = name[3:match_offset]
274+
new_name = 'vm-' + qube_name.replace('_', '+') + '.' + suffix
275+
process_cmd(['lvm', 'lvrename', '--', pool, name, new_name])
258276

259277
@asyncio.coroutine
260278
def init_cache_coro(log=logging.getLogger('qubes.storage.lvm')):

0 commit comments

Comments
 (0)