go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/os/resources/kernel/info.go (about) 1 // Copyright (c) Mondoo, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package kernel 5 6 import ( 7 "io" 8 "regexp" 9 "strings" 10 11 "github.com/cockroachdb/errors" 12 ) 13 14 var LINUX_KERNEL_ARGUMENTS_REGEX = regexp.MustCompile(`(?:^BOOT_IMAGE=([^\s]*)\s)?(?:root=([^\s]*)\s)?(.*)`) 15 16 type LinuxKernelArguments struct { 17 Path string 18 Device string 19 Arguments map[string]string 20 } 21 22 func ParseLinuxKernelArguments(r io.Reader) (LinuxKernelArguments, error) { 23 res := LinuxKernelArguments{ 24 Arguments: map[string]string{}, 25 } 26 27 data, err := io.ReadAll(r) 28 if err != nil { 29 return res, err 30 } 31 32 m := LINUX_KERNEL_ARGUMENTS_REGEX.FindStringSubmatch(string(data)) 33 34 if len(m) > 0 { 35 res.Path = m[1] 36 res.Device = m[2] 37 38 args := m[3] 39 keypairs := strings.Split(args, " ") 40 41 for i := range keypairs { 42 keypair := keypairs[i] 43 vals := strings.Split(keypair, "=") 44 45 key := vals[0] 46 value := "" 47 if len(vals) > 1 { 48 value = vals[1] 49 } 50 51 res.Arguments[key] = value 52 } 53 } 54 55 return res, nil 56 } 57 58 // kernel version includes the kernel version, build data, buildhost, compiler version and an optional build date 59 func ParseLinuxKernelVersion(r io.Reader) (string, error) { 60 data, err := io.ReadAll(r) 61 if err != nil { 62 return "", err 63 } 64 65 values := strings.Split(string(data), " ") 66 if len(values) > 2 && values[1] == "version" { 67 return values[2], nil 68 } 69 70 return "", errors.New("cannot determine kernel version") 71 }