func main() { // FIXME: argsparse ftw! envFile := os.Args[1] cmd := os.Args[2] switch cmd { case "print": env, err := uenv.Open(envFile) if err != nil { log.Fatalf("uenv.Open failed for %s: %s", envFile, err) } fmt.Print(env) case "create": size, err := strconv.Atoi(os.Args[3]) if err != nil { log.Fatalf("Atoi failed for %s: %s", envFile, err) } env, err := uenv.Create(envFile, size) if err != nil { log.Fatalf("uenv.Create failed for %s: %s", envFile, err) } if err := env.Save(); err != nil { log.Fatalf("env.Save failed: %s", err) } case "set": env, err := uenv.Open(envFile) if err != nil { log.Fatalf("uenv.Open failed for %s: %s", envFile, err) } name := os.Args[3] value := os.Args[4] env.Set(name, value) if err := env.Save(); err != nil { log.Fatalf("env.Save failed for %s: %s", envFile, err) } case "import": env, err := uenv.Open(envFile) if err != nil { log.Fatalf("uenv.Open failed for %s: %s", envFile, err) } fname := os.Args[3] r, err := os.Open(fname) if err != nil { log.Fatalf("Open failed for %s: %s", fname, err) } if err := env.Import(r); err != nil { log.Fatalf("env.Import failed for %s: %s", envFile, err) } if err := env.Save(); err != nil { log.Fatalf("env.Save failed for %s: %s", envFile, err) } default: log.Fatalf("unknown command %s", cmd) } }
func (s *PartitionTestSuite) TestUbootSetEnvNoUselessWrites(c *C) { s.makeFakeUbootEnv(c) envFile := (&uboot{}).envFile() env, err := uenv.Create(envFile, 4096) c.Assert(err, IsNil) env.Set("snap_ab", "b") env.Set("snap_mode", "") err = env.Save() c.Assert(err, IsNil) st, err := os.Stat(envFile) c.Assert(err, IsNil) time.Sleep(100 * time.Millisecond) u := newUboot() c.Assert(u, NotNil) // note that we set to the same var as above err = u.SetBootVar("snap_ab", "b") c.Assert(err, IsNil) env, err = uenv.Open(envFile) c.Assert(err, IsNil) c.Assert(env.String(), Equals, "snap_ab=b\n") st2, err := os.Stat(envFile) c.Assert(err, IsNil) c.Assert(st.ModTime(), Equals, st2.ModTime()) }
func (s *PartitionTestSuite) TestUbootSetEnvNoUselessWrites(c *C) { s.makeFakeUbootEnv(c) env, err := uenv.Create(bootloaderUbootFwEnvFile, 4096) c.Assert(err, IsNil) env.Set("snappy_ab", "b") env.Set("snappy_mode", "regular") err = env.Save() c.Assert(err, IsNil) st, err := os.Stat(bootloaderUbootFwEnvFile) c.Assert(err, IsNil) time.Sleep(100 * time.Millisecond) partition := New() u := newUboot(partition) c.Assert(u, NotNil) err = setBootVar(bootloaderRootfsVar, "b") c.Assert(err, IsNil) env, err = uenv.Open(bootloaderUbootFwEnvFile) c.Assert(err, IsNil) c.Assert(env.String(), Equals, "snappy_ab=b\nsnappy_mode=regular\n") st2, err := os.Stat(bootloaderUbootFwEnvFile) c.Assert(err, IsNil) c.Assert(st.ModTime(), Equals, st2.ModTime()) }
func (u *uboot) GetBootVar(name string) (string, error) { env, err := uenv.Open(u.envFile()) if err != nil { return "", err } return env.Get(name), nil }
func getBootVarFwEnv(name string) (string, error) { env, err := uenv.Open(bootloaderUbootFwEnvFile) if err != nil { return "", err } return env.Get(name), nil }
func getUbootConfValue(key string) (string, error) { bootConfigFile := configFiles["uboot"] env, err := uenv.Open(bootConfigFile) if err != nil { return "", err } return env.Get(key), nil }
func (u *uboot) SetBootVar(name, value string) error { env, err := uenv.Open(u.envFile()) if err != nil { return err } // already set, nothing to do if env.Get(name) == value { return nil } env.Set(name, value) return env.Save() }
func setBootVarFwEnv(name, value string) error { env, err := uenv.Open(bootloaderUbootFwEnvFile) if err != nil { return err } // already set, nothing to do if env.Get(name) == value { return nil } env.Set(name, value) return env.Save() }
func (u *uboot) GetBootVars(names ...string) (map[string]string, error) { out := map[string]string{} env, err := uenv.Open(u.envFile()) if err != nil { return nil, err } for _, name := range names { out[name] = env.Get(name) } return out, nil }
func (s *PartitionTestSuite) TestUbootMarkCurrentBootSuccessfulFwEnv(c *C) { s.makeFakeUbootEnv(c) env, err := uenv.Create(bootloaderUbootFwEnvFile, 4096) c.Assert(err, IsNil) env.Set("snappy_ab", "b") env.Set("snappy_mode", "try") env.Set("snappy_trial_boot", "1") err = env.Save() c.Assert(err, IsNil) partition := New() u := newUboot(partition) c.Assert(u, NotNil) err = u.MarkCurrentBootSuccessful("b") c.Assert(err, IsNil) env, err = uenv.Open(bootloaderUbootFwEnvFile) c.Assert(err, IsNil) c.Assert(env.String(), Equals, "snappy_ab=b\nsnappy_mode=regular\nsnappy_trial_boot=0\n") }
func (u *uboot) SetBootVars(values map[string]string) error { env, err := uenv.Open(u.envFile()) if err != nil { return err } dirty := false for k, v := range values { // already set to the right value, nothing to do if env.Get(k) == v { continue } env.Set(k, v) dirty = true } if dirty { return env.Save() } return nil }