github.com/iDigitalFlame/xmt@v0.5.4/device/local/bsd_no_crypt.go (about)

     1  //go:build !windows && !plan9 && !js && !darwin && !linux && !android && !crypt
     2  // +build !windows,!plan9,!js,!darwin,!linux,!android,!crypt
     3  
     4  // Copyright (C) 2020 - 2023 iDigitalFlame
     5  //
     6  // This program is free software: you can redistribute it and/or modify
     7  // it under the terms of the GNU General Public License as published by
     8  // the Free Software Foundation, either version 3 of the License, or
     9  // any later version.
    10  //
    11  // This program is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU General Public License
    17  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    18  //
    19  
    20  package local
    21  
    22  import (
    23  	"bytes"
    24  	"os/exec"
    25  	"runtime"
    26  	"strings"
    27  
    28  	"github.com/iDigitalFlame/xmt/data"
    29  	"github.com/iDigitalFlame/xmt/device/unix"
    30  )
    31  
    32  func sysID() []byte {
    33  	switch runtime.GOOS {
    34  	case "aix":
    35  		// AIX specific support: https://github.com/denisbrodbeck/machineid/pull/16
    36  		if b, err := exec.Command("lsattr", "-l", "sys0", "-a", "os_uuid", "-E").CombinedOutput(); err == nil {
    37  			if i := bytes.IndexByte(b, ' '); i > 0 {
    38  				return b[i+1:]
    39  			}
    40  			return b
    41  		}
    42  	case "openbsd":
    43  		// Support get hardware UUID for OpenBSD: https://github.com/denisbrodbeck/machineid/pull/14
    44  		if b, err := exec.Command("sysctl", "-n", "hw.uuid").CombinedOutput(); err == nil {
    45  			return b
    46  		}
    47  	}
    48  	if b, err := data.ReadFile("/etc/hostid"); err == nil {
    49  		return b
    50  	}
    51  	o, _ := exec.Command("kenv", "-q", "smbios.system.uuid").CombinedOutput()
    52  	return o
    53  }
    54  func version() string {
    55  	var (
    56  		ok      bool
    57  		b, n, v string
    58  	)
    59  	if m := release(); len(m) > 0 {
    60  		b = m["ID"]
    61  		if n, ok = m["PRETTY_NAME"]; !ok {
    62  			n = m["NAME"]
    63  		}
    64  		if v, ok = m["VERSION_ID"]; !ok {
    65  			v = m["VERSION"]
    66  		}
    67  	}
    68  	if len(b) == 0 && strings.Contains(runtime.GOOS, "bsd") {
    69  		if o, err := exec.Command("freebsd-version", "-k").CombinedOutput(); err == nil {
    70  			b = strings.Replace(string(o), "\n", "", -1)
    71  		}
    72  	}
    73  	if len(v) == 0 {
    74  		v = unix.Release()
    75  	}
    76  	switch {
    77  	case len(n) == 0 && len(b) == 0 && len(v) == 0:
    78  		return "BSD"
    79  	case len(n) == 0 && len(b) > 0 && len(v) > 0:
    80  		return "BSD (" + v + ", " + b + ")"
    81  	case len(n) == 0 && len(b) == 0 && len(v) > 0:
    82  		return "BSD (" + v + ")"
    83  	case len(n) == 0 && len(b) > 0 && len(v) == 0:
    84  		return "BSD (" + b + ")"
    85  	case len(n) > 0 && len(b) > 0 && len(v) > 0:
    86  		return n + " (" + v + ", " + b + ")"
    87  	case len(n) > 0 && len(b) == 0 && len(v) > 0:
    88  		return n + " (" + v + ")"
    89  	case len(n) > 0 && len(b) > 0 && len(v) == 0:
    90  		return n + " (" + b + ")"
    91  	}
    92  	return "BSD"
    93  }