dom0/qubesutils: move online resize code to QubesVm.resize_private_img() (#421)

This assume that private.img contains ext2/3/4. For now it is true.
This commit is contained in:
Marek Marczykowski 2012-02-10 20:47:21 +01:00
parent 6b59f5c7c8
commit abcdbad209
2 changed files with 25 additions and 28 deletions

View File

@ -640,10 +640,35 @@ class QubesVm(object):
return os.path.getsize(self.private_img)
def resize_private_img(self, size):
assert size >= self.get_private_img_sz(), "Cannot shrink private.img"
f_private = open (self.private_img, "a+b")
f_private.truncate (size)
f_private.close ()
retcode = 0
if self.is_running():
# find loop device
p = subprocess.Popen (["losetup", "--associated", vm.private_img],
stdout=subprocess.PIPE)
result = p.communicate()
m = re.match(r"^(/dev/loop\d+):\s", result[0])
if m is None:
raise QubesException("ERROR: Cannot find loop device!")
loop_dev = m.group(1)
# resize loop device
subprocess.check_call(["sudo", "losetup", "--set-capacity", loop_dev])
retcode = self.run("root:while [ \"`blockdev --getsize64 /dev/xvdb`\" -lt {0} ]; do " +
"head /dev/xvdb > /dev/null; sleep 0.2; done; resize2fs /dev/xvdb".format(size_bytes), wait=True)
else:
retcode = subprocess.check_call(["sudo", "resize2fs", "-f", vm.private_img])
if retcode != 0:
raise QubesException("resize2fs failed")
# FIXME: should be outside of QubesVM?
def get_timezone(self):
clock_config = open('/etc/sysconfig/clock', "r")

View File

@ -51,42 +51,14 @@ def main():
print >> sys.stderr, "A VM with the name '{0}' does not exist in the system.".format(vmname)
exit(1)
if vm.is_running() and os.geteuid() != 0:
print >> sys.stderr, "You must be root to grow private.img on running VM."
exit(1)
size_bytes = parse_size(size)
if size_bytes < vm.get_private_img_sz():
print >> sys.stderr, "Cannot shrink private.img ({0} < {1})".format(size_bytes, vm.get_private_img_sz())
exit(1)
try:
vm.resize_private_img(size_bytes)
except (IOError, OSError, QubesException) as err:
print >> sys.stderr, "ERROR: {0}".format(err)
exit (1)
if vm.is_running():
# find loop device
p = subprocess.Popen (["losetup", "--associated", vm.private_img],
stdout=subprocess.PIPE)
result = p.communicate()
m = re.match(r"^(/dev/loop\d+):\s", result[0])
if m is None:
print >> sys.stderr, "ERROR: Cannot find loop device!"
exit(1)
loop_dev = m.group(1)
# resize loop device
retcode = subprocess.check_call(["losetup", "--set-capacity", loop_dev])
retcode = subprocess.check_call([qvm_run_path, "-uroot", "--pass-io", vmname,
"while [ \"`blockdev --getsize64 /dev/xvdb`\" -lt {0} ]; do sleep 0.2; done; resize2fs /dev/xvdb".format(size_bytes) ])
else:
retcode = subprocess.check_call(["resize2fs", "-f", vm.private_img])
exit (0)