mirror of
https://github.com/kairos-io/immucore.git
synced 2025-04-27 02:50:57 +00:00
🐛 Add custom layout plugin (#87)
The default plugin does not support gpt Signed-off-by: Itxaka <itxaka.garcia@spectrocloud.com>
This commit is contained in:
parent
88a2ca8a9b
commit
b2f3615534
36
go.mod
36
go.mod
@ -6,7 +6,7 @@ require (
|
||||
github.com/containerd/containerd v1.6.19
|
||||
github.com/deniswernert/go-fstab v0.0.0-20141204152952-eb4090f26517
|
||||
github.com/hashicorp/go-multierror v1.1.1
|
||||
github.com/jaypipes/ghw v0.9.0 // 0.9.1 is broken in the partition identification, watch out
|
||||
github.com/jaypipes/ghw v0.10.0
|
||||
github.com/joho/godotenv v1.5.1
|
||||
github.com/kairos-io/kairos v1.5.0
|
||||
github.com/moby/sys/mountinfo v0.6.2
|
||||
@ -19,10 +19,15 @@ require (
|
||||
github.com/spectrocloud-labs/herd v0.4.2
|
||||
github.com/twpayne/go-vfs v1.7.2
|
||||
github.com/urfave/cli/v2 v2.25.0
|
||||
golang.org/x/sys v0.5.0
|
||||
golang.org/x/sys v0.6.0
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/rancher/elemental-cli v0.2.1
|
||||
github.com/sirupsen/logrus v1.8.1
|
||||
)
|
||||
|
||||
require (
|
||||
atomicgo.dev/cursor v0.1.1 // indirect
|
||||
atomicgo.dev/keyboard v0.2.9 // indirect
|
||||
@ -32,17 +37,23 @@ require (
|
||||
github.com/Microsoft/go-winio v0.5.2 // indirect
|
||||
github.com/Microsoft/hcsshim v0.9.7 // indirect
|
||||
github.com/ProtonMail/go-crypto v0.0.0-20220623141421-5afb4c282135 // indirect
|
||||
github.com/Sabayon/pkgs-checker v0.8.4 // indirect
|
||||
github.com/StackExchange/wmi v1.2.1 // indirect
|
||||
github.com/acomagu/bufpipe v1.0.3 // indirect
|
||||
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59 // indirect
|
||||
github.com/canonical/go-efilib v0.3.1-0.20220324150059-04e254148b45 // indirect
|
||||
github.com/cavaliergopher/grab v2.0.0+incompatible // indirect
|
||||
github.com/containerd/cgroups v1.0.4 // indirect
|
||||
github.com/containerd/console v1.0.3 // indirect
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
|
||||
github.com/crillab/gophersat v1.3.2-0.20210701121804-72b19f5b6b38 // indirect
|
||||
github.com/davidcassany/linuxkit/pkg/metadata v0.0.0-20220725140037-b49d0fec13b3 // indirect
|
||||
github.com/denisbrodbeck/machineid v1.0.1 // indirect
|
||||
github.com/diskfs/go-diskfs v1.2.1-0.20211109185859-9509735ba7a4 // indirect
|
||||
github.com/distribution/distribution v2.8.1+incompatible // indirect
|
||||
github.com/docker/distribution v2.8.0+incompatible // indirect
|
||||
github.com/docker/docker v20.10.17+incompatible // indirect
|
||||
github.com/docker/go-units v0.4.0 // indirect
|
||||
github.com/docker/libnetwork v0.8.0-dev.2.0.20200917202933-d0951081b35f // indirect
|
||||
github.com/emirpasic/gods v1.18.1 // indirect
|
||||
github.com/ghodss/yaml v1.0.0 // indirect
|
||||
@ -56,10 +67,12 @@ require (
|
||||
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
|
||||
github.com/google/go-cmp v0.5.9 // indirect
|
||||
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
|
||||
github.com/google/renameio v1.0.0 // indirect
|
||||
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
|
||||
github.com/google/uuid v1.3.0 // indirect
|
||||
github.com/gookit/color v1.5.2 // indirect
|
||||
github.com/hashicorp/errwrap v1.1.0 // indirect
|
||||
github.com/hashicorp/go-version v1.3.0 // indirect
|
||||
github.com/huandu/xstrings v1.3.2 // indirect
|
||||
github.com/imdario/mergo v0.3.13 // indirect
|
||||
github.com/ishidawataru/sctp v0.0.0-20210707070123-9a39160e9062 // indirect
|
||||
@ -67,20 +80,32 @@ require (
|
||||
github.com/itchyny/timefmt-go v0.1.5 // indirect
|
||||
github.com/jaypipes/pcidb v1.0.0 // indirect
|
||||
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
|
||||
github.com/jinzhu/copier v0.0.0-20180308034124-7e38e58719c3 // indirect
|
||||
github.com/kendru/darwin/go/depgraph v0.0.0-20221105232959-877d6a81060c // indirect
|
||||
github.com/kevinburke/ssh_config v1.2.0 // indirect
|
||||
github.com/knqyf263/go-deb-version v0.0.0-20190517075300-09fca494f03d // indirect
|
||||
github.com/lithammer/fuzzysearch v1.1.5 // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.17 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.14 // indirect
|
||||
github.com/mitchellh/copystructure v1.2.0 // indirect
|
||||
github.com/mitchellh/go-homedir v1.1.0 // indirect
|
||||
github.com/mitchellh/hashstructure/v2 v2.0.2 // indirect
|
||||
github.com/mitchellh/reflectwalk v1.0.2 // indirect
|
||||
github.com/moby/libnetwork v0.8.0-dev.2.0.20200612180813-9e99af28df21 // indirect
|
||||
github.com/moby/moby v20.10.9+incompatible // indirect
|
||||
github.com/moby/sys/mount v0.3.0 // indirect
|
||||
github.com/mudler/entities v0.0.0-20211108084227-d1414478861b // indirect
|
||||
github.com/mudler/luet v0.0.0-20220526130937-264bf53fe7ab // indirect
|
||||
github.com/mudler/topsort v0.0.0-20201103161459-db5c7901c290 // indirect
|
||||
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect
|
||||
github.com/opencontainers/go-digest v1.0.0 // indirect
|
||||
github.com/opencontainers/image-spec v1.0.3-0.20211202183452-c5a74bcca799 // indirect
|
||||
github.com/opencontainers/runc v1.1.2 // indirect
|
||||
github.com/otiai10/copy v1.2.1-0.20200916181228-26f84a0b1578 // indirect
|
||||
github.com/packethost/packngo v0.25.0 // indirect
|
||||
github.com/phayes/permbits v0.0.0-20190612203442-39d7c581d2ee // indirect
|
||||
github.com/philopon/go-toposort v0.0.0-20170620085441-9be86dbd762f // indirect
|
||||
github.com/pierrec/lz4 v2.6.1+incompatible // indirect
|
||||
github.com/pilebones/go-udev v0.0.0-20210126000448-a3c2a7a4afb7 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
@ -88,7 +113,6 @@ require (
|
||||
github.com/pterm/pterm v0.12.54 // indirect
|
||||
github.com/qeesung/image2ascii v1.0.1 // indirect
|
||||
github.com/rivo/uniseg v0.4.3 // indirect
|
||||
github.com/rogpeppe/go-internal v1.8.0 // indirect
|
||||
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||
github.com/samber/lo v1.37.0 // indirect
|
||||
github.com/satori/go.uuid v1.2.1-0.20180404165556-75cca531ea76 // indirect
|
||||
@ -105,19 +129,25 @@ require (
|
||||
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
|
||||
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
|
||||
github.com/zcalusic/sysinfo v0.9.5 // indirect
|
||||
github.com/zloylos/grsync v1.6.1 // indirect
|
||||
go.opencensus.io v0.23.0 // indirect
|
||||
golang.org/x/crypto v0.5.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 // indirect
|
||||
golang.org/x/mod v0.8.0 // indirect
|
||||
golang.org/x/net v0.7.0 // indirect
|
||||
golang.org/x/term v0.5.0 // indirect
|
||||
golang.org/x/text v0.7.0 // indirect
|
||||
golang.org/x/tools v0.6.0 // indirect
|
||||
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
|
||||
gopkg.in/djherbis/times.v1 v1.3.0 // indirect
|
||||
gopkg.in/ini.v1 v1.66.6 // indirect
|
||||
gopkg.in/warnings.v0 v0.1.2 // indirect
|
||||
gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
howett.net/plist v1.0.0 // indirect
|
||||
k8s.io/klog/v2 v2.30.0 // indirect
|
||||
k8s.io/mount-utils v0.23.0 // indirect
|
||||
k8s.io/utils v0.0.0-20211116205334-6203023598ed // indirect
|
||||
pault.ag/go/modprobe v0.1.2 // indirect
|
||||
pault.ag/go/topsort v0.1.1 // indirect
|
||||
)
|
||||
|
@ -34,4 +34,5 @@ const (
|
||||
OpWaitForSysroot = "wait-for-sysroot"
|
||||
PersistentStateTarget = "/usr/local/.state"
|
||||
LogDir = "/run/immucore"
|
||||
LinuxFs = "ext4"
|
||||
)
|
||||
|
@ -10,6 +10,7 @@ import (
|
||||
"github.com/mudler/yip/pkg/console"
|
||||
"github.com/mudler/yip/pkg/executor"
|
||||
"github.com/mudler/yip/pkg/logger"
|
||||
"github.com/mudler/yip/pkg/plugins"
|
||||
"github.com/mudler/yip/pkg/schema"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/twpayne/go-vfs"
|
||||
@ -19,6 +20,32 @@ import (
|
||||
func NewYipExecutor(l logger.Interface) executor.Executor {
|
||||
exec := executor.NewExecutor(
|
||||
executor.WithLogger(l),
|
||||
executor.WithConditionals(
|
||||
plugins.NodeConditional,
|
||||
plugins.IfConditional,
|
||||
),
|
||||
executor.WithPlugins(
|
||||
// Note, the plugin execution order depends on the order passed here
|
||||
plugins.DNS,
|
||||
plugins.Download,
|
||||
plugins.Git,
|
||||
plugins.Entities,
|
||||
plugins.EnsureDirectories,
|
||||
plugins.EnsureFiles,
|
||||
plugins.Commands,
|
||||
plugins.DeleteEntities,
|
||||
plugins.Hostname,
|
||||
plugins.Sysctl,
|
||||
plugins.User,
|
||||
plugins.SSH,
|
||||
plugins.LoadModules,
|
||||
plugins.Timesyncd,
|
||||
plugins.Systemctl,
|
||||
plugins.Environment,
|
||||
plugins.SystemdFirstboot,
|
||||
plugins.DataSources,
|
||||
YipLayoutPlugin,
|
||||
),
|
||||
)
|
||||
return exec
|
||||
}
|
||||
|
@ -7,8 +7,12 @@ import (
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jaypipes/ghw"
|
||||
"github.com/jaypipes/ghw/pkg/block"
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/kairos-io/kairos/pkg/utils"
|
||||
"github.com/kairos-io/kairos/sdk/state"
|
||||
)
|
||||
|
||||
@ -158,8 +162,17 @@ func GetState() string {
|
||||
switch runtime.BootState {
|
||||
case state.Active, state.Passive:
|
||||
label = filepath.Join("/dev/disk/by-label/", runtime.State.Label)
|
||||
// Workaround for ghw 0.10.0 while runtime uses the old one so it returns like everything is empty
|
||||
// https://github.com/kairos-io/kairos/pull/1073
|
||||
// This requires a bumped kairos sdk
|
||||
if runtime.State.Label == "" {
|
||||
label = "/dev/disk/by-label/COS_STATE"
|
||||
}
|
||||
case state.Recovery:
|
||||
label = filepath.Join("/dev/disk/by-label/", runtime.Recovery.Label)
|
||||
if runtime.Recovery.Label == "" {
|
||||
label = "/dev/disk/by-label/COS_RECOVERY"
|
||||
}
|
||||
}
|
||||
Log.Debug().Str("what", label).Msg("Get state label")
|
||||
return label
|
||||
@ -196,3 +209,24 @@ func GetHostProcCmdline() string {
|
||||
}
|
||||
return proc
|
||||
}
|
||||
|
||||
// GetPartByLabel will identify the device by a given label.
|
||||
func GetPartByLabel(label string, attempts int) (string, error) {
|
||||
for tries := 0; tries < attempts; tries++ {
|
||||
_, _ = utils.SH("udevadm settle")
|
||||
blockDevices, err := block.New(ghw.WithDisableTools(), ghw.WithDisableWarnings())
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for _, d := range blockDevices.Disks {
|
||||
for _, part := range d.Partitions {
|
||||
Log.Info().Interface("part", part).Str("label", label).Msg("getpartbylabel")
|
||||
if part.FilesystemLabel == label {
|
||||
return filepath.Join("/dev/", part.Name), nil
|
||||
}
|
||||
}
|
||||
}
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
return "", errors.New("no device found")
|
||||
}
|
||||
|
80
internal/utils/layout_plugin.go
Normal file
80
internal/utils/layout_plugin.go
Normal file
@ -0,0 +1,80 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/kairos-io/immucore/internal/constants"
|
||||
"github.com/mudler/yip/pkg/logger"
|
||||
"github.com/mudler/yip/pkg/plugins"
|
||||
"github.com/mudler/yip/pkg/schema"
|
||||
"github.com/rancher/elemental-cli/pkg/partitioner"
|
||||
"github.com/twpayne/go-vfs"
|
||||
)
|
||||
|
||||
// YipLayoutPlugin is the immucore implementation of Layout yip's plugin based
|
||||
// on partitioner package.
|
||||
func YipLayoutPlugin(l logger.Interface, s schema.Stage, fs vfs.FS, console plugins.Console) (err error) {
|
||||
if s.Layout.Device == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var dev *partitioner.Disk
|
||||
|
||||
if len(strings.TrimSpace(s.Layout.Device.Label)) > 0 {
|
||||
partDevice, err := GetPartByLabel(s.Layout.Device.Label, 5)
|
||||
if err != nil {
|
||||
l.Errorf("Exiting, disk not found:\n %s", err.Error())
|
||||
return err
|
||||
}
|
||||
dev = partitioner.NewDisk(
|
||||
partDevice,
|
||||
)
|
||||
} else if len(strings.TrimSpace(s.Layout.Device.Path)) > 0 {
|
||||
dev = partitioner.NewDisk(
|
||||
s.Layout.Device.Path,
|
||||
)
|
||||
} else {
|
||||
l.Warnf("No target device defined, nothing to do")
|
||||
return nil
|
||||
}
|
||||
|
||||
if !dev.Exists() {
|
||||
l.Errorf("Exiting, disk not found:\n %s", s.Layout.Device.Path)
|
||||
return errors.New("target disk not found")
|
||||
}
|
||||
|
||||
if s.Layout.Expand != nil {
|
||||
l.Infof("Extending last partition up to %d MiB", s.Layout.Expand.Size)
|
||||
out, err := dev.ExpandLastPartition(s.Layout.Expand.Size)
|
||||
if err != nil {
|
||||
l.Error(out)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for _, part := range s.Layout.Parts {
|
||||
_, err := GetPartByLabel(part.FSLabel, 1)
|
||||
if err == nil {
|
||||
l.Warnf("Partition with FSLabel: %s already exists, ignoring", part.FSLabel)
|
||||
continue
|
||||
}
|
||||
|
||||
// Set default filesystem
|
||||
if part.FileSystem == "" {
|
||||
part.FileSystem = constants.LinuxFs
|
||||
}
|
||||
|
||||
l.Infof("Creating %s partition", part.FSLabel)
|
||||
partNum, err := dev.AddPartition(part.Size, part.FileSystem, part.PLabel)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed creating partitions: %w", err)
|
||||
}
|
||||
out, err := dev.FormatPartition(partNum, part.FileSystem, part.FSLabel)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Formatting partition failed: %s\nError: %w", out, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
@ -328,10 +328,10 @@ var _ = Describe("mount utils", func() {
|
||||
Name: "device",
|
||||
Partitions: []*block.Partition{
|
||||
{
|
||||
Name: "device2",
|
||||
Label: "COS_OEM",
|
||||
Type: "ext4",
|
||||
MountPoint: "/oem",
|
||||
Name: "device2",
|
||||
FilesystemLabel: "COS_OEM",
|
||||
Type: "ext4",
|
||||
MountPoint: "/oem",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
@ -86,7 +86,8 @@ func (g *GhwMock) CreateDevices() {
|
||||
// Create the /sys/block/DISK_NAME/PARTITION_NAME/dev file which contains the major:minor of the partition
|
||||
_ = os.WriteFile(filepath.Join(diskPath, partition.Name, "dev"), []byte(fmt.Sprintf("%d:6%d\n", indexDisk, indexPart)), 0644)
|
||||
// Create the /run/udev/data/bMAJOR:MINOR file with the data inside to mimic the udev database
|
||||
data := []string{fmt.Sprintf("E:ID_FS_LABEL=%s\n", partition.Label)}
|
||||
data := []string{fmt.Sprintf("E:ID_PART_ENTRY_NAME=%s\n", partition.FilesystemLabel)}
|
||||
data = append(data, fmt.Sprintf("E:ID_FS_LABEL=%s\n", partition.Label))
|
||||
if partition.Type != "" {
|
||||
data = append(data, fmt.Sprintf("E:ID_FS_TYPE=%s\n", partition.Type))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user