Skip to content

Commit

Permalink
feat: Lvm support
Browse files Browse the repository at this point in the history
  • Loading branch information
matbme authored Nov 4, 2023
1 parent 0603ba5 commit e07546c
Show file tree
Hide file tree
Showing 15 changed files with 1,966 additions and 97 deletions.
21 changes: 16 additions & 5 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build
name: Build and Test

on:
push:
Expand All @@ -7,11 +7,10 @@ on:
branches: [ "main" ]

jobs:

build:
runs-on: ubuntu-latest
container: ghcr.io/vanilla-os/pico:main

steps:
- uses: actions/checkout@v4

Expand All @@ -23,10 +22,22 @@ jobs:
- name: Install build dependencies
run: |
apt-get update
apt-get install -y gcc pkg-config libbtrfs-dev libdevmapper-dev libgpgme-dev
apt-get install -y gcc pkg-config libbtrfs-dev libdevmapper-dev libgpgme-dev lvm2
- name: Build
run: go build -v ./...

test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Install test dependencies
run: |
curl -s https://raw.githubusercontent.com/89luca89/distrobox/main/install | sudo sh
./utils/create_test_env.sh
- name: Test
run: go test -v ./...
run: |
distrobox enter -r albius_test -- sudo go test -v ./...
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.buildconfig
albius

.vscode/
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ Some system dependencies are required for building Albius:
- `libbtrfs-dev`
- `libdevmapper-dev`
- `libgpgme-dev`
- `lvm2`
- A C compiler supported by CGo (`gcc`, for example)

After the packages listed above have been installed, all that's left to do is
run:
Expand All @@ -141,6 +143,22 @@ run:
$ go build
```

## Testing

Writing unit tests for new functionality is encouraged to make sure everything
still works. All tests are executed in a loop device created before the
tests are started. To execute the test suite, simply run:

```sh
$ sudo go test ./... -v
```

Sudo is necessary in order to mount the loop device, as well as run some of the
operations. If you're in an immutable base distribution like Vanilla OS and wish
to run the tests in an isolated environment, keep in mind that **the tests need
to be executed in a rootful container**. You can find a script in `utils/create_test_env.sh`
that automatically sets up a container for running Albius by using Distrobox.

## Running

Albius accepts only one positional argument, which is the path for the recipe
Expand Down
114 changes: 114 additions & 0 deletions RECIPE.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,120 @@ Same as `format` but encrypts the partition with LUKS2.
- *FsType* (`string`): The filesystem for the partition. Can be either `btrfs`, `ext[2,3,4]`, `linux-swap`, `ntfs`\*, `reiserfs`\*, `udf`\*, or `xfs`\*.
- *Password* (`string`): The password used to encrypt the partition.

### pvcreate

Creates a new LVM physical volume from a partition.

**Accepts**:
- *Partition* (`string`): The partition to use as PV.

### pvresize

Resizes an LVM physical volume.

**Accepts**:
- *PV* (`string`): The physical volume path.
- *Size* (optional `float`): The PV's desired size in MiB. If not provided, the PV will expand to the size of the underlying partition.

### pvremove

Remove LVM labels from a partition.

**Accepts**:
- *PV* (`string`): The physical volume path.

### vgcreate

Creates a new LVM volume group.

**Accepts**:
- *Name* (`string`): The VG name.
- *PVs* (optional `[string]`): List containing paths for PVs to add to the newly created VG.

### vgrename

Renames an LVM volume group.

**Accepts**:
- *OldName* (`string`): The VG's current name.
- *NewName* (`string`): The VG's new name.

### vgextend

Adds PVs to an LVM volume group.

**Accepts**:
- *Name* (`string`): The target VG's name.
- *PVs* (`[string]`): A list containing the paths of the PVs to be included.

### vgreduce

Removes PVs to an LVM volume group.

**Accepts**:
- *Name* (`string`): The target VG's name.
- *PVs* (`[string]`): A list containing the paths of the PVs to be removed.

### vgremove

Deletes LVM volume group.

**Accepts**:
- *Name* (`string`): The volume group name.

### lvcreate

Create LVM logical volume.

**Accepts**:
- *Name* (`string`): Logical volume name.
- *VG* (`string`): Volume group name.
- *Type* (`string`): Logical volume type. See lvcreate(8) for available types. If unsure, use `linear`.
- *Size* (`float` or `string`): Logical volume size in MiB or a string containing a relative size (e.g. "100%FREE").

### lvrename

Renames an LVM logical volume.

**Accepts**:
- *OldName* (`string`): The LV's current name.
- *NewName* (`string`): The LV's new name.
- *VG* (`string`): Volume group the LV belongs to.

### lvremove

Deletes LVM logical volume.

**Accepts**:
- *Name* (`string`): The logical volume name.

### make-thin-pool

Creates a new LVM thin pool from two LVs: one for metadata and another one for the data itself.

**Accepts**:
- *ThinDataLV* (`string`): The LV for storing data (in format `vg_name/lv_name`).
- *ThinMetaLV* (`string`): The LV for storing pool metadata (in format `vg_name/lv_name`).

### lvcreate-thin

Same as `lvcreate`, but creates a thin LV instead.

**Accepts**:
- *Name* (`string`): Thin logical volume name.
- *VG* (`string`): Volume group name.
- *Size* (`float`): Volume group size in MiB.
- *Thinpool* (`string`): Name of the thin pool to create the LV from.

### lvm-format

Same as `format`, but formats an LVM logical volume.

**Accepts**:
- *Name* (`string`): Thin logical volume name (in format `vg_name/lv_name`).
- *FsType* (`string`): The filesystem for the partition. Can be either `btrfs`, `ext[2,3,4]`, `linux-swap`, `ntfs`\*, `reiserfs`\*, `udf`\*, or `xfs`\*.
- *Label* (optional `string`): An optional filesystem label. If not given, no label will be set.

---

## Post-Installation
Expand Down
19 changes: 13 additions & 6 deletions core/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ func (disk *Disk) LabelDisk(label DiskLabel) error {
// If fsType is an empty string, the function will skip creating the filesystem.
// This can be useful when creating LUKS-encrypted partitions, where the format
// operation needs to be executed first.
func (target *Disk) NewPartition(name string, fsType PartitionFs, start, end int64) (*Partition, error) {
createPartCmd := "parted -s %s unit MiB mkpart%s \"%s\" %s %d %s"
func (target *Disk) NewPartition(name string, fsType PartitionFs, start, end int) (*Partition, error) {
createPartCmd := "parted -s %s unit MiB mkpart%s%s %s %d %s"

var partType string
if target.Label == MSDOS {
Expand All @@ -163,7 +163,12 @@ func (target *Disk) NewPartition(name string, fsType PartitionFs, start, end int
endStr = fmt.Sprint(end)
}

err := RunCommand(fmt.Sprintf(createPartCmd, target.Path, partType, name, fsType, start, endStr))
partName := ""
if name != "" {
partName = fmt.Sprintf(" \"%s\"", name)
}

err := RunCommand(fmt.Sprintf(createPartCmd, target.Path, partType, partName, fsType, start, endStr))
if err != nil {
return nil, fmt.Errorf("Failed to create partition: %s", err)
}
Expand Down Expand Up @@ -192,9 +197,11 @@ func (target *Disk) NewPartition(name string, fsType PartitionFs, start, end int
}
}

err = newPartition.NamePartition(name)
if err != nil {
return nil, err
if name != "" {
err = newPartition.NamePartition(name)
if err != nil {
return nil, err
}
}

return newPartition, nil
Expand Down
Loading

0 comments on commit e07546c

Please sign in to comment.