github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/pkg/parsers/kernel/kernel_windows.go (about)

     1  package kernel // import "github.com/demonoid81/moby/pkg/parsers/kernel"
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"golang.org/x/sys/windows"
     7  	"golang.org/x/sys/windows/registry"
     8  )
     9  
    10  // VersionInfo holds information about the kernel.
    11  type VersionInfo struct {
    12  	kvi   string // Version of the kernel (e.g. 6.1.7601.17592 -> 6)
    13  	major int    // Major part of the kernel version (e.g. 6.1.7601.17592 -> 1)
    14  	minor int    // Minor part of the kernel version (e.g. 6.1.7601.17592 -> 7601)
    15  	build int    // Build number of the kernel version (e.g. 6.1.7601.17592 -> 17592)
    16  }
    17  
    18  func (k *VersionInfo) String() string {
    19  	return fmt.Sprintf("%d.%d %d (%s)", k.major, k.minor, k.build, k.kvi)
    20  }
    21  
    22  // GetKernelVersion gets the current kernel version.
    23  func GetKernelVersion() (*VersionInfo, error) {
    24  
    25  	KVI := &VersionInfo{"Unknown", 0, 0, 0}
    26  
    27  	k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE)
    28  	if err != nil {
    29  		return KVI, err
    30  	}
    31  	defer k.Close()
    32  
    33  	blex, _, err := k.GetStringValue("BuildLabEx")
    34  	if err != nil {
    35  		return KVI, err
    36  	}
    37  	KVI.kvi = blex
    38  
    39  	// Important - dockerd.exe MUST be manifested for this API to return
    40  	// the correct information.
    41  	dwVersion, err := windows.GetVersion()
    42  	if err != nil {
    43  		return KVI, err
    44  	}
    45  
    46  	KVI.major = int(dwVersion & 0xFF)
    47  	KVI.minor = int((dwVersion & 0xFF00) >> 8)
    48  	KVI.build = int((dwVersion & 0xFFFF0000) >> 16)
    49  
    50  	return KVI, nil
    51  }