Skip to content

Commit

Permalink
Create pkg/machine/ignition package
Browse files Browse the repository at this point in the history
Moves all of the ignitionfiles out of the `machine` package and into
its own called `ignition`. This required `VMType` to get moved out of
the `machine` package and into the `define` package in order to prevent
a circular dependency.

Signed-off-by: Jake Correnti <[email protected]>
  • Loading branch information
jakecorrenti committed Jan 4, 2024
1 parent de797e5 commit c728eeb
Show file tree
Hide file tree
Showing 33 changed files with 259 additions and 173 deletions.
2 changes: 1 addition & 1 deletion cmd/podman/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func composeDockerHost() (string, error) {
if info.State != define.Running {
return "", fmt.Errorf("machine %s is not running but in state %s", item.Name, info.State)
}
if machineProvider.VMType() == machine.WSLVirt {
if machineProvider.VMType() == define.WSLVirt {
if info.ConnectionInfo.PodmanPipe == nil {
return "", errors.New("pipe of machine is not set")
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/podman/machine/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/containers/podman/v4/cmd/podman/registry"
"github.com/containers/podman/v4/libpod/events"
"github.com/containers/podman/v4/pkg/machine"
"github.com/containers/podman/v4/pkg/machine/define"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -141,7 +142,7 @@ func initMachine(cmd *cobra.Command, args []string) error {
}

// The vmtype names need to be reserved and cannot be used for podman machine names
if _, err := machine.ParseVMType(initOpts.Name, machine.UnknownVirt); err == nil {
if _, err := define.ParseVMType(initOpts.Name, define.UnknownVirt); err == nil {
return fmt.Errorf("cannot use %q for a machine name", initOpts.Name)
}

Expand Down
13 changes: 7 additions & 6 deletions pkg/machine/applehv/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/containers/podman/v4/pkg/machine/compression"
"github.com/containers/podman/v4/pkg/machine/define"
"github.com/containers/podman/v4/pkg/machine/vmconfigs"
"github.com/containers/podman/v4/pkg/machine/ignition"
vfConfig "github.com/crc-org/vfkit/pkg/config"
"github.com/docker/go-units"
"golang.org/x/sys/unix"
Expand Down Expand Up @@ -55,7 +56,7 @@ func (v AppleHVVirtualization) CheckExclusiveActiveVM() (bool, string, error) {
}

func (v AppleHVVirtualization) IsValidVMName(name string) (bool, error) {
configDir, err := machine.GetConfDir(machine.AppleHvVirt)
configDir, err := machine.GetConfDir(define.AppleHvVirt)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -93,7 +94,7 @@ func (v AppleHVVirtualization) List(opts machine.ListOptions) ([]*machine.ListRe
Running: vmState == define.Running,
Starting: vmState == define.Starting,
Stream: mm.ImageStream,
VMType: machine.AppleHvVirt.String(),
VMType: define.AppleHvVirt.String(),
CPUs: mm.CPUs,
Memory: mm.Memory * units.MiB,
DiskSize: mm.DiskSize * units.GiB,
Expand All @@ -118,7 +119,7 @@ func (v AppleHVVirtualization) NewMachine(opts machine.InitOptions) (machine.VM,
return nil, fmt.Errorf("USB host passthrough is not supported for applehv machines")
}

configDir, err := machine.GetConfDir(machine.AppleHvVirt)
configDir, err := machine.GetConfDir(define.AppleHvVirt)
if err != nil {
return nil, err
}
Expand All @@ -129,12 +130,12 @@ func (v AppleHVVirtualization) NewMachine(opts machine.InitOptions) (machine.VM,
}
m.ConfigPath = *configPath

dataDir, err := machine.GetDataDir(machine.AppleHvVirt)
dataDir, err := machine.GetDataDir(define.AppleHvVirt)
if err != nil {
return nil, err
}

if err := machine.SetIgnitionFile(&m.IgnitionFile, vmtype, m.Name); err != nil {
if err := ignition.SetIgnitionFile(&m.IgnitionFile, vmtype, m.Name, configDir); err != nil {
return nil, err
}

Expand All @@ -161,7 +162,7 @@ func (v AppleHVVirtualization) RemoveAndCleanMachines() error {
return machine.ErrNotImplemented
}

func (v AppleHVVirtualization) VMType() machine.VMType {
func (v AppleHVVirtualization) VMType() define.VMType {
return vmtype
}

Expand Down
51 changes: 26 additions & 25 deletions pkg/machine/applehv/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/containers/podman/v4/pkg/machine/define"
"github.com/containers/podman/v4/pkg/machine/sockets"
"github.com/containers/podman/v4/pkg/machine/vmconfigs"
"github.com/containers/podman/v4/pkg/machine/ignition"
"github.com/containers/podman/v4/pkg/strongunits"
"github.com/containers/podman/v4/pkg/util"
"github.com/containers/podman/v4/utils"
Expand All @@ -36,7 +37,7 @@ import (

var (
// vmtype refers to qemu (vs libvirt, krun, etc).
vmtype = machine.AppleHvVirt
vmtype = define.AppleHvVirt
)

const (
Expand Down Expand Up @@ -160,7 +161,7 @@ func (m *MacMachine) Init(opts machine.InitOptions) (bool, error) {
go callbackFuncs.CleanOnSignal()

callbackFuncs.Add(m.ConfigPath.Delete)
dataDir, err := machine.GetDataDir(machine.AppleHvVirt)
dataDir, err := machine.GetDataDir(define.AppleHvVirt)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -254,11 +255,11 @@ func (m *MacMachine) Init(opts machine.InitOptions) (bool, error) {
callbackFuncs.Add(m.removeSSHKeys)
}

builder := machine.NewIgnitionBuilder(machine.DynamicIgnition{
builder := ignition.NewIgnitionBuilder(ignition.DynamicIgnition{
Name: opts.Username,
Key: key,
VMName: m.Name,
VMType: machine.AppleHvVirt,
VMType: define.AppleHvVirt,
TimeZone: opts.TimeZone,
WritePath: m.IgnitionFile.GetPath(),
UID: m.UID,
Expand All @@ -273,10 +274,10 @@ func (m *MacMachine) Init(opts machine.InitOptions) (bool, error) {
return false, err
}

builder.WithUnit(machine.Unit{
Enabled: machine.BoolToPtr(true),
builder.WithUnit(ignition.Unit{
Enabled: ignition.BoolToPtr(true),
Name: "ready.service",
Contents: machine.StrToPtr(fmt.Sprintf(appleHVReadyUnit, "vsock")),
Contents: ignition.StrToPtr(fmt.Sprintf(appleHVReadyUnit, "vsock")),
})
builder.WithUnit(generateSystemDFilesForVirtiofsMounts(virtiofsMnts)...)

Expand Down Expand Up @@ -786,7 +787,7 @@ func loadMacMachineFromJSON(fqConfigPath string) (*MacMachine, error) {
}

func (m *MacMachine) jsonConfigPath() (string, error) {
configDir, err := machine.GetConfDir(machine.AppleHvVirt)
configDir, err := machine.GetConfDir(define.AppleHvVirt)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -817,7 +818,7 @@ func getVMInfos() ([]*machine.ListResponse, error) {

listEntry.Name = vm.Name
listEntry.Stream = vm.ImageStream
listEntry.VMType = machine.AppleHvVirt.String()
listEntry.VMType = define.AppleHvVirt.String()
listEntry.CPUs = vm.CPUs
listEntry.Memory = vm.Memory * units.MiB
listEntry.DiskSize = vm.DiskSize * units.GiB
Expand Down Expand Up @@ -1014,7 +1015,7 @@ func (m *MacMachine) setupAPIForwarding(cmd gvproxy.GvproxyCommand) (gvproxy.Gvp
}

func (m *MacMachine) dockerSock() (string, error) {
dd, err := machine.GetDataDir(machine.AppleHvVirt)
dd, err := machine.GetDataDir(define.AppleHvVirt)
if err != nil {
return "", err
}
Expand All @@ -1023,7 +1024,7 @@ func (m *MacMachine) dockerSock() (string, error) {

func (m *MacMachine) forwardSocketPath() (*define.VMFile, error) {
sockName := "podman.sock"
path, err := machine.GetDataDir(machine.AppleHvVirt)
path, err := machine.GetDataDir(define.AppleHvVirt)
if err != nil {
return nil, fmt.Errorf("Resolving data dir: %s", err.Error())
}
Expand Down Expand Up @@ -1060,7 +1061,7 @@ func (m *MacMachine) isFirstBoot() (bool, error) {
}

func (m *MacMachine) getIgnitionSock() (*define.VMFile, error) {
dataDir, err := machine.GetDataDir(machine.AppleHvVirt)
dataDir, err := machine.GetDataDir(define.AppleHvVirt)
if err != nil {
return nil, err
}
Expand All @@ -1087,7 +1088,7 @@ func (m *MacMachine) getRuntimeDir() (string, error) {
}

func (m *MacMachine) userGlobalSocketLink() (string, error) {
path, err := machine.GetDataDir(machine.AppleHvVirt)
path, err := machine.GetDataDir(define.AppleHvVirt)
if err != nil {
logrus.Errorf("Resolving data dir: %s", err.Error())
return "", err
Expand All @@ -1100,12 +1101,12 @@ func (m *MacMachine) isIncompatible() bool {
return m.UID == -1
}

func generateSystemDFilesForVirtiofsMounts(mounts []machine.VirtIoFs) []machine.Unit {
func generateSystemDFilesForVirtiofsMounts(mounts []machine.VirtIoFs) []ignition.Unit {
// mounting in fcos with virtiofs is a bit of a dance. we need a unit file for the mount, a unit file
// for automatic mounting on boot, and a "preparatory" service file that disables FCOS security, performs
// the mkdir of the mount point, and then re-enables security. This must be done for each mount.

var unitFiles []machine.Unit
var unitFiles []ignition.Unit
for _, mnt := range mounts {
// Here we are looping the mounts and for each mount, we are adding two unit files
// for virtiofs. One unit file is the mount itself and the second is to automount it
Expand All @@ -1126,20 +1127,20 @@ Type=virtiofs
[Install]
WantedBy=multi-user.target
`
virtiofsAutomount := machine.Unit{
Enabled: machine.BoolToPtr(true),
virtiofsAutomount := ignition.Unit{
Enabled: ignition.BoolToPtr(true),
Name: fmt.Sprintf("%s.automount", mnt.Tag),
Contents: machine.StrToPtr(fmt.Sprintf(autoMountUnit, mnt.Target, mnt.Target)),
Contents: ignition.StrToPtr(fmt.Sprintf(autoMountUnit, mnt.Target, mnt.Target)),
}
virtiofsMount := machine.Unit{
Enabled: machine.BoolToPtr(true),
virtiofsMount := ignition.Unit{
Enabled: ignition.BoolToPtr(true),
Name: fmt.Sprintf("%s.mount", mnt.Tag),
Contents: machine.StrToPtr(fmt.Sprintf(mountUnit, mnt.Tag, mnt.Target)),
Contents: ignition.StrToPtr(fmt.Sprintf(mountUnit, mnt.Tag, mnt.Target)),
}

// This "unit" simulates something like systemctl enable virtiofs-mount-prepare@
enablePrep := machine.Unit{
Enabled: machine.BoolToPtr(true),
enablePrep := ignition.Unit{
Enabled: ignition.BoolToPtr(true),
Name: fmt.Sprintf("virtiofs-mount-prepare@%s.service", mnt.Tag),
}

Expand All @@ -1163,8 +1164,8 @@ ExecStopPost=chattr +i /
[Install]
WantedBy=remote-fs.target
`
virtioFSChattr := machine.Unit{
Contents: machine.StrToPtr(mountPrep),
virtioFSChattr := ignition.Unit{
Contents: ignition.StrToPtr(mountPrep),
Name: "[email protected]",
}
unitFiles = append(unitFiles, virtioFSChattr)
Expand Down
Loading

0 comments on commit c728eeb

Please sign in to comment.