canonical / cloud-init

Official upstream for the cloud-init: cloud instance initialization
https://cloud-init.io/
Other
3.01k stars 887 forks source link

[enhancement]: disk_setup support for gpt partitions when sgdisk util is not present #5797

Open blackboxsw opened 1 month ago

blackboxsw commented 1 month ago

Enhancement

Allow disk_setup of gpt partitions in minimal images where sgdisk utility is not present. Fallback to sfdisk if necessary for handling GPT partition inspection and creation.

Steps to reproduce

  1. Launch an ubuntu cloud image, with sgdisk removed , providing disk_setup: in cloud-config which defines a gpt partition table type
  2. Expect tracebacks on inability to find sgdisk file or directory

test procedure

  1. apply the following diff to tip of main

    diff --git a/tests/integration_tests/conftest.py b/tests/integration_tests/conftest.py
    index b62dae82a..de922735d 100644
    --- a/tests/integration_tests/conftest.py
    +++ b/tests/integration_tests/conftest.py
    @@ -136,6 +136,7 @@ def setup_image(session_cloud: IntegrationCloud, request):
     if source.installs_new_version():
         log.info("Installing cloud-init from %s", source.name)
         client.install_new_cloud_init(source)
    +    client.execute("rm /usr/sbin/sgdisk")
     if (
         integration_settings.INCLUDE_PROFILE
         and integration_settings.INCLUDE_COVERAGE
    diff --git a/tests/integration_tests/modules/test_disk_setup.py b/tests/integration_tests/modules/test_disk_setup.py
    index 27a70d32f..8f8f8b3ec 100644
    --- a/tests/integration_tests/modules/test_disk_setup.py
    +++ b/tests/integration_tests/modules/test_disk_setup.py
    @@ -36,7 +36,7 @@ device_aliases:
    my_alias: /dev/sdb
    disk_setup:
    my_alias:
    -    table_type: mbr
    +    table_type: gpt
     layout: [50, 50]
     overwrite: True
    fs_setup:
  2. run integration test, keeping the instance around after test completion

    CLOUD_INIT_KEEP_INSTANCE=1 CLOUD_INIT_PLATFORM=lxd_vm CLOUD_INIT_CLOUD_INIT_SOURCE=ppa:cloud-init-dev/daily CLOUD_INIT_OS_IMAGE=jammy tox -e integration-tests -- tests/integration_tests/modules/test_disk_setup.py::TestDeviceAliases
  3. See warnings about disk setup failing due to missing utilities:

    root@cloudinit-1008-135436hv703bwv:~# cloud-init status --format=yaml
    ---
    _schema_version: '1'
    boot_status_code: enabled-by-generator
    datasource: lxd
    detail: DataSourceLXD
    errors: []
    extended_status: degraded done
    init:
    errors: []
    finished: 3.98
    recoverable_errors:
        WARNING:
        - 'Failed partitioning operation
    
            Error running partition command on /dev/sdb
    
            Unexpected error while running command.
    
            Command: [''sgdisk'', ''-p'', ''/dev/sdb'']
    
            Exit code: -
    
            Reason: [Errno 2] No such file or directory: b''sgdisk''
    
            Stdout: -
    
            Stderr: -'
        - Path %s does not exist or is not a block device
        - Path %s does not exist or is not a block device
    start: 3.31
    init-local:
    errors: []
    finished: 2.05
    recoverable_errors: {}
    start: 1.94
    last_update: Thu, 01 Jan 1970 00:00:06 +0000
    modules-config:
    errors: []
    finished: 5.8
    recoverable_errors: {}
    start: 5.55
    modules-final:
    errors: []
    finished: 6.04
    recoverable_errors: {}
    start: 5.97
    recoverable_errors:
    WARNING:
    - 'Failed partitioning operation
    
        Error running partition command on /dev/sdb
    
        Unexpected error while running command.
    
        Command: [''sgdisk'', ''-p'', ''/dev/sdb'']
    
        Exit code: -
    
        Reason: [Errno 2] No such file or directory: b''sgdisk''
    
        Stdout: -
    
        Stderr: -'
    - Path %s does not exist or is not a block device
    - Path %s does not exist or is not a block device