2121import logging
2222import os
2323import subprocess
24+ import re
2425
2526import time
2627
3031import qubes .storage
3132import qubes .utils
3233
34+ _suffix_re = re .compile (r'-(?:private|root|volatile)(?:-snap|-import|-[0-9]+-back)\Z' )
3335
3436def 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
242247def 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
260278def init_cache_coro (log = logging .getLogger ('qubes.storage.lvm' )):
0 commit comments