Skip to content

Commit

Permalink
hyperv: expand root partition
Browse files Browse the repository at this point in the history
  • Loading branch information
AASTHA RAWAT authored and LiliDeng committed Jan 21, 2025
1 parent 9044fb4 commit 729a424
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion lisa/sut_orchestrator/hyperv/platform_.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.
import re
from functools import partial
from pathlib import PurePath
from typing import Any, List, Optional, Type, cast
Expand All @@ -8,7 +9,7 @@
from lisa.environment import Environment
from lisa.node import RemoteNode
from lisa.platform_ import Platform
from lisa.tools import Cp, HyperV, Mkdir, PowerShell
from lisa.tools import Cp, HyperV, Mkdir, Mount, PowerShell
from lisa.util import LisaException, constants
from lisa.util.logger import Logger, get_logger
from lisa.util.parallel import run_in_parallel
Expand Down Expand Up @@ -313,6 +314,9 @@ def _deploy_environment(self, environment: Environment, log: Logger) -> None:
node.set_connection_info(
address=ip_addr, username=username, password=password
)
# In some cases, we observe that resize vhd resizes the entire disk
# but fails to expand the partition size.
self._expand_root_partition(node)

def _resize_vhd_if_needed(
self, vhd_path: PurePath, node_runbook: HypervNodeSchema
Expand All @@ -325,6 +329,31 @@ def _resize_vhd_if_needed(
f"-SizeBytes {node_runbook.osdisk_size_in_gb * 1024 * 1024 * 1024}"
)

def _expand_root_partition(self, node: RemoteNode) -> None:
# Get the root partition info
# The root partition is the one that has the mount point "/"
# sample root partition info: name: /dev/sda2, disk: sda,
# mount_point: /, type: ext4, options: ('rw', 'relatime')
root_partition = node.tools[Mount].get_partition_info("/")[0]
device_name = root_partition.name
partition = root_partition.disk
# for root partition name: /dev/sda2, partition is "sda" and
# we need to extract the partition number i.e. 2
root_part_num = re.findall(r"\d+", device_name)[0]
# Grow the partition and resize the filesystem
cmd_result = node.execute(
f"growpart /dev/{partition} {root_part_num}", sudo=True
)

# In case the partition is already expanded to full disk size, the
# command will print "NOCHANGE: partition 2 is size <size>. it cannot
# be grown". In this case, it returns exit code 1 which we can ignore
if cmd_result.exit_code != 0:
if "NOCHANGE" in cmd_result.stdout:
return
raise LisaException(f"Failed to grow partition: {cmd_result.stdout}")
node.execute(f"resize2fs {device_name}", sudo=True, expected_exit_code=0)

def _delete_environment(self, environment: Environment, log: Logger) -> None:
self._delete_nodes(environment, log)

Expand Down

0 comments on commit 729a424

Please sign in to comment.