diff --git a/cmd/cloudinitsave/cloudinitsave.go b/cmd/cloudinitsave/cloudinitsave.go index 6f188740..5834ce53 100755 --- a/cmd/cloudinitsave/cloudinitsave.go +++ b/cmd/cloudinitsave/cloudinitsave.go @@ -258,7 +258,11 @@ func getDatasources(datasources []string) []datasource.Datasource { case "packet": dss = append(dss, packet.NewDatasource(root)) case "vmware": - dss = append(dss, vmware.NewDatasource(root)) + // made vmware datasource dependent on detecting vmware independently, as it crashes things otherwise + v := vmware.NewDatasource(root) + if v != nil { + dss = append(dss, v) + } } } @@ -295,7 +299,7 @@ func selectDatasource(sources []datasource.Datasource) datasource.Datasource { duration := datasourceInterval for { - log.Infof("cloud-init: Checking availability of %q\n", s.Type()) + log.Infof("cloud-init: Checking availability of %q", s.Type()) if s.IsAvailable() { log.Infof("cloud-init: Datasource available: %s", s) ds <- s diff --git a/config/cloudinit/datasource/vmware/vmware_amd64.go b/config/cloudinit/datasource/vmware/vmware_amd64.go index f805f34b..a0a5187f 100755 --- a/config/cloudinit/datasource/vmware/vmware_amd64.go +++ b/config/cloudinit/datasource/vmware/vmware_amd64.go @@ -19,6 +19,7 @@ import ( "os" "github.com/rancher/os/log" + "github.com/rancher/os/util" "github.com/rancher/os/config/cloudinit/pkg" @@ -36,6 +37,9 @@ func (ovf ovfWrapper) readConfig(key string) (string, error) { } func NewDatasource(fileName string) *VMWare { + if util.GetHypervisor() != "vmware" { + return nil + } // read from provided ovf environment document (typically /media/ovfenv/ovf-env.xml) if fileName != "" { log.Printf("Using OVF environment from %s\n", fileName) @@ -69,6 +73,9 @@ func NewDatasource(fileName string) *VMWare { } func (v VMWare) IsAvailable() bool { + if util.GetHypervisor() != "vmware" { + return false + } if v.ovfFileName != "" { _, v.lastError = os.Stat(v.ovfFileName) return !os.IsNotExist(v.lastError) diff --git a/init/init.go b/init/init.go index f9af0e33..bde14389 100755 --- a/init/init.go +++ b/init/init.go @@ -19,8 +19,6 @@ import ( "github.com/rancher/os/log" "github.com/rancher/os/util" "github.com/rancher/os/util/network" - - "github.com/SvenDowideit/cpuid" ) const ( @@ -93,7 +91,7 @@ func sysInit(c *config.CloudConfig) (*config.CloudConfig, error) { } func MainInit() { - log.InitDeferedLogger() + log.InitLogger() // TODO: this breaks and does nothing if the cfg is invalid (or is it due to threading?) defer func() { if r := recover(); r != nil { @@ -327,7 +325,7 @@ func RunInit() error { }}, config.CfgFuncData{"cloud-init", func(cfg *config.CloudConfig) (*config.CloudConfig, error) { cfg.Rancher.CloudInit.Datasources = config.LoadConfigWithPrefix(state).Rancher.CloudInit.Datasources - hypervisor = checkHypervisor(cfg) + hypervisor = util.GetHypervisor() if hypervisor == "vmware" { // add vmware to the end - we don't want to over-ride an choices the user has made cfg.Rancher.CloudInit.Datasources = append(cfg.Rancher.CloudInit.Datasources, hypervisor) @@ -343,13 +341,22 @@ func RunInit() error { return config.LoadConfig(), nil }}, - config.CfgFuncData{"read cfg files", func(cfg *config.CloudConfig) (*config.CloudConfig, error) { + config.CfgFuncData{"read cfg and log files", func(cfg *config.CloudConfig) (*config.CloudConfig, error) { filesToCopy := []string{ config.CloudConfigInitFile, config.CloudConfigBootFile, config.CloudConfigNetworkFile, config.MetaDataFile, } + // And all the files in /var/log/boot/ + // TODO: I wonder if we can put this code into the log module, and have things write to the buffer until we FsReady() + bootLog := "/var/log/boot/" + if files, err := ioutil.ReadDir(bootLog); err == nil { + for _, file := range files { + filePath := filepath.Join(bootLog, file.Name()) + filesToCopy = append(filesToCopy, filePath) + } + } for _, name := range filesToCopy { if _, err := os.Lstat(name); !os.IsNotExist(err) { content, err := ioutil.ReadFile(name) @@ -357,6 +364,7 @@ func RunInit() error { log.Errorf("read cfg file (%s) %s", name, err) continue } + log.Debugf("Saved %s to memory", name) configFiles[name] = content } } @@ -373,8 +381,7 @@ func RunInit() error { return cfg, nil }}, config.CfgFuncData{"mount OEM2", mountOem}, - config.CfgFuncData{"write cfg files", func(cfg *config.CloudConfig) (*config.CloudConfig, error) { - log.FsReady() + config.CfgFuncData{"write cfg and log files", func(cfg *config.CloudConfig) (*config.CloudConfig, error) { for name, content := range configFiles { if err := os.MkdirAll(filepath.Dir(name), os.ModeDir|0700); err != nil { log.Error(err) @@ -382,6 +389,7 @@ func RunInit() error { if err := util.WriteFileAtomic(name, content, 400); err != nil { log.Error(err) } + log.Infof("Wrote log to %s", name) } if err := os.MkdirAll(config.VarRancherDir, os.ModeDir|0755); err != nil { log.Error(err) @@ -389,6 +397,8 @@ func RunInit() error { if err := os.Chmod(config.VarRancherDir, os.ModeDir|0755); err != nil { log.Error(err) } + log.FsReady() + log.Debugf("WARNING: switchroot and mount OEM2 phases not written to log file") return cfg, nil }}, @@ -442,15 +452,6 @@ func RunInit() error { return pidOne() } -func checkHypervisor(cfg *config.CloudConfig) string { - if cpuid.CPU.HypervisorName == "" { - log.Infof("ros init: No Detected Hypervisor") - } else { - log.Infof("ros init: Detected Hypervisor: %s", cpuid.CPU.HypervisorName) - } - return cpuid.CPU.HypervisorName -} - func enableHypervisorService(cfg *config.CloudConfig, hypervisorName string) { if hypervisorName == "" { return diff --git a/log/log.go b/log/log.go index dacea7d0..f6b3f944 100755 --- a/log/log.go +++ b/log/log.go @@ -5,8 +5,6 @@ import ( "os" "path/filepath" - "io/ioutil" - "github.com/Sirupsen/logrus" ) @@ -132,8 +130,8 @@ func InitLogger() { func logTheseApps() bool { // TODO: mmm, not very functional. if filepath.Base(os.Args[0]) == "ros" || - filepath.Base(os.Args[0]) == "host_ros" || - filepath.Base(os.Args[0]) == "system-docker" { + // filepath.Base(os.Args[0]) == "system-docker" || + filepath.Base(os.Args[0]) == "host_ros" { return false } return true @@ -145,7 +143,12 @@ func logTheseApps() bool { func InitDeferedLogger() { if logTheseApps() { innerInit(true) - logrus.SetOutput(ioutil.Discard) + //logrus.SetOutput(ioutil.Discard) + // write to dmesg until we can write to file. (maybe we can do this if rancher.debug=true?) + f, err := os.OpenFile("/dev/kmsg", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644) + if err == nil { + logrus.SetOutput(f) + } pwd, err := os.Getwd() if err != nil { diff --git a/os-config.tpl.yml b/os-config.tpl.yml index 0612bcaf..08b6a803 100644 --- a/os-config.tpl.yml +++ b/os-config.tpl.yml @@ -34,6 +34,7 @@ rancher: - /usr/bin/ros:/usr/bin/ros:ro - /usr/share/ros:/usr/share/ros:ro - /var/lib/rancher:/var/lib/rancher:ro + - /var/log:/var/log cloud_init_services: cloud-init: image: {{.OS_REPO}}/os-base:{{.VERSION}}{{.SUFFIX}} @@ -57,6 +58,7 @@ rancher: - /usr/share/ros:/usr/share/ros:ro - /var/lib/rancher:/var/lib/rancher - /var/lib/rancher/conf:/var/lib/rancher/conf + - /var/log:/var/log bootstrap_docker: bridge: none storage_driver: overlay diff --git a/util/util_linux.go b/util/util_linux.go index e93e228c..7525b0d8 100755 --- a/util/util_linux.go +++ b/util/util_linux.go @@ -11,6 +11,7 @@ import ( "strings" "syscall" + "github.com/SvenDowideit/cpuid" "github.com/docker/docker/pkg/mount" "github.com/rancher/os/log" ) @@ -121,3 +122,13 @@ func Blkid(label string) (deviceName, deviceType string) { } return } + +// GetHypervisor tries to detect if we're running in a VM, and returns a string for its type +func GetHypervisor() string { + if cpuid.CPU.HypervisorName == "" { + log.Infof("ros init: No Detected Hypervisor") + } else { + log.Infof("ros init: Detected Hypervisor: %s", cpuid.CPU.HypervisorName) + } + return cpuid.CPU.HypervisorName +}