From 66c6af46dd6d79357e86b1d41b7ac087df62dde7 Mon Sep 17 00:00:00 2001 From: Lleyton Gray Date: Wed, 22 May 2024 00:07:07 -0700 Subject: [PATCH 1/4] feat: allow setting GPT partition flags --- src/config.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/src/config.rs b/src/config.rs index 37686d8..ef16175 100644 --- a/src/config.rs +++ b/src/config.rs @@ -458,6 +458,16 @@ impl PartitionLayout { trace!("parted -s {disk:?} type {i} {part_type_uuid}"); cmd_lib::run_cmd!(parted -s $disk type $i $part_type_uuid 2>&1)?; + if let Some(flags) = &part.flags { + debug!("Setting partition attribute flags"); + + for flag in flags { + let position = flag.flag_position(); + trace!("parted -s {disk:?} toggle {i} {position}"); + cmd_lib::run_cmd!(parted -s $disk toggle $i $position 2>&1)?; + } + } + if part.filesystem == "efi" { debug!("Setting esp on for efi partition"); trace!("parted -s {disk:?} set {i} esp on"); @@ -514,6 +524,7 @@ fn test_partlay() { partlay.add_partition(Partition { label: Some("EFI".to_string()), partition_type: PartitionType::Esp, + flags: None, size: Some(ByteSize::mib(100)), filesystem: "efi".to_string(), mountpoint: "/boot/efi".to_string(), @@ -523,6 +534,7 @@ fn test_partlay() { partlay.add_partition(Partition { label: Some("boot".to_string()), partition_type: PartitionType::Xbootldr, + flags: None, size: Some(ByteSize::gib(100)), filesystem: "ext4".to_string(), mountpoint: "/boot".to_string(), @@ -532,6 +544,7 @@ fn test_partlay() { partlay.add_partition(Partition { label: Some("ROOT".to_string()), partition_type: PartitionType::Root, + flags: None, size: Some(ByteSize::gib(100)), filesystem: "ext4".to_string(), mountpoint: "/".to_string(), @@ -569,6 +582,7 @@ fn test_partlay() { Partition { label: Some("ROOT".to_string()), partition_type: PartitionType::Root, + flags: None, size: Some(ByteSize::gib(100)), filesystem: "ext4".to_string(), mountpoint: "/".to_string(), @@ -580,6 +594,7 @@ fn test_partlay() { Partition { label: Some("boot".to_string()), partition_type: PartitionType::Xbootldr, + flags: None, size: Some(ByteSize::gib(100)), filesystem: "ext4".to_string(), mountpoint: "/boot".to_string(), @@ -591,6 +606,7 @@ fn test_partlay() { Partition { label: Some("EFI".to_string()), partition_type: PartitionType::Esp, + flags: None, size: Some(ByteSize::mib(100)), filesystem: "efi".to_string(), mountpoint: "/boot/efi".to_string(), @@ -634,7 +650,7 @@ pub enum PartitionType { } impl PartitionType { - /// Get the GPT parition type GUID + /// Get the GPT partition type GUID fn uuid(&self, target_arch: &str) -> String { // https://uapi-group.org/specifications/specs/discoverable_partitions_specification/#partition-names match self { @@ -657,13 +673,45 @@ impl PartitionType { } } +/// Represents GPT partition attrbite flags which can be used, from https://uapi-group.org/specifications/specs/discoverable_partitions_specification/#partition-attribute-flags. +#[derive(Deserialize, Debug, Clone, Serialize, PartialEq, Eq)] +#[serde(rename_all = "kebab-case")] +pub enum PartitionFlag { + /// Disable auto discovery for the partition, preventing automatic mounting + NoAuto, + /// Mark partition for mounting as read-only + ReadOnly, + /// Enable automatically growing the underlying file system when mounted + GrowFs, + /// An arbitrary GPT attribute flag position, 0 - 63 + #[serde(untagged)] + FlagPosition(u8), +} + +impl PartitionFlag { + /// Get the position offset for this flag + fn flag_position(&self) -> u8 { + // https://uapi-group.org/specifications/specs/discoverable_partitions_specification/#partition-attribute-flags + match &self { + PartitionFlag::NoAuto => 63, + PartitionFlag::ReadOnly => 60, + PartitionFlag::GrowFs => 59, + PartitionFlag::FlagPosition(position @ 0..=63) => *position, + _ => unimplemented!(), + } + } +} + #[derive(Deserialize, Debug, Clone, Serialize, PartialEq, Eq)] pub struct Partition { pub label: Option, - // Partition type + /// Partition type #[serde(rename = "type")] pub partition_type: PartitionType, - // If not specified, the partition will be created at the end of the disk (100%) + /// GPT partition attribute flags to add + // todo: maybe represent this as a bitflag number, parted consumes the positions so I'm doing this for now + pub flags: Option>, + /// If not specified, the partition will be created at the end of the disk (100%) pub size: Option, /// Filesystem of the partition pub filesystem: String, From 60aca35014f1220d1abb620f29c25c8c4de248c8 Mon Sep 17 00:00:00 2001 From: Lleyton Gray Date: Wed, 22 May 2024 00:09:01 -0700 Subject: [PATCH 2/4] tests: update image test cases with grow-fs flag --- tests/ng/katsu-arm.yaml | 2 ++ tests/ng/katsu.yaml | 2 ++ 2 files changed, 4 insertions(+) diff --git a/tests/ng/katsu-arm.yaml b/tests/ng/katsu-arm.yaml index e01aee2..e1a88da 100644 --- a/tests/ng/katsu-arm.yaml +++ b/tests/ng/katsu-arm.yaml @@ -21,6 +21,8 @@ disk: - label: root type: root-arm64 + flags: + - grow-fs # size: 2.5MiB filesystem: ext4 mountpoint: / diff --git a/tests/ng/katsu.yaml b/tests/ng/katsu.yaml index cb6e3f7..db3c402 100644 --- a/tests/ng/katsu.yaml +++ b/tests/ng/katsu.yaml @@ -28,6 +28,8 @@ disk: - label: root type: root + flags: + - grow-fs # size: 2.5MiB filesystem: ext4 mountpoint: / From 172d3f3ae3a559fd06b18a8611f4b5bce2b45f9d Mon Sep 17 00:00:00 2001 From: Lleyton Gray Date: Wed, 22 May 2024 00:17:04 -0700 Subject: [PATCH 3/4] fix: use gdisk for setting partition attribute flags --- .github/workflows/release.yml | 74 +++++++++++++++++------------------ src/config.rs | 4 +- 2 files changed, 37 insertions(+), 41 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index c13f7b3..93789b1 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -16,6 +16,7 @@ env: dosfstools grub2 parted + gdisk util-linux-core systemd-container grub2-efi @@ -38,50 +39,45 @@ env: isomd5sum jobs: - build: - strategy: - matrix: - arch: [x86_64, aarch64] + build: + strategy: + matrix: + arch: [x86_64, aarch64] - # run job on ubuntu-latest unless aarch64 then arm64 - runs-on: ${{ matrix.arch == 'aarch64' && 'arm64' || 'ubuntu-latest' }} + # run job on ubuntu-latest unless aarch64 then arm64 + runs-on: ${{ matrix.arch == 'aarch64' && 'arm64' || 'ubuntu-latest' }} - container: - image: ghcr.io/terrapkg/builder:f38 + container: + image: ghcr.io/terrapkg/builder:f38 - steps: - - uses: actions/checkout@v4 + steps: + - uses: actions/checkout@v4 - - name: Install dependencies - run: | - dnf install -y $DNF_PKGS + - name: Install dependencies + run: | + dnf install -y $DNF_PKGS - - uses: actions-rs/toolchain@v1 - with: - profile: minimal - toolchain: stable - override: true - - - name: Build - run: - cargo build --release - - - name: Upload artifacts - uses: actions/upload-artifact@v2 - with: - name: ${{ matrix.arch }} - path: target/release/terra - - - - name: Add binary to release - uses: svenstaro/upload-release-action@v2 - with: - repo_token: ${{ secrets.GITHUB_TOKEN }} - file: target/release/katsu - asset_name: katsu-${{ matrix.arch }} - tag: ${{ github.ref }} - # release_name: ${{ github.ref }} - overwrite: true + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + - name: Build + run: cargo build --release + - name: Upload artifacts + uses: actions/upload-artifact@v2 + with: + name: ${{ matrix.arch }} + path: target/release/terra + - name: Add binary to release + uses: svenstaro/upload-release-action@v2 + with: + repo_token: ${{ secrets.GITHUB_TOKEN }} + file: target/release/katsu + asset_name: katsu-${{ matrix.arch }} + tag: ${{ github.ref }} + # release_name: ${{ github.ref }} + overwrite: true diff --git a/src/config.rs b/src/config.rs index ef16175..ba7d451 100644 --- a/src/config.rs +++ b/src/config.rs @@ -463,8 +463,8 @@ impl PartitionLayout { for flag in flags { let position = flag.flag_position(); - trace!("parted -s {disk:?} toggle {i} {position}"); - cmd_lib::run_cmd!(parted -s $disk toggle $i $position 2>&1)?; + trace!("sgdisk -A {i}:set:{position} {disk:?}"); + cmd_lib::run_cmd!(sgdisk -A $i:set:$position $disk 2>&1)?; } } From c71418e700582083188c7090461ab4ed979616fa Mon Sep 17 00:00:00 2001 From: Lleyton Gray Date: Wed, 22 May 2024 00:20:50 -0700 Subject: [PATCH 4/4] fix: add gdisk to test ci --- .github/workflows/build-test.yml | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml index 68e56da..e406b93 100644 --- a/.github/workflows/build-test.yml +++ b/.github/workflows/build-test.yml @@ -12,6 +12,7 @@ env: dosfstools grub2 parted + gdisk util-linux-core systemd-container grub2-efi @@ -138,7 +139,6 @@ jobs: name: image-x86_64 path: tests/ng/katsu-work/image/*.raw.xz - iso-x86_64: runs-on: ubuntu-latest container: @@ -189,7 +189,6 @@ jobs: name: iso-x86_64 path: tests/ng/katsu-work/image/*.iso.xz - iso-limine-x86_64: runs-on: ubuntu-latest container: @@ -240,8 +239,6 @@ jobs: name: iso-limine-x86_64 path: tests/ng/katsu-work/image/*.iso.xz - - fs-test-x86_64: runs-on: ubuntu-latest container: @@ -291,4 +288,3 @@ jobs: with: name: iso-limine-x86_64 path: tests/ng/katsu-work/chroot.tar.xz -