Skip to content

Commit 729a424

Browse files
AASTHA RAWATLiliDeng
authored andcommitted
hyperv: expand root partition
1 parent 9044fb4 commit 729a424

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

lisa/sut_orchestrator/hyperv/platform_.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Copyright (c) Microsoft Corporation.
22
# Licensed under the MIT license.
3+
import re
34
from functools import partial
45
from pathlib import PurePath
56
from typing import Any, List, Optional, Type, cast
@@ -8,7 +9,7 @@
89
from lisa.environment import Environment
910
from lisa.node import RemoteNode
1011
from lisa.platform_ import Platform
11-
from lisa.tools import Cp, HyperV, Mkdir, PowerShell
12+
from lisa.tools import Cp, HyperV, Mkdir, Mount, PowerShell
1213
from lisa.util import LisaException, constants
1314
from lisa.util.logger import Logger, get_logger
1415
from lisa.util.parallel import run_in_parallel
@@ -313,6 +314,9 @@ def _deploy_environment(self, environment: Environment, log: Logger) -> None:
313314
node.set_connection_info(
314315
address=ip_addr, username=username, password=password
315316
)
317+
# In some cases, we observe that resize vhd resizes the entire disk
318+
# but fails to expand the partition size.
319+
self._expand_root_partition(node)
316320

317321
def _resize_vhd_if_needed(
318322
self, vhd_path: PurePath, node_runbook: HypervNodeSchema
@@ -325,6 +329,31 @@ def _resize_vhd_if_needed(
325329
f"-SizeBytes {node_runbook.osdisk_size_in_gb * 1024 * 1024 * 1024}"
326330
)
327331

332+
def _expand_root_partition(self, node: RemoteNode) -> None:
333+
# Get the root partition info
334+
# The root partition is the one that has the mount point "/"
335+
# sample root partition info: name: /dev/sda2, disk: sda,
336+
# mount_point: /, type: ext4, options: ('rw', 'relatime')
337+
root_partition = node.tools[Mount].get_partition_info("/")[0]
338+
device_name = root_partition.name
339+
partition = root_partition.disk
340+
# for root partition name: /dev/sda2, partition is "sda" and
341+
# we need to extract the partition number i.e. 2
342+
root_part_num = re.findall(r"\d+", device_name)[0]
343+
# Grow the partition and resize the filesystem
344+
cmd_result = node.execute(
345+
f"growpart /dev/{partition} {root_part_num}", sudo=True
346+
)
347+
348+
# In case the partition is already expanded to full disk size, the
349+
# command will print "NOCHANGE: partition 2 is size <size>. it cannot
350+
# be grown". In this case, it returns exit code 1 which we can ignore
351+
if cmd_result.exit_code != 0:
352+
if "NOCHANGE" in cmd_result.stdout:
353+
return
354+
raise LisaException(f"Failed to grow partition: {cmd_result.stdout}")
355+
node.execute(f"resize2fs {device_name}", sudo=True, expected_exit_code=0)
356+
328357
def _delete_environment(self, environment: Environment, log: Logger) -> None:
329358
self._delete_nodes(environment, log)
330359

0 commit comments

Comments
 (0)