Skip to content

T7453: Enhance raw/qcow2 image creation #972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: current
Choose a base branch
from

Conversation

Gabin-CC
Copy link

@Gabin-CC Gabin-CC commented Jun 5, 2025

Change summary

This pull request improves the reliability and robustness of the raw image generation process in raw_image.py, specifically addressing partition mapping and environment cleanup.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes)
  • Migration from an old Vyatta component to vyos-1x, please link to related PR inside obsoleted component
  • Other (please describe):

Description

Added support for kpartx to explicitly map partitions (EFI, root) from loop devices instead of relying on /dev/loopXp*, which can be unreliable.
Introduced disk_details as an attribute of BuildContext to persist partition info throughout the image creation process.
Enhanced BuildContext.exit() to unmount all mount points, remove kpartx mappings, and detach the loop device even in failure scenarios.
Fixed a potential crash when con.disk_details was missing in mount_image().
Added debug output for better observability (e.g., loop device in use, partitions mapped).
These changes improve compatibility across a variety of environments (containers, hosts with strict udev policies) and ensure a cleaner teardown process during builds.

How Has This Been Tested?

Flavor file: cloud-init.toml
packages = [
"cloud-init",
"qemu-guest-agent"
]
image_format = ["qcow2"]
disk_size = 10
[boot_settings]
console_type = "ttyS0"

Command:
sudo ./build-vyos-image --architecture amd64 --build-by "[email protected]" --reuse-iso vyos-1.5-rolling-*.iso cloud-init

Result:

  • Image builds successfully without errors.
  • QCOW2 image boots correctly in KVM/Proxmox.
  • Partition mapping via /dev/mapper/loopXp* is successful.
  • Loop devices and mount points are cleaned up after build.

Related Task(s)

T7453

Related PR(s)

Checklist:

  • I have read the CONTRIBUTING document
  • I have linked this PR to one or more Phabricator Task(s)
  • My commit headlines contain a valid Task id
  • My change requires a change to the documentation
  • I have updated the documentation accordingly

Copy link

github-actions bot commented Jun 5, 2025

👍
No issues in PR Title / Commit Title

@github-actions github-actions bot added the current VyOS rolling release label Jun 5, 2025
@Gabin-CC Gabin-CC changed the title Enhance raw/qcow2 image creation T7453: Enhance raw/qcow2 image creation Jun 5, 2025
Copy link
Member

@dmbaturin dmbaturin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image builds work as expected and the cleanup logic looks good.

Gabin-CC added 2 commits June 6, 2025 20:58
Description
This pull request introduces improvements to the raw_image.py script responsible for building raw disk images in the VyOS build process.

Main Changes
Added use of kpartx to reliably map EFI and root partitions from the loop device.
Introduced disk_details as an attribute on the BuildContext object to pass partition metadata through the image build steps.
Improved the __exit__ method for BuildContext to unmount all mount points and clean up kpartx mappings and loop devices, even in failure cases.
Fixed a crash in mount_image() when con.disk_details was not set.
Added useful debug logs for loop device usage and partition mapping.
Motivation
The previous implementation assumed partitions like /dev/loopXp3 would appear automatically, which is unreliable across some environments (especially containers or newer systems).

This PR makes the process more reliable by explicitly mapping partitions with kpartx, a tool designed for this purpose.

It also ensures proper resource cleanup by unmounting and detaching everything cleanly, preventing leaked loop devices or stale mount points.

Test Instructions

Flavor : cloud-init.toml
packages = [
  "cloud-init",
  "qemu-guest-agent"
]

image_format = ["qcow2"]
disk_size = 10

[boot_settings]
console_type = "ttyS0"

Run:

sudo ./build-vyos-image --architecture amd64 \
  --build-by "[email protected]" \
  --reuse-iso vyos-1.5-rolling-*.iso \
  cloud-init
Expected behavior:

The build completes without errors.
The .qcow2 image file is generated and bootable (e.g., in KVM or Proxmox).
Partitions are mounted correctly via /dev/mapper/loopXp*.

Signed-off-by: Gabin-CC <[email protected]>
Enhanced the raw image creation logic to dynamically detect and assign
EFI and root partitions based on the number of partitions created by kpartx.

- Supports both 2-partition and 3-partition layouts
- Adds debug output for mapped partitions
- Avoids hardcoded assumptions about partition order
- Improves resilience in cloud-init and containerized build contexts

Fixes build failure when /dev/loopXp3 is missing or not mapped properly.

Signed-off-by: Gabin-CC <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
current VyOS rolling release
Development

Successfully merging this pull request may close these issues.

2 participants