From dd037f4663a55612dce429047086233a30a9c4e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Thu, 31 Oct 2019 01:37:06 +0100 Subject: [PATCH] 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 --- qubes/storage/file.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/qubes/storage/file.py b/qubes/storage/file.py index b768fa51..7b12518e 100644 --- a/qubes/storage/file.py +++ b/qubes/storage/file.py @@ -28,6 +28,7 @@ import os import os.path import re import subprocess +from contextlib import suppress import qubes.storage @@ -394,6 +395,17 @@ class FileVolume(qubes.storage.Volume): iso_date = qubes.storage.isodate(seconds).split('.', 1)[0] 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 def usage(self): ''' Returns the actualy used space '''