Skip to content

Commit

Permalink
incusd/instance/lxc: Cleanup DHCP client
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Graber <stgraber@stgraber.org>
  • Loading branch information
stgraber committed Nov 27, 2024
1 parent 5faf270 commit 6277b25
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cmd/incusd/main_forknet.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,13 @@ func (c *cmdForknet) RunDHCP(cmd *cobra.Command, args []string) error {
return nil
}

// Create PID file.
err = os.WriteFile(filepath.Join(args[0], "dhcp.pid"), []byte(fmt.Sprintf("%d", os.Getpid())), 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "Giving up on DHCP, couldn't write PID file: %v\n", err)
return nil
}

// Handle DHCP renewal.
for {
// Wait until it's renewal time.
Expand Down
11 changes: 11 additions & 0 deletions internal/server/instance/drivers/driver_lxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3228,6 +3228,17 @@ func (d *lxc) onStop(args map[string]string) error {
// Clean up devices.
d.cleanupDevices(false, "")

// Stop DHCP client if any.
if util.PathExists(filepath.Join(d.Path(), "network", "dhcp.pid")) {
dhcpPIDStr, err := os.ReadFile(filepath.Join(d.Path(), "network", "dhcp.pid"))
if err == nil {
dhcpPID, err := strconv.Atoi(strings.TrimSpace(string(dhcpPIDStr)))
if err == nil {
_ = unix.Kill(dhcpPID, unix.SIGTERM)
}
}
}

// Remove directory ownership (to avoid issue if uidmap is re-used)
err := os.Chown(d.Path(), 0, 0)
if err != nil {
Expand Down

0 comments on commit 6277b25

Please sign in to comment.