|
4 | 4 |
|
5 | 5 | import logging |
6 | 6 | import os |
| 7 | +import re |
7 | 8 | import subprocess |
8 | 9 | import tempfile |
9 | 10 | import uuid |
|
12 | 13 | import lib.efi as efi |
13 | 14 | from lib.basevm import BaseVM |
14 | 15 | from lib.common import ( |
| 16 | + KiB, |
15 | 17 | PackageManagerEnum, |
16 | 18 | expand_scope_relative_nodeid, |
17 | 19 | parse_xe_dict, |
@@ -506,6 +508,30 @@ def detect_package_manager(self) -> PackageManagerEnum: |
506 | 508 | return PackageManagerEnum.APK |
507 | 509 | return PackageManagerEnum.UNKNOWN |
508 | 510 |
|
| 511 | + def grow_root_partition(self) -> int | None: |
| 512 | + if self.detect_package_manager() == PackageManagerEnum.APK: |
| 513 | + # growpart is not available in alpine 3.12 |
| 514 | + # vm.ssh('apk add cloud-utils-growpart e2fsprogs-extra') |
| 515 | + self.ssh('apk add gawk util-linux e2fsprogs-extra') |
| 516 | + self.ssh( |
| 517 | + 'wget https://raw.githubusercontent.com/canonical/cloud-utils/main/bin/growpart -O /usr/bin/growpart' |
| 518 | + ) |
| 519 | + self.ssh('chmod +x /usr/bin/growpart') |
| 520 | + else: |
| 521 | + return None |
| 522 | + mount_output = self.ssh('mount').strip() |
| 523 | + root_match = re.search(r'/dev/(\w+?)(p?)(\d+) on / type (\w+)', mount_output) |
| 524 | + assert root_match is not None |
| 525 | + disk, p, partition, typ = root_match.groups() |
| 526 | + if not typ.startswith('ext'): |
| 527 | + logging.debug(f"Unsupported filesystem: {typ}") |
| 528 | + return None |
| 529 | + growpart_returncode = self.ssh_with_result(f'growpart /dev/{disk} {partition}').returncode |
| 530 | + assert growpart_returncode in [0, 1] # growpart returns 1 if the size is already the expected one |
| 531 | + self.ssh(f'resize2fs /dev/{disk}{p}{partition}') |
| 532 | + df_output = self.ssh('df /') |
| 533 | + return int(df_output.splitlines()[-1].split()[3]) * KiB |
| 534 | + |
509 | 535 | def insert_cd(self, vdi_name: str) -> None: |
510 | 536 | logging.info("Insert CD %r in VM %s", vdi_name, self.uuid) |
511 | 537 | self.host.xe('vm-cd-insert', {'uuid': self.uuid, 'cd-name': vdi_name}) |
|
0 commit comments