-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Uses the systemd unit file parser to build unit files instead of having them be just blocks of hard-coded strings. Signed-off-by: Jake Correnti <[email protected]>
- Loading branch information
1 parent
c728eeb
commit 98f332d
Showing
4 changed files
with
178 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ import ( | |
"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/systemd/parser" | ||
"github.com/containers/podman/v4/pkg/util" | ||
"github.com/containers/podman/v4/utils" | ||
"github.com/containers/storage/pkg/lockfile" | ||
|
@@ -46,21 +47,13 @@ const ( | |
apiUpTimeout = 20 * time.Second | ||
) | ||
|
||
// appleHVReadyUnit is a unit file that sets up the virtual serial device | ||
// where when the VM is done configuring, it will send an ack | ||
// so a listening host knows it can begin interacting with it | ||
const appleHVReadyUnit = `[Unit] | ||
Requires=dev-virtio\\x2dports-%s.device | ||
After=remove-moby.service sshd.socket sshd.service | ||
OnFailure=emergency.target | ||
OnFailureJobMode=isolate | ||
[Service] | ||
Type=oneshot | ||
RemainAfterExit=yes | ||
ExecStart=/bin/sh -c '/usr/bin/echo Ready | socat - VSOCK-CONNECT:2:1025' | ||
[Install] | ||
RequiredBy=default.target | ||
` | ||
// VfkitHelper describes the use of vfkit: cmdline and endpoint | ||
type VfkitHelper struct { | ||
LogLevel logrus.Level | ||
Endpoint string | ||
VfkitBinaryPath *define.VMFile | ||
VirtualMachine *vfConfig.VirtualMachine | ||
} | ||
|
||
type MacMachine struct { | ||
// ConfigPath is the fully qualified path to the configuration file | ||
|
@@ -274,10 +267,15 @@ func (m *MacMachine) Init(opts machine.InitOptions) (bool, error) { | |
return false, err | ||
} | ||
|
||
readyUnitFile, err := createReadyUnitFile() | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
builder.WithUnit(ignition.Unit{ | ||
Enabled: ignition.BoolToPtr(true), | ||
Name: "ready.service", | ||
Contents: ignition.StrToPtr(fmt.Sprintf(appleHVReadyUnit, "vsock")), | ||
Contents: ignition.StrToPtr(readyUnitFile), | ||
}) | ||
builder.WithUnit(generateSystemDFilesForVirtiofsMounts(virtiofsMnts)...) | ||
|
||
|
@@ -288,6 +286,13 @@ func (m *MacMachine) Init(opts machine.InitOptions) (bool, error) { | |
return err == nil, err | ||
} | ||
|
||
func createReadyUnitFile() (string, error) { | ||
readyUnit := ignition.DefaultReadyUnitFile() | ||
readyUnit.Add("Unit", "Requires", "dev-virtio\\x2dports-vsock.device") | ||
readyUnit.Add("Service", "ExecStart", "/bin/sh -c '/usr/bin/echo Ready | socat - VSOCK-CONNECT:2:1025'") | ||
return readyUnit.ToString() | ||
} | ||
|
||
func (m *MacMachine) removeSSHKeys() error { | ||
if err := os.Remove(fmt.Sprintf("%s.pub", m.IdentityPath)); err != nil { | ||
logrus.Error(err) | ||
|
@@ -1111,31 +1116,34 @@ func generateSystemDFilesForVirtiofsMounts(mounts []machine.VirtIoFs) []ignition | |
// 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 | ||
// on boot. | ||
autoMountUnit := `[Automount] | ||
Where=%s | ||
[Install] | ||
WantedBy=multi-user.target | ||
[Unit] | ||
Description=Mount virtiofs volume %s | ||
` | ||
mountUnit := `[Mount] | ||
What=%s | ||
Where=%s | ||
Type=virtiofs | ||
[Install] | ||
WantedBy=multi-user.target | ||
` | ||
autoMountUnit := parser.NewUnitFile() | ||
autoMountUnit.Add("Automount", "Where", "%s") | ||
autoMountUnit.Add("Install", "WantedBy", "multi-user.target") | ||
autoMountUnit.Add("Unit", "Description", "Mount virtiofs volume %s") | ||
autoMountUnitFile, err := autoMountUnit.ToString() | ||
if err != nil { | ||
logrus.Warnf(err.Error()) | ||
} | ||
|
||
mountUnit := parser.NewUnitFile() | ||
mountUnit.Add("Mount", "What", "%s") | ||
mountUnit.Add("Mount", "Where", "%s") | ||
mountUnit.Add("Mount", "Type", "virtiofs") | ||
mountUnit.Add("Install", "WantedBy", "multi-user.target") | ||
mountUnitFile, err := mountUnit.ToString() | ||
if err != nil { | ||
logrus.Warnf(err.Error()) | ||
} | ||
|
||
virtiofsAutomount := ignition.Unit{ | ||
Enabled: ignition.BoolToPtr(true), | ||
Name: fmt.Sprintf("%s.automount", mnt.Tag), | ||
Contents: ignition.StrToPtr(fmt.Sprintf(autoMountUnit, mnt.Target, mnt.Target)), | ||
Contents: ignition.StrToPtr(fmt.Sprintf(autoMountUnitFile, mnt.Target, mnt.Target)), | ||
} | ||
virtiofsMount := ignition.Unit{ | ||
Enabled: ignition.BoolToPtr(true), | ||
Name: fmt.Sprintf("%s.mount", mnt.Tag), | ||
Contents: ignition.StrToPtr(fmt.Sprintf(mountUnit, mnt.Tag, mnt.Target)), | ||
Contents: ignition.StrToPtr(fmt.Sprintf(mountUnitFile, mnt.Tag, mnt.Target)), | ||
} | ||
|
||
// This "unit" simulates something like systemctl enable virtiofs-mount-prepare@ | ||
|
@@ -1149,23 +1157,24 @@ WantedBy=multi-user.target | |
|
||
// mount prep is a way to workaround the FCOS limitation of creating directories | ||
// at the rootfs / and then mounting to them. | ||
mountPrep := ` | ||
[Unit] | ||
Description=Allow virtios to mount to / | ||
DefaultDependencies=no | ||
ConditionPathExists=!%f | ||
[Service] | ||
Type=oneshot | ||
ExecStartPre=chattr -i / | ||
ExecStart=mkdir -p '%f' | ||
ExecStopPost=chattr +i / | ||
[Install] | ||
WantedBy=remote-fs.target | ||
` | ||
mountPrep := parser.NewUnitFile() | ||
mountPrep.Add("Unit", "Description", "Allow virtios to mount to /") | ||
mountPrep.Add("Unit", "DefaultDependencies", "no") | ||
mountPrep.Add("Unit", "ConditionPathExists", "!%f") | ||
|
||
mountPrep.Add("Service", "Type", "oneshot") | ||
mountPrep.Add("Service", "ExecStartPre", "chattr -i /") | ||
mountPrep.Add("Service", "ExecStart", "mkdir -p '%f'") | ||
mountPrep.Add("Service", "ExecStopPost", "chattr +i /") | ||
|
||
mountPrep.Add("Install", "WantedBy", "remote-fs.target") | ||
mountPrepFile, err := mountPrep.ToString() | ||
if err != nil { | ||
logrus.Warnf(err.Error()) | ||
} | ||
|
||
virtioFSChattr := ignition.Unit{ | ||
Contents: ignition.StrToPtr(mountPrep), | ||
Contents: ignition.StrToPtr(mountPrepFile), | ||
Name: "[email protected]", | ||
} | ||
unitFiles = append(unitFiles, virtioFSChattr) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.