1
1
# Copyright (c) Microsoft Corporation.
2
2
# Licensed under the MIT license.
3
+ import re
3
4
from functools import partial
4
5
from pathlib import PurePath
5
6
from typing import Any , List , Optional , Type , cast
8
9
from lisa .environment import Environment
9
10
from lisa .node import RemoteNode
10
11
from lisa .platform_ import Platform
11
- from lisa .tools import Cp , HyperV , Mkdir , PowerShell
12
+ from lisa .tools import Cp , HyperV , Mkdir , Mount , PowerShell
12
13
from lisa .util import LisaException , constants
13
14
from lisa .util .logger import Logger , get_logger
14
15
from lisa .util .parallel import run_in_parallel
@@ -313,6 +314,9 @@ def _deploy_environment(self, environment: Environment, log: Logger) -> None:
313
314
node .set_connection_info (
314
315
address = ip_addr , username = username , password = password
315
316
)
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 )
316
320
317
321
def _resize_vhd_if_needed (
318
322
self , vhd_path : PurePath , node_runbook : HypervNodeSchema
@@ -325,6 +329,31 @@ def _resize_vhd_if_needed(
325
329
f"-SizeBytes { node_runbook .osdisk_size_in_gb * 1024 * 1024 * 1024 } "
326
330
)
327
331
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
+
328
357
def _delete_environment (self , environment : Environment , log : Logger ) -> None :
329
358
self ._delete_nodes (environment , log )
330
359
0 commit comments