Skip to content

Commit

Permalink
Merge pull request #26 from FyraLabs/feat/part-attr-flags
Browse files Browse the repository at this point in the history
feat: allow setting GPT partition flags
  • Loading branch information
lleyton authored May 22, 2024
2 parents 0931c1b + c71418e commit 5a23656
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 47 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ env:
dosfstools
grub2
parted
gdisk
util-linux-core
systemd-container
grub2-efi
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -291,4 +288,3 @@ jobs:
with:
name: iso-limine-x86_64
path: tests/ng/katsu-work/chroot.tar.xz

74 changes: 35 additions & 39 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ env:
dosfstools
grub2
parted
gdisk
util-linux-core
systemd-container
grub2-efi
Expand All @@ -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
54 changes: 51 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!("sgdisk -A {i}:set:{position} {disk:?}");
cmd_lib::run_cmd!(sgdisk -A $i:set:$position $disk 2>&1)?;
}
}

if part.filesystem == "efi" {
debug!("Setting esp on for efi partition");
trace!("parted -s {disk:?} set {i} esp on");
Expand Down Expand Up @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand Down Expand Up @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand Down Expand Up @@ -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 {
Expand All @@ -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<String>,
// 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<Vec<PartitionFlag>>,
/// If not specified, the partition will be created at the end of the disk (100%)
pub size: Option<ByteSize>,
/// Filesystem of the partition
pub filesystem: String,
Expand Down
2 changes: 2 additions & 0 deletions tests/ng/katsu-arm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ disk:

- label: root
type: root-arm64
flags:
- grow-fs
# size: 2.5MiB
filesystem: ext4
mountpoint: /
Expand Down
2 changes: 2 additions & 0 deletions tests/ng/katsu.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ disk:

- label: root
type: root
flags:
- grow-fs
# size: 2.5MiB
filesystem: ext4
mountpoint: /
Expand Down

0 comments on commit 5a23656

Please sign in to comment.