storage/file: get volume size from the actual image file size

Don't realy on a volume configuration only, it's easy to miss updating
it. Specifically, import_volume() function didn't updated the size based
on the source volume.
The size that the actual VM sees is based on the
file size, and so is the filesystem inside. Outdated size property can
lead to a data loss if the user perform an action based on a incorrect
assumption - like extending size, which actually will shrink the volume.

Fixes QubesOS/qubes-issues#4821
This commit is contained in:
Marek Marczykowski-Górecki 2019-10-31 01:37:06 +01:00
parent 598d059c57
commit dd037f4663
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724

View File

@ -28,6 +28,7 @@ import os
import os.path import os.path
import re import re
import subprocess import subprocess
from contextlib import suppress
import qubes.storage import qubes.storage
@ -394,6 +395,17 @@ class FileVolume(qubes.storage.Volume):
iso_date = qubes.storage.isodate(seconds).split('.', 1)[0] iso_date = qubes.storage.isodate(seconds).split('.', 1)[0]
return {'old': iso_date} return {'old': iso_date}
@property
def size(self):
with suppress(FileNotFoundError):
self._size = os.path.getsize(self.path)
return self._size
@size.setter
def size(self, _):
raise qubes.storage.StoragePoolException(
"You shouldn't use volume size setter, use resize method instead")
@property @property
def usage(self): def usage(self):
''' Returns the actualy used space ''' ''' Returns the actualy used space '''