Skip to content

Commit 9c65053

Browse files
committed
cmd/vfkit: add --pidfile flag to write vfkit process PID to file
Introduce a new --pidfile flag to optionally specify a file where the running vfkit process writes its PID. This allows easier process management and monitoring.
1 parent aa41a0b commit 9c65053

File tree

5 files changed

+19
-4
lines changed

5 files changed

+19
-4
lines changed

cmd/vfkit/main.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"github.com/Code-Hex/vz/v3"
3636
"github.com/crc-org/vfkit/pkg/cmdline"
3737
"github.com/crc-org/vfkit/pkg/config"
38+
"github.com/crc-org/vfkit/pkg/process"
3839
"github.com/crc-org/vfkit/pkg/rest"
3940
restvf "github.com/crc-org/vfkit/pkg/rest/vf"
4041
"github.com/crc-org/vfkit/pkg/vf"
@@ -100,14 +101,26 @@ func newVMConfiguration(opts *cmdline.Options) (*config.VirtualMachine, error) {
100101
opts.Devices = append(opts.Devices, fmt.Sprintf("virtio-blk,path=%s", cloudInitISO))
101102
}
102103

104+
if opts.PidFile != "" {
105+
execPath, err := os.Executable()
106+
if err != nil {
107+
return nil, fmt.Errorf("could not determine executable path: %w", err)
108+
}
109+
vfProcess := process.New(os.Args[0], opts.PidFile, execPath)
110+
pid := os.Getpid()
111+
err = vfProcess.WritePidFile(pid)
112+
if err != nil {
113+
return nil, fmt.Errorf("could not write PID: %v", err)
114+
}
115+
}
116+
103117
if err := vmConfig.AddDevicesFromCmdLine(opts.Devices); err != nil {
104118
return nil, err
105119
}
106120

107121
if err := vmConfig.AddIgnitionFileFromCmdLine(opts.IgnitionPath); err != nil {
108122
return nil, fmt.Errorf("failed to add ignition file: %w", err)
109123
}
110-
111124
return vmConfig, nil
112125
}
113126

pkg/cmdline/cmdline.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ type Options struct {
2727
IgnitionPath string
2828

2929
CloudInitFiles stringSliceValue
30+
31+
PidFile string
3032
}
3133

3234
const DefaultRestfulURI = "none://"
@@ -56,4 +58,5 @@ func AddFlags(cmd *cobra.Command, opts *Options) {
5658

5759
cmd.Flags().StringVar(&opts.IgnitionPath, "ignition", "", "path to the ignition file")
5860
cmd.Flags().VarP(&opts.CloudInitFiles, "cloud-init", "", "path to user-data and meta-data cloud-init configuration files")
61+
cmd.Flags().StringVar(&opts.PidFile, "pidfile", "", "path to the pid file")
5962
}

pkg/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type VirtualMachine struct {
3232
Devices []VirtioDevice `json:"devices,omitempty"`
3333
Timesync *TimeSync `json:"timesync,omitempty"`
3434
Ignition *Ignition `json:"ignition,omitempty"`
35+
PidFile string `json:"pidFile,omitempty"`
3536
}
3637

3738
// TimeSync enables synchronization of the host time to the linux guest after the host was suspended.

pkg/config/json_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ var jsonStabilityTests = map[string]jsonStabilityTest{
231231

232232
return vm
233233
},
234-
skipFields: []string{"Bootloader", "Devices", "Timesync", "Ignition"},
234+
skipFields: []string{"Bootloader", "Devices", "Timesync", "Ignition", "PidFile"},
235235
expectedJSON: `{"vcpus":3,"memoryBytes":3,"bootloader":{"kind":"linuxBootloader","vmlinuzPath":"/vmlinuz","kernelCmdLine":"console=hvc0","initrdPath":"/initrd"},"devices":[{"kind":"virtiorng"}],"timesync":{"vsockPort":1234}}`,
236236
},
237237
"RosettaShare": {

pkg/process/process.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ func (p *Process) findProcess() (*process.Process, error) {
8080
return nil, fmt.Errorf("cannot find process exe: %v", err)
8181
}
8282
if exe != p.ExecutablePath {
83-
fmt.Println("HII", exe, p.ExecutablePath)
84-
8583
return nil, os.ErrNotExist
8684
}
8785
return proc, nil

0 commit comments

Comments
 (0)