Skip to content

Commit e066350

Browse files
authored
Merge pull request #1234 from gibmat/add-os-info-to-instance-state
Add operating system details to instance state
2 parents ba93e12 + 977fd74 commit e066350

File tree

19 files changed

+2052
-1608
lines changed

19 files changed

+2052
-1608
lines changed

cmd/incus-agent/state.go

+35
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import (
1010
"strconv"
1111
"strings"
1212

13+
"github.com/lxc/incus/v6/internal/linux"
1314
"github.com/lxc/incus/v6/internal/server/response"
1415
"github.com/lxc/incus/v6/shared/api"
1516
"github.com/lxc/incus/v6/shared/logger"
17+
"github.com/lxc/incus/v6/shared/osarch"
1618
"github.com/lxc/incus/v6/shared/util"
1719
)
1820

@@ -39,6 +41,7 @@ func renderState() *api.InstanceState {
3941
Network: networkState(),
4042
Pid: 1,
4143
Processes: processesState(),
44+
OSInfo: osState(),
4245
}
4346
}
4447

@@ -242,3 +245,35 @@ func processesState() int64 {
242245

243246
return int64(len(pids))
244247
}
248+
249+
func osState() *api.InstanceStateOSInfo {
250+
osInfo := &api.InstanceStateOSInfo{}
251+
252+
// Get information about the OS.
253+
lsbRelease, err := osarch.GetLSBRelease()
254+
if err == nil {
255+
osInfo.OS = lsbRelease["NAME"]
256+
osInfo.OSVersion = lsbRelease["VERSION"]
257+
}
258+
259+
// Get information about the kernel version.
260+
uname, err := linux.Uname()
261+
if err == nil {
262+
osInfo.KernelVersion = uname.Release
263+
}
264+
265+
// Get the hostname.
266+
hostname, err := os.Hostname()
267+
if err == nil {
268+
osInfo.Hostname = hostname
269+
}
270+
271+
// Get the FQDN. To avoid needing to run `hostname -f`, do a reverse host lookup for 127.0.1.1, and if found, return the first hostname as the FQDN.
272+
fqdn, err := net.LookupAddr("127.0.1.1")
273+
if err == nil {
274+
// Take the first returned hostname and trim the trailing dot.
275+
osInfo.FQDN = strings.TrimSuffix(fqdn[0], ".")
276+
}
277+
278+
return osInfo
279+
}

cmd/incus/info.go

+11
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,17 @@ func (c *cmdInfo) instanceInfo(d incus.InstanceServer, remote config.Remote, nam
672672
fmt.Printf(i18n.G("Started: %s")+"\n", inst.State.StartedAt.Local().Format(dateLayout))
673673
}
674674

675+
// Operating System info
676+
if inst.State.OSInfo != nil {
677+
fmt.Println("\n" + i18n.G("Operating System:"))
678+
osInfo := fmt.Sprintf(" %s: %s\n", i18n.G("OS"), inst.State.OSInfo.OS)
679+
osInfo += fmt.Sprintf(" %s: %s\n", i18n.G("OS Version"), inst.State.OSInfo.OSVersion)
680+
osInfo += fmt.Sprintf(" %s: %s\n", i18n.G("Kernel Version"), inst.State.OSInfo.KernelVersion)
681+
osInfo += fmt.Sprintf(" %s: %s\n", i18n.G("Hostname"), inst.State.OSInfo.Hostname)
682+
osInfo += fmt.Sprintf(" %s: %s\n", i18n.G("FQDN"), inst.State.OSInfo.FQDN)
683+
fmt.Print(osInfo)
684+
}
685+
675686
fmt.Println("\n" + i18n.G("Resources:"))
676687
// Processes
677688
fmt.Printf(" "+i18n.G("Processes: %d")+"\n", inst.State.Processes)

doc/api-extensions.md

+4
Original file line numberDiff line numberDiff line change
@@ -2606,3 +2606,7 @@ This implements a new `security.promiscuous` configuration option on OVN NICs.
26062606
## `ovn_nic_ip_address_none`
26072607

26082608
This adds `none` as a value for `ipv4.address` and `ipv6.address` for OVN NICs.
2609+
2610+
## `instances_state_os_info`
2611+
2612+
This extension adds a pointer to an `InstanceStateOSInfo` struct to the instance's state API.

doc/rest-api.yaml

+32
Original file line numberDiff line numberDiff line change
@@ -2161,6 +2161,8 @@ definitions:
21612161
description: Network usage key/value pairs
21622162
type: object
21632163
x-go-name: Network
2164+
os_info:
2165+
$ref: '#/definitions/InstanceStateOSInfo'
21642166
pid:
21652167
description: PID of the runtime
21662168
example: 7281
@@ -2375,6 +2377,36 @@ definitions:
23752377
x-go-name: PacketsSent
23762378
type: object
23772379
x-go-package: github.com/lxc/incus/v6/shared/api
2380+
InstanceStateOSInfo:
2381+
properties:
2382+
fqdn:
2383+
description: FQDN of the instance.
2384+
example: myhost.mydomain.local
2385+
type: string
2386+
x-go-name: FQDN
2387+
hostname:
2388+
description: Hostname of the instance.
2389+
example: myhost
2390+
type: string
2391+
x-go-name: Hostname
2392+
kernel_version:
2393+
description: Version of the kernel running in the instance.
2394+
example: 6.1.0-25-amd64
2395+
type: string
2396+
x-go-name: KernelVersion
2397+
os:
2398+
description: Operating system running in the instance.
2399+
example: Debian GNU/Linux
2400+
type: string
2401+
x-go-name: OS
2402+
os_version:
2403+
description: Version of the operating system.
2404+
example: 12 (bookworm)
2405+
type: string
2406+
x-go-name: OSVersion
2407+
title: InstanceStateOSInfo represents the operating system information section of an instance's state.
2408+
type: object
2409+
x-go-package: github.com/lxc/incus/v6/shared/api
23782410
InstanceStatePut:
23792411
properties:
23802412
action:

internal/version/api.go

+1
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ var APIExtensions = []string{
442442
"storage_lvm_metadatasize",
443443
"ovn_nic_promiscuous",
444444
"ovn_nic_ip_address_none",
445+
"instances_state_os_info",
445446
}
446447

447448
// APIExtensionsCount returns the number of available API extensions.

0 commit comments

Comments
 (0)