Merge lp:~sergiusens/snappy/ubootNoUnnecessaryRewrites into lp:~snappy-dev/snappy/snappy-moved-to-github

Proposed by Sergio Schvezov on 2015-05-21
Status: Merged
Approved by: Michael Vogt on 2015-05-22
Approved revision: 471
Merged at revision: 470
Proposed branch: lp:~sergiusens/snappy/ubootNoUnnecessaryRewrites
Merge into: lp:~snappy-dev/snappy/snappy-moved-to-github
Diff against target: 134 lines (+59/-3)
2 files modified
partition/bootloader_uboot.go (+20/-3)
partition/bootloader_uboot_test.go (+39/-0)
To merge this branch: bzr merge lp:~sergiusens/snappy/ubootNoUnnecessaryRewrites
Reviewer Review Type Date Requested Status
Michael Vogt 2015-05-21 Approve on 2015-05-22
John Lenton 2015-05-21 Approve on 2015-05-21
Review via email: mp+259739@code.launchpad.net

Commit Message

Avoid writing to snappy-system.txt if not required

Description of the Change

This code base needs some love and I would of done it here if we didn't need to backport this commit to the 15.04 tree.

To post a comment you must log in.
John Lenton (chipaca) :
review: Approve
Michael Vogt (mvo) wrote :

Looks good, some comments inline, feel free to ignore.

Sergio Schvezov (sergiusens) wrote :

(BeagleBoneBlack)ubuntu@localhost:~$ ls /boot/uboot/ -l
total 4
drwxr-xr-x 3 root root 512 May 21 08:47 a
drwxr-xr-x 3 root root 512 May 21 08:47 b
-rwxr-xr-x 1 root root 1585 May 21 08:47 snappy-system.txt
-rwxr-xr-x 1 root root 237 May 21 08:47 uEnv.txt
(BeagleBoneBlack)ubuntu@localhost:~$ sudo snappy booted
(BeagleBoneBlack)ubuntu@localhost:~$ ls /boot/uboot/ -l
total 4
drwxr-xr-x 3 root root 512 May 21 08:47 a
drwxr-xr-x 3 root root 512 May 21 08:47 b
-rwxr-xr-x 1 root root 1585 May 21 08:47 snappy-system.txt
-rwxr-xr-x 1 root root 237 May 21 08:47 uEnv.txt
(BeagleBoneBlack)ubuntu@localhost:~$ sudo snappy list --verbose
Name Date Version Developer
ubuntu-core 2015-05-21 2 ubuntu*
ubuntu-core 2015-05-21 2 ubuntu
webdm 2015-05-21 0.6.1 *
beagleblack 2015-05-21 1.7.1 *
(BeagleBoneBlack)ubuntu@localhost:~$ sudo snappy update
Installing ubuntu-core (60)
Starting download of ubuntu-core
138.23 MB / 138.23 MB [=============================] 100.00 % 241.93 KB/s 9m45s
Done
Name Date Version Developer
ubuntu-core 2015-05-20 60 ubuntu!
Reboot to use the new ubuntu-core.

# timestamp is updated (regular->try a->b)
(BeagleBoneBlack)ubuntu@localhost:~$ ls /boot/uboot/ -l
total 4
drwxr-xr-x 3 root root 512 May 21 08:47 a
drwxr-xr-x 3 root root 512 May 21 08:47 b
-rwxr-xr-x 1 root root 1581 May 21 12:46 snappy-system.txt
-rwxr-xr-x 1 root root 237 May 21 08:47 uEnv.txt
(BeagleBoneBlack)ubuntu@localhost:~$ sudo reboot
# ...
# timestamp updated (try->regular)
(BeagleBoneBlack)ubuntu@localhost:~$ ls /boot/uboot/ -l
total 4
drwxr-xr-x 3 root root 512 May 21 08:47 a
drwxr-xr-x 3 root root 512 May 21 08:47 b
-rwxr-xr-x 1 root root 1585 May 21 12:54 snappy-system.txt
-rwxr-xr-x 1 root root 237 May 21 08:47 uEnv.txt

I'll do another test later

Sergio Schvezov (sergiusens) wrote :

thanks for the review, will update in a bit

469. By Sergio Schvezov on 2015-05-21

Remove unnecessary check if atomicWrite was called.

470. By Sergio Schvezov on 2015-05-21

Don't make atomicFileUpdateImpl a var

471. By Sergio Schvezov on 2015-05-21

Taking into consideration the almost impossible case of snappy-system.txt being incomplete.

Michael Vogt (mvo) wrote :

Thanks a lot for this update Sergio! This is good to go now :) In a better world we would also have a unittest for atomicFileUpdate() but I think the tests you added are just fine for now (as this needs a 15.04 backport). And we can improve further later. In any case, thanks a lot for working on this!

review: Approve
Michael Vogt (mvo) wrote :

What should we do about the cherry pick to 15.04? Will there be a separate branch?

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'partition/bootloader_uboot.go'
2--- partition/bootloader_uboot.go 2015-05-15 13:33:27 +0000
3+++ partition/bootloader_uboot.go 2015-05-21 17:13:00 +0000
4@@ -53,6 +53,7 @@
5 bootloaderUbootConfigFile = bootloaderUbootConfigFileReal
6 bootloaderUbootStampFile = bootloaderUbootStampFileReal
7 bootloaderUbootEnvFile = bootloaderUbootEnvFileReal
8+ atomicFileUpdate = atomicFileUpdateImpl
9 )
10
11 const bootloaderNameUboot bootloaderName = "u-boot"
12@@ -66,6 +67,7 @@
13 }
14
15 // Stores a Name and a Value to be added as a name=value pair in a file.
16+// TODO convert to map
17 type configFileChange struct {
18 Name string
19 Value string
20@@ -322,7 +324,7 @@
21
22 // Write lines to file atomically. File does not have to preexist.
23 // FIXME: put into utils package
24-func atomicFileUpdate(file string, lines []string) (err error) {
25+func atomicFileUpdateImpl(file string, lines []string) (err error) {
26 tmpFile := fmt.Sprintf("%s.NEW", file)
27
28 // XXX: if go switches to use aio_fsync, we need to open the dir for writing
29@@ -351,6 +353,7 @@
30 // appended to the file.
31 //
32 // FIXME: put into utils package
33+// FIXME: improve logic
34 func modifyNameValueFile(file string, changes []configFileChange) (err error) {
35 var updated []configFileChange
36
37@@ -360,12 +363,20 @@
38 }
39
40 var new []string
41+ // we won't write to a file if we don't need to.
42+ updateNeeded := false
43
44 for _, line := range lines {
45 for _, change := range changes {
46 if strings.HasPrefix(line, fmt.Sprintf("%s=", change.Name)) {
47- line = fmt.Sprintf("%s=%s", change.Name, change.Value)
48+ value := strings.SplitN(line, "=", 2)[1]
49+ // updated is used later to see if you had the originally requested
50+ // value.
51 updated = append(updated, change)
52+ if value != change.Value {
53+ line = fmt.Sprintf("%s=%s", change.Name, change.Value)
54+ updateNeeded = true
55+ }
56 }
57 }
58 new = append(new, line)
59@@ -383,6 +394,8 @@
60 }
61
62 if !got {
63+ updateNeeded = true
64+
65 // name/value pair did not exist in original
66 // file, so append
67 lines = append(lines, fmt.Sprintf("%s=%s",
68@@ -390,7 +403,11 @@
69 }
70 }
71
72- return atomicFileUpdate(file, lines)
73+ if updateNeeded {
74+ return atomicFileUpdate(file, lines)
75+ }
76+
77+ return nil
78 }
79
80 func (u *uboot) AdditionalBindMounts() []string {
81
82=== modified file 'partition/bootloader_uboot_test.go'
83--- partition/bootloader_uboot_test.go 2015-05-15 13:33:27 +0000
84+++ partition/bootloader_uboot_test.go 2015-05-21 17:13:00 +0000
85@@ -29,6 +29,8 @@
86 "launchpad.net/snappy/helpers"
87 )
88
89+// TODO move to uboot specific test suite.
90+
91 const fakeUbootEnvData = `
92 # This is a snappy variables and boot logic file and is entirely generated and
93 # managed by Snappy. Modifications may break boot
94@@ -291,3 +293,40 @@
95 c.Assert(strings.Contains(string(bytes), "snappy_mode=regular"), Equals, true)
96 c.Assert(strings.Contains(string(bytes), "snappy_ab=a"), Equals, true)
97 }
98+
99+func (s *PartitionTestSuite) TestNoWriteNotNeeded(c *C) {
100+ s.makeFakeUbootEnv(c)
101+
102+ atomiCall := false
103+ atomicFileUpdate = func(a string, b []string) error { atomiCall = true; return atomicFileUpdateImpl(a, b) }
104+
105+ partition := New()
106+ u := newUboot(partition)
107+ c.Assert(u, NotNil)
108+
109+ c.Check(u.MarkCurrentBootSuccessful(), IsNil)
110+ c.Assert(atomiCall, Equals, false)
111+}
112+
113+func (s *PartitionTestSuite) TestWriteDueToMissingValues(c *C) {
114+ s.makeFakeUbootEnv(c)
115+
116+ // this file needs specific data
117+ c.Assert(ioutil.WriteFile(bootloaderUbootEnvFile, []byte(""), 0644), IsNil)
118+
119+ atomiCall := false
120+ atomicFileUpdate = func(a string, b []string) error { atomiCall = true; return atomicFileUpdateImpl(a, b) }
121+
122+ partition := New()
123+ u := newUboot(partition)
124+ c.Assert(u, NotNil)
125+
126+ c.Check(u.MarkCurrentBootSuccessful(), IsNil)
127+ c.Assert(atomiCall, Equals, true)
128+
129+ bytes, err := ioutil.ReadFile(bootloaderUbootEnvFile)
130+ c.Assert(err, IsNil)
131+ c.Check(strings.Contains(string(bytes), "snappy_mode=try"), Equals, false)
132+ c.Check(strings.Contains(string(bytes), "snappy_mode=regular"), Equals, true)
133+ c.Check(strings.Contains(string(bytes), "snappy_ab=a"), Equals, true)
134+}

Subscribers

People subscribed via source and target branches