Skip to content

Commit

Permalink
Support non-default libvirt socket URLs and URI
Browse files Browse the repository at this point in the history
On some systems, such as Fedora 40, there is no longer a "legacy"
/run/libvirt/libvirtd-sock. Instead, all the driver-specific daemons have
their own socket, such as /run/libvirt/virtqemud-sock.

So, let users specify the socket they want to use. While at it, also allow
them to chose different connection URIs, such as qemu:///session.
  • Loading branch information
WanzenBug committed Apr 26, 2024
1 parent 1b6424f commit 1477f5b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
10 changes: 10 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ import (
// configuration file.
// The default config file also contains some inline documentation for each option.
const defaultConfigTemplate = `[libvirt]
# socket is the path to the unix domain socket for libvirt.
# Default value: "{{ get "libvirt.socket" }}"
socket = "{{ get "libvirt.socket" }}"
# uri is the libvirt uri to connect to a specific driver.
# Default value: "{{ get "libvirt.uri" }}"
uri = "{{ get "libvirt.uri" }}"
# pool is the libvirt pool that virter should use.
# The user is responsible for ensuring that this pool exists and is active.
# Default value: "{{ get "libvirt.pool" }}"
Expand Down Expand Up @@ -107,6 +115,8 @@ pull = "{{ get "container.pull" }}"

// initConfig reads in config file and ENV variables if set.
func initConfig() {
viper.SetDefault("libvirt.socket", "/var/run/libvirt/libvirt-sock")
viper.SetDefault("libvirt.uri", "qemu:///system")
viper.SetDefault("libvirt.pool", "default")
viper.SetDefault("libvirt.network", "default")
viper.SetDefault("libvirt.static_dhcp", false)
Expand Down
14 changes: 6 additions & 8 deletions cmd/virter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package cmd

import (
"fmt"
"net"
"time"

"github.com/digitalocean/go-libvirt"
"github.com/digitalocean/go-libvirt/socket/dialers"
"github.com/spf13/viper"

"github.com/LINBIT/virter/internal/virter"
Expand All @@ -14,13 +14,11 @@ import (

// InitVirter initializes virter by connecting to the local libvirt instance and configures the ssh keystore.
func InitVirter() (*virter.Virter, error) {
c, err := net.DialTimeout("unix", "/var/run/libvirt/libvirt-sock", 2*time.Second)
if err != nil {
return nil, fmt.Errorf("failed to dial libvirt: %w", err)
}

l := libvirt.New(c)
if err := l.Connect(); err != nil {
l := libvirt.NewWithDialer(dialers.NewLocal(
dialers.WithSocket(viper.GetString("libvirt.socket")),
dialers.WithLocalTimeout(2*time.Second),
))
if err := l.ConnectToURI(libvirt.ConnectURI(viper.GetString("libvirt.uri"))); err != nil {
return nil, fmt.Errorf("failed to connect to libvirt socket: %w", err)
}

Expand Down

0 comments on commit 1477f5b

Please sign in to comment.