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 (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) makeFakeUbootEnv(c *C) { u := &uboot{} // ensure that we have a valid uboot.env too env, err := uenv.Create(u.envFile(), 4096) c.Assert(err, IsNil) err = env.Save() c.Assert(err, IsNil) }
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 createUbootConfigFile(c *check.C, contents map[string]string) (name string) { tmpDir, err := ioutil.TempDir("", "snappy-uboot-test") c.Assert(err, check.IsNil, check.Commentf("Error creating temp dir: %s", err)) fileName := path.Join(tmpDir, "uboot.env") env, err := uenv.Create(fileName, 4096) c.Assert(err, check.IsNil, check.Commentf("Error creating the uboot env file: %s", err)) for key, value := range contents { env.Set(key, value) } err = env.Save() c.Assert(err, check.IsNil, check.Commentf("Error saving the uboot env file: %s", err)) return fileName }
func (s *PartitionTestSuite) TestUbootGetBootVarFwEnv(c *C) { s.makeFakeUbootEnv(c) env, err := uenv.Create(bootloaderUbootFwEnvFile(), 4096) c.Assert(err, IsNil) env.Set("key2", "value2") err = env.Save() c.Assert(err, IsNil) partition := New() u := newUboot(partition) content, err := u.GetBootVar("key2") c.Assert(err, IsNil) c.Assert(content, Equals, "value2") }
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") }