diff --git a/cmd/cloudinitexecute/authorize_ssh_keys.go b/cmd/cloudinitexecute/authorize_ssh_keys.go index e0929889..22842f7f 100644 --- a/cmd/cloudinitexecute/authorize_ssh_keys.go +++ b/cmd/cloudinitexecute/authorize_ssh_keys.go @@ -7,8 +7,8 @@ import ( "strconv" "strings" - "github.com/rancher/os/log" - "github.com/rancher/os/util" + "github.com/rancher/os/pkg/log" + "github.com/rancher/os/pkg/util" ) const ( diff --git a/cmd/cloudinitexecute/cloudinitexecute.go b/cmd/cloudinitexecute/cloudinitexecute.go index e6d0bb3e..9ba35b12 100644 --- a/cmd/cloudinitexecute/cloudinitexecute.go +++ b/cmd/cloudinitexecute/cloudinitexecute.go @@ -12,9 +12,9 @@ import ( rancherConfig "github.com/rancher/os/config" "github.com/rancher/os/config/cloudinit/config" "github.com/rancher/os/config/cloudinit/system" - "github.com/rancher/os/docker" - "github.com/rancher/os/log" - "github.com/rancher/os/util" + "github.com/rancher/os/pkg/docker" + "github.com/rancher/os/pkg/log" + "github.com/rancher/os/pkg/util" "golang.org/x/net/context" ) diff --git a/cmd/cloudinitsave/cloudinitsave.go b/cmd/cloudinitsave/cloudinitsave.go index 67525163..ce9de454 100644 --- a/cmd/cloudinitsave/cloudinitsave.go +++ b/cmd/cloudinitsave/cloudinitsave.go @@ -43,9 +43,9 @@ import ( "github.com/rancher/os/config/cloudinit/datasource/url" "github.com/rancher/os/config/cloudinit/datasource/vmware" "github.com/rancher/os/config/cloudinit/pkg" - "github.com/rancher/os/log" - "github.com/rancher/os/netconf" - "github.com/rancher/os/util" + "github.com/rancher/os/pkg/log" + "github.com/rancher/os/pkg/netconf" + "github.com/rancher/os/pkg/util" ) const ( diff --git a/cmd/control/autologin.go b/cmd/control/autologin.go index 6bb4c00b..75afebda 100644 --- a/cmd/control/autologin.go +++ b/cmd/control/autologin.go @@ -10,7 +10,7 @@ import ( "github.com/codegangsta/cli" "github.com/rancher/os/config" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/log" ) func AutologinMain() { diff --git a/cmd/control/bootstrap.go b/cmd/control/bootstrap.go index 5111ff2f..729efc05 100644 --- a/cmd/control/bootstrap.go +++ b/cmd/control/bootstrap.go @@ -8,8 +8,8 @@ import ( "time" "github.com/rancher/os/config" - "github.com/rancher/os/log" - "github.com/rancher/os/util" + "github.com/rancher/os/pkg/log" + "github.com/rancher/os/pkg/util" ) func BootstrapMain() { diff --git a/cmd/control/cli.go b/cmd/control/cli.go index 3a1679b7..8ce2cb10 100644 --- a/cmd/control/cli.go +++ b/cmd/control/cli.go @@ -7,7 +7,7 @@ import ( "github.com/codegangsta/cli" "github.com/rancher/os/cmd/control/service" "github.com/rancher/os/config" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/log" ) func Main() { diff --git a/cmd/control/config.go b/cmd/control/config.go index da4e0f89..e9e95014 100644 --- a/cmd/control/config.go +++ b/cmd/control/config.go @@ -11,11 +11,11 @@ import ( "text/template" yaml "github.com/cloudfoundry-incubator/candiedyaml" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/log" "github.com/codegangsta/cli" "github.com/rancher/os/config" - "github.com/rancher/os/util" + "github.com/rancher/os/pkg/util" ) func configSubcommands() []cli.Command { diff --git a/cmd/control/console.go b/cmd/control/console.go index 439e3feb..bffe42ae 100644 --- a/cmd/control/console.go +++ b/cmd/control/console.go @@ -12,12 +12,12 @@ import ( composeConfig "github.com/docker/libcompose/config" "github.com/docker/libcompose/project/options" "github.com/rancher/os/cmd/control/service" - "github.com/rancher/os/compose" "github.com/rancher/os/config" - "github.com/rancher/os/docker" - "github.com/rancher/os/log" - "github.com/rancher/os/util" - "github.com/rancher/os/util/network" + "github.com/rancher/os/pkg/compose" + "github.com/rancher/os/pkg/docker" + "github.com/rancher/os/pkg/log" + "github.com/rancher/os/pkg/util" + "github.com/rancher/os/pkg/util/network" ) func consoleSubcommands() []cli.Command { diff --git a/cmd/control/console_init.go b/cmd/control/console_init.go index 1eaa78da..39d62861 100644 --- a/cmd/control/console_init.go +++ b/cmd/control/console_init.go @@ -15,11 +15,11 @@ import ( "github.com/codegangsta/cli" "github.com/rancher/os/cmd/cloudinitexecute" - "github.com/rancher/os/compose" "github.com/rancher/os/config" "github.com/rancher/os/config/cmdline" - "github.com/rancher/os/log" - "github.com/rancher/os/util" + "github.com/rancher/os/pkg/compose" + "github.com/rancher/os/pkg/log" + "github.com/rancher/os/pkg/util" ) const ( diff --git a/cmd/control/dev.go b/cmd/control/dev.go index b7626909..b26aa9df 100644 --- a/cmd/control/dev.go +++ b/cmd/control/dev.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/codegangsta/cli" - "github.com/rancher/os/util" + "github.com/rancher/os/pkg/util" ) func devAction(c *cli.Context) error { diff --git a/cmd/control/docker_init.go b/cmd/control/docker_init.go index 513efb16..874a6411 100644 --- a/cmd/control/docker_init.go +++ b/cmd/control/docker_init.go @@ -10,8 +10,8 @@ import ( "time" "github.com/codegangsta/cli" - "github.com/rancher/os/log" - "github.com/rancher/os/util" + "github.com/rancher/os/pkg/log" + "github.com/rancher/os/pkg/util" ) const ( diff --git a/cmd/control/engine.go b/cmd/control/engine.go index 158bf607..585ae5d5 100644 --- a/cmd/control/engine.go +++ b/cmd/control/engine.go @@ -23,12 +23,12 @@ import ( "github.com/pkg/errors" "github.com/rancher/os/cmd/control/service" "github.com/rancher/os/cmd/control/service/app" - "github.com/rancher/os/compose" "github.com/rancher/os/config" - "github.com/rancher/os/docker" - "github.com/rancher/os/log" - "github.com/rancher/os/util" - "github.com/rancher/os/util/network" + "github.com/rancher/os/pkg/compose" + "github.com/rancher/os/pkg/docker" + "github.com/rancher/os/pkg/log" + "github.com/rancher/os/pkg/util" + "github.com/rancher/os/pkg/util/network" ) var ( diff --git a/cmd/control/entrypoint.go b/cmd/control/entrypoint.go index b7219e08..666ca699 100644 --- a/cmd/control/entrypoint.go +++ b/cmd/control/entrypoint.go @@ -6,13 +6,13 @@ import ( "syscall" "github.com/codegangsta/cli" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/log" "golang.org/x/net/context" "github.com/rancher/os/cmd/cloudinitexecute" "github.com/rancher/os/config" - "github.com/rancher/os/docker" - "github.com/rancher/os/util" + "github.com/rancher/os/pkg/docker" + "github.com/rancher/os/pkg/util" ) const ( diff --git a/cmd/control/env.go b/cmd/control/env.go index 166c6ad3..7a598eeb 100644 --- a/cmd/control/env.go +++ b/cmd/control/env.go @@ -8,7 +8,7 @@ import ( "github.com/codegangsta/cli" "github.com/rancher/os/config" - "github.com/rancher/os/util" + "github.com/rancher/os/pkg/util" ) func envAction(c *cli.Context) error { diff --git a/cmd/control/install.go b/cmd/control/install.go index 1908193d..1d497346 100644 --- a/cmd/control/install.go +++ b/cmd/control/install.go @@ -14,7 +14,7 @@ import ( "strconv" "strings" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/log" "github.com/codegangsta/cli" "github.com/pkg/errors" @@ -22,8 +22,8 @@ import ( "github.com/rancher/os/cmd/control/install" "github.com/rancher/os/cmd/power" "github.com/rancher/os/config" - "github.com/rancher/os/dfs" // TODO: move CopyFile into util or something. - "github.com/rancher/os/util" + "github.com/rancher/os/pkg/dfs" // TODO: move CopyFile into util or something. + "github.com/rancher/os/pkg/util" ) var installCommand = cli.Command{ diff --git a/cmd/control/install/grub.go b/cmd/control/install/grub.go index 79ffbc58..e6c9152e 100644 --- a/cmd/control/install/grub.go +++ b/cmd/control/install/grub.go @@ -6,7 +6,7 @@ import ( "os/exec" "path/filepath" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/log" ) func RunGrub(baseName, device string) error { diff --git a/cmd/control/install/install.go b/cmd/control/install/install.go index 7798a9bc..3b2bb2a1 100644 --- a/cmd/control/install/install.go +++ b/cmd/control/install/install.go @@ -7,8 +7,8 @@ import ( "strings" "github.com/rancher/os/config" - "github.com/rancher/os/log" - "github.com/rancher/os/util" + "github.com/rancher/os/pkg/log" + "github.com/rancher/os/pkg/util" ) const BootDir = "boot/" diff --git a/cmd/control/install/syslinux.go b/cmd/control/install/syslinux.go index f19bc1fb..ca15acf6 100644 --- a/cmd/control/install/syslinux.go +++ b/cmd/control/install/syslinux.go @@ -9,7 +9,7 @@ import ( "path/filepath" "strings" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/log" ) func syslinuxConfig(menu BootVars) error { diff --git a/cmd/control/os.go b/cmd/control/os.go index ff196576..5fc44885 100644 --- a/cmd/control/os.go +++ b/cmd/control/os.go @@ -11,18 +11,18 @@ import ( "golang.org/x/net/context" yaml "github.com/cloudfoundry-incubator/candiedyaml" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/log" "github.com/codegangsta/cli" dockerClient "github.com/docker/engine-api/client" composeConfig "github.com/docker/libcompose/config" "github.com/docker/libcompose/project/options" "github.com/rancher/os/cmd/power" - "github.com/rancher/os/compose" "github.com/rancher/os/config" - "github.com/rancher/os/docker" - "github.com/rancher/os/util" - "github.com/rancher/os/util/network" + "github.com/rancher/os/pkg/compose" + "github.com/rancher/os/pkg/docker" + "github.com/rancher/os/pkg/util" + "github.com/rancher/os/pkg/util/network" ) type Images struct { diff --git a/cmd/control/preload.go b/cmd/control/preload.go index 8455ae35..f7a829dd 100644 --- a/cmd/control/preload.go +++ b/cmd/control/preload.go @@ -16,8 +16,8 @@ import ( dockerClient "github.com/docker/engine-api/client" "github.com/docker/engine-api/types" "github.com/rancher/os/config" - "github.com/rancher/os/docker" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/docker" + "github.com/rancher/os/pkg/log" ) const ( diff --git a/cmd/control/recovery_init.go b/cmd/control/recovery_init.go index 21435e30..705a469d 100644 --- a/cmd/control/recovery_init.go +++ b/cmd/control/recovery_init.go @@ -6,7 +6,7 @@ import ( "syscall" "github.com/codegangsta/cli" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/log" ) func recoveryInitAction(c *cli.Context) error { diff --git a/cmd/control/service/app/app.go b/cmd/control/service/app/app.go index a213db21..04aa95dc 100644 --- a/cmd/control/service/app/app.go +++ b/cmd/control/service/app/app.go @@ -7,7 +7,7 @@ import ( "strings" "syscall" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/log" "golang.org/x/net/context" "github.com/codegangsta/cli" diff --git a/cmd/control/service/service.go b/cmd/control/service/service.go index a7d5f83d..8180dceb 100644 --- a/cmd/control/service/service.go +++ b/cmd/control/service/service.go @@ -8,11 +8,11 @@ import ( dockerApp "github.com/docker/libcompose/cli/docker/app" "github.com/docker/libcompose/project" "github.com/rancher/os/cmd/control/service/command" - "github.com/rancher/os/compose" "github.com/rancher/os/config" - "github.com/rancher/os/log" - "github.com/rancher/os/util" - "github.com/rancher/os/util/network" + "github.com/rancher/os/pkg/compose" + "github.com/rancher/os/pkg/log" + "github.com/rancher/os/pkg/util" + "github.com/rancher/os/pkg/util/network" ) type projectFactory struct { diff --git a/cmd/control/switch_console.go b/cmd/control/switch_console.go index b4e1b712..fb5ccc3b 100644 --- a/cmd/control/switch_console.go +++ b/cmd/control/switch_console.go @@ -5,9 +5,9 @@ import ( "github.com/codegangsta/cli" "github.com/docker/libcompose/project/options" - "github.com/rancher/os/compose" "github.com/rancher/os/config" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/compose" + "github.com/rancher/os/pkg/log" "golang.org/x/net/context" ) diff --git a/cmd/control/tlsconf.go b/cmd/control/tlsconf.go index 708c9e99..3764f15e 100644 --- a/cmd/control/tlsconf.go +++ b/cmd/control/tlsconf.go @@ -5,12 +5,12 @@ import ( "os" "path/filepath" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/log" "github.com/codegangsta/cli" machineUtil "github.com/docker/machine/utils" "github.com/rancher/os/config" - "github.com/rancher/os/util" + "github.com/rancher/os/pkg/util" ) const ( diff --git a/cmd/control/udevsettle.go b/cmd/control/udevsettle.go index 1f378140..f185da94 100644 --- a/cmd/control/udevsettle.go +++ b/cmd/control/udevsettle.go @@ -5,7 +5,7 @@ import ( "os/exec" "github.com/codegangsta/cli" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/log" ) func udevSettleAction(c *cli.Context) { diff --git a/cmd/control/user_docker.go b/cmd/control/user_docker.go index 7a5f0695..ba337f32 100644 --- a/cmd/control/user_docker.go +++ b/cmd/control/user_docker.go @@ -15,11 +15,11 @@ import ( "github.com/codegangsta/cli" composeClient "github.com/docker/libcompose/docker/client" "github.com/docker/libcompose/project" - "github.com/rancher/os/compose" "github.com/rancher/os/config" - rosDocker "github.com/rancher/os/docker" - "github.com/rancher/os/log" - "github.com/rancher/os/util" + "github.com/rancher/os/pkg/compose" + rosDocker "github.com/rancher/os/pkg/docker" + "github.com/rancher/os/pkg/log" + "github.com/rancher/os/pkg/util" ) const ( diff --git a/cmd/control/util.go b/cmd/control/util.go index 7df8b9aa..2a3abc99 100644 --- a/cmd/control/util.go +++ b/cmd/control/util.go @@ -7,7 +7,7 @@ import ( "strings" "github.com/rancher/os/config" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/log" ) func yes(question string) bool { diff --git a/cmd/init/init.go b/cmd/init/init.go new file mode 100644 index 00000000..c881575f --- /dev/null +++ b/cmd/init/init.go @@ -0,0 +1,92 @@ +// +build linux + +package init + +import ( + "fmt" + + "github.com/rancher/os/pkg/init/selinux" + + "github.com/rancher/os/pkg/init/docker" + "github.com/rancher/os/pkg/init/one" + + "github.com/rancher/os/config" + "github.com/rancher/os/pkg/dfs" + "github.com/rancher/os/pkg/init/b2d" + "github.com/rancher/os/pkg/init/cloudinit" + "github.com/rancher/os/pkg/init/configfiles" + "github.com/rancher/os/pkg/init/debug" + "github.com/rancher/os/pkg/init/env" + "github.com/rancher/os/pkg/init/fsmount" + "github.com/rancher/os/pkg/init/hypervisor" + "github.com/rancher/os/pkg/init/modules" + "github.com/rancher/os/pkg/init/prepare" + "github.com/rancher/os/pkg/init/recovery" + "github.com/rancher/os/pkg/init/sharedroot" + "github.com/rancher/os/pkg/init/switchroot" + "github.com/rancher/os/pkg/log" + "github.com/rancher/os/pkg/sysinit" +) + +func MainInit() { + 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 { + fmt.Printf("Starting Recovery console: %v\n", r) + recovery.Recovery(nil) + } + }() + + if err := RunInit(); err != nil { + log.Fatal(err) + } +} + +func RunInit() error { + initFuncs := config.CfgFuncs{ + {"set env", env.Init}, + {"preparefs", prepare.FS}, + {"save init cmdline", prepare.SaveCmdline}, + {"mount OEM", fsmount.OEM}, + {"debug save cfg", debug.PrintAndLoadConfig}, + {"load modules", modules.LoadModules}, + {"recovery console", recovery.LoadRecoveryConsole}, + {"cloud-init", cloudinit.CloudInit}, + {"b2d env", b2d.B2D}, + {"mount and bootstrap", fsmount.MountAndBootstrap}, + {"read cfg and log files", configfiles.ReadConfigFiles}, + {"switchroot", switchroot.SwitchRoot}, + {"mount OEM2", fsmount.OEM}, + {"write cfg and log files", configfiles.WriteConfigFiles}, + {"hypervisor Env", hypervisor.Env}, + {"b2d Env", b2d.Env}, + {"hypervisor tools", hypervisor.Tools}, + {"preparefs2", prepare.FS}, + {"load modules2", modules.LoadModules}, + {"set proxy env", env.Proxy}, + {"init SELinux", selinux.Initialize}, + {"setupSharedRoot", sharedroot.Setup}, + {"sysinit", sysinit.RunSysInit}, + } + + cfg, err := config.ChainCfgFuncs(nil, initFuncs) + if err != nil { + recovery.Recovery(err) + } + + launchConfig, args := docker.GetLaunchConfig(cfg, &cfg.Rancher.SystemDocker) + launchConfig.Fork = !cfg.Rancher.SystemDocker.Exec + //launchConfig.NoLog = true + + log.Info("Launching System Docker") + _, err = dfs.LaunchDocker(launchConfig, config.SystemDockerBin, args...) + if err != nil { + log.Errorf("Error Launching System Docker: %s", err) + recovery.Recovery(err) + return err + } + // Code never gets here - rancher.system_docker.exec=true + + return one.PidOne() +} diff --git a/cmd/network/network.go b/cmd/network/network.go index 9db85d1f..35ae00f5 100644 --- a/cmd/network/network.go +++ b/cmd/network/network.go @@ -3,15 +3,15 @@ package network import ( "golang.org/x/net/context" - "github.com/rancher/os/docker" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/docker" + "github.com/rancher/os/pkg/log" "io/ioutil" "github.com/docker/libnetwork/resolvconf" "github.com/rancher/os/config" - "github.com/rancher/os/hostname" - "github.com/rancher/os/netconf" + "github.com/rancher/os/pkg/hostname" + "github.com/rancher/os/pkg/netconf" ) func Main() { diff --git a/cmd/power/power.go b/cmd/power/power.go index 41e95444..851a85ff 100644 --- a/cmd/power/power.go +++ b/cmd/power/power.go @@ -17,10 +17,10 @@ import ( "github.com/docker/engine-api/types/filters" "github.com/rancher/os/cmd/control/install" "github.com/rancher/os/config" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/log" - "github.com/rancher/os/docker" - "github.com/rancher/os/util" + "github.com/rancher/os/pkg/docker" + "github.com/rancher/os/pkg/util" ) // You can't shutdown the system from a process in console because we want to stop the console container. diff --git a/cmd/power/shutdown.go b/cmd/power/shutdown.go index 23f05d45..daa11ad7 100644 --- a/cmd/power/shutdown.go +++ b/cmd/power/shutdown.go @@ -10,7 +10,7 @@ import ( "github.com/codegangsta/cli" "github.com/rancher/os/cmd/control/install" "github.com/rancher/os/config" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/log" ) var ( diff --git a/cmd/respawn/respawn.go b/cmd/respawn/respawn.go index 2ef47b08..58a35a83 100644 --- a/cmd/respawn/respawn.go +++ b/cmd/respawn/respawn.go @@ -15,7 +15,7 @@ import ( "github.com/codegangsta/cli" "github.com/rancher/os/config" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/log" ) var ( diff --git a/cmd/sysinit/sysinit.go b/cmd/sysinit/sysinit.go index 2585d283..0489a44a 100644 --- a/cmd/sysinit/sysinit.go +++ b/cmd/sysinit/sysinit.go @@ -4,8 +4,8 @@ import ( "io/ioutil" "os" - initPkg "github.com/rancher/os/init" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/log" + "github.com/rancher/os/pkg/sysinit" ) func Main() { @@ -15,7 +15,7 @@ func Main() { log.Infof("Resolv.conf == [%s], %v", resolve, err) log.Infof("Exec %v", os.Args) - if err := initPkg.SysInit(); err != nil { + if err := sysinit.SysInit(); err != nil { log.Fatal(err) } } diff --git a/cmd/wait/wait.go b/cmd/wait/wait.go index fd41c515..9e2fcbe9 100644 --- a/cmd/wait/wait.go +++ b/cmd/wait/wait.go @@ -4,8 +4,8 @@ import ( "os" "github.com/rancher/os/config" - "github.com/rancher/os/docker" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/docker" + "github.com/rancher/os/pkg/log" ) func Main() { diff --git a/config/cloudinit/cover b/config/cloudinit/cover index d8db5731..b4b046a7 100755 --- a/config/cloudinit/cover +++ b/config/cloudinit/cover @@ -22,6 +22,6 @@ COVERPKG=${COVERPKG//./} # generate arg for "go test" export COVER="-coverprofile ${COVEROUT}/${COVERPKG}.out" -source ./test +source test go tool cover -html=${COVEROUT}/${COVERPKG}.out diff --git a/config/cloudinit/datasource/configdrive/configdrive.go b/config/cloudinit/datasource/configdrive/configdrive.go index a42b197e..b1b09ba2 100644 --- a/config/cloudinit/datasource/configdrive/configdrive.go +++ b/config/cloudinit/datasource/configdrive/configdrive.go @@ -22,11 +22,11 @@ import ( "path" "syscall" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/log" "github.com/docker/docker/pkg/mount" "github.com/rancher/os/config/cloudinit/datasource" - "github.com/rancher/os/util" + "github.com/rancher/os/pkg/util" ) const ( diff --git a/config/cloudinit/datasource/datasource.go b/config/cloudinit/datasource/datasource.go index dfd522eb..8af8d655 100644 --- a/config/cloudinit/datasource/datasource.go +++ b/config/cloudinit/datasource/datasource.go @@ -17,7 +17,7 @@ package datasource import ( "net" - "github.com/rancher/os/netconf" + "github.com/rancher/os/pkg/netconf" ) type Datasource interface { diff --git a/config/cloudinit/datasource/metadata/aliyun/metadata.go b/config/cloudinit/datasource/metadata/aliyun/metadata.go index 539bfe05..467cb60b 100644 --- a/config/cloudinit/datasource/metadata/aliyun/metadata.go +++ b/config/cloudinit/datasource/metadata/aliyun/metadata.go @@ -5,7 +5,7 @@ import ( "log" "strings" - "github.com/rancher/os/netconf" + "github.com/rancher/os/pkg/netconf" "github.com/rancher/os/config/cloudinit/datasource" "github.com/rancher/os/config/cloudinit/datasource/metadata" diff --git a/config/cloudinit/datasource/metadata/cloudstack/metadata.go b/config/cloudinit/datasource/metadata/cloudstack/metadata.go index d8261ef7..f20efffa 100644 --- a/config/cloudinit/datasource/metadata/cloudstack/metadata.go +++ b/config/cloudinit/datasource/metadata/cloudstack/metadata.go @@ -19,12 +19,12 @@ import ( "strconv" "strings" - "github.com/rancher/os/netconf" + "github.com/rancher/os/pkg/netconf" "github.com/rancher/os/config/cloudinit/datasource" "github.com/rancher/os/config/cloudinit/datasource/metadata" "github.com/rancher/os/config/cloudinit/pkg" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/log" ) const ( diff --git a/config/cloudinit/datasource/metadata/digitalocean/metadata.go b/config/cloudinit/datasource/metadata/digitalocean/metadata.go index 378c402e..5974afc6 100755 --- a/config/cloudinit/datasource/metadata/digitalocean/metadata.go +++ b/config/cloudinit/datasource/metadata/digitalocean/metadata.go @@ -19,7 +19,7 @@ import ( "fmt" "strconv" - "github.com/rancher/os/netconf" + "github.com/rancher/os/pkg/netconf" "net" diff --git a/config/cloudinit/datasource/metadata/digitalocean/metadata_test.go b/config/cloudinit/datasource/metadata/digitalocean/metadata_test.go index 335f1a85..2df116ed 100755 --- a/config/cloudinit/datasource/metadata/digitalocean/metadata_test.go +++ b/config/cloudinit/datasource/metadata/digitalocean/metadata_test.go @@ -20,7 +20,7 @@ import ( "reflect" "testing" - "github.com/rancher/os/netconf" + "github.com/rancher/os/pkg/netconf" "github.com/rancher/os/config/cloudinit/datasource" "github.com/rancher/os/config/cloudinit/datasource/metadata" diff --git a/config/cloudinit/datasource/metadata/ec2/metadata.go b/config/cloudinit/datasource/metadata/ec2/metadata.go index b060db62..6ed29070 100644 --- a/config/cloudinit/datasource/metadata/ec2/metadata.go +++ b/config/cloudinit/datasource/metadata/ec2/metadata.go @@ -20,7 +20,7 @@ import ( "net" "strings" - "github.com/rancher/os/netconf" + "github.com/rancher/os/pkg/netconf" "github.com/rancher/os/config/cloudinit/datasource" "github.com/rancher/os/config/cloudinit/datasource/metadata" diff --git a/config/cloudinit/datasource/metadata/ec2/metadata_test.go b/config/cloudinit/datasource/metadata/ec2/metadata_test.go index 87ceff97..e08c95ac 100644 --- a/config/cloudinit/datasource/metadata/ec2/metadata_test.go +++ b/config/cloudinit/datasource/metadata/ec2/metadata_test.go @@ -24,7 +24,7 @@ import ( "github.com/rancher/os/config/cloudinit/datasource/metadata" "github.com/rancher/os/config/cloudinit/datasource/metadata/test" "github.com/rancher/os/config/cloudinit/pkg" - "github.com/rancher/os/netconf" + "github.com/rancher/os/pkg/netconf" ) func TestType(t *testing.T) { diff --git a/config/cloudinit/datasource/metadata/metadata.go b/config/cloudinit/datasource/metadata/metadata.go index 023d6d20..70355065 100644 --- a/config/cloudinit/datasource/metadata/metadata.go +++ b/config/cloudinit/datasource/metadata/metadata.go @@ -22,7 +22,7 @@ import ( "strings" "github.com/rancher/os/config/cloudinit/pkg" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/log" ) type Service struct { diff --git a/config/cloudinit/datasource/metadata/packet/metadata.go b/config/cloudinit/datasource/metadata/packet/metadata.go index d7423fbc..b522f234 100644 --- a/config/cloudinit/datasource/metadata/packet/metadata.go +++ b/config/cloudinit/datasource/metadata/packet/metadata.go @@ -21,8 +21,8 @@ import ( "github.com/rancher/os/config/cloudinit/datasource" "github.com/rancher/os/config/cloudinit/datasource/metadata" - "github.com/rancher/os/log" - "github.com/rancher/os/netconf" + "github.com/rancher/os/pkg/log" + "github.com/rancher/os/pkg/netconf" yaml "github.com/cloudfoundry-incubator/candiedyaml" packetMetadata "github.com/packethost/packngo/metadata" diff --git a/config/cloudinit/datasource/proccmdline/proc_cmdline.go b/config/cloudinit/datasource/proccmdline/proc_cmdline.go index cc7cd4bb..d5438c65 100644 --- a/config/cloudinit/datasource/proccmdline/proc_cmdline.go +++ b/config/cloudinit/datasource/proccmdline/proc_cmdline.go @@ -20,7 +20,7 @@ import ( "io/ioutil" "strings" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/log" "github.com/rancher/os/config/cloudinit/datasource" "github.com/rancher/os/config/cloudinit/pkg" diff --git a/config/cloudinit/datasource/url/url.go b/config/cloudinit/datasource/url/url.go index 9ee62a24..56ee3e76 100644 --- a/config/cloudinit/datasource/url/url.go +++ b/config/cloudinit/datasource/url/url.go @@ -19,7 +19,7 @@ import ( "github.com/rancher/os/config/cloudinit/datasource" "github.com/rancher/os/config/cloudinit/pkg" - "github.com/rancher/os/util/network" + "github.com/rancher/os/pkg/util/network" ) type RemoteFile struct { diff --git a/config/cloudinit/datasource/vmware/vmware.go b/config/cloudinit/datasource/vmware/vmware.go index e5eb1206..c5bcbed5 100644 --- a/config/cloudinit/datasource/vmware/vmware.go +++ b/config/cloudinit/datasource/vmware/vmware.go @@ -21,8 +21,8 @@ import ( "github.com/rancher/os/config/cloudinit/config" "github.com/rancher/os/config/cloudinit/datasource" - "github.com/rancher/os/log" - "github.com/rancher/os/netconf" + "github.com/rancher/os/pkg/log" + "github.com/rancher/os/pkg/netconf" ) type readConfigFunction func(key string) (string, error) diff --git a/config/cloudinit/datasource/vmware/vmware_amd64.go b/config/cloudinit/datasource/vmware/vmware_amd64.go index a0a5187f..a0b71c27 100644 --- a/config/cloudinit/datasource/vmware/vmware_amd64.go +++ b/config/cloudinit/datasource/vmware/vmware_amd64.go @@ -18,8 +18,8 @@ import ( "io/ioutil" "os" - "github.com/rancher/os/log" - "github.com/rancher/os/util" + "github.com/rancher/os/pkg/log" + "github.com/rancher/os/pkg/util" "github.com/rancher/os/config/cloudinit/pkg" diff --git a/config/cloudinit/datasource/vmware/vmware_test.go b/config/cloudinit/datasource/vmware/vmware_test.go index c2eab990..0d03d4ee 100644 --- a/config/cloudinit/datasource/vmware/vmware_test.go +++ b/config/cloudinit/datasource/vmware/vmware_test.go @@ -25,7 +25,7 @@ import ( "testing" "github.com/rancher/os/config/cloudinit/datasource" - "github.com/rancher/os/netconf" + "github.com/rancher/os/pkg/netconf" ) type MockHypervisor map[string]string diff --git a/config/cloudinit/datasource/waagent/waagent.go b/config/cloudinit/datasource/waagent/waagent.go index bd04599c..d132f090 100644 --- a/config/cloudinit/datasource/waagent/waagent.go +++ b/config/cloudinit/datasource/waagent/waagent.go @@ -22,7 +22,7 @@ import ( "os" "path" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/log" "github.com/rancher/os/config/cloudinit/datasource" ) diff --git a/config/cloudinit/network/packet.go b/config/cloudinit/network/packet.go index 8bed1888..3adde8e6 100644 --- a/config/cloudinit/network/packet.go +++ b/config/cloudinit/network/packet.go @@ -17,7 +17,7 @@ package network import ( "net" - "github.com/rancher/os/netconf" + "github.com/rancher/os/pkg/netconf" ) func ProcessPacketNetconf(netdata netconf.NetworkConfig) ([]InterfaceGenerator, error) { diff --git a/config/cloudinit/test b/config/cloudinit/test index be9454ab..8cd4dd41 100755 --- a/config/cloudinit/test +++ b/config/cloudinit/test @@ -1,6 +1,6 @@ #!/bin/bash -e -source ./build +source build SRC=$(find . -name '*.go' \ -not -path "./vendor/*") diff --git a/config/cmdline/cmdline.go b/config/cmdline/cmdline.go index edcecd49..8af4f3fe 100644 --- a/config/cmdline/cmdline.go +++ b/config/cmdline/cmdline.go @@ -5,7 +5,7 @@ import ( "strings" yaml "github.com/cloudfoundry-incubator/candiedyaml" - "github.com/rancher/os/util" + "github.com/rancher/os/pkg/util" ) func Read(parseAll bool) (m map[interface{}]interface{}, err error) { @@ -125,7 +125,7 @@ func UnmarshalOrReturnString(value string) (result interface{}) { } func Parse(cmdLine string, parseAll bool) map[interface{}]interface{} { - result := make(map[interface{}]interface{}) + result := map[interface{}]interface{}{} outer: for _, part := range strings.Split(cmdLine, " ") { diff --git a/config/config.go b/config/config.go index 9aa4021d..81e6f723 100644 --- a/config/config.go +++ b/config/config.go @@ -6,7 +6,7 @@ import ( yaml "github.com/cloudfoundry-incubator/candiedyaml" "github.com/rancher/os/config/cmdline" - "github.com/rancher/os/util" + "github.com/rancher/os/pkg/util" ) const Banner = ` diff --git a/config/config_test.go b/config/config_test.go index 44204567..bfbce1c7 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -6,7 +6,7 @@ import ( yaml "github.com/cloudfoundry-incubator/candiedyaml" "github.com/rancher/os/config/cmdline" - "github.com/rancher/os/util" + "github.com/rancher/os/pkg/util" "github.com/stretchr/testify/require" ) diff --git a/config/data_funcs.go b/config/data_funcs.go index 82636d43..c337edc6 100644 --- a/config/data_funcs.go +++ b/config/data_funcs.go @@ -1,8 +1,8 @@ package config import ( - "github.com/rancher/os/log" - "github.com/rancher/os/util" + "github.com/rancher/os/pkg/log" + "github.com/rancher/os/pkg/util" ) type CfgFunc func(*CloudConfig) (*CloudConfig, error) diff --git a/config/disk.go b/config/disk.go index ab54a91e..59be8e30 100644 --- a/config/disk.go +++ b/config/disk.go @@ -15,8 +15,8 @@ import ( "github.com/rancher/os/config/cloudinit/datasource" "github.com/rancher/os/config/cloudinit/initialize" "github.com/rancher/os/config/cmdline" - "github.com/rancher/os/log" - "github.com/rancher/os/util" + "github.com/rancher/os/pkg/log" + "github.com/rancher/os/pkg/util" ) func ReadConfig(bytes []byte, substituteMetadataVars bool, files ...string) (*CloudConfig, error) { diff --git a/config/types.go b/config/types.go index 483e9210..3f45be15 100644 --- a/config/types.go +++ b/config/types.go @@ -8,28 +8,22 @@ import ( composeConfig "github.com/docker/libcompose/config" "github.com/rancher/os/config/cloudinit/config" "github.com/rancher/os/config/yaml" - "github.com/rancher/os/netconf" + "github.com/rancher/os/pkg/netconf" ) const ( OEM = "/usr/share/ros/oem" - DockerBin = "/usr/bin/docker" - DockerDistBin = "/usr/bin/docker.dist" RosBin = "/usr/bin/ros" SysInitBin = "/usr/bin/ros-sysinit" - SystemDockerHome = "/var/lib/system-docker" SystemDockerHost = "unix:///var/run/system-docker.sock" DockerHost = "unix:///var/run/docker.sock" ImagesPath = "/usr/share/ros" InitImages = "images-init.tar" SystemImages = "images-system.tar" - ModulesArchive = "/modules.tar" Debug = false SystemDockerLog = "/var/log/system-docker.log" SystemDockerBin = "/usr/bin/system-dockerd" - HashLabel = "io.rancher.os.hash" - IDLabel = "io.rancher.os.id" DetachLabel = "io.rancher.os.detach" CreateOnlyLabel = "io.rancher.os.createonly" ReloadConfigLabel = "io.rancher.os.reloadconfig" @@ -53,6 +47,7 @@ const ( EtcResolvConfFile = "/etc/resolv.conf" MultiDockerConfFile = "/var/lib/rancher/conf.d/m-user-docker.yml" MultiDockerDataDir = "/var/lib/m-user-docker" + State = "/state" ) var ( diff --git a/config/validate_test.go b/config/validate_test.go index 0c975907..83f8d0a8 100644 --- a/config/validate_test.go +++ b/config/validate_test.go @@ -6,7 +6,7 @@ import ( "testing" yaml "github.com/cloudfoundry-incubator/candiedyaml" - "github.com/rancher/os/util" + "github.com/rancher/os/pkg/util" ) func testValidate(t *testing.T, cfg []byte, contains string) { diff --git a/init/bootstrap.go b/init/bootstrap.go deleted file mode 100644 index 2dae2b5b..00000000 --- a/init/bootstrap.go +++ /dev/null @@ -1,91 +0,0 @@ -package init - -import ( - "syscall" - - "github.com/rancher/os/compose" - "github.com/rancher/os/config" - "github.com/rancher/os/dfs" - "github.com/rancher/os/log" - "github.com/rancher/os/util" -) - -func bootstrapServices(cfg *config.CloudConfig) (*config.CloudConfig, error) { - if util.ResolveDevice(cfg.Rancher.State.Dev) != "" && len(cfg.Bootcmd) == 0 { - log.Info("NOT Running Bootstrap") - - return cfg, nil - } - log.Info("Running Bootstrap") - _, err := compose.RunServiceSet("bootstrap", cfg, cfg.Rancher.BootstrapContainers) - return cfg, err -} - -func runCloudInitServiceSet(cfg *config.CloudConfig) (*config.CloudConfig, error) { - log.Info("Running cloud-init services") - _, err := compose.RunServiceSet("cloud-init", cfg, cfg.Rancher.CloudInitServices) - return cfg, err -} - -func startDocker(cfg *config.CloudConfig) (chan interface{}, error) { - launchConfig, args := getLaunchConfig(cfg, &cfg.Rancher.BootstrapDocker) - launchConfig.Fork = true - launchConfig.LogFile = "" - launchConfig.NoLog = true - - cmd, err := dfs.LaunchDocker(launchConfig, config.SystemDockerBin, args...) - if err != nil { - return nil, err - } - - c := make(chan interface{}) - go func() { - <-c - cmd.Process.Signal(syscall.SIGTERM) - cmd.Wait() - c <- struct{}{} - }() - - return c, nil -} - -func stopDocker(c chan interface{}) error { - c <- struct{}{} - <-c - - return nil -} - -func bootstrap(cfg *config.CloudConfig) error { - log.Info("Launching Bootstrap Docker") - - c, err := startDocker(cfg) - if err != nil { - return err - } - - defer stopDocker(c) - - _, err = config.ChainCfgFuncs(cfg, - []config.CfgFuncData{ - config.CfgFuncData{"bootstrap loadImages", loadBootstrapImages}, - config.CfgFuncData{"bootstrap Services", bootstrapServices}, - }) - return err -} - -func runCloudInitServices(cfg *config.CloudConfig) error { - c, err := startDocker(cfg) - if err != nil { - return err - } - - defer stopDocker(c) - - _, err = config.ChainCfgFuncs(cfg, - []config.CfgFuncData{ - config.CfgFuncData{"cloudinit loadImages", loadBootstrapImages}, - config.CfgFuncData{"cloudinit Services", runCloudInitServiceSet}, - }) - return err -} diff --git a/init/init.go b/init/init.go deleted file mode 100644 index b5416f71..00000000 --- a/init/init.go +++ /dev/null @@ -1,564 +0,0 @@ -// +build linux - -package init - -import ( - "bufio" - "fmt" - "io/ioutil" - "os" - "os/exec" - "path/filepath" - "strings" - "syscall" - - "github.com/docker/docker/pkg/mount" - "github.com/rancher/os/config" - "github.com/rancher/os/dfs" - "github.com/rancher/os/log" - "github.com/rancher/os/util" - "github.com/rancher/os/util/network" -) - -const ( - state string = "/state" - boot2DockerMagic string = "boot2docker, please format-me" - - tmpfsMagic int64 = 0x01021994 - ramfsMagic int64 = 0x858458f6 -) - -var ( - mountConfig = dfs.Config{ - CgroupHierarchy: map[string]string{ - "cpu": "cpu", - "cpuacct": "cpu", - "net_cls": "net_cls", - "net_prio": "net_cls", - }, - } -) - -func loadModules(cfg *config.CloudConfig) (*config.CloudConfig, error) { - mounted := map[string]bool{} - - f, err := os.Open("/proc/modules") - if err != nil { - return cfg, err - } - defer f.Close() - - reader := bufio.NewScanner(f) - for reader.Scan() { - mounted[strings.SplitN(reader.Text(), " ", 2)[0]] = true - } - - if util.GetHypervisor() == "hyperv" { - cfg.Rancher.Modules = append(cfg.Rancher.Modules, "hv_utils") - } - - for _, module := range cfg.Rancher.Modules { - if mounted[module] { - continue - } - - log.Debugf("Loading module %s", module) - // split module and module parameters - cmdParam := strings.SplitN(module, " ", -1) - cmd := exec.Command("modprobe", cmdParam...) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - if err := cmd.Run(); err != nil { - log.Errorf("Could not load module %s, err %v", module, err) - } - } - - return cfg, nil -} - -func sysInit(c *config.CloudConfig) (*config.CloudConfig, error) { - args := append([]string{config.SysInitBin}, os.Args[1:]...) - - cmd := &exec.Cmd{ - Path: config.RosBin, - Args: args, - } - - cmd.Stdin = os.Stdin - cmd.Stderr = os.Stderr - cmd.Stdout = os.Stdout - - if err := cmd.Start(); err != nil { - return c, err - } - - return c, os.Stdin.Close() -} - -func MainInit() { - 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 { - fmt.Printf("Starting Recovery console: %v\n", r) - recovery(nil) - } - }() - - if err := RunInit(); err != nil { - log.Fatal(err) - } -} - -func mountConfigured(display, dev, fsType, target string) error { - var err error - - if dev == "" { - return nil - } - - dev = util.ResolveDevice(dev) - if dev == "" { - return fmt.Errorf("Could not resolve device %q", dev) - } - if fsType == "auto" { - fsType, err = util.GetFsType(dev) - } - - if err != nil { - return err - } - - log.Debugf("FsType has been set to %s", fsType) - log.Infof("Mounting %s device %s to %s", display, dev, target) - return util.Mount(dev, target, fsType, "") -} - -func mountState(cfg *config.CloudConfig) error { - return mountConfigured("state", cfg.Rancher.State.Dev, cfg.Rancher.State.FsType, state) -} - -func mountOem(cfg *config.CloudConfig) (*config.CloudConfig, error) { - if cfg == nil { - cfg = config.LoadConfig() - } - if err := mountConfigured("oem", cfg.Rancher.State.OemDev, cfg.Rancher.State.OemFsType, config.OEM); err != nil { - log.Debugf("Not mounting OEM: %v", err) - } else { - log.Infof("Mounted OEM: %s", cfg.Rancher.State.OemDev) - } - return cfg, nil -} - -func tryMountState(cfg *config.CloudConfig) error { - err := mountState(cfg) - if err == nil { - return nil - } - log.Infof("Skipped an error when first mounting: %v", err) - - // If we failed to mount lets run bootstrap and try again - if err := bootstrap(cfg); err != nil { - return err - } - - return mountState(cfg) -} - -func tryMountAndBootstrap(cfg *config.CloudConfig) (*config.CloudConfig, bool, error) { - if !isInitrd() || cfg.Rancher.State.Dev == "" { - return cfg, false, nil - } - - if err := tryMountState(cfg); !cfg.Rancher.State.Required && err != nil { - return cfg, false, nil - } else if err != nil { - return cfg, false, err - } - - return cfg, true, nil -} - -func getLaunchConfig(cfg *config.CloudConfig, dockerCfg *config.DockerConfig) (*dfs.Config, []string) { - var launchConfig dfs.Config - - args := dfs.ParseConfig(&launchConfig, dockerCfg.FullArgs()...) - - launchConfig.DNSConfig.Nameservers = cfg.Rancher.Defaults.Network.DNS.Nameservers - launchConfig.DNSConfig.Search = cfg.Rancher.Defaults.Network.DNS.Search - launchConfig.Environment = dockerCfg.Environment - - if !cfg.Rancher.Debug { - launchConfig.LogFile = config.SystemDockerLog - } - - return &launchConfig, args -} - -func isInitrd() bool { - var stat syscall.Statfs_t - syscall.Statfs("/", &stat) - return int64(stat.Type) == tmpfsMagic || int64(stat.Type) == ramfsMagic -} - -func setupSharedRoot(c *config.CloudConfig) (*config.CloudConfig, error) { - if c.Rancher.NoSharedRoot { - return c, nil - } - - if isInitrd() { - for _, i := range []string{"/mnt", "/media", "/var/lib/system-docker"} { - if err := os.MkdirAll(i, 0755); err != nil { - return c, err - } - if err := mount.Mount("tmpfs", i, "tmpfs", "rw"); err != nil { - return c, err - } - if err := mount.MakeShared(i); err != nil { - return c, err - } - } - return c, nil - } - - return c, mount.MakeShared("/") -} - -func PrintConfig() { - cfgString, err := config.Export(false, true) - if err != nil { - log.WithFields(log.Fields{"err": err}).Error("Error serializing config") - } else { - log.Debugf("Config: %s", cfgString) - } -} - -func RunInit() error { - os.Setenv("PATH", "/sbin:/usr/sbin:/usr/bin") - if isInitrd() { - log.Debug("Booting off an in-memory filesystem") - // Magic setting to tell Docker to do switch_root and not pivot_root - os.Setenv("DOCKER_RAMDISK", "true") - } else { - log.Debug("Booting off a persistent filesystem") - } - - boot2DockerEnvironment := false - var shouldSwitchRoot bool - hypervisor := "" - - configFiles := make(map[string][]byte) - - initFuncs := []config.CfgFuncData{ - config.CfgFuncData{"preparefs", func(c *config.CloudConfig) (*config.CloudConfig, error) { - return c, dfs.PrepareFs(&mountConfig) - }}, - config.CfgFuncData{"save init cmdline", func(c *config.CloudConfig) (*config.CloudConfig, error) { - // the Kernel Patch added for RancherOS passes `--` (only) elided kernel boot params to the init process - cmdLineArgs := strings.Join(os.Args, " ") - config.SaveInitCmdline(cmdLineArgs) - - cfg := config.LoadConfig() - log.Debugf("Cmdline debug = %t", cfg.Rancher.Debug) - if cfg.Rancher.Debug { - log.SetLevel(log.DebugLevel) - } else { - log.SetLevel(log.InfoLevel) - } - - return cfg, nil - }}, - config.CfgFuncData{"mount OEM", mountOem}, - config.CfgFuncData{"debug save cfg", func(_ *config.CloudConfig) (*config.CloudConfig, error) { - PrintConfig() - - cfg := config.LoadConfig() - return cfg, nil - }}, - config.CfgFuncData{"load modules", loadModules}, - config.CfgFuncData{"recovery console", func(cfg *config.CloudConfig) (*config.CloudConfig, error) { - if cfg.Rancher.Recovery { - recovery(nil) - } - return cfg, nil - }}, - config.CfgFuncData{"cloud-init", func(cfg *config.CloudConfig) (*config.CloudConfig, error) { - cfg.Rancher.CloudInit.Datasources = config.LoadConfigWithPrefix(state).Rancher.CloudInit.Datasources - hypervisor = util.GetHypervisor() - if hypervisor == "" { - log.Infof("ros init: No Detected Hypervisor") - } else { - log.Infof("ros init: Detected Hypervisor: %s", hypervisor) - } - 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) - } - - if err := config.Set("rancher.cloud_init.datasources", cfg.Rancher.CloudInit.Datasources); err != nil { - log.Error(err) - } - - log.Infof("init, runCloudInitServices(%v)", cfg.Rancher.CloudInit.Datasources) - if err := runCloudInitServices(cfg); err != nil { - log.Error(err) - } - - // It'd be nice to push to rsyslog before this, but we don't have network - log.AddRSyslogHook() - - return config.LoadConfig(), nil - }}, - config.CfgFuncData{"b2d env", func(cfg *config.CloudConfig) (*config.CloudConfig, error) { - if _, err := os.Stat("/var/lib/boot2docker"); os.IsNotExist(err) { - err := os.Mkdir("/var/lib/boot2docker", 0755) - if err != nil { - log.Errorf("Failed to create boot2docker directory: %v", err) - } - } - - if dev := util.ResolveDevice("LABEL=B2D_STATE"); dev != "" { - boot2DockerEnvironment = true - cfg.Rancher.State.Dev = "LABEL=B2D_STATE" - log.Infof("boot2DockerEnvironment %s: %s", cfg.Rancher.State.Dev, dev) - return cfg, nil - } - - devices := []string{"/dev/sda", "/dev/vda"} - data := make([]byte, len(boot2DockerMagic)) - - for _, device := range devices { - f, err := os.Open(device) - if err == nil { - defer f.Close() - - _, err = f.Read(data) - if err == nil && string(data) == boot2DockerMagic { - boot2DockerEnvironment = true - cfg.Rancher.State.Dev = "LABEL=B2D_STATE" - cfg.Rancher.State.Autoformat = []string{device} - log.Infof("boot2DockerEnvironment %s: Autoformat %s", cfg.Rancher.State.Dev, cfg.Rancher.State.Autoformat[0]) - - break - } - } - } - - // save here so the bootstrap service can see it (when booting from iso, its very early) - if boot2DockerEnvironment { - if err := config.Set("rancher.state.dev", cfg.Rancher.State.Dev); err != nil { - log.Errorf("Failed to update rancher.state.dev: %v", err) - } - if err := config.Set("rancher.state.autoformat", cfg.Rancher.State.Autoformat); err != nil { - log.Errorf("Failed to update rancher.state.autoformat: %v", err) - } - } - - return config.LoadConfig(), nil - }}, - config.CfgFuncData{"mount and bootstrap", func(cfg *config.CloudConfig) (*config.CloudConfig, error) { - var err error - cfg, shouldSwitchRoot, err = tryMountAndBootstrap(cfg) - - if err != nil { - return nil, err - } - return cfg, nil - }}, - config.CfgFuncData{"read cfg and log files", func(cfg *config.CloudConfig) (*config.CloudConfig, error) { - filesToCopy := []string{ - config.CloudConfigInitFile, - config.CloudConfigScriptFile, - config.CloudConfigBootFile, - config.CloudConfigNetworkFile, - config.MetaDataFile, - config.EtcResolvConfFile, - } - // 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/" - if files, err := ioutil.ReadDir(bootLog); err == nil { - for _, file := range files { - if !file.IsDir() { - filePath := filepath.Join(bootLog, file.Name()) - filesToCopy = append(filesToCopy, filePath) - log.Debugf("Swizzle: Found %s to save", filePath) - } - } - } - 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) - log.Debugf("Swizzle: Found %s to save", filePath) - } - } - for _, name := range filesToCopy { - if _, err := os.Lstat(name); !os.IsNotExist(err) { - content, err := ioutil.ReadFile(name) - if err != nil { - log.Errorf("read cfg file (%s) %s", name, err) - continue - } - log.Debugf("Swizzle: Saved %s to memory", name) - configFiles[name] = content - } - } - return cfg, nil - }}, - config.CfgFuncData{"switchroot", func(cfg *config.CloudConfig) (*config.CloudConfig, error) { - if !shouldSwitchRoot { - return cfg, nil - } - log.Debugf("Switching to new root at %s %s", state, cfg.Rancher.State.Directory) - if err := switchRoot(state, cfg.Rancher.State.Directory, cfg.Rancher.RmUsr); err != nil { - return cfg, err - } - return cfg, nil - }}, - config.CfgFuncData{"mount OEM2", mountOem}, - config.CfgFuncData{"write cfg and log files", func(cfg *config.CloudConfig) (*config.CloudConfig, error) { - for name, content := range configFiles { - dirMode := os.ModeDir | 0755 - fileMode := os.FileMode(0444) - if strings.HasPrefix(name, "/var/lib/rancher/conf/") { - // only make the conf files harder to get to - dirMode = os.ModeDir | 0700 - if name == config.CloudConfigScriptFile { - fileMode = os.FileMode(0755) - } else { - fileMode = os.FileMode(0400) - } - } - if err := os.MkdirAll(filepath.Dir(name), dirMode); err != nil { - log.Error(err) - } - if err := util.WriteFileAtomic(name, content, fileMode); err != nil { - log.Error(err) - } - log.Infof("Swizzle: Wrote file to %s", name) - } - if err := os.MkdirAll(config.VarRancherDir, os.ModeDir|0755); err != nil { - log.Error(err) - } - 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 - }}, - config.CfgFuncData{"b2d Env", func(cfg *config.CloudConfig) (*config.CloudConfig, error) { - - log.Debugf("memory Resolve.conf == [%s]", configFiles["/etc/resolv.conf"]) - - // this code make sure the open-vm-tools service can be started correct when there is no network - if hypervisor == "vmware" { - // make sure the cache directory exist - if err := os.MkdirAll("/var/lib/rancher/cache/", os.ModeDir|0755); err != nil { - log.Errorf("Create service cache diretory error: %v", err) - } - - // move os-services cache file - if _, err := os.Stat("/usr/share/ros/services-cache"); err == nil { - files, err := ioutil.ReadDir("/usr/share/ros/services-cache/") - if err != nil { - log.Errorf("Read file error: %v", err) - } - for _, f := range files { - err := os.Rename("/usr/share/ros/services-cache/"+f.Name(), "/var/lib/rancher/cache/"+f.Name()) - if err != nil { - log.Errorf("Rename file error: %v", err) - } - } - if err := os.Remove("/usr/share/ros/services-cache"); err != nil { - log.Errorf("Remove file error: %v", err) - } - } - - } - - if boot2DockerEnvironment { - if err := config.Set("rancher.state.dev", cfg.Rancher.State.Dev); err != nil { - log.Errorf("Failed to update rancher.state.dev: %v", err) - } - if err := config.Set("rancher.state.autoformat", cfg.Rancher.State.Autoformat); err != nil { - log.Errorf("Failed to update rancher.state.autoformat: %v", err) - } - } - - return config.LoadConfig(), nil - }}, - config.CfgFuncData{"hypervisor tools", func(cfg *config.CloudConfig) (*config.CloudConfig, error) { - enableHypervisorService(cfg, hypervisor) - return config.LoadConfig(), nil - }}, - config.CfgFuncData{"preparefs2", func(cfg *config.CloudConfig) (*config.CloudConfig, error) { - return cfg, dfs.PrepareFs(&mountConfig) - }}, - config.CfgFuncData{"load modules2", loadModules}, - config.CfgFuncData{"set proxy env", func(cfg *config.CloudConfig) (*config.CloudConfig, error) { - network.SetProxyEnvironmentVariables() - - return cfg, nil - }}, - config.CfgFuncData{"init SELinux", initializeSelinux}, - config.CfgFuncData{"setupSharedRoot", setupSharedRoot}, - config.CfgFuncData{"sysinit", sysInit}, - } - - cfg, err := config.ChainCfgFuncs(nil, initFuncs) - if err != nil { - recovery(err) - } - - launchConfig, args := getLaunchConfig(cfg, &cfg.Rancher.SystemDocker) - launchConfig.Fork = !cfg.Rancher.SystemDocker.Exec - //launchConfig.NoLog = true - - log.Info("Launching System Docker") - _, err = dfs.LaunchDocker(launchConfig, config.SystemDockerBin, args...) - if err != nil { - log.Errorf("Error Launching System Docker: %s", err) - recovery(err) - return err - } - // Code never gets here - rancher.system_docker.exec=true - - return pidOne() -} - -func enableHypervisorService(cfg *config.CloudConfig, hypervisorName string) { - if hypervisorName == "" { - return - } - - // only enable open-vm-tools for vmware - // these services(xenhvm-vm-tools, kvm-vm-tools, hyperv-vm-tools and bhyve-vm-tools) don't exist yet - serviceName := "" - switch hypervisorName { - case "vmware": - serviceName = "open-vm-tools" - case "hyperv": - serviceName = "hyperv-vm-tools" - default: - log.Infof("no hypervisor matched") - } - - if serviceName != "" { - if !cfg.Rancher.HypervisorService { - log.Infof("Skipping %s as `rancher.hypervisor_service` is set to false", serviceName) - return - } - - // Check removed - there's an x509 cert failure on first boot of an installed system - // check quickly to see if there is a yml file available - // if service.ValidService(serviceName, cfg) { - log.Infof("Setting rancher.services_include. %s=true", serviceName) - if err := config.Set("rancher.services_include."+serviceName, "true"); err != nil { - log.Error(err) - } - } -} diff --git a/init/one.go b/init/one.go deleted file mode 100644 index 9f6a2c2f..00000000 --- a/init/one.go +++ /dev/null @@ -1,24 +0,0 @@ -// +build linux - -package init - -import ( - "os" - "os/signal" - "syscall" -) - -func pidOne() error { - c := make(chan os.Signal, 2048) - signal.Notify(c, syscall.SIGCHLD) - - for range c { - for { - if pid, err := syscall.Wait4(-1, nil, syscall.WNOHANG, nil); err != nil || pid <= 0 { - break - } - } - } - - return nil -} diff --git a/main.go b/main.go index b554de85..10bd68b8 100644 --- a/main.go +++ b/main.go @@ -9,13 +9,13 @@ import ( "github.com/rancher/os/cmd/cloudinitexecute" "github.com/rancher/os/cmd/cloudinitsave" "github.com/rancher/os/cmd/control" + osInit "github.com/rancher/os/cmd/init" "github.com/rancher/os/cmd/network" "github.com/rancher/os/cmd/power" "github.com/rancher/os/cmd/respawn" "github.com/rancher/os/cmd/sysinit" "github.com/rancher/os/cmd/wait" - "github.com/rancher/os/dfs" - osInit "github.com/rancher/os/init" + "github.com/rancher/os/pkg/dfs" ) var entrypoints = map[string]func(){ diff --git a/compose/project.go b/pkg/compose/project.go similarity index 98% rename from compose/project.go rename to pkg/compose/project.go index 6210cc32..333663bd 100644 --- a/compose/project.go +++ b/pkg/compose/project.go @@ -18,10 +18,10 @@ import ( "github.com/docker/libcompose/project/events" "github.com/docker/libcompose/project/options" "github.com/rancher/os/config" - rosDocker "github.com/rancher/os/docker" - "github.com/rancher/os/log" - "github.com/rancher/os/util" - "github.com/rancher/os/util/network" + rosDocker "github.com/rancher/os/pkg/docker" + "github.com/rancher/os/pkg/log" + "github.com/rancher/os/pkg/util" + "github.com/rancher/os/pkg/util/network" ) func CreateService(cfg *config.CloudConfig, name string, serviceConfig *composeConfig.ServiceConfigV1) (project.Service, error) { diff --git a/compose/reload.go b/pkg/compose/reload.go similarity index 96% rename from compose/reload.go rename to pkg/compose/reload.go index 2d8253a1..7eba1e2a 100644 --- a/compose/reload.go +++ b/pkg/compose/reload.go @@ -7,9 +7,9 @@ import ( composeConfig "github.com/docker/libcompose/config" "github.com/docker/libcompose/project" "github.com/rancher/os/config" - "github.com/rancher/os/docker" - "github.com/rancher/os/log" - "github.com/rancher/os/util/network" + "github.com/rancher/os/pkg/docker" + "github.com/rancher/os/pkg/log" + "github.com/rancher/os/pkg/util/network" ) func LoadService(p *project.Project, cfg *config.CloudConfig, useNetwork bool, service string) error { diff --git a/dfs/scratch.go b/pkg/dfs/scratch.go similarity index 98% rename from dfs/scratch.go rename to pkg/dfs/scratch.go index a3a4dd6f..66a1f6a1 100644 --- a/dfs/scratch.go +++ b/pkg/dfs/scratch.go @@ -11,11 +11,13 @@ import ( "strings" "syscall" + "github.com/rancher/os/pkg/init/one" + "github.com/docker/libnetwork/resolvconf" - "github.com/rancher/os/log" - "github.com/rancher/os/netconf" - "github.com/rancher/os/selinux" - "github.com/rancher/os/util" + "github.com/rancher/os/pkg/log" + "github.com/rancher/os/pkg/netconf" + "github.com/rancher/os/pkg/selinux" + "github.com/rancher/os/pkg/util" ) const ( @@ -205,7 +207,7 @@ func execDocker(config *Config, docker, cmd string, args []string) (*exec.Cmd, e return cmd, err } if config.PidOne { - PidOne() + one.PidOne() } return cmd, err } diff --git a/docker/auth.go b/pkg/docker/auth.go similarity index 98% rename from docker/auth.go rename to pkg/docker/auth.go index bac015a6..a0f9679a 100644 --- a/docker/auth.go +++ b/pkg/docker/auth.go @@ -9,7 +9,7 @@ import ( "github.com/docker/engine-api/types" "github.com/docker/libcompose/docker" "github.com/rancher/os/config" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/log" ) // ConfigAuthLookup will lookup registry auth info from cloud config diff --git a/docker/client.go b/pkg/docker/client.go similarity index 100% rename from docker/client.go rename to pkg/docker/client.go diff --git a/docker/client_factory.go b/pkg/docker/client_factory.go similarity index 96% rename from docker/client_factory.go rename to pkg/docker/client_factory.go index 78fc3cbe..6901872a 100644 --- a/docker/client_factory.go +++ b/pkg/docker/client_factory.go @@ -10,8 +10,8 @@ import ( composeClient "github.com/docker/libcompose/docker/client" "github.com/docker/libcompose/project" "github.com/rancher/os/config" - "github.com/rancher/os/log" - "github.com/rancher/os/util" + "github.com/rancher/os/pkg/log" + "github.com/rancher/os/pkg/util" ) type ClientFactory struct { diff --git a/docker/env.go b/pkg/docker/env.go similarity index 98% rename from docker/env.go rename to pkg/docker/env.go index bbb9724c..5f235cf8 100644 --- a/docker/env.go +++ b/pkg/docker/env.go @@ -6,7 +6,7 @@ import ( composeConfig "github.com/docker/libcompose/config" "github.com/rancher/os/config" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/log" ) type ConfigEnvironment struct { diff --git a/docker/service.go b/pkg/docker/service.go similarity index 99% rename from docker/service.go rename to pkg/docker/service.go index 4773c17b..04b0f94e 100644 --- a/docker/service.go +++ b/pkg/docker/service.go @@ -10,7 +10,7 @@ import ( "github.com/docker/libcompose/project" "github.com/docker/libcompose/project/options" "github.com/rancher/os/config" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/log" "golang.org/x/net/context" ) diff --git a/docker/service_factory.go b/pkg/docker/service_factory.go similarity index 96% rename from docker/service_factory.go rename to pkg/docker/service_factory.go index dae45361..bd0a4347 100644 --- a/docker/service_factory.go +++ b/pkg/docker/service_factory.go @@ -4,7 +4,7 @@ import ( composeConfig "github.com/docker/libcompose/config" "github.com/docker/libcompose/docker" "github.com/docker/libcompose/project" - "github.com/rancher/os/util" + "github.com/rancher/os/pkg/util" ) type ServiceFactory struct { diff --git a/docker/util.go b/pkg/docker/util.go similarity index 100% rename from docker/util.go rename to pkg/docker/util.go diff --git a/hostname/hostname.go b/pkg/hostname/hostname.go similarity index 100% rename from hostname/hostname.go rename to pkg/hostname/hostname.go diff --git a/pkg/init/b2d/b2d.go b/pkg/init/b2d/b2d.go new file mode 100644 index 00000000..f6c67784 --- /dev/null +++ b/pkg/init/b2d/b2d.go @@ -0,0 +1,81 @@ +package b2d + +import ( + "os" + + "github.com/rancher/os/config" + "github.com/rancher/os/pkg/init/configfiles" + "github.com/rancher/os/pkg/log" + "github.com/rancher/os/pkg/util" +) + +const ( + boot2DockerMagic string = "boot2docker, please format-me" +) + +var ( + boot2DockerEnvironment bool +) + +func B2D(cfg *config.CloudConfig) (*config.CloudConfig, error) { + if _, err := os.Stat("/var/lib/boot2docker"); os.IsNotExist(err) { + err := os.Mkdir("/var/lib/boot2docker", 0755) + if err != nil { + log.Errorf("Failed to create boot2docker directory: %v", err) + } + } + + if dev := util.ResolveDevice("LABEL=B2D_STATE"); dev != "" { + boot2DockerEnvironment = true + cfg.Rancher.State.Dev = "LABEL=B2D_STATE" + log.Infof("boot2DockerEnvironment %s: %s", cfg.Rancher.State.Dev, dev) + return cfg, nil + } + + devices := []string{"/dev/sda", "/dev/vda"} + data := make([]byte, len(boot2DockerMagic)) + + for _, device := range devices { + f, err := os.Open(device) + if err == nil { + defer f.Close() + + _, err = f.Read(data) + if err == nil && string(data) == boot2DockerMagic { + boot2DockerEnvironment = true + cfg.Rancher.State.Dev = "LABEL=B2D_STATE" + cfg.Rancher.State.Autoformat = []string{device} + log.Infof("boot2DockerEnvironment %s: Autoformat %s", cfg.Rancher.State.Dev, cfg.Rancher.State.Autoformat[0]) + + break + } + } + } + + // save here so the bootstrap service can see it (when booting from iso, its very early) + if boot2DockerEnvironment { + if err := config.Set("rancher.state.dev", cfg.Rancher.State.Dev); err != nil { + log.Errorf("Failed to update rancher.state.dev: %v", err) + } + if err := config.Set("rancher.state.autoformat", cfg.Rancher.State.Autoformat); err != nil { + log.Errorf("Failed to update rancher.state.autoformat: %v", err) + } + } + + return config.LoadConfig(), nil +} + +func Env(cfg *config.CloudConfig) (*config.CloudConfig, error) { + log.Debugf("memory Resolve.conf == [%s]", configfiles.ConfigFiles["/etc/resolv.conf"]) + + if boot2DockerEnvironment { + if err := config.Set("rancher.state.dev", cfg.Rancher.State.Dev); err != nil { + log.Errorf("Failed to update rancher.state.dev: %v", err) + } + if err := config.Set("rancher.state.autoformat", cfg.Rancher.State.Autoformat); err != nil { + log.Errorf("Failed to update rancher.state.autoformat: %v", err) + } + } + + return config.LoadConfig(), nil +} diff --git a/pkg/init/bootstrap/bootstrap.go b/pkg/init/bootstrap/bootstrap.go new file mode 100644 index 00000000..6cc872a7 --- /dev/null +++ b/pkg/init/bootstrap/bootstrap.go @@ -0,0 +1,39 @@ +package bootstrap + +import ( + "github.com/rancher/os/config" + "github.com/rancher/os/pkg/compose" + "github.com/rancher/os/pkg/init/docker" + "github.com/rancher/os/pkg/log" + "github.com/rancher/os/pkg/sysinit" + "github.com/rancher/os/pkg/util" +) + +func bootstrapServices(cfg *config.CloudConfig) (*config.CloudConfig, error) { + if util.ResolveDevice(cfg.Rancher.State.Dev) != "" && len(cfg.Bootcmd) == 0 { + log.Info("NOT Running Bootstrap") + + return cfg, nil + } + log.Info("Running Bootstrap") + _, err := compose.RunServiceSet("bootstrap", cfg, cfg.Rancher.BootstrapContainers) + return cfg, err +} + +func Bootstrap(cfg *config.CloudConfig) error { + log.Info("Launching Bootstrap Docker") + + c, err := docker.Start(cfg) + if err != nil { + return err + } + + defer docker.Stop(c) + + _, err = config.ChainCfgFuncs(cfg, + []config.CfgFuncData{ + {"bootstrap loadImages", sysinit.LoadBootstrapImages}, + {"bootstrap Services", bootstrapServices}, + }) + return err +} diff --git a/pkg/init/cloudinit/cloudinit.go b/pkg/init/cloudinit/cloudinit.go new file mode 100644 index 00000000..77bbf329 --- /dev/null +++ b/pkg/init/cloudinit/cloudinit.go @@ -0,0 +1,60 @@ +package cloudinit + +import ( + "github.com/rancher/os/config" + "github.com/rancher/os/pkg/compose" + "github.com/rancher/os/pkg/init/docker" + "github.com/rancher/os/pkg/log" + "github.com/rancher/os/pkg/sysinit" + "github.com/rancher/os/pkg/util" +) + +func CloudInit(cfg *config.CloudConfig) (*config.CloudConfig, error) { + cfg.Rancher.CloudInit.Datasources = config.LoadConfigWithPrefix(config.State).Rancher.CloudInit.Datasources + hypervisor := util.GetHypervisor() + if hypervisor == "" { + log.Infof("ros init: No Detected Hypervisor") + } else { + log.Infof("ros init: Detected Hypervisor: %s", hypervisor) + } + 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) + } + + if err := config.Set("rancher.cloud_init.datasources", cfg.Rancher.CloudInit.Datasources); err != nil { + log.Error(err) + } + + log.Infof("init, runCloudInitServices(%v)", cfg.Rancher.CloudInit.Datasources) + if err := runCloudInitServices(cfg); err != nil { + log.Error(err) + } + + // It'd be nice to push to rsyslog before this, but we don't have network + log.AddRSyslogHook() + + return config.LoadConfig(), nil +} + +func runCloudInitServices(cfg *config.CloudConfig) error { + c, err := docker.Start(cfg) + if err != nil { + return err + } + + defer docker.Stop(c) + + _, err = config.ChainCfgFuncs(cfg, + []config.CfgFuncData{ + {"cloudinit loadImages", sysinit.LoadBootstrapImages}, + {"cloudinit Services", runCloudInitServiceSet}, + }) + return err +} + +func runCloudInitServiceSet(cfg *config.CloudConfig) (*config.CloudConfig, error) { + log.Info("Running cloud-init services") + _, err := compose.RunServiceSet("cloud-init", cfg, cfg.Rancher.CloudInitServices) + return cfg, err +} diff --git a/pkg/init/configfiles/configfiles.go b/pkg/init/configfiles/configfiles.go new file mode 100644 index 00000000..113b3303 --- /dev/null +++ b/pkg/init/configfiles/configfiles.go @@ -0,0 +1,92 @@ +package configfiles + +import ( + "io/ioutil" + "os" + "path/filepath" + "strings" + + "github.com/rancher/os/config" + "github.com/rancher/os/pkg/log" + "github.com/rancher/os/pkg/util" +) + +var ( + ConfigFiles = map[string][]byte{} +) + +func ReadConfigFiles(cfg *config.CloudConfig) (*config.CloudConfig, error) { + filesToCopy := []string{ + config.CloudConfigInitFile, + config.CloudConfigScriptFile, + config.CloudConfigBootFile, + config.CloudConfigNetworkFile, + config.MetaDataFile, + config.EtcResolvConfFile, + } + // 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/" + if files, err := ioutil.ReadDir(bootLog); err == nil { + for _, file := range files { + if !file.IsDir() { + filePath := filepath.Join(bootLog, file.Name()) + filesToCopy = append(filesToCopy, filePath) + log.Debugf("Swizzle: Found %s to save", filePath) + } + } + } + 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) + log.Debugf("Swizzle: Found %s to save", filePath) + } + } + for _, name := range filesToCopy { + if _, err := os.Lstat(name); !os.IsNotExist(err) { + content, err := ioutil.ReadFile(name) + if err != nil { + log.Errorf("read cfg file (%s) %s", name, err) + continue + } + log.Debugf("Swizzle: Saved %s to memory", name) + ConfigFiles[name] = content + } + } + return cfg, nil +} + +func WriteConfigFiles(cfg *config.CloudConfig) (*config.CloudConfig, error) { + for name, content := range ConfigFiles { + dirMode := os.ModeDir | 0755 + fileMode := os.FileMode(0444) + if strings.HasPrefix(name, "/var/lib/rancher/conf/") { + // only make the conf files harder to get to + dirMode = os.ModeDir | 0700 + if name == config.CloudConfigScriptFile { + fileMode = os.FileMode(0755) + } else { + fileMode = os.FileMode(0400) + } + } + if err := os.MkdirAll(filepath.Dir(name), dirMode); err != nil { + log.Error(err) + } + if err := util.WriteFileAtomic(name, content, fileMode); err != nil { + log.Error(err) + } + log.Infof("Swizzle: Wrote file to %s", name) + } + if err := os.MkdirAll(config.VarRancherDir, os.ModeDir|0755); err != nil { + log.Error(err) + } + 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 +} diff --git a/pkg/init/debug/debug.go b/pkg/init/debug/debug.go new file mode 100644 index 00000000..0481470b --- /dev/null +++ b/pkg/init/debug/debug.go @@ -0,0 +1,22 @@ +package debug + +import ( + "github.com/rancher/os/config" + "github.com/rancher/os/pkg/log" +) + +func PrintAndLoadConfig(_ *config.CloudConfig) (*config.CloudConfig, error) { + PrintConfig() + + cfg := config.LoadConfig() + return cfg, nil +} + +func PrintConfig() { + cfgString, err := config.Export(false, true) + if err != nil { + log.WithFields(log.Fields{"err": err}).Error("Error serializing config") + } else { + log.Debugf("Config: %s", cfgString) + } +} diff --git a/pkg/init/docker/docker.go b/pkg/init/docker/docker.go new file mode 100644 index 00000000..8f2cfc6d --- /dev/null +++ b/pkg/init/docker/docker.go @@ -0,0 +1,53 @@ +package docker + +import ( + "syscall" + + "github.com/rancher/os/config" + "github.com/rancher/os/pkg/dfs" +) + +func Start(cfg *config.CloudConfig) (chan interface{}, error) { + launchConfig, args := GetLaunchConfig(cfg, &cfg.Rancher.BootstrapDocker) + launchConfig.Fork = true + launchConfig.LogFile = "" + launchConfig.NoLog = true + + cmd, err := dfs.LaunchDocker(launchConfig, config.SystemDockerBin, args...) + if err != nil { + return nil, err + } + + c := make(chan interface{}) + go func() { + <-c + cmd.Process.Signal(syscall.SIGTERM) + cmd.Wait() + c <- struct{}{} + }() + + return c, nil +} + +func Stop(c chan interface{}) error { + c <- struct{}{} + <-c + + return nil +} + +func GetLaunchConfig(cfg *config.CloudConfig, dockerCfg *config.DockerConfig) (*dfs.Config, []string) { + var launchConfig dfs.Config + + args := dfs.ParseConfig(&launchConfig, dockerCfg.FullArgs()...) + + launchConfig.DNSConfig.Nameservers = cfg.Rancher.Defaults.Network.DNS.Nameservers + launchConfig.DNSConfig.Search = cfg.Rancher.Defaults.Network.DNS.Search + launchConfig.Environment = dockerCfg.Environment + + if !cfg.Rancher.Debug { + launchConfig.LogFile = config.SystemDockerLog + } + + return &launchConfig, args +} diff --git a/pkg/init/env/env.go b/pkg/init/env/env.go new file mode 100644 index 00000000..3658eff5 --- /dev/null +++ b/pkg/init/env/env.go @@ -0,0 +1,29 @@ +package env + +import ( + "os" + + "github.com/rancher/os/config" + "github.com/rancher/os/pkg/init/fsmount" + "github.com/rancher/os/pkg/log" + "github.com/rancher/os/pkg/util/network" +) + +func Init(c *config.CloudConfig) (*config.CloudConfig, error) { + os.Setenv("PATH", "/sbin:/usr/sbin:/usr/bin") + if fsmount.IsInitrd() { + log.Debug("Booting off an in-memory filesystem") + // Magic setting to tell Docker to do switch_root and not pivot_root + os.Setenv("DOCKER_RAMDISK", "true") + } else { + log.Debug("Booting off a persistent filesystem") + } + + return c, nil +} + +func Proxy(cfg *config.CloudConfig) (*config.CloudConfig, error) { + network.SetProxyEnvironmentVariables() + + return cfg, nil +} diff --git a/pkg/init/fsmount/fsmount.go b/pkg/init/fsmount/fsmount.go new file mode 100644 index 00000000..54e53d51 --- /dev/null +++ b/pkg/init/fsmount/fsmount.go @@ -0,0 +1,105 @@ +package fsmount + +import ( + "fmt" + "syscall" + + "github.com/rancher/os/config" + "github.com/rancher/os/pkg/init/bootstrap" + "github.com/rancher/os/pkg/log" + "github.com/rancher/os/pkg/util" +) + +const ( + tmpfsMagic int64 = 0x01021994 + ramfsMagic int64 = 0x858458f6 +) + +var ( + ShouldSwitchRoot bool +) + +func OEM(cfg *config.CloudConfig) (*config.CloudConfig, error) { + if cfg == nil { + cfg = config.LoadConfig() + } + if err := mountConfigured("oem", cfg.Rancher.State.OemDev, cfg.Rancher.State.OemFsType, config.OEM); err != nil { + log.Debugf("Not mounting OEM: %v", err) + } else { + log.Infof("Mounted OEM: %s", cfg.Rancher.State.OemDev) + } + return cfg, nil +} + +func mountConfigured(display, dev, fsType, target string) error { + var err error + + if dev == "" { + return nil + } + + dev = util.ResolveDevice(dev) + if dev == "" { + return fmt.Errorf("Could not resolve device %q", dev) + } + if fsType == "auto" { + fsType, err = util.GetFsType(dev) + } + + if err != nil { + return err + } + + log.Debugf("FsType has been set to %s", fsType) + log.Infof("Mounting %s device %s to %s", display, dev, target) + return util.Mount(dev, target, fsType, "") +} + +func mountState(cfg *config.CloudConfig) error { + return mountConfigured("state", cfg.Rancher.State.Dev, cfg.Rancher.State.FsType, config.State) +} + +func tryMountState(cfg *config.CloudConfig) error { + err := mountState(cfg) + if err == nil { + return nil + } + log.Infof("Skipped an error when first mounting: %v", err) + + // If we failed to mount lets run bootstrap and try again + if err := bootstrap.Bootstrap(cfg); err != nil { + return err + } + + return mountState(cfg) +} + +func tryMountAndBootstrap(cfg *config.CloudConfig) (*config.CloudConfig, bool, error) { + if !IsInitrd() || cfg.Rancher.State.Dev == "" { + return cfg, false, nil + } + + if err := tryMountState(cfg); !cfg.Rancher.State.Required && err != nil { + return cfg, false, nil + } else if err != nil { + return cfg, false, err + } + + return cfg, true, nil +} + +func IsInitrd() bool { + var stat syscall.Statfs_t + syscall.Statfs("/", &stat) + return int64(stat.Type) == tmpfsMagic || int64(stat.Type) == ramfsMagic +} + +func MountAndBootstrap(cfg *config.CloudConfig) (*config.CloudConfig, error) { + var err error + cfg, ShouldSwitchRoot, err = tryMountAndBootstrap(cfg) + + if err != nil { + return nil, err + } + return cfg, nil +} diff --git a/pkg/init/hypervisor/hypervisor.go b/pkg/init/hypervisor/hypervisor.go new file mode 100644 index 00000000..2401af4d --- /dev/null +++ b/pkg/init/hypervisor/hypervisor.go @@ -0,0 +1,80 @@ +package hypervisor + +import ( + "io/ioutil" + "os" + + "github.com/rancher/os/config" + "github.com/rancher/os/pkg/log" + "github.com/rancher/os/pkg/util" +) + +func Tools(cfg *config.CloudConfig) (*config.CloudConfig, error) { + enableHypervisorService(cfg, util.GetHypervisor()) + return config.LoadConfig(), nil +} + +func Env(cfg *config.CloudConfig) (*config.CloudConfig, error) { + hypervisor := util.GetHypervisor() + + // this code make sure the open-vm-tools service can be started correct when there is no network + if hypervisor == "vmware" { + // make sure the cache directory exist + if err := os.MkdirAll("/var/lib/rancher/cache/", os.ModeDir|0755); err != nil { + log.Errorf("Create service cache diretory error: %v", err) + } + + // move os-services cache file + if _, err := os.Stat("/usr/share/ros/services-cache"); err == nil { + files, err := ioutil.ReadDir("/usr/share/ros/services-cache/") + if err != nil { + log.Errorf("Read file error: %v", err) + } + for _, f := range files { + err := os.Rename("/usr/share/ros/services-cache/"+f.Name(), "/var/lib/rancher/cache/"+f.Name()) + if err != nil { + log.Errorf("Rename file error: %v", err) + } + } + if err := os.Remove("/usr/share/ros/services-cache"); err != nil { + log.Errorf("Remove file error: %v", err) + } + } + + } + + return cfg, nil +} + +func enableHypervisorService(cfg *config.CloudConfig, hypervisorName string) { + if hypervisorName == "" { + return + } + + // only enable open-vm-tools for vmware + // these services(xenhvm-vm-tools, kvm-vm-tools, hyperv-vm-tools and bhyve-vm-tools) don't exist yet + serviceName := "" + switch hypervisorName { + case "vmware": + serviceName = "open-vm-tools" + case "hyperv": + serviceName = "hyperv-vm-tools" + default: + log.Infof("no hypervisor matched") + } + + if serviceName != "" { + if !cfg.Rancher.HypervisorService { + log.Infof("Skipping %s as `rancher.hypervisor_service` is set to false", serviceName) + return + } + + // Check removed - there's an x509 cert failure on first boot of an installed system + // check quickly to see if there is a yml file available + // if service.ValidService(serviceName, cfg) { + log.Infof("Setting rancher.services_include. %s=true", serviceName) + if err := config.Set("rancher.services_include."+serviceName, "true"); err != nil { + log.Error(err) + } + } +} diff --git a/pkg/init/modules/modules.go b/pkg/init/modules/modules.go new file mode 100644 index 00000000..899e9f2c --- /dev/null +++ b/pkg/init/modules/modules.go @@ -0,0 +1,49 @@ +package modules + +import ( + "bufio" + "os" + "os/exec" + "strings" + + "github.com/rancher/os/config" + "github.com/rancher/os/pkg/log" + "github.com/rancher/os/pkg/util" +) + +func LoadModules(cfg *config.CloudConfig) (*config.CloudConfig, error) { + mounted := map[string]bool{} + + f, err := os.Open("/proc/modules") + if err != nil { + return cfg, err + } + defer f.Close() + + reader := bufio.NewScanner(f) + for reader.Scan() { + mounted[strings.SplitN(reader.Text(), " ", 2)[0]] = true + } + + if util.GetHypervisor() == "hyperv" { + cfg.Rancher.Modules = append(cfg.Rancher.Modules, "hv_utils") + } + + for _, module := range cfg.Rancher.Modules { + if mounted[module] { + continue + } + + log.Debugf("Loading module %s", module) + // split module and module parameters + cmdParam := strings.SplitN(module, " ", -1) + cmd := exec.Command("modprobe", cmdParam...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + if err := cmd.Run(); err != nil { + log.Errorf("Could not load module %s, err %v", module, err) + } + } + + return cfg, reader.Err() +} diff --git a/dfs/one.go b/pkg/init/one/one.go similarity index 96% rename from dfs/one.go rename to pkg/init/one/one.go index 72e4ec58..14f665c0 100644 --- a/dfs/one.go +++ b/pkg/init/one/one.go @@ -1,6 +1,6 @@ // +build linux -package dfs +package one import ( "os" diff --git a/pkg/init/prepare/prepare.go b/pkg/init/prepare/prepare.go new file mode 100644 index 00000000..7c45fca1 --- /dev/null +++ b/pkg/init/prepare/prepare.go @@ -0,0 +1,41 @@ +package prepare + +import ( + "os" + "strings" + + "github.com/rancher/os/config" + "github.com/rancher/os/pkg/dfs" + "github.com/rancher/os/pkg/log" +) + +var ( + mountConfig = dfs.Config{ + CgroupHierarchy: map[string]string{ + "cpu": "cpu", + "cpuacct": "cpu", + "net_cls": "net_cls", + "net_prio": "net_cls", + }, + } +) + +func FS(c *config.CloudConfig) (*config.CloudConfig, error) { + return c, dfs.PrepareFs(&mountConfig) +} + +func SaveCmdline(c *config.CloudConfig) (*config.CloudConfig, error) { + // the Kernel Patch added for RancherOS passes `--` (only) elided kernel boot params to the init process + cmdLineArgs := strings.Join(os.Args, " ") + config.SaveInitCmdline(cmdLineArgs) + + cfg := config.LoadConfig() + log.Debugf("Cmdline debug = %t", cfg.Rancher.Debug) + if cfg.Rancher.Debug { + log.SetLevel(log.DebugLevel) + } else { + log.SetLevel(log.InfoLevel) + } + + return cfg, nil +} diff --git a/init/recovery.go b/pkg/init/recovery/recovery.go similarity index 80% rename from init/recovery.go rename to pkg/init/recovery/recovery.go index f73cf96a..0b51a7d9 100644 --- a/init/recovery.go +++ b/pkg/init/recovery/recovery.go @@ -1,16 +1,17 @@ -package init +package recovery import ( composeConfig "github.com/docker/libcompose/config" "github.com/docker/libcompose/yaml" - "github.com/rancher/os/compose" "github.com/rancher/os/config" - "github.com/rancher/os/log" - "github.com/rancher/os/netconf" + "github.com/rancher/os/pkg/compose" + "github.com/rancher/os/pkg/init/docker" + "github.com/rancher/os/pkg/log" + "github.com/rancher/os/pkg/netconf" + "github.com/rancher/os/pkg/sysinit" ) var ( - // TODO: move this into the os-config file so it can be customised. recoveryDockerService = composeConfig.ServiceConfigV1{ Image: config.OsBase, @@ -44,14 +45,14 @@ var ( } ) -func recoveryServices(cfg *config.CloudConfig) (*config.CloudConfig, error) { - _, err := compose.RunServiceSet("recovery", cfg, map[string]*composeConfig.ServiceConfigV1{ - "recovery": &recoveryDockerService, - }) - return nil, err +func LoadRecoveryConsole(cfg *config.CloudConfig) (*config.CloudConfig, error) { + if cfg.Rancher.Recovery { + Recovery(nil) + } + return cfg, nil } -func recovery(initFailure error) { +func Recovery(initFailure error) { if initFailure != nil { log.Errorf("RancherOS has failed to boot: %v", initFailure) } @@ -80,17 +81,24 @@ func recovery(initFailure error) { }, } - _, err := startDocker(&recoveryConfig) + _, err := docker.Start(&recoveryConfig) if err != nil { log.Fatal(err) } _, err = config.ChainCfgFuncs(&recoveryConfig, []config.CfgFuncData{ - config.CfgFuncData{"loadSystemImages", loadSystemImages}, - config.CfgFuncData{"recovery console", recoveryServices}, + {"loadSystemImages", sysinit.LoadSystemImages}, + {"recovery console", recoveryServices}, }) if err != nil { log.Fatal(err) } } + +func recoveryServices(cfg *config.CloudConfig) (*config.CloudConfig, error) { + _, err := compose.RunServiceSet("recovery", cfg, map[string]*composeConfig.ServiceConfigV1{ + "recovery": &recoveryDockerService, + }) + return nil, err +} diff --git a/init/selinux.go b/pkg/init/selinux/selinux.go similarity index 77% rename from init/selinux.go rename to pkg/init/selinux/selinux.go index aeaf068e..30af75e7 100644 --- a/init/selinux.go +++ b/pkg/init/selinux/selinux.go @@ -1,15 +1,16 @@ // +build linux -package init +package selinux import ( - "github.com/rancher/os/config" - "github.com/rancher/os/log" - "github.com/rancher/os/selinux" "io/ioutil" + + "github.com/rancher/os/config" + "github.com/rancher/os/pkg/log" + "github.com/rancher/os/pkg/selinux" ) -func initializeSelinux(c *config.CloudConfig) (*config.CloudConfig, error) { +func Initialize(c *config.CloudConfig) (*config.CloudConfig, error) { ret, _ := selinux.InitializeSelinux() if ret != 0 { diff --git a/pkg/init/sharedroot/sharedroot.go b/pkg/init/sharedroot/sharedroot.go new file mode 100644 index 00000000..92673ad8 --- /dev/null +++ b/pkg/init/sharedroot/sharedroot.go @@ -0,0 +1,32 @@ +package sharedroot + +import ( + "os" + + "github.com/docker/docker/pkg/mount" + "github.com/rancher/os/config" + "github.com/rancher/os/pkg/init/fsmount" +) + +func Setup(c *config.CloudConfig) (*config.CloudConfig, error) { + if c.Rancher.NoSharedRoot { + return c, nil + } + + if fsmount.IsInitrd() { + for _, i := range []string{"/mnt", "/media", "/var/lib/system-docker"} { + if err := os.MkdirAll(i, 0755); err != nil { + return c, err + } + if err := mount.Mount("tmpfs", i, "tmpfs", "rw"); err != nil { + return c, err + } + if err := mount.MakeShared(i); err != nil { + return c, err + } + } + return c, nil + } + + return c, mount.MakeShared("/") +} diff --git a/init/root.go b/pkg/init/switchroot/root.go similarity index 88% rename from init/root.go rename to pkg/init/switchroot/root.go index f87d5c48..061c0c6d 100644 --- a/init/root.go +++ b/pkg/init/switchroot/root.go @@ -1,4 +1,4 @@ -package init +package switchroot import ( "fmt" @@ -10,10 +10,22 @@ import ( "github.com/docker/docker/pkg/archive" "github.com/rancher/os/config" - "github.com/rancher/os/dfs" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/dfs" + "github.com/rancher/os/pkg/init/fsmount" + "github.com/rancher/os/pkg/log" ) +func SwitchRoot(cfg *config.CloudConfig) (*config.CloudConfig, error) { + if !fsmount.ShouldSwitchRoot { + return cfg, nil + } + log.Debugf("Switching to new root at %s %s", config.State, cfg.Rancher.State.Directory) + if err := switchRoot(config.State, cfg.Rancher.State.Directory, cfg.Rancher.RmUsr); err != nil { + return cfg, err + } + return cfg, nil +} + func cleanupTarget(rootfs, targetUsr, usr, usrVer, tmpDir string) (bool, error) { log.Debugf("Deleting %s", targetUsr) if err := os.Remove(targetUsr); err != nil && !os.IsNotExist(err) { diff --git a/pkg/init/switchroot/switchroot.go b/pkg/init/switchroot/switchroot.go new file mode 100644 index 00000000..d6949662 --- /dev/null +++ b/pkg/init/switchroot/switchroot.go @@ -0,0 +1 @@ +package switchroot diff --git a/log/log.go b/pkg/log/log.go similarity index 100% rename from log/log.go rename to pkg/log/log.go diff --git a/log/showuserlog.go b/pkg/log/showuserlog.go similarity index 100% rename from log/showuserlog.go rename to pkg/log/showuserlog.go diff --git a/netconf/bonding.go b/pkg/netconf/bonding.go similarity index 98% rename from netconf/bonding.go rename to pkg/netconf/bonding.go index 62b173d1..2194beb2 100644 --- a/netconf/bonding.go +++ b/pkg/netconf/bonding.go @@ -7,7 +7,7 @@ import ( "strings" "time" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/log" "github.com/vishvananda/netlink" ) diff --git a/netconf/bridge.go b/pkg/netconf/bridge.go similarity index 100% rename from netconf/bridge.go rename to pkg/netconf/bridge.go diff --git a/netconf/ipv4ll_linux.go b/pkg/netconf/ipv4ll_linux.go similarity index 98% rename from netconf/ipv4ll_linux.go rename to pkg/netconf/ipv4ll_linux.go index deb38b20..09c0a913 100644 --- a/netconf/ipv4ll_linux.go +++ b/pkg/netconf/ipv4ll_linux.go @@ -6,7 +6,7 @@ import ( "math/rand" "net" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/log" "github.com/j-keck/arping" "github.com/vishvananda/netlink" diff --git a/netconf/netconf_linux.go b/pkg/netconf/netconf_linux.go similarity index 99% rename from netconf/netconf_linux.go rename to pkg/netconf/netconf_linux.go index 1d1de7aa..a8e73ed6 100644 --- a/netconf/netconf_linux.go +++ b/pkg/netconf/netconf_linux.go @@ -11,7 +11,7 @@ import ( "syscall" shlex "github.com/flynn/go-shlex" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/log" glob "github.com/ryanuber/go-glob" "github.com/vishvananda/netlink" diff --git a/netconf/types.go b/pkg/netconf/types.go similarity index 100% rename from netconf/types.go rename to pkg/netconf/types.go diff --git a/netconf/vlan.go b/pkg/netconf/vlan.go similarity index 100% rename from netconf/vlan.go rename to pkg/netconf/vlan.go diff --git a/selinux/selinux_linux.go b/pkg/selinux/selinux_linux.go similarity index 100% rename from selinux/selinux_linux.go rename to pkg/selinux/selinux_linux.go diff --git a/init/sysinit.go b/pkg/sysinit/sysinit.go similarity index 72% rename from init/sysinit.go rename to pkg/sysinit/sysinit.go index a6a2a129..2561facf 100644 --- a/init/sysinit.go +++ b/pkg/sysinit/sysinit.go @@ -1,4 +1,4 @@ -package init +package sysinit import ( "os" @@ -7,15 +7,14 @@ import ( "path/filepath" "syscall" - "golang.org/x/net/context" - "github.com/docker/engine-api/types" "github.com/docker/libcompose/project/options" "github.com/rancher/os/cmd/control" - "github.com/rancher/os/compose" "github.com/rancher/os/config" - "github.com/rancher/os/docker" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/compose" + "github.com/rancher/os/pkg/docker" + "github.com/rancher/os/pkg/log" + "golang.org/x/net/context" ) const ( @@ -23,7 +22,7 @@ const ( ) func hasImage(name string) bool { - stamp := path.Join(state, name) + stamp := path.Join(config.State, name) if _, err := os.Stat(stamp); os.IsNotExist(err) { return false } @@ -41,11 +40,11 @@ func getImagesArchive(bootstrap bool) string { return archive } -func loadBootstrapImages(cfg *config.CloudConfig) (*config.CloudConfig, error) { +func LoadBootstrapImages(cfg *config.CloudConfig) (*config.CloudConfig, error) { return loadImages(cfg, true) } -func loadSystemImages(cfg *config.CloudConfig) (*config.CloudConfig, error) { +func LoadSystemImages(cfg *config.CloudConfig) (*config.CloudConfig, error) { return loadImages(cfg, false) } @@ -89,9 +88,9 @@ func SysInit() error { } _, err := config.ChainCfgFuncs(cfg, - []config.CfgFuncData{ - config.CfgFuncData{"loadSystemImages", loadSystemImages}, - config.CfgFuncData{"start project", func(cfg *config.CloudConfig) (*config.CloudConfig, error) { + config.CfgFuncs{ + {"loadSystemImages", LoadSystemImages}, + {"start project", func(cfg *config.CloudConfig) (*config.CloudConfig, error) { p, err := compose.GetProject(cfg, false, true) if err != nil { return cfg, err @@ -103,13 +102,32 @@ func SysInit() error { Log: cfg.Rancher.Log, }) }}, - config.CfgFuncData{"sync", func(cfg *config.CloudConfig) (*config.CloudConfig, error) { + {"sync", func(cfg *config.CloudConfig) (*config.CloudConfig, error) { syscall.Sync() return cfg, nil }}, - config.CfgFuncData{"banner", func(cfg *config.CloudConfig) (*config.CloudConfig, error) { + {"banner", func(cfg *config.CloudConfig) (*config.CloudConfig, error) { log.Infof("RancherOS %s started", config.Version) return cfg, nil }}}) return err } + +func RunSysInit(c *config.CloudConfig) (*config.CloudConfig, error) { + args := append([]string{config.SysInitBin}, os.Args[1:]...) + + cmd := &exec.Cmd{ + Path: config.RosBin, + Args: args, + } + + cmd.Stdin = os.Stdin + cmd.Stderr = os.Stderr + cmd.Stdout = os.Stdout + + if err := cmd.Start(); err != nil { + return c, err + } + + return c, os.Stdin.Close() +} diff --git a/util/backoff.go b/pkg/util/backoff.go similarity index 100% rename from util/backoff.go rename to pkg/util/backoff.go diff --git a/util/cutil.go b/pkg/util/cutil.go similarity index 100% rename from util/cutil.go rename to pkg/util/cutil.go diff --git a/util/network/cache.go b/pkg/util/network/cache.go similarity index 96% rename from util/network/cache.go rename to pkg/util/network/cache.go index cba17518..225d2084 100644 --- a/util/network/cache.go +++ b/pkg/util/network/cache.go @@ -6,7 +6,7 @@ import ( "io/ioutil" "os" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/log" ) const ( diff --git a/util/network/network.go b/pkg/util/network/network.go similarity index 99% rename from util/network/network.go rename to pkg/util/network/network.go index 1a522c98..25515b79 100644 --- a/util/network/network.go +++ b/pkg/util/network/network.go @@ -12,7 +12,7 @@ import ( composeConfig "github.com/docker/libcompose/config" "github.com/rancher/os/config" - "github.com/rancher/os/log" + "github.com/rancher/os/pkg/log" ) var ( diff --git a/util/network/network_test.go b/pkg/util/network/network_test.go similarity index 100% rename from util/network/network_test.go rename to pkg/util/network/network_test.go diff --git a/util/term.go b/pkg/util/term.go similarity index 100% rename from util/term.go rename to pkg/util/term.go diff --git a/util/util.go b/pkg/util/util.go similarity index 100% rename from util/util.go rename to pkg/util/util.go diff --git a/util/util_linux.go b/pkg/util/util_linux.go similarity index 100% rename from util/util_linux.go rename to pkg/util/util_linux.go diff --git a/util/util_test.go b/pkg/util/util_test.go similarity index 100% rename from util/util_test.go rename to pkg/util/util_test.go