Skip to content

Commit 2ef526b

Browse files
committed
fix:(disk_setup):handle empty disk in check_partition_gpt_layout_sfdisk
When checking GPT partition layout using sfdisk, handle the case where a disk has no partition table yet. Previously, this would raise an exception. Now it returns an empty list. Add unit test to cover this empty disk scenario. Fixes GH-6682 Signed-off-by: Amy Chen <xiachen@redhat.com>
1 parent 961fc84 commit 2ef526b

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

cloudinit/config/cc_disk_setup.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,11 @@ def check_partition_gpt_layout_sfdisk(device, layout):
805805
# Use sfdisk's JSON output for reliability
806806
prt_cmd = ["sfdisk", "-l", "-J", device]
807807
try:
808-
out, _err = subp.subp(prt_cmd, update_env=LANG_C_ENV)
808+
out, _err = subp.subp(prt_cmd, update_env=LANG_C_ENV, rcs=[0, 1])
809+
# Device has no partition table or other error, return empty list
810+
if not out:
811+
return []
812+
# Try to parse JSON output
809813
ptable = json.loads(out)["partitiontable"]
810814
if "partitions" in ptable:
811815
partitions = ptable["partitions"]

tests/unittests/config/test_cc_disk_setup.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,25 @@ def test_simple1_gpt(self, *args):
137137
"gpt", "/dev/xvdb1", [(100, Linux_GUID)]
138138
)
139139

140+
@mock.patch(
141+
"cloudinit.config.cc_disk_setup.subp.subp",
142+
return_value=(
143+
"",
144+
"/dev/sdb: does not contain a recognized partition table",
145+
),
146+
)
147+
def test_empty_disk_no_partition_table(self, m_subp):
148+
"""Test that empty disk (no partition table) returns empty list."""
149+
result = cc_disk_setup.check_partition_gpt_layout_sfdisk(
150+
"/dev/sdb", []
151+
)
152+
assert result == []
153+
m_subp.assert_called_once_with(
154+
["sfdisk", "-l", "-J", "/dev/sdb"],
155+
update_env={"LANG": "C"},
156+
rcs=[0, 1],
157+
)
158+
140159

141160
class TestUpdateFsSetupDevices:
142161
def test_regression_1634678(self):

0 commit comments

Comments
 (0)