lvm.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. #
  2. # The Qubes OS Project, http://www.qubes-os.org
  3. #
  4. # Copyright (C) 2016 Bahtiar `kalkin-` Gadimov <bahtiar@gadimov.de>
  5. #
  6. # This library is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU Lesser General Public
  8. # License as published by the Free Software Foundation; either
  9. # version 2.1 of the License, or (at your option) any later version.
  10. #
  11. # This library is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. # Lesser General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Lesser General Public
  17. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  18. #
  19. ''' Driver for storing vm images in a LVM thin pool '''
  20. import logging
  21. import operator
  22. import os
  23. import subprocess
  24. import time
  25. import asyncio
  26. import qubes
  27. import qubes.storage
  28. import qubes.utils
  29. def check_lvm_version():
  30. #Check if lvm is very very old, like in Travis-CI
  31. try:
  32. lvm_help = subprocess.check_output(['lvm', 'lvcreate', '--help'],
  33. stderr=subprocess.DEVNULL).decode()
  34. return '--setactivationskip' not in lvm_help
  35. except (subprocess.CalledProcessError, FileNotFoundError):
  36. pass
  37. lvm_is_very_old = check_lvm_version()
  38. class ThinPool(qubes.storage.Pool):
  39. ''' LVM Thin based pool implementation
  40. ''' # pylint: disable=protected-access
  41. size_cache = None
  42. driver = 'lvm_thin'
  43. def __init__(self, volume_group, thin_pool, revisions_to_keep=1, **kwargs):
  44. super(ThinPool, self).__init__(revisions_to_keep=revisions_to_keep,
  45. **kwargs)
  46. self.volume_group = volume_group
  47. self.thin_pool = thin_pool
  48. self._pool_id = "{!s}/{!s}".format(volume_group, thin_pool)
  49. self.log = logging.getLogger('qube.storage.lvm.%s' % self._pool_id)
  50. self._volume_objects_cache = {}
  51. @property
  52. def config(self):
  53. return {
  54. 'name': self.name,
  55. 'volume_group': self.volume_group,
  56. 'thin_pool': self.thin_pool,
  57. 'driver': ThinPool.driver
  58. }
  59. def destroy(self):
  60. pass # TODO Should we remove an existing pool?
  61. def init_volume(self, vm, volume_config):
  62. ''' Initialize a :py:class:`qubes.storage.Volume` from `volume_config`.
  63. '''
  64. if 'vid' not in volume_config.keys():
  65. if vm and hasattr(vm, 'name'):
  66. vm_name = vm.name
  67. else:
  68. # for the future if we have volumes not belonging to a vm
  69. vm_name = qubes.utils.random_string()
  70. assert self.name
  71. volume_config['vid'] = "{!s}/vm-{!s}-{!s}".format(
  72. self.volume_group, vm_name, volume_config['name'])
  73. volume_config['volume_group'] = self.volume_group
  74. volume_config['pool'] = self
  75. volume = ThinVolume(**volume_config)
  76. self._volume_objects_cache[volume_config['vid']] = volume
  77. return volume
  78. def setup(self):
  79. pass # TODO Should we create a non existing pool?
  80. def get_volume(self, vid):
  81. ''' Return a volume with given vid'''
  82. if vid in self._volume_objects_cache:
  83. return self._volume_objects_cache[vid]
  84. config = {
  85. 'pool': self,
  86. 'vid': vid,
  87. 'name': vid,
  88. 'volume_group': self.volume_group,
  89. }
  90. # don't cache this object, as it doesn't carry full configuration
  91. return ThinVolume(**config)
  92. def list_volumes(self):
  93. ''' Return a list of volumes managed by this pool '''
  94. volumes = []
  95. for vid, vol_info in size_cache.items():
  96. if not vid.startswith(self.volume_group + '/'):
  97. continue
  98. if vol_info['pool_lv'] != self.thin_pool:
  99. continue
  100. if vid.endswith('-snap'):
  101. # implementation detail volume
  102. continue
  103. if vid.endswith('-back'):
  104. # old revisions
  105. continue
  106. config = {
  107. 'pool': self,
  108. 'vid': vid,
  109. 'name': vid,
  110. 'volume_group': self.volume_group,
  111. 'rw': vol_info['attr'][1] == 'w',
  112. }
  113. volumes += [ThinVolume(**config)]
  114. return volumes
  115. @property
  116. def size(self):
  117. try:
  118. return qubes.storage.lvm.size_cache[
  119. self.volume_group + '/' + self.thin_pool]['size']
  120. except KeyError:
  121. return 0
  122. @property
  123. def usage(self):
  124. try:
  125. return qubes.storage.lvm.size_cache[
  126. self.volume_group + '/' + self.thin_pool]['usage']
  127. except KeyError:
  128. return 0
  129. def init_cache(log=logging.getLogger('qubes.storage.lvm')):
  130. cmd = ['lvs', '--noheadings', '-o',
  131. 'vg_name,pool_lv,name,lv_size,data_percent,lv_attr,origin',
  132. '--units', 'b', '--separator', ';']
  133. if os.getuid() != 0:
  134. cmd.insert(0, 'sudo')
  135. p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
  136. close_fds=True)
  137. out, err = p.communicate()
  138. return_code = p.returncode
  139. if return_code == 0 and err:
  140. log.warning(err)
  141. elif return_code != 0:
  142. raise qubes.storage.StoragePoolException(err)
  143. result = {}
  144. for line in out.splitlines():
  145. line = line.decode().strip()
  146. pool_name, pool_lv, name, size, usage_percent, attr, \
  147. origin = line.split(';', 6)
  148. if '' in [pool_name, name, size, usage_percent]:
  149. continue
  150. name = pool_name + "/" + name
  151. size = int(size[:-1]) # Remove 'B' suffix
  152. usage = int(size / 100 * float(usage_percent))
  153. result[name] = {'size': size, 'usage': usage, 'pool_lv': pool_lv,
  154. 'attr': attr, 'origin': origin}
  155. return result
  156. size_cache = init_cache()
  157. class ThinVolume(qubes.storage.Volume):
  158. ''' Default LVM thin volume implementation
  159. ''' # pylint: disable=too-few-public-methods
  160. def __init__(self, volume_group, size=0, **kwargs):
  161. self.volume_group = volume_group
  162. super(ThinVolume, self).__init__(size=size, **kwargs)
  163. self.log = logging.getLogger('qube.storage.lvm.%s' % str(self.pool))
  164. if self.snap_on_start or self.save_on_stop:
  165. self._vid_snap = self.vid + '-snap'
  166. self._size = size
  167. @property
  168. def path(self):
  169. return '/dev/' + self.vid
  170. @property
  171. def revisions(self):
  172. name_prefix = self.vid + '-'
  173. revisions = {}
  174. for revision_vid in size_cache:
  175. if not revision_vid.startswith(name_prefix):
  176. continue
  177. if not revision_vid.endswith('-back'):
  178. continue
  179. revision_vid = revision_vid[len(name_prefix):]
  180. seconds = int(revision_vid[:-len('-back')])
  181. iso_date = qubes.storage.isodate(seconds).split('.', 1)[0]
  182. revisions[revision_vid] = iso_date
  183. return revisions
  184. @property
  185. def size(self):
  186. try:
  187. return qubes.storage.lvm.size_cache[self.vid]['size']
  188. except KeyError:
  189. return self._size
  190. @size.setter
  191. def size(self, _):
  192. raise qubes.storage.StoragePoolException(
  193. "You shouldn't use lvm size setter")
  194. def _reset(self):
  195. ''' Resets a volatile volume '''
  196. assert not self.snap_on_start and not self.save_on_stop, \
  197. "Not a volatile volume"
  198. self.log.debug('Resetting volatile ' + self.vid)
  199. try:
  200. cmd = ['remove', self.vid]
  201. qubes_lvm(cmd, self.log)
  202. except qubes.storage.StoragePoolException:
  203. pass
  204. # pylint: disable=protected-access
  205. cmd = ['create', self.pool._pool_id, self.vid.split('/')[1],
  206. str(self.size)]
  207. qubes_lvm(cmd, self.log)
  208. def _remove_revisions(self, revisions=None):
  209. '''Remove old volume revisions.
  210. If no revisions list is given, it removes old revisions according to
  211. :py:attr:`revisions_to_keep`
  212. :param revisions: list of revisions to remove
  213. '''
  214. if revisions is None:
  215. revisions = sorted(self.revisions.items(),
  216. key=operator.itemgetter(1))
  217. revisions = revisions[:-self.revisions_to_keep]
  218. revisions = [rev_id for rev_id, _ in revisions]
  219. for rev_id in revisions:
  220. try:
  221. cmd = ['remove', self.vid + rev_id]
  222. qubes_lvm(cmd, self.log)
  223. except qubes.storage.StoragePoolException:
  224. pass
  225. def _commit(self):
  226. msg = "Trying to commit {!s}, but it has save_on_stop == False"
  227. msg = msg.format(self)
  228. assert self.save_on_stop, msg
  229. msg = "Trying to commit {!s}, but it has rw == False"
  230. msg = msg.format(self)
  231. assert self.rw, msg
  232. assert hasattr(self, '_vid_snap')
  233. if self.revisions_to_keep > 0:
  234. cmd = ['clone', self.vid,
  235. '{}-{}-back'.format(self.vid, int(time.time()))]
  236. qubes_lvm(cmd, self.log)
  237. self._remove_revisions()
  238. # TODO: when converting this function to coroutine, this _must_ be
  239. # under a lock
  240. # remove old volume only after _successful_ clone of the new one
  241. cmd = ['rename', self.vid, self.vid + '-tmp']
  242. qubes_lvm(cmd, self.log)
  243. try:
  244. cmd = ['clone', self._vid_snap, self.vid]
  245. qubes_lvm(cmd, self.log)
  246. except:
  247. # restore original volume
  248. cmd = ['rename', self.vid + '-tmp', self.vid]
  249. qubes_lvm(cmd, self.log)
  250. raise
  251. else:
  252. cmd = ['remove', self.vid + '-tmp']
  253. qubes_lvm(cmd, self.log)
  254. def create(self):
  255. assert self.vid
  256. assert self.size
  257. if self.save_on_stop:
  258. if self.source:
  259. cmd = ['clone', str(self.source), self.vid]
  260. else:
  261. cmd = [
  262. 'create',
  263. self.pool._pool_id, # pylint: disable=protected-access
  264. self.vid.split('/', 1)[1],
  265. str(self.size)
  266. ]
  267. qubes_lvm(cmd, self.log)
  268. reset_cache()
  269. return self
  270. def remove(self):
  271. assert self.vid
  272. if self.is_dirty():
  273. cmd = ['remove', self._vid_snap]
  274. qubes_lvm(cmd, self.log)
  275. self._remove_revisions(self.revisions.keys())
  276. if not os.path.exists(self.path):
  277. return
  278. cmd = ['remove', self.vid]
  279. qubes_lvm(cmd, self.log)
  280. reset_cache()
  281. # pylint: disable=protected-access
  282. self.pool._volume_objects_cache.pop(self.vid, None)
  283. def export(self):
  284. ''' Returns an object that can be `open()`. '''
  285. # make sure the device node is available
  286. qubes_lvm(['activate', self.vid], self.log)
  287. devpath = '/dev/' + self.vid
  288. return devpath
  289. @asyncio.coroutine
  290. def import_volume(self, src_volume):
  291. if not src_volume.save_on_stop:
  292. return self
  293. # HACK: neat trick to speed up testing if you have same physical thin
  294. # pool assigned to two qubes-pools i.e: qubes_dom0 and test-lvm
  295. # pylint: disable=line-too-long
  296. if isinstance(src_volume.pool, ThinPool) and \
  297. src_volume.pool.thin_pool == self.pool.thin_pool: # NOQA
  298. cmd = ['remove', self.vid]
  299. qubes_lvm(cmd, self.log)
  300. cmd = ['clone', str(src_volume), str(self)]
  301. qubes_lvm(cmd, self.log)
  302. else:
  303. if src_volume.size != self.size:
  304. self.resize(src_volume.size)
  305. src_path = src_volume.export()
  306. cmd = ['dd', 'if=' + src_path, 'of=/dev/' + self.vid,
  307. 'conv=sparse']
  308. p = yield from asyncio.create_subprocess_exec(*cmd)
  309. yield from p.wait()
  310. if p.returncode != 0:
  311. raise qubes.storage.StoragePoolException(
  312. 'Failed to import volume {!r}, dd exit code: {}'.format(
  313. src_volume, p.returncode))
  314. reset_cache()
  315. return self
  316. def import_data(self):
  317. ''' Returns an object that can be `open()`. '''
  318. devpath = '/dev/' + self.vid
  319. return devpath
  320. def is_dirty(self):
  321. if self.save_on_stop:
  322. return os.path.exists('/dev/' + self._vid_snap)
  323. return False
  324. def is_outdated(self):
  325. if not self.snap_on_start:
  326. return False
  327. if self._vid_snap not in size_cache:
  328. return False
  329. return (size_cache[self._vid_snap]['origin'] !=
  330. self.source.vid.split('/')[1])
  331. def revert(self, revision=None):
  332. if revision is None:
  333. revision = \
  334. max(self.revisions.items(), key=operator.itemgetter(1))[0]
  335. old_path = self.path + '-' + revision
  336. if not os.path.exists(old_path):
  337. msg = "Volume {!s} has no {!s}".format(self, old_path)
  338. raise qubes.storage.StoragePoolException(msg)
  339. cmd = ['remove', self.vid]
  340. qubes_lvm(cmd, self.log)
  341. cmd = ['clone', self.vid + '-' + revision, self.vid]
  342. qubes_lvm(cmd, self.log)
  343. reset_cache()
  344. return self
  345. def resize(self, size):
  346. ''' Expands volume, throws
  347. :py:class:`qubst.storage.qubes.storage.StoragePoolException` if
  348. given size is less than current_size
  349. '''
  350. if not self.rw:
  351. msg = 'Can not resize reađonly volume {!s}'.format(self)
  352. raise qubes.storage.StoragePoolException(msg)
  353. if size < self.size:
  354. raise qubes.storage.StoragePoolException(
  355. 'For your own safety, shrinking of %s is'
  356. ' disabled. If you really know what you'
  357. ' are doing, use `lvresize` on %s manually.' %
  358. (self.name, self.vid))
  359. if size == self.size:
  360. return
  361. cmd = ['extend', self.vid, str(size)]
  362. qubes_lvm(cmd, self.log)
  363. if self.is_dirty():
  364. cmd = ['extend', self._vid_snap, str(size)]
  365. qubes_lvm(cmd, self.log)
  366. reset_cache()
  367. def _snapshot(self):
  368. try:
  369. cmd = ['remove', self._vid_snap]
  370. qubes_lvm(cmd, self.log)
  371. except: # pylint: disable=bare-except
  372. pass
  373. if self.source is None:
  374. cmd = ['clone', self.vid, self._vid_snap]
  375. else:
  376. cmd = ['clone', str(self.source), self._vid_snap]
  377. qubes_lvm(cmd, self.log)
  378. def start(self):
  379. try:
  380. if self.snap_on_start or self.save_on_stop:
  381. if not self.save_on_stop or not self.is_dirty():
  382. self._snapshot()
  383. else:
  384. self._reset()
  385. finally:
  386. reset_cache()
  387. return self
  388. def stop(self):
  389. try:
  390. if self.save_on_stop:
  391. self._commit()
  392. if self.snap_on_start or self.save_on_stop:
  393. cmd = ['remove', self._vid_snap]
  394. qubes_lvm(cmd, self.log)
  395. else:
  396. cmd = ['remove', self.vid]
  397. qubes_lvm(cmd, self.log)
  398. finally:
  399. reset_cache()
  400. return self
  401. def verify(self):
  402. ''' Verifies the volume. '''
  403. if not self.save_on_stop and not self.snap_on_start:
  404. # volatile volumes don't need any files
  405. return True
  406. if self.source is not None:
  407. vid = str(self.source)
  408. else:
  409. vid = self.vid
  410. try:
  411. vol_info = size_cache[vid]
  412. if vol_info['attr'][4] != 'a':
  413. raise qubes.storage.StoragePoolException(
  414. 'volume {} not active'.format(vid))
  415. except KeyError:
  416. raise qubes.storage.StoragePoolException(
  417. 'volume {} missing'.format(vid))
  418. def block_device(self):
  419. ''' Return :py:class:`qubes.storage.BlockDevice` for serialization in
  420. the libvirt XML template as <disk>.
  421. '''
  422. if self.snap_on_start or self.save_on_stop:
  423. return qubes.storage.BlockDevice(
  424. '/dev/' + self._vid_snap, self.name, self.script,
  425. self.rw, self.domain, self.devtype)
  426. return super(ThinVolume, self).block_device()
  427. @property
  428. def usage(self): # lvm thin usage always returns at least the same usage as
  429. # the parent
  430. try:
  431. return qubes.storage.lvm.size_cache[self.vid]['usage']
  432. except KeyError:
  433. return 0
  434. def pool_exists(pool_id):
  435. ''' Return true if pool exists '''
  436. try:
  437. vol_info = size_cache[pool_id]
  438. return vol_info['attr'][0] == 't'
  439. except KeyError:
  440. return False
  441. def qubes_lvm(cmd, log=logging.getLogger('qubes.storage.lvm')):
  442. ''' Call :program:`lvm` to execute an LVM operation '''
  443. action = cmd[0]
  444. if action == 'remove':
  445. lvm_cmd = ['lvremove', '-f', cmd[1]]
  446. elif action == 'clone':
  447. lvm_cmd = ['lvcreate', '-kn', '-ay', '-s', cmd[1], '-n', cmd[2]]
  448. elif action == 'create':
  449. lvm_cmd = ['lvcreate', '-T', cmd[1], '-kn', '-ay', '-n', cmd[2], '-V',
  450. str(cmd[3]) + 'B']
  451. elif action == 'extend':
  452. size = int(cmd[2]) / (1024 * 1024)
  453. lvm_cmd = ["lvextend", "-L%s" % size, cmd[1]]
  454. elif action == 'activate':
  455. lvm_cmd = ['lvchange', '-ay', cmd[1]]
  456. elif action == 'rename':
  457. lvm_cmd = ['lvrename', cmd[1], cmd[2]]
  458. else:
  459. raise NotImplementedError('unsupported action: ' + action)
  460. if lvm_is_very_old:
  461. # old lvm in trusty image used there does not support -k option
  462. lvm_cmd = [x for x in lvm_cmd if x != '-kn']
  463. if os.getuid() != 0:
  464. cmd = ['sudo', 'lvm'] + lvm_cmd
  465. else:
  466. cmd = ['lvm'] + lvm_cmd
  467. p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
  468. close_fds=True)
  469. out, err = p.communicate()
  470. return_code = p.returncode
  471. if out:
  472. log.debug(out)
  473. if return_code == 0 and err:
  474. log.warning(err)
  475. elif return_code != 0:
  476. assert err, "Command exited unsuccessful, but printed nothing to stderr"
  477. raise qubes.storage.StoragePoolException(err)
  478. return True
  479. def reset_cache():
  480. qubes.storage.lvm.size_cache = init_cache()