github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/pkg/boot/stboot/hostvars.go (about)

     1  // Copyright 2018 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package stboot
     6  
     7  import (
     8  	"encoding/json"
     9  	"fmt"
    10  	"io/ioutil"
    11  	"os"
    12  	"path"
    13  )
    14  
    15  // HostVars contains contains platform-specific data
    16  type HostVars struct {
    17  	HostIP         string `json:"host_ip"`
    18  	HostNetmask    string `json:"netmask"`
    19  	DefaultGateway string `json:"gateway"`
    20  	DNSServer      string `json:"dns"`
    21  
    22  	BootstrapURL string `json:"bootstrap_url"`
    23  
    24  	MinimalSignaturesMatch int `json:"minimal_signatures_match"`
    25  }
    26  
    27  // FindHostVarsInInitramfs looks for netvars.json at a given path inside
    28  // the initramfs file system. The hostvars.json is
    29  // expected to be in /etc.
    30  func FindHostVarsInInitramfs() (HostVars, error) {
    31  	var vars HostVars
    32  	file := path.Join("etc/", HostVarsName)
    33  	if _, err := os.Stat(file); os.IsNotExist(err) {
    34  		return vars, fmt.Errorf("%s not found: %v", file, err)
    35  	}
    36  	data, err := ioutil.ReadFile(file)
    37  	if err != nil {
    38  		return vars, fmt.Errorf("cant open %s: %v", file, err)
    39  	}
    40  	if err = json.Unmarshal(data, &vars); err != nil {
    41  		return vars, fmt.Errorf("cant parse data from %s", file)
    42  	}
    43  	return vars, nil
    44  }
    45  
    46  // FindHostVarsOnPartition mounts all possible devices with every possible
    47  // // file system and looks for hostvars.json at root of partition
    48  // func FindHostVarsOnPartition() (HostVars, error) {
    49  // 	var vars HostVars
    50  // 	devices, err := storage.GetBlockDevices()
    51  // 	if err != nil {
    52  // 		log.Fatal(err)
    53  // 	}
    54  
    55  // 	filesystems, err := storage.GetSupportedFilesystems()
    56  // 	if err != nil {
    57  // 		log.Fatal(err)
    58  // 	}
    59  // 	var mounted []storage.Mountpoint
    60  
    61  // 	mounted = make([]storage.Mountpoint, 0)
    62  // 	for _, dev := range devices {
    63  // 		devname := path.Join("/dev", dev.Name)
    64  // 		mountpath := path.Join("/mnt", dev.Name)
    65  // 		if mountpoint, err := storage.Mount(devname, mountpath, filesystems); err != nil {
    66  // 			fmt.Printf("Failed to mount %s on %s: %v", devname, mountpath, err)
    67  // 		} else {
    68  // 			mounted = append(mounted, *mountpoint)
    69  // 		}
    70  // 	}
    71  // 	defer func() {
    72  // 		// clean up
    73  // 		for _, mountpoint := range mounted {
    74  // 			syscall.Unmount(mountpoint.Path, syscall.MNT_DETACH)
    75  // 		}
    76  // 	}()
    77  
    78  // 	var data []byte
    79  // 	var file string
    80  // 	for _, mountpoint := range mounted {
    81  // 		file = path.Join(mountpoint.Path, HostVarsName)
    82  // 		log.Printf("Trying to read %s", file)
    83  // 		data, err = ioutil.ReadFile(file)
    84  // 		if err == nil {
    85  // 			break
    86  // 		}
    87  // 		log.Printf("cannot open %s: %v", file, err)
    88  // 	}
    89  
    90  // 	if err = json.Unmarshal(data, &vars); err != nil {
    91  // 		return vars, fmt.Errorf("unable to parse %s: %v", file, err)
    92  // 	}
    93  
    94  // 	return vars, nil
    95  // }