Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

incusd/instance/qemu: Cap hotplug CPU slots to 64 #516

Merged
merged 4 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion internal/server/instance/drivers/driver_qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -807,12 +807,16 @@ func (d *qemu) ovmfPath() string {
func (d *qemu) killQemuProcess(pid int) error {
proc, err := os.FindProcess(pid)
if err != nil {
if err == os.ErrProcessDone {
return nil
}

return err
}

err = proc.Kill()
if err != nil {
if strings.Contains(err.Error(), "process already finished") {
if err == os.ErrProcessDone {
return nil
}

Expand Down
10 changes: 9 additions & 1 deletion internal/server/instance/drivers/driver_qemu_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,16 @@ func qemuCPU(opts *qemuCPUOpts, pinning bool) []cfgSection {
return nil
}

// Cap the max number of CPUs to 64 unless directly assigned more.
max := 64
if int(cpu.Total) < max {
max = int(cpu.Total)
} else if opts.cpuCount > max {
max = opts.cpuCount
}

entries = append(entries, cfgEntry{
key: "maxcpus", value: fmt.Sprintf("%d", cpu.Total),
key: "maxcpus", value: fmt.Sprintf("%d", max),
})
}

Expand Down
2 changes: 1 addition & 1 deletion internal/server/storage/drivers/driver_lvm_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func (d *lvm) CreateVolumeFromMigration(vol Volume, conn io.ReadWriteCloser, vol
break
}

time.Sleep(10*time.Second)
time.Sleep(10 * time.Second)
}
}(volDevPath)

Expand Down
68 changes: 53 additions & 15 deletions shared/subprocess/proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io"
"os"
"os/exec"
"strings"
"syscall"

"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -59,13 +58,25 @@ func (p *Process) hasApparmor() bool {

// GetPid returns the pid for the given process object.
func (p *Process) GetPid() (int64, error) {
pr, _ := os.FindProcess(int(p.PID))
err := pr.Signal(syscall.Signal(0))
if err == nil {
return p.PID, nil
pr, err := os.FindProcess(int(p.PID))
if err != nil {
if err == os.ErrProcessDone {
return 0, ErrNotRunning
}

return 0, err
}

err = pr.Signal(syscall.Signal(0))
if err != nil {
if err == os.ErrProcessDone {
return 0, ErrNotRunning
}

return 0, err
}

return 0, ErrNotRunning
return p.PID, nil
}

// SetApparmor allows setting the AppArmor profile.
Expand All @@ -81,10 +92,21 @@ func (p *Process) SetCreds(uid uint32, gid uint32) {

// Stop will stop the given process object.
func (p *Process) Stop() error {
pr, _ := os.FindProcess(int(p.PID))
pr, err := os.FindProcess(int(p.PID))
if err != nil {
if err == os.ErrProcessDone {
if p.hasMonitor {
<-p.chExit
}

return ErrNotRunning
}

return err
}

// Check if process exists.
err := pr.Signal(syscall.Signal(0))
err = pr.Signal(syscall.Signal(0))
if err == nil {
err = pr.Kill()
if err == nil {
Expand All @@ -97,7 +119,7 @@ func (p *Process) Stop() error {
}

// Check if either the existence check or the kill resulted in an already finished error.
if strings.Contains(err.Error(), "process already finished") {
if err == os.ErrProcessDone {
if p.hasMonitor {
<-p.chExit
}
Expand Down Expand Up @@ -212,16 +234,24 @@ func (p *Process) Restart(ctx context.Context) error {

// Reload sends the SIGHUP signal to the given process object.
func (p *Process) Reload() error {
pr, _ := os.FindProcess(int(p.PID))
err := pr.Signal(syscall.Signal(0))
pr, err := os.FindProcess(int(p.PID))
if err != nil {
if err == os.ErrProcessDone {
return ErrNotRunning
}

return fmt.Errorf("Could not reload process: %w", err)
}

err = pr.Signal(syscall.Signal(0))
if err == nil {
err = pr.Signal(syscall.SIGHUP)
if err != nil {
return fmt.Errorf("Could not reload process: %w", err)
}

return nil
} else if strings.Contains(err.Error(), "process already finished") {
} else if err == os.ErrProcessDone {
return ErrNotRunning
}

Expand All @@ -245,16 +275,24 @@ func (p *Process) Save(path string) error {

// Signal will send a signal to the given process object given a signal value.
func (p *Process) Signal(signal int64) error {
pr, _ := os.FindProcess(int(p.PID))
err := pr.Signal(syscall.Signal(0))
pr, err := os.FindProcess(int(p.PID))
if err != nil {
if err == os.ErrProcessDone {
return ErrNotRunning
}

return err
}

err = pr.Signal(syscall.Signal(0))
if err == nil {
err = pr.Signal(syscall.Signal(signal))
if err != nil {
return fmt.Errorf("Could not signal process: %w", err)
}

return nil
} else if strings.Contains(err.Error(), "process already finished") {
} else if err == os.ErrProcessDone {
return ErrNotRunning
}

Expand Down
Loading