Merge lp:~mvo/snappy/snappy-all-snap2 into lp:~snappy-dev/snappy/snappy-moved-to-github
- snappy-all-snap2
- Merge into snappy-moved-to-github
Proposed by
Michael Vogt
Status: | Work in progress |
---|---|
Proposed branch: | lp:~mvo/snappy/snappy-all-snap2 |
Merge into: | lp:~snappy-dev/snappy/snappy-moved-to-github |
Diff against target: |
5273 lines (+682/-3895) 35 files modified
bootloader/bootloader.go (+65/-229) bootloader/bootloader_grub.go (+4/-51) bootloader/bootloader_grub_test.go (+47/-122) bootloader/bootloader_test.go (+94/-381) bootloader/bootloader_uboot.go (+25/-216) bootloader/bootloader_uboot_test.go (+47/-285) bootloader/utils.go (+63/-0) bootloader/utils_test.go (+47/-0) cmd/snappy/cmd_booted.go (+2/-8) gen-coverage.sh (+2/-2) partition/assets.go (+0/-87) partition/assets_test.go (+0/-36) partition/dirs.go (+0/-41) partition/mount.go (+0/-153) partition/mount_test.go (+0/-64) partition/partition.go (+0/-622) partition/utils.go (+0/-82) partition/utils_test.go (+0/-47) pkg/types.go (+2/-0) snappy/click.go (+2/-1) snappy/dirs.go (+4/-0) snappy/install.go (+2/-2) snappy/install_test.go (+0/-92) snappy/kernel.go (+103/-0) snappy/oem.go (+11/-0) snappy/os.go (+68/-0) snappy/parts.go (+4/-8) snappy/purge.go (+1/-1) snappy/purge_test.go (+1/-1) snappy/snapp.go (+80/-15) snappy/snapp_test.go (+8/-11) snappy/systemimage.go (+0/-518) snappy/systemimage_native.go (+0/-211) snappy/systemimage_native_test.go (+0/-123) snappy/systemimage_test.go (+0/-486) |
To merge this branch: | bzr merge lp:~mvo/snappy/snappy-all-snap2 |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Snappy Developers | Pending | ||
Review via email: mp+269922@code.launchpad.net |
Commit message
Description of the change
This moves the os/kernel into snaps.
To post a comment you must log in.
lp:~mvo/snappy/snappy-all-snap2
updated
- 667. By Michael Vogt
-
fix activate in the kernel
- 668. By Michael Vogt
-
re-add utils.go
Unmerged revisions
- 668. By Michael Vogt
-
re-add utils.go
- 667. By Michael Vogt
-
fix activate in the kernel
- 666. By Michael Vogt
-
add {Oem,OsKernel}Snap types
- 665. By Michael Vogt
-
re-add test hook function
- 664. By Michael Vogt
-
rename partition to bootloader
- 663. By Michael Vogt
-
add tests for the new code
- 662. By Michael Vogt
-
rename PartitionTestSu
ite->Bootloader TestSuite - 661. By Michael Vogt
-
unset snappy_trial_boot
- 660. By Michael Vogt
-
move partition.go bits into bootloader as we do not care about partitioning at this level anymore
- 659. By Michael Vogt
-
kill partition.
IsNextBootOther
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === renamed directory 'partition' => 'bootloader' | |||
2 | === modified file 'bootloader/bootloader.go' | |||
3 | --- partition/bootloader.go 2015-07-16 11:07:30 +0000 | |||
4 | +++ bootloader/bootloader.go 2015-09-03 08:14:56 +0000 | |||
5 | @@ -17,70 +17,40 @@ | |||
6 | 17 | * | 17 | * |
7 | 18 | */ | 18 | */ |
8 | 19 | 19 | ||
10 | 20 | package partition | 20 | package bootloader |
11 | 21 | 21 | ||
12 | 22 | import ( | 22 | import ( |
16 | 23 | "fmt" | 23 | "errors" |
17 | 24 | "os" | 24 | "strings" |
18 | 25 | "path/filepath" | 25 | ) |
19 | 26 | 26 | ||
21 | 27 | "launchpad.net/snappy/helpers" | 27 | var ( |
22 | 28 | // ErrBootloader is returned if the bootloader can not be determined | ||
23 | 29 | ErrBootloader = errors.New("Unable to determine bootloader") | ||
24 | 28 | ) | 30 | ) |
25 | 29 | 31 | ||
26 | 30 | const ( | 32 | const ( |
27 | 31 | // bootloader variable used to denote which rootfs to boot from | ||
28 | 32 | bootloaderRootfsVar = "snappy_ab" | ||
29 | 33 | |||
30 | 34 | // bootloader variable used to determine if boot was successful. | 33 | // bootloader variable used to determine if boot was successful. |
31 | 35 | // Set to value of either bootloaderBootmodeTry (when attempting | 34 | // Set to value of either bootloaderBootmodeTry (when attempting |
32 | 36 | // to boot a new rootfs) or bootloaderBootmodeSuccess (to denote | 35 | // to boot a new rootfs) or bootloaderBootmodeSuccess (to denote |
33 | 37 | // that the boot of the new rootfs was successful). | 36 | // that the boot of the new rootfs was successful). |
34 | 38 | bootloaderBootmodeVar = "snappy_mode" | 37 | bootloaderBootmodeVar = "snappy_mode" |
35 | 39 | 38 | ||
36 | 40 | bootloaderTrialBootVar = "snappy_trial_boot" | ||
37 | 41 | |||
38 | 42 | // Initial and final values | 39 | // Initial and final values |
39 | 43 | bootloaderBootmodeTry = "try" | 40 | bootloaderBootmodeTry = "try" |
40 | 44 | bootloaderBootmodeSuccess = "regular" | 41 | bootloaderBootmodeSuccess = "regular" |
41 | 45 | 42 | ||
44 | 46 | // textual description in hardware.yaml for AB systems | 43 | // set by the bootloader itself to "1" and it will be cleared |
45 | 47 | bootloaderSystemAB = "system-AB" | 44 | // as the last action of the boot |
46 | 45 | bootloaderTrialBootVar = "snappy_trial_boot" | ||
47 | 48 | ) | 46 | ) |
48 | 49 | 47 | ||
49 | 50 | type bootloaderName string | ||
50 | 51 | |||
51 | 52 | type bootLoader interface { | 48 | type bootLoader interface { |
52 | 53 | // Name of the bootloader | ||
53 | 54 | Name() bootloaderName | ||
54 | 55 | |||
55 | 56 | // Switch bootloader configuration so that the "other" root | ||
56 | 57 | // filesystem partition will be used on next boot. | ||
57 | 58 | ToggleRootFS(otherRootfs string) error | ||
58 | 59 | |||
59 | 60 | // Hook function called before system-image starts downloading | ||
60 | 61 | // and applying archives that allows files to be copied between | ||
61 | 62 | // partitions. | ||
62 | 63 | SyncBootFiles(bootAssets map[string]string) error | ||
63 | 64 | |||
64 | 65 | // Install any hardware-specific files that system-image | ||
65 | 66 | // downloaded. | ||
66 | 67 | HandleAssets() error | ||
67 | 68 | |||
68 | 69 | // Return the value of the specified bootloader variable | 49 | // Return the value of the specified bootloader variable |
69 | 70 | GetBootVar(name string) (string, error) | 50 | GetBootVar(name string) (string, error) |
70 | 71 | 51 | ||
83 | 72 | // Return the 1-character name corresponding to the | 52 | // Set the value of the specified bootloader variable |
84 | 73 | // rootfs that will be used on _next_ boot. | 53 | SetBootVar(key, value string) error |
73 | 74 | // | ||
74 | 75 | // XXX: Note the distinction between this method and | ||
75 | 76 | // GetOtherRootFSName(): the latter corresponds to the other | ||
76 | 77 | // partition, whereas the value returned by this method is | ||
77 | 78 | // queried directly from the bootloader. | ||
78 | 79 | GetNextBootRootFSName() (string, error) | ||
79 | 80 | |||
80 | 81 | // Update the bootloader configuration to mark the | ||
81 | 82 | // currently-booted rootfs as having booted successfully. | ||
82 | 83 | MarkCurrentBootSuccessful(currentRootfs string) error | ||
85 | 84 | 54 | ||
86 | 85 | // BootDir returns the (writable) bootloader-specific boot | 55 | // BootDir returns the (writable) bootloader-specific boot |
87 | 86 | // directory. | 56 | // directory. |
88 | @@ -88,16 +58,16 @@ | |||
89 | 88 | } | 58 | } |
90 | 89 | 59 | ||
91 | 90 | // Factory method that returns a new bootloader for the given partition | 60 | // Factory method that returns a new bootloader for the given partition |
93 | 91 | var bootloader = bootloaderImpl | 61 | var new = bootloaderImpl |
94 | 92 | 62 | ||
96 | 93 | func bootloaderImpl(p *Partition) (bootLoader, error) { | 63 | func bootloaderImpl() (bootLoader, error) { |
97 | 94 | // try uboot | 64 | // try uboot |
99 | 95 | if uboot := newUboot(p); uboot != nil { | 65 | if uboot := newUboot(); uboot != nil { |
100 | 96 | return uboot, nil | 66 | return uboot, nil |
101 | 97 | } | 67 | } |
102 | 98 | 68 | ||
103 | 99 | // no, try grub | 69 | // no, try grub |
105 | 100 | if grub := newGrub(p); grub != nil { | 70 | if grub := newGrub(); grub != nil { |
106 | 101 | return grub, nil | 71 | return grub, nil |
107 | 102 | } | 72 | } |
108 | 103 | 73 | ||
109 | @@ -105,196 +75,62 @@ | |||
110 | 105 | return nil, ErrBootloader | 75 | return nil, ErrBootloader |
111 | 106 | } | 76 | } |
112 | 107 | 77 | ||
271 | 108 | type bootloaderType struct { | 78 | // MarkBootSuccessful marks the boot as successful |
272 | 109 | partition *Partition | 79 | func MarkBootSuccessful() (err error) { |
273 | 110 | 80 | bootloader, err := new() | |
274 | 111 | // each rootfs partition has a corresponding u-boot directory named | 81 | if err != nil { |
275 | 112 | // from the last character of the partition name ('a' or 'b'). | 82 | return err |
276 | 113 | currentRootfs string | 83 | } |
277 | 114 | otherRootfs string | 84 | |
278 | 115 | 85 | // FIXME: these vars should be applied all in a single run | |
279 | 116 | // full path to rootfs-specific assets on boot partition | 86 | for _, k := range []string{"snappy_os", "snappy_kernel"} { |
280 | 117 | currentBootPath string | 87 | value, err := bootloader.GetBootVar(k) |
123 | 118 | otherBootPath string | ||
124 | 119 | |||
125 | 120 | // FIXME: this should /boot if possible | ||
126 | 121 | // the dir that the bootloader lives in (e.g. /boot/uboot) | ||
127 | 122 | bootloaderDir string | ||
128 | 123 | } | ||
129 | 124 | |||
130 | 125 | func newBootLoader(partition *Partition, bootloaderDir string) *bootloaderType { | ||
131 | 126 | // FIXME: is this the right thing to do? i.e. what should we do | ||
132 | 127 | // on a single partition system? | ||
133 | 128 | if partition.otherRootPartition() == nil { | ||
134 | 129 | return nil | ||
135 | 130 | } | ||
136 | 131 | |||
137 | 132 | // full label of the system {system-a,system-b} | ||
138 | 133 | currentLabel := partition.rootPartition().name | ||
139 | 134 | otherLabel := partition.otherRootPartition().name | ||
140 | 135 | |||
141 | 136 | // single letter description of the rootfs {a,b} | ||
142 | 137 | currentRootfs := string(currentLabel[len(currentLabel)-1]) | ||
143 | 138 | otherRootfs := string(otherLabel[len(otherLabel)-1]) | ||
144 | 139 | |||
145 | 140 | return &bootloaderType{ | ||
146 | 141 | partition: partition, | ||
147 | 142 | |||
148 | 143 | currentRootfs: currentRootfs, | ||
149 | 144 | otherRootfs: otherRootfs, | ||
150 | 145 | |||
151 | 146 | // the paths that the kernel/initramfs are loaded, e.g. | ||
152 | 147 | // /boot/uboot/a | ||
153 | 148 | currentBootPath: filepath.Join(bootloaderDir, currentRootfs), | ||
154 | 149 | otherBootPath: filepath.Join(bootloaderDir, otherRootfs), | ||
155 | 150 | |||
156 | 151 | // the base bootloader dir, e.g. /boot/uboot or /boot/grub | ||
157 | 152 | bootloaderDir: bootloaderDir, | ||
158 | 153 | } | ||
159 | 154 | } | ||
160 | 155 | |||
161 | 156 | // FIXME: | ||
162 | 157 | // - populate kernel if missing | ||
163 | 158 | func (b *bootloaderType) SyncBootFiles(bootAssets map[string]string) (err error) { | ||
164 | 159 | for src, dst := range bootAssets { | ||
165 | 160 | if err := helpers.CopyIfDifferent(src, filepath.Join(b.bootloaderDir, dst)); err != nil { | ||
166 | 161 | return err | ||
167 | 162 | } | ||
168 | 163 | } | ||
169 | 164 | |||
170 | 165 | srcDir := b.currentBootPath | ||
171 | 166 | destDir := b.otherBootPath | ||
172 | 167 | |||
173 | 168 | // ensure they exist | ||
174 | 169 | for _, dir := range []string{srcDir, destDir} { | ||
175 | 170 | if err := os.MkdirAll(dir, 0755); err != nil { | ||
176 | 171 | return err | ||
177 | 172 | } | ||
178 | 173 | |||
179 | 174 | } | ||
180 | 175 | return helpers.RSyncWithDelete(srcDir, destDir) | ||
181 | 176 | } | ||
182 | 177 | |||
183 | 178 | // FIXME: | ||
184 | 179 | // - if this fails it will never be re-tried because the "other" patition | ||
185 | 180 | // is updated to revision-N in /etc/system-image/channel.ini | ||
186 | 181 | // so the system only downloads from revision-N onwards even though the | ||
187 | 182 | // complete update was not applied (i.e. kernel missing) | ||
188 | 183 | func (b *bootloaderType) HandleAssets() (err error) { | ||
189 | 184 | // check if we have anything, if there is no hardware yaml, there is nothing | ||
190 | 185 | // to process. | ||
191 | 186 | hardware, err := readHardwareSpec() | ||
192 | 187 | if err == ErrNoHardwareYaml { | ||
193 | 188 | return nil | ||
194 | 189 | } else if err != nil { | ||
195 | 190 | return err | ||
196 | 191 | } | ||
197 | 192 | // ensure to remove the file if there are no errors | ||
198 | 193 | defer func() { | ||
199 | 194 | if err == nil { | ||
200 | 195 | os.Remove(hardwareSpecFile) | ||
201 | 196 | } | ||
202 | 197 | }() | ||
203 | 198 | |||
204 | 199 | /* | ||
205 | 200 | // validate bootloader | ||
206 | 201 | if hardware.Bootloader != b.Name() { | ||
207 | 202 | return fmt.Errorf( | ||
208 | 203 | "bootloader is of type %s but hardware spec requires %s", | ||
209 | 204 | b.Name(), | ||
210 | 205 | hardware.Bootloader) | ||
211 | 206 | } | ||
212 | 207 | */ | ||
213 | 208 | |||
214 | 209 | // validate partition layout | ||
215 | 210 | if b.partition.dualRootPartitions() && hardware.PartitionLayout != bootloaderSystemAB { | ||
216 | 211 | return fmt.Errorf("hardware spec requires dual root partitions") | ||
217 | 212 | } | ||
218 | 213 | |||
219 | 214 | // ensure we have the destdir | ||
220 | 215 | destDir := b.otherBootPath | ||
221 | 216 | if err := os.MkdirAll(destDir, dirMode); err != nil { | ||
222 | 217 | return err | ||
223 | 218 | } | ||
224 | 219 | |||
225 | 220 | // install kernel+initrd | ||
226 | 221 | for _, file := range []string{hardware.Kernel, hardware.Initrd} { | ||
227 | 222 | |||
228 | 223 | if file == "" { | ||
229 | 224 | continue | ||
230 | 225 | } | ||
231 | 226 | |||
232 | 227 | // expand path | ||
233 | 228 | path := filepath.Join(cacheDir, file) | ||
234 | 229 | |||
235 | 230 | if !helpers.FileExists(path) { | ||
236 | 231 | return fmt.Errorf("can not find file %s", path) | ||
237 | 232 | } | ||
238 | 233 | |||
239 | 234 | // ensure we remove the dir later | ||
240 | 235 | defer func() { | ||
241 | 236 | if err == nil { | ||
242 | 237 | os.RemoveAll(filepath.Dir(path)) | ||
243 | 238 | } | ||
244 | 239 | }() | ||
245 | 240 | |||
246 | 241 | if err := runCommand("/bin/cp", path, destDir); err != nil { | ||
247 | 242 | return err | ||
248 | 243 | } | ||
249 | 244 | } | ||
250 | 245 | |||
251 | 246 | // TODO: look at the OEM package for dtb changes too once that is | ||
252 | 247 | // fully speced | ||
253 | 248 | |||
254 | 249 | // install .dtb files | ||
255 | 250 | dtbSrcDir := filepath.Join(cacheDir, hardware.DtbDir) | ||
256 | 251 | // ensure there is a DtbDir specified | ||
257 | 252 | if hardware.DtbDir != "" && helpers.FileExists(dtbSrcDir) { | ||
258 | 253 | // ensure we cleanup the source dir | ||
259 | 254 | defer func() { | ||
260 | 255 | if err == nil { | ||
261 | 256 | os.RemoveAll(dtbSrcDir) | ||
262 | 257 | } | ||
263 | 258 | }() | ||
264 | 259 | |||
265 | 260 | dtbDestDir := filepath.Join(destDir, "dtbs") | ||
266 | 261 | if err := os.MkdirAll(dtbDestDir, dirMode); err != nil { | ||
267 | 262 | return err | ||
268 | 263 | } | ||
269 | 264 | |||
270 | 265 | files, err := filepath.Glob(filepath.Join(dtbSrcDir, "*")) | ||
281 | 266 | if err != nil { | 88 | if err != nil { |
282 | 267 | return err | 89 | return err |
283 | 268 | } | 90 | } |
284 | 269 | 91 | ||
298 | 270 | for _, file := range files { | 92 | // FIXME: a bit ugly |
299 | 271 | if err := runCommand("/bin/cp", file, dtbDestDir); err != nil { | 93 | newKey := strings.Replace(k, "snappy_", "snappy_good_", -1) |
300 | 272 | return err | 94 | if err := bootloader.SetBootVar(newKey, value); err != nil { |
288 | 273 | } | ||
289 | 274 | } | ||
290 | 275 | } | ||
291 | 276 | |||
292 | 277 | if helpers.FileExists(flashAssetsDir) { | ||
293 | 278 | // FIXME: we don't currently do anything with the | ||
294 | 279 | // MLO + uImage files since they are not specified in | ||
295 | 280 | // the hardware spec. So for now, just remove them. | ||
296 | 281 | |||
297 | 282 | if err := os.RemoveAll(flashAssetsDir); err != nil { | ||
301 | 283 | return err | 95 | return err |
302 | 284 | } | 96 | } |
306 | 285 | } | 97 | |
307 | 286 | 98 | } | |
308 | 287 | return err | 99 | |
309 | 100 | if err := bootloader.SetBootVar("snappy_mode", "regular"); err != nil { | ||
310 | 101 | return err | ||
311 | 102 | } | ||
312 | 103 | |||
313 | 104 | return bootloader.SetBootVar("snappy_trial_boot", "0") | ||
314 | 288 | } | 105 | } |
315 | 289 | 106 | ||
317 | 290 | // BootloaderDir returns the full path to the (mounted and writable) | 107 | // Dir returns the full path to the (mounted and writable) |
318 | 291 | // bootloader-specific boot directory. | 108 | // bootloader-specific boot directory. |
327 | 292 | func BootloaderDir() string { | 109 | func Dir() string { |
328 | 293 | if helpers.FileExists(bootloaderUbootDir) { | 110 | bootloader, err := new() |
329 | 294 | return bootloaderUbootDir | 111 | if err != nil { |
330 | 295 | } else if helpers.FileExists(bootloaderGrubDir) { | 112 | return "" |
331 | 296 | return bootloaderGrubDir | 113 | } |
332 | 297 | } | 114 | |
333 | 298 | 115 | return bootloader.BootDir() | |
334 | 299 | return "" | 116 | } |
335 | 117 | |||
336 | 118 | // SetBootVar sets a boot variable | ||
337 | 119 | func SetBootVar(key, value string) error { | ||
338 | 120 | bootloader, err := new() | ||
339 | 121 | if err != nil { | ||
340 | 122 | return err | ||
341 | 123 | } | ||
342 | 124 | |||
343 | 125 | return bootloader.SetBootVar(key, value) | ||
344 | 126 | } | ||
345 | 127 | |||
346 | 128 | // GetBootVar sets a boot variable | ||
347 | 129 | func GetBootVar(key string) (string, error) { | ||
348 | 130 | bootloader, err := new() | ||
349 | 131 | if err != nil { | ||
350 | 132 | return "", err | ||
351 | 133 | } | ||
352 | 134 | |||
353 | 135 | return bootloader.GetBootVar(key) | ||
354 | 300 | } | 136 | } |
355 | 301 | 137 | ||
356 | === modified file 'bootloader/bootloader_grub.go' | |||
357 | --- partition/bootloader_grub.go 2015-07-23 11:54:14 +0000 | |||
358 | +++ bootloader/bootloader_grub.go 2015-09-03 08:14:56 +0000 | |||
359 | @@ -17,7 +17,7 @@ | |||
360 | 17 | * | 17 | * |
361 | 18 | */ | 18 | */ |
362 | 19 | 19 | ||
364 | 20 | package partition | 20 | package bootloader |
365 | 21 | 21 | ||
366 | 22 | import ( | 22 | import ( |
367 | 23 | "fmt" | 23 | "fmt" |
368 | @@ -45,45 +45,15 @@ | |||
369 | 45 | ) | 45 | ) |
370 | 46 | 46 | ||
371 | 47 | type grub struct { | 47 | type grub struct { |
372 | 48 | bootloaderType | ||
373 | 49 | } | 48 | } |
374 | 50 | 49 | ||
375 | 51 | const bootloaderNameGrub bootloaderName = "grub" | ||
376 | 52 | |||
377 | 53 | // newGrub create a new Grub bootloader object | 50 | // newGrub create a new Grub bootloader object |
379 | 54 | func newGrub(partition *Partition) bootLoader { | 51 | func newGrub() bootLoader { |
380 | 55 | if !helpers.FileExists(bootloaderGrubConfigFile) { | 52 | if !helpers.FileExists(bootloaderGrubConfigFile) { |
381 | 56 | return nil | 53 | return nil |
382 | 57 | } | 54 | } |
383 | 58 | 55 | ||
412 | 59 | b := newBootLoader(partition, bootloaderGrubDir) | 56 | return &grub{} |
385 | 60 | if b == nil { | ||
386 | 61 | return nil | ||
387 | 62 | } | ||
388 | 63 | g := grub{bootloaderType: *b} | ||
389 | 64 | |||
390 | 65 | return &g | ||
391 | 66 | } | ||
392 | 67 | |||
393 | 68 | func (g *grub) Name() bootloaderName { | ||
394 | 69 | return bootloaderNameGrub | ||
395 | 70 | } | ||
396 | 71 | |||
397 | 72 | // ToggleRootFS make the Grub bootloader switch rootfs's. | ||
398 | 73 | // | ||
399 | 74 | // Approach: | ||
400 | 75 | // | ||
401 | 76 | // Update the grub configuration. | ||
402 | 77 | func (g *grub) ToggleRootFS(otherRootfs string) (err error) { | ||
403 | 78 | |||
404 | 79 | if err := g.setBootVar(bootloaderBootmodeVar, bootloaderBootmodeTry); err != nil { | ||
405 | 80 | return err | ||
406 | 81 | } | ||
407 | 82 | |||
408 | 83 | // Record the partition that will be used for next boot. This | ||
409 | 84 | // isn't necessary for correct operation under grub, but allows | ||
410 | 85 | // us to query the next boot device easily. | ||
411 | 86 | return g.setBootVar(bootloaderRootfsVar, otherRootfs) | ||
413 | 87 | } | 57 | } |
414 | 88 | 58 | ||
415 | 89 | func (g *grub) GetBootVar(name string) (value string, err error) { | 59 | func (g *grub) GetBootVar(name string) (value string, err error) { |
416 | @@ -103,7 +73,7 @@ | |||
417 | 103 | return cfg.Get("", name) | 73 | return cfg.Get("", name) |
418 | 104 | } | 74 | } |
419 | 105 | 75 | ||
421 | 106 | func (g *grub) setBootVar(name, value string) (err error) { | 76 | func (g *grub) SetBootVar(name, value string) (err error) { |
422 | 107 | // note that strings are not quoted since because | 77 | // note that strings are not quoted since because |
423 | 108 | // RunCommand() does not use a shell and thus adding quotes | 78 | // RunCommand() does not use a shell and thus adding quotes |
424 | 109 | // stores them in the environment file (which is not desirable) | 79 | // stores them in the environment file (which is not desirable) |
425 | @@ -111,23 +81,6 @@ | |||
426 | 111 | return runCommand(bootloaderGrubEnvCmd, bootloaderGrubEnvFile, "set", arg) | 81 | return runCommand(bootloaderGrubEnvCmd, bootloaderGrubEnvFile, "set", arg) |
427 | 112 | } | 82 | } |
428 | 113 | 83 | ||
429 | 114 | func (g *grub) GetNextBootRootFSName() (label string, err error) { | ||
430 | 115 | return g.GetBootVar(bootloaderRootfsVar) | ||
431 | 116 | } | ||
432 | 117 | |||
433 | 118 | func (g *grub) MarkCurrentBootSuccessful(currentRootfs string) (err error) { | ||
434 | 119 | // Clear the variable set on boot to denote a good boot. | ||
435 | 120 | if err := g.setBootVar(bootloaderTrialBootVar, "0"); err != nil { | ||
436 | 121 | return err | ||
437 | 122 | } | ||
438 | 123 | |||
439 | 124 | if err := g.setBootVar(bootloaderRootfsVar, currentRootfs); err != nil { | ||
440 | 125 | return err | ||
441 | 126 | } | ||
442 | 127 | |||
443 | 128 | return g.setBootVar(bootloaderBootmodeVar, bootloaderBootmodeSuccess) | ||
444 | 129 | } | ||
445 | 130 | |||
446 | 131 | func (g *grub) BootDir() string { | 84 | func (g *grub) BootDir() string { |
447 | 132 | return bootloaderGrubDir | 85 | return bootloaderGrubDir |
448 | 133 | } | 86 | } |
449 | 134 | 87 | ||
450 | === modified file 'bootloader/bootloader_grub_test.go' | |||
451 | --- partition/bootloader_grub_test.go 2015-07-23 11:54:14 +0000 | |||
452 | +++ bootloader/bootloader_grub_test.go 2015-09-03 08:14:56 +0000 | |||
453 | @@ -17,15 +17,13 @@ | |||
454 | 17 | * | 17 | * |
455 | 18 | */ | 18 | */ |
456 | 19 | 19 | ||
458 | 20 | package partition | 20 | package bootloader |
459 | 21 | 21 | ||
460 | 22 | import ( | 22 | import ( |
461 | 23 | "fmt" | 23 | "fmt" |
462 | 24 | "io/ioutil" | 24 | "io/ioutil" |
463 | 25 | "os" | 25 | "os" |
467 | 26 | "path/filepath" | 26 | "strings" |
465 | 27 | |||
466 | 28 | "launchpad.net/snappy/helpers" | ||
468 | 29 | 27 | ||
469 | 30 | . "gopkg.in/check.v1" | 28 | . "gopkg.in/check.v1" |
470 | 31 | ) | 29 | ) |
471 | @@ -35,7 +33,21 @@ | |||
472 | 35 | c.Assert(err, IsNil) | 33 | c.Assert(err, IsNil) |
473 | 36 | } | 34 | } |
474 | 37 | 35 | ||
476 | 38 | func (s *PartitionTestSuite) makeFakeGrubEnv(c *C) { | 36 | type singleCommand []string |
477 | 37 | |||
478 | 38 | var allCommands = []singleCommand{} | ||
479 | 39 | |||
480 | 40 | func mockRunCommandWithCapture(args ...string) (err error) { | ||
481 | 41 | allCommands = append(allCommands, args) | ||
482 | 42 | return nil | ||
483 | 43 | } | ||
484 | 44 | |||
485 | 45 | func mockGrubEditenvList(cmd ...string) (string, error) { | ||
486 | 46 | mockGrubEditenvOutput := fmt.Sprintf("%s=regular", bootloaderBootmodeVar) | ||
487 | 47 | return mockGrubEditenvOutput, nil | ||
488 | 48 | } | ||
489 | 49 | |||
490 | 50 | func (s *BootloaderTestSuite) makeFakeGrubEnv(c *C) { | ||
491 | 39 | // create bootloader | 51 | // create bootloader |
492 | 40 | err := os.MkdirAll(bootloaderGrubDir, 0755) | 52 | err := os.MkdirAll(bootloaderGrubDir, 0755) |
493 | 41 | c.Assert(err, IsNil) | 53 | c.Assert(err, IsNil) |
494 | @@ -48,135 +60,48 @@ | |||
495 | 48 | runCommand = mockRunCommandWithCapture | 60 | runCommand = mockRunCommandWithCapture |
496 | 49 | } | 61 | } |
497 | 50 | 62 | ||
499 | 51 | func (s *PartitionTestSuite) TestNewGrubNoGrubReturnsNil(c *C) { | 63 | func (s *BootloaderTestSuite) TestNewGrubNoGrubReturnsNil(c *C) { |
500 | 52 | bootloaderGrubConfigFile = "no-such-dir" | 64 | bootloaderGrubConfigFile = "no-such-dir" |
501 | 53 | 65 | ||
504 | 54 | partition := New() | 66 | g := newGrub() |
503 | 55 | g := newGrub(partition) | ||
505 | 56 | c.Assert(g, IsNil) | 67 | c.Assert(g, IsNil) |
506 | 57 | } | 68 | } |
507 | 58 | 69 | ||
557 | 59 | func (s *PartitionTestSuite) TestNewGrub(c *C) { | 70 | func (s *BootloaderTestSuite) TestNewGrub(c *C) { |
558 | 60 | s.makeFakeGrubEnv(c) | 71 | s.makeFakeGrubEnv(c) |
559 | 61 | 72 | ||
560 | 62 | partition := New() | 73 | g := newGrub() |
561 | 63 | g := newGrub(partition) | 74 | c.Assert(g, NotNil) |
562 | 64 | c.Assert(g, NotNil) | 75 | } |
563 | 65 | c.Assert(g.Name(), Equals, bootloaderNameGrub) | 76 | |
564 | 66 | } | 77 | func (s *BootloaderTestSuite) TestGetBootVer(c *C) { |
516 | 67 | |||
517 | 68 | type singleCommand []string | ||
518 | 69 | |||
519 | 70 | var allCommands = []singleCommand{} | ||
520 | 71 | |||
521 | 72 | func mockRunCommandWithCapture(args ...string) (err error) { | ||
522 | 73 | allCommands = append(allCommands, args) | ||
523 | 74 | return nil | ||
524 | 75 | } | ||
525 | 76 | |||
526 | 77 | func (s *PartitionTestSuite) TestToggleRootFS(c *C) { | ||
527 | 78 | s.makeFakeGrubEnv(c) | ||
528 | 79 | allCommands = []singleCommand{} | ||
529 | 80 | |||
530 | 81 | partition := New() | ||
531 | 82 | g := newGrub(partition) | ||
532 | 83 | c.Assert(g, NotNil) | ||
533 | 84 | err := g.ToggleRootFS("b") | ||
534 | 85 | c.Assert(err, IsNil) | ||
535 | 86 | |||
536 | 87 | // this is always called | ||
537 | 88 | mp := singleCommand{"/bin/mountpoint", mountTarget} | ||
538 | 89 | c.Assert(allCommands[0], DeepEquals, mp) | ||
539 | 90 | |||
540 | 91 | expectedGrubSet := singleCommand{bootloaderGrubEnvCmd, bootloaderGrubEnvFile, "set", "snappy_mode=try"} | ||
541 | 92 | c.Assert(allCommands[1], DeepEquals, expectedGrubSet) | ||
542 | 93 | |||
543 | 94 | // the https://developer.ubuntu.com/en/snappy/porting guide says | ||
544 | 95 | // we always use the short names | ||
545 | 96 | expectedGrubSet = singleCommand{bootloaderGrubEnvCmd, bootloaderGrubEnvFile, "set", "snappy_ab=b"} | ||
546 | 97 | c.Assert(allCommands[2], DeepEquals, expectedGrubSet) | ||
547 | 98 | |||
548 | 99 | c.Assert(len(allCommands), Equals, 3) | ||
549 | 100 | } | ||
550 | 101 | |||
551 | 102 | func mockGrubEditenvList(cmd ...string) (string, error) { | ||
552 | 103 | mockGrubEditenvOutput := fmt.Sprintf("%s=regular", bootloaderBootmodeVar) | ||
553 | 104 | return mockGrubEditenvOutput, nil | ||
554 | 105 | } | ||
555 | 106 | |||
556 | 107 | func (s *PartitionTestSuite) TestGetBootVer(c *C) { | ||
565 | 108 | s.makeFakeGrubEnv(c) | 78 | s.makeFakeGrubEnv(c) |
566 | 109 | runCommandWithStdout = mockGrubEditenvList | 79 | runCommandWithStdout = mockGrubEditenvList |
567 | 110 | 80 | ||
570 | 111 | partition := New() | 81 | g := newGrub() |
569 | 112 | g := newGrub(partition) | ||
571 | 113 | 82 | ||
572 | 114 | v, err := g.GetBootVar(bootloaderBootmodeVar) | 83 | v, err := g.GetBootVar(bootloaderBootmodeVar) |
573 | 115 | c.Assert(err, IsNil) | 84 | c.Assert(err, IsNil) |
574 | 116 | c.Assert(v, Equals, "regular") | 85 | c.Assert(v, Equals, "regular") |
575 | 117 | } | 86 | } |
576 | 118 | 87 | ||
640 | 119 | func (s *PartitionTestSuite) TestGetBootloaderWithGrub(c *C) { | 88 | func (s *BootloaderTestSuite) TestSetBootVer(c *C) { |
641 | 120 | s.makeFakeGrubEnv(c) | 89 | s.makeFakeGrubEnv(c) |
642 | 121 | p := New() | 90 | runCommandWithStdout = mockGrubEditenvList |
643 | 122 | bootloader, err := bootloader(p) | 91 | |
644 | 123 | c.Assert(err, IsNil) | 92 | g := newGrub() |
645 | 124 | c.Assert(bootloader.Name(), Equals, bootloaderNameGrub) | 93 | |
646 | 125 | } | 94 | err := g.SetBootVar("snappy_os", "123") |
647 | 126 | 95 | c.Assert(err, IsNil) | |
648 | 127 | func (s *PartitionTestSuite) TestGrubMarkCurrentBootSuccessful(c *C) { | 96 | |
649 | 128 | s.makeFakeGrubEnv(c) | 97 | expectedGrubSet := singleCommand{bootloaderGrubEnvCmd, bootloaderGrubEnvFile, "set", "snappy_os=123"} |
650 | 129 | allCommands = []singleCommand{} | 98 | c.Assert(allCommands[0], DeepEquals, expectedGrubSet) |
651 | 130 | 99 | c.Assert(allCommands, HasLen, 1) | |
652 | 131 | partition := New() | 100 | } |
653 | 132 | g := newGrub(partition) | 101 | |
654 | 133 | c.Assert(g, NotNil) | 102 | func (s *BootloaderTestSuite) TestGrubBootdir(c *C) { |
655 | 134 | err := g.MarkCurrentBootSuccessful("a") | 103 | s.makeFakeGrubEnv(c) |
656 | 135 | c.Assert(err, IsNil) | 104 | |
657 | 136 | 105 | g := newGrub() | |
658 | 137 | // this is always called | 106 | c.Assert(strings.HasSuffix(g.BootDir(), "/boot/grub"), Equals, true) |
596 | 138 | mp := singleCommand{"/bin/mountpoint", mountTarget} | ||
597 | 139 | c.Assert(allCommands[0], DeepEquals, mp) | ||
598 | 140 | |||
599 | 141 | expectedGrubSet := singleCommand{bootloaderGrubEnvCmd, bootloaderGrubEnvFile, "set", "snappy_trial_boot=0"} | ||
600 | 142 | |||
601 | 143 | c.Assert(allCommands[1], DeepEquals, expectedGrubSet) | ||
602 | 144 | |||
603 | 145 | expectedGrubSet2 := singleCommand{bootloaderGrubEnvCmd, bootloaderGrubEnvFile, "set", "snappy_ab=a"} | ||
604 | 146 | |||
605 | 147 | c.Assert(allCommands[2], DeepEquals, expectedGrubSet2) | ||
606 | 148 | |||
607 | 149 | expectedGrubSet3 := singleCommand{bootloaderGrubEnvCmd, bootloaderGrubEnvFile, "set", "snappy_mode=regular"} | ||
608 | 150 | |||
609 | 151 | c.Assert(allCommands[3], DeepEquals, expectedGrubSet3) | ||
610 | 152 | |||
611 | 153 | } | ||
612 | 154 | |||
613 | 155 | func (s *PartitionTestSuite) TestSyncBootFilesWithAssets(c *C) { | ||
614 | 156 | err := os.MkdirAll(bootloaderGrubDir, 0755) | ||
615 | 157 | c.Assert(err, IsNil) | ||
616 | 158 | |||
617 | 159 | runCommand = mockRunCommand | ||
618 | 160 | b := grub{ | ||
619 | 161 | bootloaderType{ | ||
620 | 162 | currentBootPath: c.MkDir(), | ||
621 | 163 | otherBootPath: c.MkDir(), | ||
622 | 164 | bootloaderDir: c.MkDir(), | ||
623 | 165 | }, | ||
624 | 166 | } | ||
625 | 167 | |||
626 | 168 | bootfile := filepath.Join(c.MkDir(), "bootfile") | ||
627 | 169 | err = ioutil.WriteFile(bootfile, []byte(bootfile), 0644) | ||
628 | 170 | c.Assert(err, IsNil) | ||
629 | 171 | |||
630 | 172 | bootassets := map[string]string{ | ||
631 | 173 | bootfile: filepath.Base(bootfile), | ||
632 | 174 | } | ||
633 | 175 | |||
634 | 176 | err = b.SyncBootFiles(bootassets) | ||
635 | 177 | c.Assert(err, IsNil) | ||
636 | 178 | |||
637 | 179 | dst := filepath.Join(b.bootloaderDir, bootassets[bootfile]) | ||
638 | 180 | c.Check(helpers.FileExists(dst), Equals, true) | ||
639 | 181 | c.Check(helpers.FilesAreEqual(bootfile, dst), Equals, true) | ||
659 | 182 | } | 107 | } |
660 | 183 | 108 | ||
661 | === renamed file 'partition/partition_test.go' => 'bootloader/bootloader_test.go' | |||
662 | --- partition/partition_test.go 2015-07-16 11:07:30 +0000 | |||
663 | +++ bootloader/bootloader_test.go 2015-09-03 08:14:56 +0000 | |||
664 | @@ -17,14 +17,11 @@ | |||
665 | 17 | * | 17 | * |
666 | 18 | */ | 18 | */ |
667 | 19 | 19 | ||
669 | 20 | package partition | 20 | package bootloader |
670 | 21 | 21 | ||
671 | 22 | import ( | 22 | import ( |
672 | 23 | "errors" | ||
673 | 24 | "io/ioutil" | ||
674 | 25 | "os" | 23 | "os" |
675 | 26 | "path/filepath" | 24 | "path/filepath" |
676 | 27 | "strings" | ||
677 | 28 | "testing" | 25 | "testing" |
678 | 29 | 26 | ||
679 | 30 | . "gopkg.in/check.v1" | 27 | . "gopkg.in/check.v1" |
680 | @@ -34,22 +31,14 @@ | |||
681 | 34 | func Test(t *testing.T) { TestingT(t) } | 31 | func Test(t *testing.T) { TestingT(t) } |
682 | 35 | 32 | ||
683 | 36 | // partition specific testsuite | 33 | // partition specific testsuite |
685 | 37 | type PartitionTestSuite struct { | 34 | type BootloaderTestSuite struct { |
686 | 38 | tempdir string | 35 | tempdir string |
687 | 39 | } | 36 | } |
688 | 40 | 37 | ||
696 | 41 | var _ = Suite(&PartitionTestSuite{}) | 38 | var _ = Suite(&BootloaderTestSuite{}) |
697 | 42 | 39 | ||
698 | 43 | func mockRunCommand(args ...string) (err error) { | 40 | func (s *BootloaderTestSuite) SetUpTest(c *C) { |
692 | 44 | return err | ||
693 | 45 | } | ||
694 | 46 | |||
695 | 47 | func (s *PartitionTestSuite) SetUpTest(c *C) { | ||
699 | 48 | s.tempdir = c.MkDir() | 41 | s.tempdir = c.MkDir() |
700 | 49 | runLsblk = mockRunLsblkDualSnappy | ||
701 | 50 | |||
702 | 51 | // custom mount target | ||
703 | 52 | mountTarget = c.MkDir() | ||
704 | 53 | 42 | ||
705 | 54 | // setup fake paths for grub | 43 | // setup fake paths for grub |
706 | 55 | bootloaderGrubDir = filepath.Join(s.tempdir, "boot", "grub") | 44 | bootloaderGrubDir = filepath.Join(s.tempdir, "boot", "grub") |
707 | @@ -58,23 +47,15 @@ | |||
708 | 58 | 47 | ||
709 | 59 | // and uboot | 48 | // and uboot |
710 | 60 | bootloaderUbootDir = filepath.Join(s.tempdir, "boot", "uboot") | 49 | bootloaderUbootDir = filepath.Join(s.tempdir, "boot", "uboot") |
711 | 61 | bootloaderUbootConfigFile = filepath.Join(bootloaderUbootDir, "uEnv.txt") | ||
712 | 62 | bootloaderUbootEnvFile = filepath.Join(bootloaderUbootDir, "uEnv.txt") | ||
713 | 63 | bootloaderUbootFwEnvFile = filepath.Join(bootloaderUbootDir, "uboot.env") | 50 | bootloaderUbootFwEnvFile = filepath.Join(bootloaderUbootDir, "uboot.env") |
714 | 64 | bootloaderUbootStampFile = filepath.Join(bootloaderUbootDir, "snappy-stamp.txt") | ||
715 | 65 | |||
716 | 66 | c.Assert(mounts, DeepEquals, mountEntryArray(nil)) | ||
717 | 67 | } | 51 | } |
718 | 68 | 52 | ||
720 | 69 | func (s *PartitionTestSuite) TearDownTest(c *C) { | 53 | func (s *BootloaderTestSuite) TearDownTest(c *C) { |
721 | 70 | os.RemoveAll(s.tempdir) | 54 | os.RemoveAll(s.tempdir) |
722 | 71 | 55 | ||
723 | 72 | // always restore what we might have mocked away | 56 | // always restore what we might have mocked away |
724 | 73 | runCommand = runCommandImpl | 57 | runCommand = runCommandImpl |
729 | 74 | bootloader = bootloaderImpl | 58 | new = bootloaderImpl |
726 | 75 | cacheDir = cacheDirReal | ||
727 | 76 | hardwareSpecFile = hardwareSpecFileReal | ||
728 | 77 | mountTarget = mountTargetReal | ||
730 | 78 | 59 | ||
731 | 79 | // grub vars | 60 | // grub vars |
732 | 80 | bootloaderGrubConfigFile = bootloaderGrubConfigFileReal | 61 | bootloaderGrubConfigFile = bootloaderGrubConfigFileReal |
733 | @@ -82,369 +63,101 @@ | |||
734 | 82 | 63 | ||
735 | 83 | // uboot vars | 64 | // uboot vars |
736 | 84 | bootloaderUbootDir = bootloaderUbootDirReal | 65 | bootloaderUbootDir = bootloaderUbootDirReal |
737 | 85 | bootloaderUbootConfigFile = bootloaderUbootConfigFileReal | ||
738 | 86 | bootloaderUbootEnvFile = bootloaderUbootEnvFileReal | ||
739 | 87 | bootloaderUbootStampFile = bootloaderUbootStampFileReal | ||
740 | 88 | |||
741 | 89 | c.Assert(mounts, DeepEquals, mountEntryArray(nil)) | ||
742 | 90 | } | ||
743 | 91 | |||
744 | 92 | func makeHardwareYaml(c *C, hardwareYaml string) (outPath string) { | ||
745 | 93 | tmp, err := ioutil.TempFile(c.MkDir(), "hw-") | ||
746 | 94 | c.Assert(err, IsNil) | ||
747 | 95 | defer tmp.Close() | ||
748 | 96 | |||
749 | 97 | if hardwareYaml == "" { | ||
750 | 98 | hardwareYaml = ` | ||
751 | 99 | kernel: assets/vmlinuz | ||
752 | 100 | initrd: assets/initrd.img | ||
753 | 101 | dtbs: assets/dtbs | ||
754 | 102 | partition-layout: system-AB | ||
755 | 103 | bootloader: u-boot | ||
756 | 104 | ` | ||
757 | 105 | } | ||
758 | 106 | _, err = tmp.Write([]byte(hardwareYaml)) | ||
759 | 107 | c.Assert(err, IsNil) | ||
760 | 108 | |||
761 | 109 | return tmp.Name() | ||
762 | 110 | } | ||
763 | 111 | |||
764 | 112 | func mockRunLsblkDualSnappy() (output []string, err error) { | ||
765 | 113 | dualData := ` | ||
766 | 114 | NAME="sda" LABEL="" PKNAME="" MOUNTPOINT="" | ||
767 | 115 | NAME="sda1" LABEL="" PKNAME="sda" MOUNTPOINT="" | ||
768 | 116 | NAME="sda2" LABEL="system-boot" PKNAME="sda" MOUNTPOINT="/boot/efi" | ||
769 | 117 | NAME="sda3" LABEL="system-a" PKNAME="sda" MOUNTPOINT="/" | ||
770 | 118 | NAME="sda4" LABEL="system-b" PKNAME="sda" MOUNTPOINT="" | ||
771 | 119 | NAME="sda5" LABEL="writable" PKNAME="sda" MOUNTPOINT="/writable" | ||
772 | 120 | NAME="sr0" LABEL="" PKNAME="" MOUNTPOINT="" | ||
773 | 121 | ` | ||
774 | 122 | return strings.Split(dualData, "\n"), err | ||
775 | 123 | } | ||
776 | 124 | |||
777 | 125 | func (s *PartitionTestSuite) TestSnappyDualRoot(c *C) { | ||
778 | 126 | p := New() | ||
779 | 127 | c.Assert(p.dualRootPartitions(), Equals, true) | ||
780 | 128 | c.Assert(p.singleRootPartition(), Equals, false) | ||
781 | 129 | |||
782 | 130 | rootPartitions := p.rootPartitions() | ||
783 | 131 | c.Assert(rootPartitions[0].name, Equals, "system-a") | ||
784 | 132 | c.Assert(rootPartitions[0].device, Equals, "/dev/sda3") | ||
785 | 133 | c.Assert(rootPartitions[0].parentName, Equals, "/dev/sda") | ||
786 | 134 | c.Assert(rootPartitions[1].name, Equals, "system-b") | ||
787 | 135 | c.Assert(rootPartitions[1].device, Equals, "/dev/sda4") | ||
788 | 136 | c.Assert(rootPartitions[1].parentName, Equals, "/dev/sda") | ||
789 | 137 | |||
790 | 138 | wp := p.writablePartition() | ||
791 | 139 | c.Assert(wp.name, Equals, "writable") | ||
792 | 140 | c.Assert(wp.device, Equals, "/dev/sda5") | ||
793 | 141 | c.Assert(wp.parentName, Equals, "/dev/sda") | ||
794 | 142 | |||
795 | 143 | boot := p.bootPartition() | ||
796 | 144 | c.Assert(boot.name, Equals, "system-boot") | ||
797 | 145 | c.Assert(boot.device, Equals, "/dev/sda2") | ||
798 | 146 | c.Assert(boot.parentName, Equals, "/dev/sda") | ||
799 | 147 | |||
800 | 148 | root := p.rootPartition() | ||
801 | 149 | c.Assert(root.name, Equals, "system-a") | ||
802 | 150 | c.Assert(root.device, Equals, "/dev/sda3") | ||
803 | 151 | c.Assert(root.parentName, Equals, "/dev/sda") | ||
804 | 152 | |||
805 | 153 | other := p.otherRootPartition() | ||
806 | 154 | c.Assert(other.name, Equals, "system-b") | ||
807 | 155 | c.Assert(other.device, Equals, "/dev/sda4") | ||
808 | 156 | c.Assert(other.parentName, Equals, "/dev/sda") | ||
809 | 157 | } | ||
810 | 158 | |||
811 | 159 | func (s *PartitionTestSuite) TestRunWithOtherDualParitionRO(c *C) { | ||
812 | 160 | p := New() | ||
813 | 161 | reportedRoot := "" | ||
814 | 162 | err := p.RunWithOther(RO, func(otherRoot string) (err error) { | ||
815 | 163 | reportedRoot = otherRoot | ||
816 | 164 | return nil | ||
817 | 165 | }) | ||
818 | 166 | c.Assert(err, IsNil) | ||
819 | 167 | c.Assert(reportedRoot, Equals, mountTarget) | ||
820 | 168 | } | ||
821 | 169 | |||
822 | 170 | func (s *PartitionTestSuite) TestRunWithOtherDualParitionRWFuncErr(c *C) { | ||
823 | 171 | c.Assert(mounts, DeepEquals, mountEntryArray(nil)) | ||
824 | 172 | |||
825 | 173 | runCommand = mockRunCommand | ||
826 | 174 | |||
827 | 175 | p := New() | ||
828 | 176 | err := p.RunWithOther(RW, func(otherRoot string) (err error) { | ||
829 | 177 | return errors.New("canary") | ||
830 | 178 | }) | ||
831 | 179 | |||
832 | 180 | // ensure we actually got the right error | ||
833 | 181 | c.Assert(err, NotNil) | ||
834 | 182 | c.Assert(err.Error(), Equals, "canary") | ||
835 | 183 | |||
836 | 184 | // ensure cleanup happend | ||
837 | 185 | |||
838 | 186 | // FIXME: mounts are global | ||
839 | 187 | expected := mountEntry{ | ||
840 | 188 | source: "/dev/sda4", | ||
841 | 189 | target: mountTarget, | ||
842 | 190 | options: "", | ||
843 | 191 | bindMount: false, | ||
844 | 192 | } | ||
845 | 193 | |||
846 | 194 | // At program exit, "other" should still be mounted | ||
847 | 195 | c.Assert(mounts, DeepEquals, mountEntryArray{expected}) | ||
848 | 196 | |||
849 | 197 | undoMounts(false) | ||
850 | 198 | |||
851 | 199 | c.Assert(mounts, DeepEquals, mountEntryArray(nil)) | ||
852 | 200 | } | ||
853 | 201 | |||
854 | 202 | func (s *PartitionTestSuite) TestRunWithOtherSingleParitionRO(c *C) { | ||
855 | 203 | runLsblk = mockRunLsblkSingleRootSnappy | ||
856 | 204 | p := New() | ||
857 | 205 | err := p.RunWithOther(RO, func(otherRoot string) (err error) { | ||
858 | 206 | return nil | ||
859 | 207 | }) | ||
860 | 208 | c.Assert(err, Equals, ErrNoDualPartition) | ||
861 | 209 | } | ||
862 | 210 | |||
863 | 211 | func mockRunLsblkSingleRootSnappy() (output []string, err error) { | ||
864 | 212 | dualData := ` | ||
865 | 213 | NAME="sda" LABEL="" PKNAME="" MOUNTPOINT="" | ||
866 | 214 | NAME="sda1" LABEL="" PKNAME="sda" MOUNTPOINT="" | ||
867 | 215 | NAME="sda2" LABEL="system-boot" PKNAME="sda" MOUNTPOINT="" | ||
868 | 216 | NAME="sda3" LABEL="system-a" PKNAME="sda" MOUNTPOINT="/" | ||
869 | 217 | NAME="sda5" LABEL="writable" PKNAME="sda" MOUNTPOINT="/writable" | ||
870 | 218 | ` | ||
871 | 219 | return strings.Split(dualData, "\n"), err | ||
872 | 220 | } | ||
873 | 221 | func (s *PartitionTestSuite) TestSnappySingleRoot(c *C) { | ||
874 | 222 | runLsblk = mockRunLsblkSingleRootSnappy | ||
875 | 223 | |||
876 | 224 | p := New() | ||
877 | 225 | c.Assert(p.dualRootPartitions(), Equals, false) | ||
878 | 226 | c.Assert(p.singleRootPartition(), Equals, true) | ||
879 | 227 | |||
880 | 228 | root := p.rootPartition() | ||
881 | 229 | c.Assert(root.name, Equals, "system-a") | ||
882 | 230 | c.Assert(root.device, Equals, "/dev/sda3") | ||
883 | 231 | c.Assert(root.parentName, Equals, "/dev/sda") | ||
884 | 232 | |||
885 | 233 | other := p.otherRootPartition() | ||
886 | 234 | c.Assert(other, IsNil) | ||
887 | 235 | |||
888 | 236 | rootPartitions := p.rootPartitions() | ||
889 | 237 | c.Assert(&rootPartitions[0], DeepEquals, root) | ||
890 | 238 | } | ||
891 | 239 | |||
892 | 240 | func (s *PartitionTestSuite) TestMountUnmountTracking(c *C) { | ||
893 | 241 | runCommand = mockRunCommand | ||
894 | 242 | |||
895 | 243 | p := New() | ||
896 | 244 | c.Assert(p, NotNil) | ||
897 | 245 | |||
898 | 246 | p.mountOtherRootfs(false) | ||
899 | 247 | expected := mountEntry{ | ||
900 | 248 | source: "/dev/sda4", | ||
901 | 249 | target: mountTarget, | ||
902 | 250 | options: "", | ||
903 | 251 | bindMount: false, | ||
904 | 252 | } | ||
905 | 253 | |||
906 | 254 | c.Assert(mounts, DeepEquals, mountEntryArray{expected}) | ||
907 | 255 | |||
908 | 256 | p.unmountOtherRootfs() | ||
909 | 257 | c.Assert(mounts, DeepEquals, mountEntryArray(nil)) | ||
910 | 258 | } | ||
911 | 259 | |||
912 | 260 | func (s *PartitionTestSuite) TestUnmountRequiredFilesystems(c *C) { | ||
913 | 261 | runCommand = mockRunCommand | ||
914 | 262 | s.makeFakeGrubEnv(c) | ||
915 | 263 | |||
916 | 264 | p := New() | ||
917 | 265 | c.Assert(c, NotNil) | ||
918 | 266 | |||
919 | 267 | p.bindmountRequiredFilesystems() | ||
920 | 268 | c.Assert(mounts, DeepEquals, mountEntryArray{ | ||
921 | 269 | mountEntry{source: "/dev", target: mountTarget + "/dev", | ||
922 | 270 | options: "bind", bindMount: true}, | ||
923 | 271 | |||
924 | 272 | mountEntry{source: "/proc", target: mountTarget + "/proc", | ||
925 | 273 | options: "bind", bindMount: true}, | ||
926 | 274 | |||
927 | 275 | mountEntry{source: "/sys", target: mountTarget + "/sys", | ||
928 | 276 | options: "bind", bindMount: true}, | ||
929 | 277 | mountEntry{source: "/boot/efi", target: mountTarget + "/boot/efi", | ||
930 | 278 | options: "bind", bindMount: true}, | ||
931 | 279 | |||
932 | 280 | // Required to allow grub inside the chroot to access | ||
933 | 281 | // the "current" rootfs outside the chroot (used | ||
934 | 282 | // to generate the grub menuitems). | ||
935 | 283 | mountEntry{source: "/", | ||
936 | 284 | target: mountTarget + mountTarget, | ||
937 | 285 | options: "bind,ro", bindMount: true}, | ||
938 | 286 | }) | ||
939 | 287 | p.unmountRequiredFilesystems() | ||
940 | 288 | c.Assert(mounts, DeepEquals, mountEntryArray(nil)) | ||
941 | 289 | } | ||
942 | 290 | |||
943 | 291 | func (s *PartitionTestSuite) TestUndoMounts(c *C) { | ||
944 | 292 | runCommand = mockRunCommand | ||
945 | 293 | |||
946 | 294 | p := New() | ||
947 | 295 | c.Assert(c, NotNil) | ||
948 | 296 | |||
949 | 297 | err := p.remountOther(RW) | ||
950 | 298 | c.Assert(err, IsNil) | ||
951 | 299 | |||
952 | 300 | p.bindmountRequiredFilesystems() | ||
953 | 301 | c.Assert(mounts, DeepEquals, mountEntryArray{ | ||
954 | 302 | |||
955 | 303 | mountEntry{source: "/dev/sda4", target: mountTarget, | ||
956 | 304 | options: "", bindMount: false}, | ||
957 | 305 | |||
958 | 306 | mountEntry{source: "/dev", target: mountTarget + "/dev", | ||
959 | 307 | options: "bind", bindMount: true}, | ||
960 | 308 | |||
961 | 309 | mountEntry{source: "/proc", target: mountTarget + "/proc", | ||
962 | 310 | options: "bind", bindMount: true}, | ||
963 | 311 | |||
964 | 312 | mountEntry{source: "/sys", target: mountTarget + "/sys", | ||
965 | 313 | options: "bind", bindMount: true}, | ||
966 | 314 | |||
967 | 315 | mountEntry{source: "/boot/efi", target: mountTarget + "/boot/efi", | ||
968 | 316 | options: "bind", bindMount: true}, | ||
969 | 317 | |||
970 | 318 | mountEntry{source: "/", | ||
971 | 319 | target: mountTarget + mountTarget, | ||
972 | 320 | options: "bind,ro", bindMount: true}, | ||
973 | 321 | }) | ||
974 | 322 | |||
975 | 323 | // should leave non-bind mounts | ||
976 | 324 | undoMounts(true) | ||
977 | 325 | |||
978 | 326 | c.Assert(mounts, DeepEquals, mountEntryArray{ | ||
979 | 327 | mountEntry{ | ||
980 | 328 | source: "/dev/sda4", | ||
981 | 329 | target: mountTarget, | ||
982 | 330 | options: "", | ||
983 | 331 | bindMount: false, | ||
984 | 332 | }, | ||
985 | 333 | }) | ||
986 | 334 | |||
987 | 335 | // should unmount everything | ||
988 | 336 | undoMounts(false) | ||
989 | 337 | |||
990 | 338 | c.Assert(mounts, DeepEquals, mountEntryArray(nil)) | ||
991 | 339 | } | ||
992 | 340 | |||
993 | 341 | func mockRunLsblkNoSnappy() (output []string, err error) { | ||
994 | 342 | dualData := ` | ||
995 | 343 | NAME="sda" LABEL="" PKNAME="" MOUNTPOINT="" | ||
996 | 344 | NAME="sda1" LABEL="meep" PKNAME="sda" MOUNTPOINT="/" | ||
997 | 345 | NAME="sr0" LABEL="" PKNAME="" MOUNTPOINT="" | ||
998 | 346 | ` | ||
999 | 347 | return strings.Split(dualData, "\n"), err | ||
1000 | 348 | } | ||
1001 | 349 | |||
1002 | 350 | func (s *PartitionTestSuite) TestSnappyNoSnappyPartitions(c *C) { | ||
1003 | 351 | runLsblk = mockRunLsblkNoSnappy | ||
1004 | 352 | |||
1005 | 353 | p := New() | ||
1006 | 354 | err := p.getPartitionDetails() | ||
1007 | 355 | c.Assert(err, Equals, ErrPartitionDetection) | ||
1008 | 356 | |||
1009 | 357 | c.Assert(p.dualRootPartitions(), Equals, false) | ||
1010 | 358 | c.Assert(p.singleRootPartition(), Equals, false) | ||
1011 | 359 | |||
1012 | 360 | c.Assert(p.rootPartition(), IsNil) | ||
1013 | 361 | c.Assert(p.bootPartition(), IsNil) | ||
1014 | 362 | c.Assert(p.writablePartition(), IsNil) | ||
1015 | 363 | c.Assert(p.otherRootPartition(), IsNil) | ||
1016 | 364 | } | 66 | } |
1017 | 365 | 67 | ||
1018 | 366 | // mock bootloader for the tests | 68 | // mock bootloader for the tests |
1019 | 367 | type mockBootloader struct { | 69 | type mockBootloader struct { |
1024 | 368 | ToggleRootFSCalled bool | 70 | bootvars map[string]string |
1021 | 369 | HandleAssetsCalled bool | ||
1022 | 370 | MarkCurrentBootSuccessfulCalled bool | ||
1023 | 371 | SyncBootFilesCalled bool | ||
1025 | 372 | } | 71 | } |
1026 | 373 | 72 | ||
1027 | 374 | func (b *mockBootloader) Name() bootloaderName { | ||
1028 | 375 | return "mocky" | ||
1029 | 376 | } | ||
1030 | 377 | func (b *mockBootloader) ToggleRootFS(otherRootfs string) error { | ||
1031 | 378 | b.ToggleRootFSCalled = true | ||
1032 | 379 | return nil | ||
1033 | 380 | } | ||
1034 | 381 | func (b *mockBootloader) SyncBootFiles(bootAssets map[string]string) error { | ||
1035 | 382 | b.SyncBootFilesCalled = true | ||
1036 | 383 | return nil | ||
1037 | 384 | } | ||
1038 | 385 | func (b *mockBootloader) HandleAssets() error { | ||
1039 | 386 | b.HandleAssetsCalled = true | ||
1040 | 387 | return nil | ||
1041 | 388 | } | ||
1042 | 389 | func (b *mockBootloader) GetBootVar(name string) (string, error) { | 73 | func (b *mockBootloader) GetBootVar(name string) (string, error) { |
1050 | 390 | return "", nil | 74 | return b.bootvars[name], nil |
1051 | 391 | } | 75 | } |
1052 | 392 | func (b *mockBootloader) GetNextBootRootFSName() (string, error) { | 76 | func (b *mockBootloader) SetBootVar(key, value string) error { |
1053 | 393 | return "", nil | 77 | b.bootvars[key] = value |
1047 | 394 | } | ||
1048 | 395 | func (b *mockBootloader) MarkCurrentBootSuccessful(currentRootfs string) error { | ||
1049 | 396 | b.MarkCurrentBootSuccessfulCalled = true | ||
1054 | 397 | return nil | 78 | return nil |
1055 | 398 | } | 79 | } |
1056 | 399 | func (b *mockBootloader) BootDir() string { | 80 | func (b *mockBootloader) BootDir() string { |
1107 | 400 | return "" | 81 | return "/boot/mock" |
1108 | 401 | } | 82 | } |
1109 | 402 | 83 | ||
1110 | 403 | func (s *PartitionTestSuite) TestToggleBootloaderRootfs(c *C) { | 84 | func mockRunCommand(args ...string) (err error) { |
1111 | 404 | runCommand = mockRunCommand | 85 | return err |
1112 | 405 | b := &mockBootloader{} | 86 | } |
1113 | 406 | bootloader = func(p *Partition) (bootLoader, error) { | 87 | |
1114 | 407 | return b, nil | 88 | func (s *BootloaderTestSuite) TestMarkBootSuccessful(c *C) { |
1115 | 408 | } | 89 | runCommand = mockRunCommand |
1116 | 409 | 90 | b := &mockBootloader{ | |
1117 | 410 | p := New() | 91 | bootvars: map[string]string{ |
1118 | 411 | c.Assert(c, NotNil) | 92 | "snappy_os": "os1", |
1119 | 412 | 93 | "snappy_kernel": "k1", | |
1120 | 413 | err := p.toggleBootloaderRootfs() | 94 | }, |
1121 | 414 | c.Assert(err, IsNil) | 95 | } |
1122 | 415 | c.Assert(b.ToggleRootFSCalled, Equals, true) | 96 | new = func() (bootLoader, error) { |
1123 | 416 | c.Assert(b.HandleAssetsCalled, Equals, true) | 97 | return b, nil |
1124 | 417 | 98 | } | |
1125 | 418 | p.unmountOtherRootfs() | 99 | |
1126 | 419 | c.Assert(mounts, DeepEquals, mountEntryArray(nil)) | 100 | err := MarkBootSuccessful() |
1127 | 420 | } | 101 | c.Assert(err, IsNil) |
1128 | 421 | 102 | c.Assert(b.bootvars["snappy_good_os"], Equals, "os1") | |
1129 | 422 | func (s *PartitionTestSuite) TestMarkBootSuccessful(c *C) { | 103 | c.Assert(b.bootvars["snappy_good_kernel"], Equals, "k1") |
1130 | 423 | runCommand = mockRunCommand | 104 | c.Assert(b.bootvars["snappy_mode"], Equals, "regular") |
1131 | 424 | b := &mockBootloader{} | 105 | c.Assert(b.bootvars["snappy_trial_boot"], Equals, "0") |
1132 | 425 | bootloader = func(p *Partition) (bootLoader, error) { | 106 | } |
1133 | 426 | return b, nil | 107 | |
1134 | 427 | } | 108 | func (s *BootloaderTestSuite) TestSetBootVar(c *C) { |
1135 | 428 | 109 | runCommand = mockRunCommand | |
1136 | 429 | p := New() | 110 | b := &mockBootloader{ |
1137 | 430 | c.Assert(c, NotNil) | 111 | bootvars: map[string]string{}, |
1138 | 431 | 112 | } | |
1139 | 432 | err := p.MarkBootSuccessful() | 113 | new = func() (bootLoader, error) { |
1140 | 433 | c.Assert(err, IsNil) | 114 | return b, nil |
1141 | 434 | c.Assert(b.MarkCurrentBootSuccessfulCalled, Equals, true) | 115 | } |
1142 | 435 | } | 116 | |
1143 | 436 | 117 | err := SetBootVar("foo", "bar") | |
1144 | 437 | func (s *PartitionTestSuite) TestSyncBootFiles(c *C) { | 118 | c.Assert(err, IsNil) |
1145 | 438 | runCommand = mockRunCommand | 119 | c.Assert(b.bootvars["foo"], Equals, "bar") |
1146 | 439 | b := &mockBootloader{} | 120 | } |
1147 | 440 | bootloader = func(p *Partition) (bootLoader, error) { | 121 | |
1148 | 441 | return b, nil | 122 | func (s *BootloaderTestSuite) TestGetBootVar(c *C) { |
1149 | 442 | } | 123 | runCommand = mockRunCommand |
1150 | 443 | 124 | b := &mockBootloader{ | |
1151 | 444 | p := New() | 125 | bootvars: map[string]string{"baz": "foobar"}, |
1152 | 445 | c.Assert(c, NotNil) | 126 | } |
1153 | 446 | 127 | new = func() (bootLoader, error) { | |
1154 | 447 | err := p.SyncBootloaderFiles(nil) | 128 | return b, nil |
1155 | 448 | c.Assert(err, IsNil) | 129 | } |
1156 | 449 | c.Assert(b.SyncBootFilesCalled, Equals, true) | 130 | |
1157 | 131 | v, err := GetBootVar("baz") | ||
1158 | 132 | c.Assert(err, IsNil) | ||
1159 | 133 | c.Assert(v, Equals, "foobar") | ||
1160 | 134 | } | ||
1161 | 135 | |||
1162 | 136 | func (s *BootloaderTestSuite) TestGetBootDir(c *C) { | ||
1163 | 137 | runCommand = mockRunCommand | ||
1164 | 138 | b := &mockBootloader{ | ||
1165 | 139 | bootvars: map[string]string{"baz": "foobar"}, | ||
1166 | 140 | } | ||
1167 | 141 | new = func() (bootLoader, error) { | ||
1168 | 142 | return b, nil | ||
1169 | 143 | } | ||
1170 | 144 | |||
1171 | 145 | d := Dir() | ||
1172 | 146 | c.Assert(d, Equals, "/boot/mock") | ||
1173 | 147 | } | ||
1174 | 148 | |||
1175 | 149 | func (s *BootloaderTestSuite) TestBootloaderIsGrub(c *C) { | ||
1176 | 150 | s.makeFakeGrubEnv(c) | ||
1177 | 151 | |||
1178 | 152 | g, err := new() | ||
1179 | 153 | c.Assert(err, IsNil) | ||
1180 | 154 | c.Assert(g, FitsTypeOf, &grub{}) | ||
1181 | 155 | } | ||
1182 | 156 | |||
1183 | 157 | func (s *BootloaderTestSuite) TestBootloaderIsUboot(c *C) { | ||
1184 | 158 | s.makeFakeUbootEnv(c) | ||
1185 | 159 | |||
1186 | 160 | u, err := new() | ||
1187 | 161 | c.Assert(err, IsNil) | ||
1188 | 162 | c.Assert(u, FitsTypeOf, &uboot{}) | ||
1189 | 450 | } | 163 | } |
1190 | 451 | 164 | ||
1191 | === modified file 'bootloader/bootloader_uboot.go' | |||
1192 | --- partition/bootloader_uboot.go 2015-07-24 12:00:01 +0000 | |||
1193 | +++ bootloader/bootloader_uboot.go 2015-09-03 08:14:56 +0000 | |||
1194 | @@ -17,252 +17,61 @@ | |||
1195 | 17 | * | 17 | * |
1196 | 18 | */ | 18 | */ |
1197 | 19 | 19 | ||
1199 | 20 | package partition | 20 | package bootloader |
1200 | 21 | 21 | ||
1201 | 22 | import ( | 22 | import ( |
1202 | 23 | "bufio" | ||
1203 | 24 | "bytes" | ||
1204 | 25 | "fmt" | ||
1205 | 26 | "os" | ||
1206 | 27 | "strings" | ||
1207 | 28 | |||
1208 | 29 | "launchpad.net/snappy/helpers" | 23 | "launchpad.net/snappy/helpers" |
1209 | 30 | 24 | ||
1210 | 31 | "github.com/mvo5/goconfigparser" | ||
1211 | 32 | "github.com/mvo5/uboot-go/uenv" | 25 | "github.com/mvo5/uboot-go/uenv" |
1212 | 33 | ) | 26 | ) |
1213 | 34 | 27 | ||
1214 | 35 | const ( | 28 | const ( |
1228 | 36 | bootloaderUbootDirReal = "/boot/uboot" | 29 | bootloaderUbootDirReal = "/boot/uboot" |
1216 | 37 | bootloaderUbootConfigFileReal = "/boot/uboot/uEnv.txt" | ||
1217 | 38 | |||
1218 | 39 | // File created by u-boot itself when | ||
1219 | 40 | // bootloaderBootmodeTry == "try" which the | ||
1220 | 41 | // successfully booted system must remove to flag to u-boot that | ||
1221 | 42 | // this partition is "good". | ||
1222 | 43 | bootloaderUbootStampFileReal = "/boot/uboot/snappy-stamp.txt" | ||
1223 | 44 | |||
1224 | 45 | // DEPRECATED: | ||
1225 | 46 | bootloaderUbootEnvFileReal = "/boot/uboot/snappy-system.txt" | ||
1226 | 47 | |||
1227 | 48 | // the real uboot env | ||
1229 | 49 | bootloaderUbootFwEnvFileReal = "/boot/uboot/uboot.env" | 30 | bootloaderUbootFwEnvFileReal = "/boot/uboot/uboot.env" |
1230 | 50 | ) | 31 | ) |
1231 | 51 | 32 | ||
1232 | 52 | // var to make it testable | 33 | // var to make it testable |
1233 | 53 | var ( | 34 | var ( |
1241 | 54 | bootloaderUbootDir = bootloaderUbootDirReal | 35 | bootloaderUbootDir = bootloaderUbootDirReal |
1242 | 55 | bootloaderUbootConfigFile = bootloaderUbootConfigFileReal | 36 | bootloaderUbootFwEnvFile = bootloaderUbootFwEnvFileReal |
1236 | 56 | bootloaderUbootStampFile = bootloaderUbootStampFileReal | ||
1237 | 57 | bootloaderUbootEnvFile = bootloaderUbootEnvFileReal | ||
1238 | 58 | bootloaderUbootFwEnvFile = bootloaderUbootFwEnvFileReal | ||
1239 | 59 | |||
1240 | 60 | atomicWriteFile = helpers.AtomicWriteFile | ||
1243 | 61 | ) | 37 | ) |
1244 | 62 | 38 | ||
1245 | 63 | const bootloaderNameUboot bootloaderName = "u-boot" | ||
1246 | 64 | |||
1247 | 65 | type uboot struct { | 39 | type uboot struct { |
1260 | 66 | bootloaderType | 40 | } |
1249 | 67 | } | ||
1250 | 68 | |||
1251 | 69 | // Stores a Name and a Value to be added as a name=value pair in a file. | ||
1252 | 70 | // TODO convert to map | ||
1253 | 71 | type configFileChange struct { | ||
1254 | 72 | Name string | ||
1255 | 73 | Value string | ||
1256 | 74 | } | ||
1257 | 75 | |||
1258 | 76 | var setBootVar = func(name, value string) error { return nil } | ||
1259 | 77 | var getBootVar = func(name string) (string, error) { return "", nil } | ||
1261 | 78 | 41 | ||
1262 | 79 | // newUboot create a new Uboot bootloader object | 42 | // newUboot create a new Uboot bootloader object |
1324 | 80 | func newUboot(partition *Partition) bootLoader { | 43 | func newUboot() bootLoader { |
1325 | 81 | if !helpers.FileExists(bootloaderUbootConfigFile) { | 44 | if !helpers.FileExists(bootloaderUbootFwEnvFile) { |
1326 | 82 | return nil | 45 | return nil |
1327 | 83 | } | 46 | } |
1328 | 84 | 47 | ||
1329 | 85 | b := newBootLoader(partition, bootloaderUbootDir) | 48 | return &uboot{} |
1330 | 86 | if b == nil { | 49 | } |
1331 | 87 | return nil | 50 | |
1332 | 88 | } | 51 | func (u *uboot) GetBootVar(name string) (value string, err error) { |
1333 | 89 | u := uboot{bootloaderType: *b} | 52 | env, err := uenv.Open(bootloaderUbootFwEnvFile) |
1334 | 90 | 53 | if err != nil { | |
1335 | 91 | if helpers.FileExists(bootloaderUbootFwEnvFile) { | 54 | return "", err |
1336 | 92 | setBootVar = setBootVarFwEnv | 55 | } |
1337 | 93 | getBootVar = getBootVarFwEnv | 56 | |
1338 | 94 | } else { | 57 | return env.Get(name), nil |
1339 | 95 | setBootVar = setBootVarLegacy | 58 | } |
1340 | 96 | getBootVar = getBootVarLegacy | 59 | |
1341 | 97 | } | 60 | func (u *uboot) SetBootVar(key, value string) error { |
1281 | 98 | |||
1282 | 99 | return &u | ||
1283 | 100 | } | ||
1284 | 101 | |||
1285 | 102 | func (u *uboot) Name() bootloaderName { | ||
1286 | 103 | return bootloaderNameUboot | ||
1287 | 104 | } | ||
1288 | 105 | |||
1289 | 106 | func (u *uboot) ToggleRootFS(otherRootfs string) (err error) { | ||
1290 | 107 | if err := setBootVar(bootloaderRootfsVar, string(otherRootfs)); err != nil { | ||
1291 | 108 | return err | ||
1292 | 109 | } | ||
1293 | 110 | |||
1294 | 111 | return setBootVar(bootloaderBootmodeVar, bootloaderBootmodeTry) | ||
1295 | 112 | } | ||
1296 | 113 | |||
1297 | 114 | func getBootVarLegacy(name string) (value string, err error) { | ||
1298 | 115 | cfg := goconfigparser.New() | ||
1299 | 116 | cfg.AllowNoSectionHeader = true | ||
1300 | 117 | if err := cfg.ReadFile(bootloaderUbootEnvFile); err != nil { | ||
1301 | 118 | return "", nil | ||
1302 | 119 | } | ||
1303 | 120 | |||
1304 | 121 | return cfg.Get("", name) | ||
1305 | 122 | } | ||
1306 | 123 | |||
1307 | 124 | func setBootVarLegacy(name, value string) error { | ||
1308 | 125 | curVal, err := getBootVarLegacy(name) | ||
1309 | 126 | if err == nil && curVal == value { | ||
1310 | 127 | return nil | ||
1311 | 128 | } | ||
1312 | 129 | |||
1313 | 130 | changes := []configFileChange{ | ||
1314 | 131 | configFileChange{ | ||
1315 | 132 | Name: name, | ||
1316 | 133 | Value: value, | ||
1317 | 134 | }, | ||
1318 | 135 | } | ||
1319 | 136 | |||
1320 | 137 | return modifyNameValueFile(bootloaderUbootEnvFile, changes) | ||
1321 | 138 | } | ||
1322 | 139 | |||
1323 | 140 | func setBootVarFwEnv(name, value string) error { | ||
1342 | 141 | env, err := uenv.Open(bootloaderUbootFwEnvFile) | 61 | env, err := uenv.Open(bootloaderUbootFwEnvFile) |
1343 | 142 | if err != nil { | 62 | if err != nil { |
1344 | 143 | return err | 63 | return err |
1345 | 144 | } | 64 | } |
1346 | 145 | 65 | ||
1347 | 146 | // already set, nothing to do | 66 | // already set, nothing to do |
1349 | 147 | if env.Get(name) == value { | 67 | if env.Get(key) == value { |
1350 | 148 | return nil | 68 | return nil |
1351 | 149 | } | 69 | } |
1352 | 150 | 70 | ||
1354 | 151 | env.Set(name, value) | 71 | env.Set(key, value) |
1355 | 152 | return env.Save() | 72 | return env.Save() |
1356 | 153 | } | 73 | } |
1357 | 154 | 74 | ||
1358 | 155 | func getBootVarFwEnv(name string) (string, error) { | ||
1359 | 156 | env, err := uenv.Open(bootloaderUbootFwEnvFile) | ||
1360 | 157 | if err != nil { | ||
1361 | 158 | return "", err | ||
1362 | 159 | } | ||
1363 | 160 | |||
1364 | 161 | return env.Get(name), nil | ||
1365 | 162 | } | ||
1366 | 163 | |||
1367 | 164 | func (u *uboot) GetBootVar(name string) (value string, err error) { | ||
1368 | 165 | return getBootVar(name) | ||
1369 | 166 | } | ||
1370 | 167 | |||
1371 | 168 | func (u *uboot) GetNextBootRootFSName() (label string, err error) { | ||
1372 | 169 | value, err := u.GetBootVar(bootloaderRootfsVar) | ||
1373 | 170 | if err != nil { | ||
1374 | 171 | // should never happen | ||
1375 | 172 | return "", err | ||
1376 | 173 | } | ||
1377 | 174 | |||
1378 | 175 | return value, nil | ||
1379 | 176 | } | ||
1380 | 177 | |||
1381 | 178 | // FIXME: this is super similar to grub now, refactor to extract the | ||
1382 | 179 | // common code | ||
1383 | 180 | func (u *uboot) MarkCurrentBootSuccessful(currentRootfs string) error { | ||
1384 | 181 | // Clear the variable set on boot to denote a good boot. | ||
1385 | 182 | if err := setBootVar(bootloaderTrialBootVar, "0"); err != nil { | ||
1386 | 183 | return err | ||
1387 | 184 | } | ||
1388 | 185 | |||
1389 | 186 | if err := setBootVar(bootloaderRootfsVar, currentRootfs); err != nil { | ||
1390 | 187 | return err | ||
1391 | 188 | } | ||
1392 | 189 | |||
1393 | 190 | if err := setBootVar(bootloaderBootmodeVar, bootloaderBootmodeSuccess); err != nil { | ||
1394 | 191 | return err | ||
1395 | 192 | } | ||
1396 | 193 | |||
1397 | 194 | // legacy support, does not error if the file is not there | ||
1398 | 195 | return os.RemoveAll(bootloaderUbootStampFile) | ||
1399 | 196 | } | ||
1400 | 197 | |||
1401 | 198 | func (u *uboot) BootDir() string { | 75 | func (u *uboot) BootDir() string { |
1402 | 199 | return bootloaderUbootDir | 76 | return bootloaderUbootDir |
1403 | 200 | } | 77 | } |
1404 | 201 | |||
1405 | 202 | // Rewrite the specified file, applying the specified set of changes. | ||
1406 | 203 | // Lines not in the changes slice are left alone. | ||
1407 | 204 | // If the original file does not contain any of the name entries (from | ||
1408 | 205 | // the corresponding configFileChange objects), those entries are | ||
1409 | 206 | // appended to the file. | ||
1410 | 207 | // | ||
1411 | 208 | // FIXME: put into utils package | ||
1412 | 209 | // FIXME: improve logic | ||
1413 | 210 | func modifyNameValueFile(path string, changes []configFileChange) error { | ||
1414 | 211 | var updated []configFileChange | ||
1415 | 212 | |||
1416 | 213 | // we won't write to a file if we don't need to. | ||
1417 | 214 | updateNeeded := false | ||
1418 | 215 | |||
1419 | 216 | file, err := os.Open(path) | ||
1420 | 217 | if err != nil { | ||
1421 | 218 | return err | ||
1422 | 219 | } | ||
1423 | 220 | defer file.Close() | ||
1424 | 221 | |||
1425 | 222 | buf := bytes.NewBuffer(nil) | ||
1426 | 223 | scanner := bufio.NewScanner(file) | ||
1427 | 224 | for scanner.Scan() { | ||
1428 | 225 | line := scanner.Text() | ||
1429 | 226 | for _, change := range changes { | ||
1430 | 227 | if strings.HasPrefix(line, fmt.Sprintf("%s=", change.Name)) { | ||
1431 | 228 | value := strings.SplitN(line, "=", 2)[1] | ||
1432 | 229 | // updated is used later to see if you had the originally requested | ||
1433 | 230 | // value. | ||
1434 | 231 | updated = append(updated, change) | ||
1435 | 232 | if value != change.Value { | ||
1436 | 233 | line = fmt.Sprintf("%s=%s", change.Name, change.Value) | ||
1437 | 234 | updateNeeded = true | ||
1438 | 235 | } | ||
1439 | 236 | } | ||
1440 | 237 | } | ||
1441 | 238 | if _, err := fmt.Fprintln(buf, line); err != nil { | ||
1442 | 239 | return err | ||
1443 | 240 | } | ||
1444 | 241 | } | ||
1445 | 242 | |||
1446 | 243 | for _, change := range changes { | ||
1447 | 244 | got := false | ||
1448 | 245 | for _, update := range updated { | ||
1449 | 246 | if update.Name == change.Name { | ||
1450 | 247 | got = true | ||
1451 | 248 | break | ||
1452 | 249 | } | ||
1453 | 250 | } | ||
1454 | 251 | |||
1455 | 252 | if !got { | ||
1456 | 253 | updateNeeded = true | ||
1457 | 254 | |||
1458 | 255 | // name/value pair did not exist in original | ||
1459 | 256 | // file, so append | ||
1460 | 257 | if _, err := fmt.Fprintf(buf, "%s=%s\n", change.Name, change.Value); err != nil { | ||
1461 | 258 | return err | ||
1462 | 259 | } | ||
1463 | 260 | } | ||
1464 | 261 | } | ||
1465 | 262 | |||
1466 | 263 | if updateNeeded { | ||
1467 | 264 | return atomicWriteFile(path, buf.Bytes(), 0644) | ||
1468 | 265 | } | ||
1469 | 266 | |||
1470 | 267 | return nil | ||
1471 | 268 | } | ||
1472 | 269 | 78 | ||
1473 | === modified file 'bootloader/bootloader_uboot_test.go' | |||
1474 | --- partition/bootloader_uboot_test.go 2015-07-24 11:58:04 +0000 | |||
1475 | +++ bootloader/bootloader_uboot_test.go 2015-09-03 08:14:56 +0000 | |||
1476 | @@ -17,17 +17,14 @@ | |||
1477 | 17 | * | 17 | * |
1478 | 18 | */ | 18 | */ |
1479 | 19 | 19 | ||
1481 | 20 | package partition | 20 | package bootloader |
1482 | 21 | 21 | ||
1483 | 22 | import ( | 22 | import ( |
1484 | 23 | "io/ioutil" | ||
1485 | 24 | "os" | 23 | "os" |
1486 | 25 | "path/filepath" | ||
1487 | 26 | "strings" | 24 | "strings" |
1488 | 27 | "time" | 25 | "time" |
1489 | 28 | 26 | ||
1490 | 29 | . "gopkg.in/check.v1" | 27 | . "gopkg.in/check.v1" |
1491 | 30 | "launchpad.net/snappy/helpers" | ||
1492 | 31 | 28 | ||
1493 | 32 | "github.com/mvo5/uboot-go/uenv" | 29 | "github.com/mvo5/uboot-go/uenv" |
1494 | 33 | ) | 30 | ) |
1495 | @@ -66,319 +63,84 @@ | |||
1496 | 66 | snappy_boot=if test "${snappy_mode}" = "try"; then if test -e mmc ${bootpart} ${snappy_stamp}; then if test "${snappy_ab}" = "a"; then setenv snappy_ab "b"; else setenv snappy_ab "a"; fi; else fatwrite mmc ${mmcdev}:${mmcpart} 0x0 ${snappy_stamp} 0; fi; fi; run loadfiles; setenv mmcroot /dev/disk/by-label/system-${snappy_ab} ${snappy_cmdline}; run mmcargs; bootz ${loadaddr} ${initrd_addr}:${initrd_size} ${fdtaddr} | 63 | snappy_boot=if test "${snappy_mode}" = "try"; then if test -e mmc ${bootpart} ${snappy_stamp}; then if test "${snappy_ab}" = "a"; then setenv snappy_ab "b"; else setenv snappy_ab "a"; fi; else fatwrite mmc ${mmcdev}:${mmcpart} 0x0 ${snappy_stamp} 0; fi; fi; run loadfiles; setenv mmcroot /dev/disk/by-label/system-${snappy_ab} ${snappy_cmdline}; run mmcargs; bootz ${loadaddr} ${initrd_addr}:${initrd_size} ${fdtaddr} |
1497 | 67 | ` | 64 | ` |
1498 | 68 | 65 | ||
1500 | 69 | func (s *PartitionTestSuite) makeFakeUbootEnv(c *C) { | 66 | func (s *BootloaderTestSuite) makeFakeUbootEnv(c *C) { |
1501 | 70 | err := os.MkdirAll(bootloaderUbootDir, 0755) | 67 | err := os.MkdirAll(bootloaderUbootDir, 0755) |
1502 | 71 | c.Assert(err, IsNil) | 68 | c.Assert(err, IsNil) |
1503 | 72 | 69 | ||
1506 | 73 | // this file just needs to exist | 70 | env, err := uenv.Create(bootloaderUbootFwEnvFile, 4096) |
1505 | 74 | err = ioutil.WriteFile(bootloaderUbootConfigFile, []byte(""), 0644) | ||
1507 | 75 | c.Assert(err, IsNil) | 71 | c.Assert(err, IsNil) |
1511 | 76 | 72 | env.Set("snappy_ab", "a") | |
1512 | 77 | // this file needs specific data | 73 | env.Set("snappy_mode", "regular") |
1513 | 78 | err = ioutil.WriteFile(bootloaderUbootEnvFile, []byte(fakeUbootEnvData), 0644) | 74 | err = env.Save() |
1514 | 79 | c.Assert(err, IsNil) | 75 | c.Assert(err, IsNil) |
1515 | 80 | } | 76 | } |
1516 | 81 | 77 | ||
1520 | 82 | func (s *PartitionTestSuite) TestNewUbootNoUbootReturnsNil(c *C) { | 78 | func (s *BootloaderTestSuite) TestNewUbootNoUbootReturnsNil(c *C) { |
1521 | 83 | partition := New() | 79 | u := newUboot() |
1519 | 84 | u := newUboot(partition) | ||
1522 | 85 | c.Assert(u, IsNil) | 80 | c.Assert(u, IsNil) |
1523 | 86 | } | 81 | } |
1524 | 87 | 82 | ||
1573 | 88 | func (s *PartitionTestSuite) TestNewUboot(c *C) { | 83 | func (s *BootloaderTestSuite) TestNewUboot(c *C) { |
1574 | 89 | s.makeFakeUbootEnv(c) | 84 | s.makeFakeUbootEnv(c) |
1575 | 90 | 85 | ||
1576 | 91 | partition := New() | 86 | u := newUboot() |
1577 | 92 | u := newUboot(partition) | 87 | c.Assert(u, NotNil) |
1578 | 93 | c.Assert(u, NotNil) | 88 | } |
1579 | 94 | c.Assert(u.Name(), Equals, bootloaderNameUboot) | 89 | |
1580 | 95 | } | 90 | func (s *BootloaderTestSuite) TestUbootGetEnvVar(c *C) { |
1581 | 96 | 91 | s.makeFakeUbootEnv(c) | |
1582 | 97 | func (s *PartitionTestSuite) TestUbootGetBootVar(c *C) { | 92 | |
1583 | 98 | s.makeFakeUbootEnv(c) | 93 | u := newUboot() |
1536 | 99 | |||
1537 | 100 | partition := New() | ||
1538 | 101 | u := newUboot(partition) | ||
1539 | 102 | |||
1540 | 103 | nextBoot, err := u.GetBootVar(bootloaderRootfsVar) | ||
1541 | 104 | c.Assert(err, IsNil) | ||
1542 | 105 | // the https://developer.ubuntu.com/en/snappy/porting guide says | ||
1543 | 106 | // we always use the short names | ||
1544 | 107 | c.Assert(nextBoot, Equals, "a") | ||
1545 | 108 | |||
1546 | 109 | // ensure that nextBootIsOther works too | ||
1547 | 110 | c.Assert(partition.IsNextBootOther(), Equals, false) | ||
1548 | 111 | } | ||
1549 | 112 | |||
1550 | 113 | func (s *PartitionTestSuite) TestUbootToggleRootFS(c *C) { | ||
1551 | 114 | s.makeFakeUbootEnv(c) | ||
1552 | 115 | |||
1553 | 116 | partition := New() | ||
1554 | 117 | u := newUboot(partition) | ||
1555 | 118 | c.Assert(u, NotNil) | ||
1556 | 119 | |||
1557 | 120 | err := u.ToggleRootFS("b") | ||
1558 | 121 | c.Assert(err, IsNil) | ||
1559 | 122 | |||
1560 | 123 | nextBoot, err := u.GetBootVar(bootloaderRootfsVar) | ||
1561 | 124 | c.Assert(err, IsNil) | ||
1562 | 125 | c.Assert(nextBoot, Equals, "b") | ||
1563 | 126 | |||
1564 | 127 | // ensure that nextBootIsOther works too | ||
1565 | 128 | c.Assert(partition.IsNextBootOther(), Equals, true) | ||
1566 | 129 | } | ||
1567 | 130 | |||
1568 | 131 | func (s *PartitionTestSuite) TestUbootGetEnvVar(c *C) { | ||
1569 | 132 | s.makeFakeUbootEnv(c) | ||
1570 | 133 | |||
1571 | 134 | partition := New() | ||
1572 | 135 | u := newUboot(partition) | ||
1584 | 136 | c.Assert(u, NotNil) | 94 | c.Assert(u, NotNil) |
1585 | 137 | 95 | ||
1586 | 138 | v, err := u.GetBootVar(bootloaderBootmodeVar) | 96 | v, err := u.GetBootVar(bootloaderBootmodeVar) |
1587 | 139 | c.Assert(err, IsNil) | 97 | c.Assert(err, IsNil) |
1588 | 140 | c.Assert(v, Equals, "regular") | 98 | c.Assert(v, Equals, "regular") |
1589 | 141 | 99 | ||
1591 | 142 | v, err = u.GetBootVar(bootloaderRootfsVar) | 100 | v, err = u.GetBootVar("snappy_ab") |
1592 | 143 | c.Assert(err, IsNil) | 101 | c.Assert(err, IsNil) |
1593 | 144 | c.Assert(v, Equals, "a") | 102 | c.Assert(v, Equals, "a") |
1594 | 145 | } | 103 | } |
1595 | 146 | 104 | ||
1814 | 147 | func (s *PartitionTestSuite) TestGetBootloaderWithUboot(c *C) { | 105 | func (s *BootloaderTestSuite) TestUbootSetEnvVar(c *C) { |
1815 | 148 | s.makeFakeUbootEnv(c) | 106 | s.makeFakeUbootEnv(c) |
1816 | 149 | p := New() | 107 | |
1817 | 150 | bootloader, err := bootloader(p) | 108 | u := newUboot() |
1818 | 151 | c.Assert(err, IsNil) | 109 | c.Assert(u, NotNil) |
1819 | 152 | c.Assert(bootloader.Name(), Equals, bootloaderNameUboot) | 110 | |
1820 | 153 | } | 111 | err := u.SetBootVar("snappy_os", "123") |
1821 | 154 | 112 | c.Assert(err, IsNil) | |
1822 | 155 | func makeMockAssetsDir(c *C) { | 113 | |
1823 | 156 | for _, f := range []string{"assets/vmlinuz", "assets/initrd.img", "assets/dtbs/foo.dtb", "assets/dtbs/bar.dtb"} { | 114 | v, err := u.GetBootVar("snappy_os") |
1824 | 157 | p := filepath.Join(cacheDir, f) | 115 | c.Assert(err, IsNil) |
1825 | 158 | os.MkdirAll(filepath.Dir(p), 0755) | 116 | c.Assert(v, Equals, "123") |
1826 | 159 | err := ioutil.WriteFile(p, []byte(f), 0644) | 117 | } |
1827 | 160 | c.Assert(err, IsNil) | 118 | |
1828 | 161 | } | 119 | func (s *BootloaderTestSuite) TestUbootSetEnvNoUselessWrites(c *C) { |
1829 | 162 | } | 120 | s.makeFakeUbootEnv(c) |
1612 | 163 | |||
1613 | 164 | func (s *PartitionTestSuite) TestHandleAssets(c *C) { | ||
1614 | 165 | s.makeFakeUbootEnv(c) | ||
1615 | 166 | p := New() | ||
1616 | 167 | bootloader, err := bootloader(p) | ||
1617 | 168 | c.Assert(err, IsNil) | ||
1618 | 169 | |||
1619 | 170 | // mock the hardwareYaml and the cacheDir | ||
1620 | 171 | hardwareSpecFile = makeHardwareYaml(c, "") | ||
1621 | 172 | cacheDir = c.MkDir() | ||
1622 | 173 | |||
1623 | 174 | // create mock assets/ | ||
1624 | 175 | makeMockAssetsDir(c) | ||
1625 | 176 | |||
1626 | 177 | // run the handle assets code | ||
1627 | 178 | err = bootloader.HandleAssets() | ||
1628 | 179 | c.Assert(err, IsNil) | ||
1629 | 180 | |||
1630 | 181 | // ensure the files are where we expect them | ||
1631 | 182 | otherBootPath := bootloader.(*uboot).otherBootPath | ||
1632 | 183 | for _, f := range []string{"vmlinuz", "initrd.img", "dtbs/foo.dtb", "dtbs/bar.dtb"} { | ||
1633 | 184 | content, err := ioutil.ReadFile(filepath.Join(otherBootPath, f)) | ||
1634 | 185 | c.Assert(err, IsNil) | ||
1635 | 186 | // match content | ||
1636 | 187 | c.Assert(strings.HasSuffix(string(content), f), Equals, true) | ||
1637 | 188 | } | ||
1638 | 189 | |||
1639 | 190 | // ensure nothing left behind | ||
1640 | 191 | c.Assert(helpers.FileExists(filepath.Join(cacheDir, "assets")), Equals, false) | ||
1641 | 192 | c.Assert(helpers.FileExists(hardwareSpecFile), Equals, false) | ||
1642 | 193 | } | ||
1643 | 194 | |||
1644 | 195 | func (s *PartitionTestSuite) TestHandleAssetsVerifyBootloader(c *C) { | ||
1645 | 196 | s.makeFakeUbootEnv(c) | ||
1646 | 197 | p := New() | ||
1647 | 198 | bootloader, err := bootloader(p) | ||
1648 | 199 | c.Assert(err, IsNil) | ||
1649 | 200 | |||
1650 | 201 | // mock the hardwareYaml and the cacheDir | ||
1651 | 202 | hardwareSpecFile = makeHardwareYaml(c, "bootloader: grub") | ||
1652 | 203 | cacheDir = c.MkDir() | ||
1653 | 204 | |||
1654 | 205 | err = bootloader.HandleAssets() | ||
1655 | 206 | c.Assert(err, NotNil) | ||
1656 | 207 | } | ||
1657 | 208 | |||
1658 | 209 | func (s *PartitionTestSuite) TestHandleAssetsFailVerifyPartitionLayout(c *C) { | ||
1659 | 210 | s.makeFakeUbootEnv(c) | ||
1660 | 211 | p := New() | ||
1661 | 212 | bootloader, err := bootloader(p) | ||
1662 | 213 | c.Assert(err, IsNil) | ||
1663 | 214 | |||
1664 | 215 | // mock the hardwareYaml and the cacheDir | ||
1665 | 216 | hardwareSpecFile = makeHardwareYaml(c, ` | ||
1666 | 217 | bootloader: u-boot | ||
1667 | 218 | partition-layout: inplace | ||
1668 | 219 | `) | ||
1669 | 220 | err = bootloader.HandleAssets() | ||
1670 | 221 | c.Assert(err, NotNil) | ||
1671 | 222 | } | ||
1672 | 223 | |||
1673 | 224 | func (s *PartitionTestSuite) TestHandleAssetsNoHardwareYaml(c *C) { | ||
1674 | 225 | s.makeFakeUbootEnv(c) | ||
1675 | 226 | hardwareSpecFile = filepath.Join(c.MkDir(), "non-existent.yaml") | ||
1676 | 227 | |||
1677 | 228 | p := New() | ||
1678 | 229 | bootloader, err := bootloader(p) | ||
1679 | 230 | c.Assert(err, IsNil) | ||
1680 | 231 | |||
1681 | 232 | c.Assert(bootloader.HandleAssets(), IsNil) | ||
1682 | 233 | } | ||
1683 | 234 | |||
1684 | 235 | func (s *PartitionTestSuite) TestHandleAssetsBadHardwareYaml(c *C) { | ||
1685 | 236 | s.makeFakeUbootEnv(c) | ||
1686 | 237 | p := New() | ||
1687 | 238 | bootloader, err := bootloader(p) | ||
1688 | 239 | c.Assert(err, IsNil) | ||
1689 | 240 | |||
1690 | 241 | hardwareSpecFile = makeHardwareYaml(c, ` | ||
1691 | 242 | bootloader u-boot | ||
1692 | 243 | `) | ||
1693 | 244 | |||
1694 | 245 | c.Assert(bootloader.HandleAssets(), NotNil) | ||
1695 | 246 | } | ||
1696 | 247 | |||
1697 | 248 | func (s *PartitionTestSuite) TestUbootMarkCurrentBootSuccessful(c *C) { | ||
1698 | 249 | s.makeFakeUbootEnv(c) | ||
1699 | 250 | |||
1700 | 251 | // To simulate what uboot does for a "try" mode boot, create a | ||
1701 | 252 | // stamp file. uboot will expect this file to be removed by | ||
1702 | 253 | // "snappy booted" if the system boots successfully. If this | ||
1703 | 254 | // file exists when uboot starts, it will know that the previous | ||
1704 | 255 | // boot failed, and will therefore toggle to the other rootfs. | ||
1705 | 256 | err := ioutil.WriteFile(bootloaderUbootStampFile, []byte(""), 0640) | ||
1706 | 257 | c.Assert(err, IsNil) | ||
1707 | 258 | c.Assert(helpers.FileExists(bootloaderUbootStampFile), Equals, true) | ||
1708 | 259 | |||
1709 | 260 | partition := New() | ||
1710 | 261 | u := newUboot(partition) | ||
1711 | 262 | c.Assert(u, NotNil) | ||
1712 | 263 | |||
1713 | 264 | // enter "try" mode so that we check to ensure that snappy | ||
1714 | 265 | // correctly modifies the snappy_mode variable from "try" to | ||
1715 | 266 | // "regular" to denote a good boot. | ||
1716 | 267 | err = u.ToggleRootFS("b") | ||
1717 | 268 | c.Assert(err, IsNil) | ||
1718 | 269 | |||
1719 | 270 | c.Assert(helpers.FileExists(bootloaderUbootEnvFile), Equals, true) | ||
1720 | 271 | bytes, err := ioutil.ReadFile(bootloaderUbootEnvFile) | ||
1721 | 272 | c.Assert(err, IsNil) | ||
1722 | 273 | c.Assert(strings.Contains(string(bytes), "snappy_mode=try"), Equals, true) | ||
1723 | 274 | c.Assert(strings.Contains(string(bytes), "snappy_mode=regular"), Equals, false) | ||
1724 | 275 | c.Assert(strings.Contains(string(bytes), "snappy_ab=b"), Equals, true) | ||
1725 | 276 | |||
1726 | 277 | err = u.MarkCurrentBootSuccessful("b") | ||
1727 | 278 | c.Assert(err, IsNil) | ||
1728 | 279 | |||
1729 | 280 | c.Assert(helpers.FileExists(bootloaderUbootStampFile), Equals, false) | ||
1730 | 281 | c.Assert(helpers.FileExists(bootloaderUbootEnvFile), Equals, true) | ||
1731 | 282 | |||
1732 | 283 | bytes, err = ioutil.ReadFile(bootloaderUbootEnvFile) | ||
1733 | 284 | c.Assert(err, IsNil) | ||
1734 | 285 | c.Assert(strings.Contains(string(bytes), "snappy_mode=try"), Equals, false) | ||
1735 | 286 | c.Assert(strings.Contains(string(bytes), "snappy_mode=regular"), Equals, true) | ||
1736 | 287 | c.Assert(strings.Contains(string(bytes), "snappy_ab=b"), Equals, true) | ||
1737 | 288 | } | ||
1738 | 289 | |||
1739 | 290 | func (s *PartitionTestSuite) TestNoWriteNotNeeded(c *C) { | ||
1740 | 291 | s.makeFakeUbootEnv(c) | ||
1741 | 292 | |||
1742 | 293 | atomiCall := false | ||
1743 | 294 | atomicWriteFile = func(a string, b []byte, c os.FileMode) error { | ||
1744 | 295 | atomiCall = true | ||
1745 | 296 | return helpers.AtomicWriteFile(a, b, c) | ||
1746 | 297 | } | ||
1747 | 298 | |||
1748 | 299 | partition := New() | ||
1749 | 300 | u := newUboot(partition) | ||
1750 | 301 | c.Assert(u, NotNil) | ||
1751 | 302 | |||
1752 | 303 | c.Check(u.MarkCurrentBootSuccessful("a"), IsNil) | ||
1753 | 304 | c.Assert(atomiCall, Equals, false) | ||
1754 | 305 | } | ||
1755 | 306 | |||
1756 | 307 | func (s *PartitionTestSuite) TestWriteDueToMissingValues(c *C) { | ||
1757 | 308 | s.makeFakeUbootEnv(c) | ||
1758 | 309 | |||
1759 | 310 | // this file needs specific data | ||
1760 | 311 | c.Assert(ioutil.WriteFile(bootloaderUbootEnvFile, []byte(""), 0644), IsNil) | ||
1761 | 312 | |||
1762 | 313 | atomiCall := false | ||
1763 | 314 | atomicWriteFile = func(a string, b []byte, c os.FileMode) error { | ||
1764 | 315 | atomiCall = true | ||
1765 | 316 | return helpers.AtomicWriteFile(a, b, c) | ||
1766 | 317 | } | ||
1767 | 318 | |||
1768 | 319 | partition := New() | ||
1769 | 320 | u := newUboot(partition) | ||
1770 | 321 | c.Assert(u, NotNil) | ||
1771 | 322 | |||
1772 | 323 | c.Check(u.MarkCurrentBootSuccessful("a"), IsNil) | ||
1773 | 324 | c.Assert(atomiCall, Equals, true) | ||
1774 | 325 | |||
1775 | 326 | bytes, err := ioutil.ReadFile(bootloaderUbootEnvFile) | ||
1776 | 327 | c.Assert(err, IsNil) | ||
1777 | 328 | c.Check(strings.Contains(string(bytes), "snappy_mode=try"), Equals, false) | ||
1778 | 329 | c.Check(strings.Contains(string(bytes), "snappy_mode=regular"), Equals, true) | ||
1779 | 330 | c.Check(strings.Contains(string(bytes), "snappy_ab=a"), Equals, true) | ||
1780 | 331 | } | ||
1781 | 332 | |||
1782 | 333 | func (s *PartitionTestSuite) TestUbootMarkCurrentBootSuccessfulFwEnv(c *C) { | ||
1783 | 334 | s.makeFakeUbootEnv(c) | ||
1784 | 335 | |||
1785 | 336 | env, err := uenv.Create(bootloaderUbootFwEnvFile, 4096) | ||
1786 | 337 | c.Assert(err, IsNil) | ||
1787 | 338 | env.Set("snappy_ab", "b") | ||
1788 | 339 | env.Set("snappy_mode", "try") | ||
1789 | 340 | env.Set("snappy_trial_boot", "1") | ||
1790 | 341 | err = env.Save() | ||
1791 | 342 | c.Assert(err, IsNil) | ||
1792 | 343 | |||
1793 | 344 | partition := New() | ||
1794 | 345 | u := newUboot(partition) | ||
1795 | 346 | c.Assert(u, NotNil) | ||
1796 | 347 | |||
1797 | 348 | err = u.MarkCurrentBootSuccessful("b") | ||
1798 | 349 | c.Assert(err, IsNil) | ||
1799 | 350 | |||
1800 | 351 | env, err = uenv.Open(bootloaderUbootFwEnvFile) | ||
1801 | 352 | c.Assert(err, IsNil) | ||
1802 | 353 | c.Assert(env.String(), Equals, "snappy_ab=b\nsnappy_mode=regular\nsnappy_trial_boot=0\n") | ||
1803 | 354 | } | ||
1804 | 355 | |||
1805 | 356 | func (s *PartitionTestSuite) TestUbootSetEnvNoUselessWrites(c *C) { | ||
1806 | 357 | s.makeFakeUbootEnv(c) | ||
1807 | 358 | |||
1808 | 359 | env, err := uenv.Create(bootloaderUbootFwEnvFile, 4096) | ||
1809 | 360 | c.Assert(err, IsNil) | ||
1810 | 361 | env.Set("snappy_ab", "b") | ||
1811 | 362 | env.Set("snappy_mode", "regular") | ||
1812 | 363 | err = env.Save() | ||
1813 | 364 | c.Assert(err, IsNil) | ||
1830 | 365 | 121 | ||
1831 | 366 | st, err := os.Stat(bootloaderUbootFwEnvFile) | 122 | st, err := os.Stat(bootloaderUbootFwEnvFile) |
1832 | 367 | c.Assert(err, IsNil) | 123 | c.Assert(err, IsNil) |
1833 | 368 | time.Sleep(100 * time.Millisecond) | 124 | time.Sleep(100 * time.Millisecond) |
1834 | 369 | 125 | ||
1837 | 370 | partition := New() | 126 | u := newUboot() |
1836 | 371 | u := newUboot(partition) | ||
1838 | 372 | c.Assert(u, NotNil) | 127 | c.Assert(u, NotNil) |
1839 | 373 | 128 | ||
1841 | 374 | err = setBootVar(bootloaderRootfsVar, "b") | 129 | err = u.SetBootVar("snappy_ab", "a") |
1842 | 375 | c.Assert(err, IsNil) | 130 | c.Assert(err, IsNil) |
1843 | 376 | 131 | ||
1845 | 377 | env, err = uenv.Open(bootloaderUbootFwEnvFile) | 132 | env, err := uenv.Open(bootloaderUbootFwEnvFile) |
1846 | 378 | c.Assert(err, IsNil) | 133 | c.Assert(err, IsNil) |
1848 | 379 | c.Assert(env.String(), Equals, "snappy_ab=b\nsnappy_mode=regular\n") | 134 | c.Assert(env.String(), Equals, "snappy_ab=a\nsnappy_mode=regular\n") |
1849 | 380 | 135 | ||
1850 | 381 | st2, err := os.Stat(bootloaderUbootFwEnvFile) | 136 | st2, err := os.Stat(bootloaderUbootFwEnvFile) |
1851 | 382 | c.Assert(err, IsNil) | 137 | c.Assert(err, IsNil) |
1852 | 383 | c.Assert(st.ModTime(), Equals, st2.ModTime()) | 138 | c.Assert(st.ModTime(), Equals, st2.ModTime()) |
1853 | 384 | } | 139 | } |
1854 | 140 | |||
1855 | 141 | func (s *BootloaderTestSuite) TestUbootBootdir(c *C) { | ||
1856 | 142 | s.makeFakeUbootEnv(c) | ||
1857 | 143 | |||
1858 | 144 | u := newUboot() | ||
1859 | 145 | c.Assert(strings.HasSuffix(u.BootDir(), "/boot/uboot"), Equals, true) | ||
1860 | 146 | } | ||
1861 | 385 | 147 | ||
1862 | === added file 'bootloader/utils.go' | |||
1863 | --- bootloader/utils.go 1970-01-01 00:00:00 +0000 | |||
1864 | +++ bootloader/utils.go 2015-09-03 08:14:56 +0000 | |||
1865 | @@ -0,0 +1,63 @@ | |||
1866 | 1 | // -*- Mode: Go; indent-tabs-mode: t -*- | ||
1867 | 2 | |||
1868 | 3 | /* | ||
1869 | 4 | * Copyright (C) 2014-2015 Canonical Ltd | ||
1870 | 5 | * | ||
1871 | 6 | * This program is free software: you can redistribute it and/or modify | ||
1872 | 7 | * it under the terms of the GNU General Public License version 3 as | ||
1873 | 8 | * published by the Free Software Foundation. | ||
1874 | 9 | * | ||
1875 | 10 | * This program is distributed in the hope that it will be useful, | ||
1876 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
1877 | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
1878 | 13 | * GNU General Public License for more details. | ||
1879 | 14 | * | ||
1880 | 15 | * You should have received a copy of the GNU General Public License | ||
1881 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
1882 | 17 | * | ||
1883 | 18 | */ | ||
1884 | 19 | |||
1885 | 20 | package bootloader | ||
1886 | 21 | |||
1887 | 22 | import ( | ||
1888 | 23 | "errors" | ||
1889 | 24 | "fmt" | ||
1890 | 25 | "os/exec" | ||
1891 | 26 | "strings" | ||
1892 | 27 | ) | ||
1893 | 28 | |||
1894 | 29 | // FIXME: would it make sense to differenciate between launch errors and | ||
1895 | 30 | // exit code? (i.e. something like (returnCode, error) ?) | ||
1896 | 31 | func runCommandImpl(args ...string) (err error) { | ||
1897 | 32 | if len(args) == 0 { | ||
1898 | 33 | return errors.New("no command specified") | ||
1899 | 34 | } | ||
1900 | 35 | |||
1901 | 36 | if out, err := exec.Command(args[0], args[1:]...).CombinedOutput(); err != nil { | ||
1902 | 37 | cmdline := strings.Join(args, " ") | ||
1903 | 38 | return fmt.Errorf("Failed to run command '%s': %s (%s)", | ||
1904 | 39 | cmdline, out, err) | ||
1905 | 40 | } | ||
1906 | 41 | return nil | ||
1907 | 42 | } | ||
1908 | 43 | |||
1909 | 44 | // Run the command specified by args | ||
1910 | 45 | // This is a var instead of a function to making mocking in the tests easier | ||
1911 | 46 | var runCommand = runCommandImpl | ||
1912 | 47 | |||
1913 | 48 | // Run command specified by args and return the output | ||
1914 | 49 | func runCommandWithStdoutImpl(args ...string) (output string, err error) { | ||
1915 | 50 | if len(args) == 0 { | ||
1916 | 51 | return "", errors.New("no command specified") | ||
1917 | 52 | } | ||
1918 | 53 | |||
1919 | 54 | bytes, err := exec.Command(args[0], args[1:]...).Output() | ||
1920 | 55 | if err != nil { | ||
1921 | 56 | return "", err | ||
1922 | 57 | } | ||
1923 | 58 | |||
1924 | 59 | return string(bytes), err | ||
1925 | 60 | } | ||
1926 | 61 | |||
1927 | 62 | // This is a var instead of a function to making mocking in the tests easier | ||
1928 | 63 | var runCommandWithStdout = runCommandWithStdoutImpl | ||
1929 | 0 | 64 | ||
1930 | === added file 'bootloader/utils_test.go' | |||
1931 | --- bootloader/utils_test.go 1970-01-01 00:00:00 +0000 | |||
1932 | +++ bootloader/utils_test.go 2015-09-03 08:14:56 +0000 | |||
1933 | @@ -0,0 +1,47 @@ | |||
1934 | 1 | // -*- Mode: Go; indent-tabs-mode: t -*- | ||
1935 | 2 | |||
1936 | 3 | /* | ||
1937 | 4 | * Copyright (C) 2014-2015 Canonical Ltd | ||
1938 | 5 | * | ||
1939 | 6 | * This program is free software: you can redistribute it and/or modify | ||
1940 | 7 | * it under the terms of the GNU General Public License version 3 as | ||
1941 | 8 | * published by the Free Software Foundation. | ||
1942 | 9 | * | ||
1943 | 10 | * This program is distributed in the hope that it will be useful, | ||
1944 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
1945 | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
1946 | 13 | * GNU General Public License for more details. | ||
1947 | 14 | * | ||
1948 | 15 | * You should have received a copy of the GNU General Public License | ||
1949 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
1950 | 17 | * | ||
1951 | 18 | */ | ||
1952 | 19 | |||
1953 | 20 | package bootloader | ||
1954 | 21 | |||
1955 | 22 | import ( | ||
1956 | 23 | . "gopkg.in/check.v1" | ||
1957 | 24 | ) | ||
1958 | 25 | |||
1959 | 26 | type UtilsTestSuite struct { | ||
1960 | 27 | } | ||
1961 | 28 | |||
1962 | 29 | var _ = Suite(&UtilsTestSuite{}) | ||
1963 | 30 | |||
1964 | 31 | func (s *UtilsTestSuite) SetUpTest(c *C) { | ||
1965 | 32 | } | ||
1966 | 33 | |||
1967 | 34 | func (s *UtilsTestSuite) TestRunCommand(c *C) { | ||
1968 | 35 | err := runCommandImpl("false") | ||
1969 | 36 | c.Assert(err, NotNil) | ||
1970 | 37 | |||
1971 | 38 | err = runCommandImpl("no-such-command") | ||
1972 | 39 | c.Assert(err, NotNil) | ||
1973 | 40 | } | ||
1974 | 41 | |||
1975 | 42 | func (s *UtilsTestSuite) TestRunCommandWithStdout(c *C) { | ||
1976 | 43 | runCommandWithStdout = runCommandWithStdoutImpl | ||
1977 | 44 | output, err := runCommandWithStdout("sh", "-c", "printf 'foo\nbar'") | ||
1978 | 45 | c.Assert(err, IsNil) | ||
1979 | 46 | c.Assert(output, DeepEquals, "foo\nbar") | ||
1980 | 47 | } | ||
1981 | 0 | 48 | ||
1982 | === modified file 'cmd/snappy/cmd_booted.go' | |||
1983 | --- cmd/snappy/cmd_booted.go 2015-06-15 07:38:57 +0000 | |||
1984 | +++ cmd/snappy/cmd_booted.go 2015-09-03 08:14:56 +0000 | |||
1985 | @@ -20,9 +20,8 @@ | |||
1986 | 20 | package main | 20 | package main |
1987 | 21 | 21 | ||
1988 | 22 | import ( | 22 | import ( |
1989 | 23 | "launchpad.net/snappy/bootloader" | ||
1990 | 23 | "launchpad.net/snappy/logger" | 24 | "launchpad.net/snappy/logger" |
1991 | 24 | "launchpad.net/snappy/pkg" | ||
1992 | 25 | "launchpad.net/snappy/snappy" | ||
1993 | 26 | ) | 25 | ) |
1994 | 27 | 26 | ||
1995 | 28 | type cmdBooted struct { | 27 | type cmdBooted struct { |
1996 | @@ -43,10 +42,5 @@ | |||
1997 | 43 | } | 42 | } |
1998 | 44 | 43 | ||
1999 | 45 | func (x *cmdBooted) doBooted() error { | 44 | func (x *cmdBooted) doBooted() error { |
2006 | 46 | parts, err := snappy.ActiveSnapsByType(pkg.TypeCore) | 45 | return bootloader.MarkBootSuccessful() |
2001 | 47 | if err != nil { | ||
2002 | 48 | return err | ||
2003 | 49 | } | ||
2004 | 50 | |||
2005 | 51 | return parts[0].(*snappy.SystemImagePart).MarkBootSuccessful() | ||
2007 | 52 | } | 46 | } |
2008 | 53 | 47 | ||
2009 | === modified file 'gen-coverage.sh' | |||
2010 | --- gen-coverage.sh 2015-06-09 13:02:49 +0000 | |||
2011 | +++ gen-coverage.sh 2015-09-03 08:14:56 +0000 | |||
2012 | @@ -10,8 +10,8 @@ | |||
2013 | 10 | 10 | ||
2014 | 11 | (cd snappy && | 11 | (cd snappy && |
2015 | 12 | $GOPATH/bin/gocov test | $GOPATH/bin/gocov-html > $OUTPUTDIR/cov-snappy.html) | 12 | $GOPATH/bin/gocov test | $GOPATH/bin/gocov-html > $OUTPUTDIR/cov-snappy.html) |
2018 | 13 | (cd partition && | 13 | (cd bootloader && |
2019 | 14 | $GOPATH/bin/gocov test | $GOPATH/bin/gocov-html > $OUTPUTDIR/cov-partition.html) | 14 | $GOPATH/bin/gocov test | $GOPATH/bin/gocov-html > $OUTPUTDIR/cov-bootloader.html) |
2020 | 15 | (cd logger && | 15 | (cd logger && |
2021 | 16 | $GOPATH/bin/gocov test | $GOPATH/bin/gocov-html > $OUTPUTDIR/cov-logger.html) | 16 | $GOPATH/bin/gocov test | $GOPATH/bin/gocov-html > $OUTPUTDIR/cov-logger.html) |
2022 | 17 | (cd helpers && | 17 | (cd helpers && |
2023 | 18 | 18 | ||
2024 | === removed file 'partition/assets.go' | |||
2025 | --- partition/assets.go 2015-06-11 13:08:19 +0000 | |||
2026 | +++ partition/assets.go 1970-01-01 00:00:00 +0000 | |||
2027 | @@ -1,87 +0,0 @@ | |||
2028 | 1 | // -*- Mode: Go; indent-tabs-mode: t -*- | ||
2029 | 2 | |||
2030 | 3 | /* | ||
2031 | 4 | * Copyright (C) 2014-2015 Canonical Ltd | ||
2032 | 5 | * | ||
2033 | 6 | * This program is free software: you can redistribute it and/or modify | ||
2034 | 7 | * it under the terms of the GNU General Public License version 3 as | ||
2035 | 8 | * published by the Free Software Foundation. | ||
2036 | 9 | * | ||
2037 | 10 | * This program is distributed in the hope that it will be useful, | ||
2038 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
2039 | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
2040 | 13 | * GNU General Public License for more details. | ||
2041 | 14 | * | ||
2042 | 15 | * You should have received a copy of the GNU General Public License | ||
2043 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
2044 | 17 | * | ||
2045 | 18 | */ | ||
2046 | 19 | |||
2047 | 20 | package partition | ||
2048 | 21 | |||
2049 | 22 | import ( | ||
2050 | 23 | "errors" | ||
2051 | 24 | "io/ioutil" | ||
2052 | 25 | "os" | ||
2053 | 26 | "path/filepath" | ||
2054 | 27 | |||
2055 | 28 | "gopkg.in/yaml.v2" | ||
2056 | 29 | ) | ||
2057 | 30 | |||
2058 | 31 | // Representation of the yaml in the hardwareSpecFile | ||
2059 | 32 | type hardwareSpecType struct { | ||
2060 | 33 | Kernel string `yaml:"kernel"` | ||
2061 | 34 | Initrd string `yaml:"initrd"` | ||
2062 | 35 | DtbDir string `yaml:"dtbs"` | ||
2063 | 36 | PartitionLayout string `yaml:"partition-layout"` | ||
2064 | 37 | Bootloader bootloaderName `yaml:"bootloader"` | ||
2065 | 38 | } | ||
2066 | 39 | |||
2067 | 40 | var ( | ||
2068 | 41 | // ErrNoHardwareYaml is returned when no hardware yaml is found in | ||
2069 | 42 | // the update, this means that there is nothing to process with regards | ||
2070 | 43 | // to device parts. | ||
2071 | 44 | ErrNoHardwareYaml = errors.New("no hardware.yaml") | ||
2072 | 45 | |||
2073 | 46 | // Declarative specification of the type of system which specifies such | ||
2074 | 47 | // details as: | ||
2075 | 48 | // | ||
2076 | 49 | // - the location of initrd+kernel within the system-image archive. | ||
2077 | 50 | // - the location of hardware-specific .dtb files within the | ||
2078 | 51 | // system-image archive. | ||
2079 | 52 | // - the type of bootloader that should be used for this system. | ||
2080 | 53 | // - expected system partition layout (single or dual rootfs's). | ||
2081 | 54 | hardwareSpecFileReal = filepath.Join(cacheDir, "hardware.yaml") | ||
2082 | 55 | |||
2083 | 56 | // useful to override in the tests | ||
2084 | 57 | hardwareSpecFile = hardwareSpecFileReal | ||
2085 | 58 | |||
2086 | 59 | // Directory that _may_ get automatically created on unpack that | ||
2087 | 60 | // contains updated hardware-specific boot assets (such as initrd, | ||
2088 | 61 | // kernel) | ||
2089 | 62 | assetsDir = filepath.Join(cacheDir, "assets") | ||
2090 | 63 | |||
2091 | 64 | // Directory that _may_ get automatically created on unpack that | ||
2092 | 65 | // contains updated hardware-specific assets that require flashing | ||
2093 | 66 | // to the disk (such as uBoot, MLO) | ||
2094 | 67 | flashAssetsDir = filepath.Join(cacheDir, "flashtool-assets") | ||
2095 | 68 | ) | ||
2096 | 69 | |||
2097 | 70 | func readHardwareSpec() (*hardwareSpecType, error) { | ||
2098 | 71 | var h hardwareSpecType | ||
2099 | 72 | |||
2100 | 73 | data, err := ioutil.ReadFile(hardwareSpecFile) | ||
2101 | 74 | // if hardware.yaml does not exist it just means that there was no | ||
2102 | 75 | // device part in the update. | ||
2103 | 76 | if os.IsNotExist(err) { | ||
2104 | 77 | return nil, ErrNoHardwareYaml | ||
2105 | 78 | } else if err != nil { | ||
2106 | 79 | return nil, err | ||
2107 | 80 | } | ||
2108 | 81 | |||
2109 | 82 | if err := yaml.Unmarshal([]byte(data), &h); err != nil { | ||
2110 | 83 | return nil, err | ||
2111 | 84 | } | ||
2112 | 85 | |||
2113 | 86 | return &h, nil | ||
2114 | 87 | } | ||
2115 | 88 | 0 | ||
2116 | === removed file 'partition/assets_test.go' | |||
2117 | --- partition/assets_test.go 2015-06-11 07:06:21 +0000 | |||
2118 | +++ partition/assets_test.go 1970-01-01 00:00:00 +0000 | |||
2119 | @@ -1,36 +0,0 @@ | |||
2120 | 1 | // -*- Mode: Go; indent-tabs-mode: t -*- | ||
2121 | 2 | |||
2122 | 3 | /* | ||
2123 | 4 | * Copyright (C) 2014-2015 Canonical Ltd | ||
2124 | 5 | * | ||
2125 | 6 | * This program is free software: you can redistribute it and/or modify | ||
2126 | 7 | * it under the terms of the GNU General Public License version 3 as | ||
2127 | 8 | * published by the Free Software Foundation. | ||
2128 | 9 | * | ||
2129 | 10 | * This program is distributed in the hope that it will be useful, | ||
2130 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
2131 | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
2132 | 13 | * GNU General Public License for more details. | ||
2133 | 14 | * | ||
2134 | 15 | * You should have received a copy of the GNU General Public License | ||
2135 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
2136 | 17 | * | ||
2137 | 18 | */ | ||
2138 | 19 | |||
2139 | 20 | package partition | ||
2140 | 21 | |||
2141 | 22 | import ( | ||
2142 | 23 | . "gopkg.in/check.v1" | ||
2143 | 24 | ) | ||
2144 | 25 | |||
2145 | 26 | func (s *PartitionTestSuite) TestHardwareSpec(c *C) { | ||
2146 | 27 | |||
2147 | 28 | hardwareSpecFile = makeHardwareYaml(c, "") | ||
2148 | 29 | hw, err := readHardwareSpec() | ||
2149 | 30 | c.Assert(err, IsNil) | ||
2150 | 31 | c.Assert(hw.Kernel, Equals, "assets/vmlinuz") | ||
2151 | 32 | c.Assert(hw.Initrd, Equals, "assets/initrd.img") | ||
2152 | 33 | c.Assert(hw.DtbDir, Equals, "assets/dtbs") | ||
2153 | 34 | c.Assert(hw.PartitionLayout, Equals, bootloaderSystemAB) | ||
2154 | 35 | c.Assert(hw.Bootloader, Equals, bootloaderNameUboot) | ||
2155 | 36 | } | ||
2156 | 37 | 0 | ||
2157 | === removed file 'partition/dirs.go' | |||
2158 | --- partition/dirs.go 2015-06-11 08:05:14 +0000 | |||
2159 | +++ partition/dirs.go 1970-01-01 00:00:00 +0000 | |||
2160 | @@ -1,41 +0,0 @@ | |||
2161 | 1 | // -*- Mode: Go; indent-tabs-mode: t -*- | ||
2162 | 2 | |||
2163 | 3 | /* | ||
2164 | 4 | * Copyright (C) 2014-2015 Canonical Ltd | ||
2165 | 5 | * | ||
2166 | 6 | * This program is free software: you can redistribute it and/or modify | ||
2167 | 7 | * it under the terms of the GNU General Public License version 3 as | ||
2168 | 8 | * published by the Free Software Foundation. | ||
2169 | 9 | * | ||
2170 | 10 | * This program is distributed in the hope that it will be useful, | ||
2171 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
2172 | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
2173 | 13 | * GNU General Public License for more details. | ||
2174 | 14 | * | ||
2175 | 15 | * You should have received a copy of the GNU General Public License | ||
2176 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
2177 | 17 | * | ||
2178 | 18 | */ | ||
2179 | 19 | |||
2180 | 20 | package partition | ||
2181 | 21 | |||
2182 | 22 | import ( | ||
2183 | 23 | "path/filepath" | ||
2184 | 24 | ) | ||
2185 | 25 | |||
2186 | 26 | // The full path to the cache directory, which is used as a | ||
2187 | 27 | // scratch pad, for downloading new images to and bind mounting the | ||
2188 | 28 | // rootfs. | ||
2189 | 29 | const cacheDirReal = "/writable/cache" | ||
2190 | 30 | |||
2191 | 31 | var ( | ||
2192 | 32 | // useful for overwriting in the tests | ||
2193 | 33 | cacheDir = cacheDirReal | ||
2194 | 34 | |||
2195 | 35 | // Directory to mount writable root filesystem below the cache | ||
2196 | 36 | // diretory. | ||
2197 | 37 | mountTargetReal = filepath.Join(cacheDir, "system") | ||
2198 | 38 | |||
2199 | 39 | // useful to override in tests | ||
2200 | 40 | mountTarget = mountTargetReal | ||
2201 | 41 | ) | ||
2202 | 42 | 0 | ||
2203 | === removed file 'partition/mount.go' | |||
2204 | --- partition/mount.go 2015-06-11 13:08:19 +0000 | |||
2205 | +++ partition/mount.go 1970-01-01 00:00:00 +0000 | |||
2206 | @@ -1,153 +0,0 @@ | |||
2207 | 1 | // -*- Mode: Go; indent-tabs-mode: t -*- | ||
2208 | 2 | |||
2209 | 3 | /* | ||
2210 | 4 | * Copyright (C) 2014-2015 Canonical Ltd | ||
2211 | 5 | * | ||
2212 | 6 | * This program is free software: you can redistribute it and/or modify | ||
2213 | 7 | * it under the terms of the GNU General Public License version 3 as | ||
2214 | 8 | * published by the Free Software Foundation. | ||
2215 | 9 | * | ||
2216 | 10 | * This program is distributed in the hope that it will be useful, | ||
2217 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
2218 | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
2219 | 13 | * GNU General Public License for more details. | ||
2220 | 14 | * | ||
2221 | 15 | * You should have received a copy of the GNU General Public License | ||
2222 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
2223 | 17 | * | ||
2224 | 18 | */ | ||
2225 | 19 | |||
2226 | 20 | package partition | ||
2227 | 21 | |||
2228 | 22 | import ( | ||
2229 | 23 | "fmt" | ||
2230 | 24 | "sort" | ||
2231 | 25 | ) | ||
2232 | 26 | |||
2233 | 27 | // MountOption represents how the partition should be mounted, currently | ||
2234 | 28 | // RO (read-only) and RW (read-write) are supported | ||
2235 | 29 | type MountOption int | ||
2236 | 30 | |||
2237 | 31 | const ( | ||
2238 | 32 | // RO mounts the partition read-only | ||
2239 | 33 | RO MountOption = iota | ||
2240 | 34 | // RW mounts the partition read-only | ||
2241 | 35 | RW | ||
2242 | 36 | ) | ||
2243 | 37 | |||
2244 | 38 | // mountEntry represents a mount this package has created. | ||
2245 | 39 | type mountEntry struct { | ||
2246 | 40 | source string | ||
2247 | 41 | target string | ||
2248 | 42 | |||
2249 | 43 | options string | ||
2250 | 44 | |||
2251 | 45 | // true if target refers to a bind mount. We could derive this | ||
2252 | 46 | // from options, but this field saves the effort. | ||
2253 | 47 | bindMount bool | ||
2254 | 48 | } | ||
2255 | 49 | |||
2256 | 50 | // mountEntryArray represents an array of mountEntry objects. | ||
2257 | 51 | type mountEntryArray []mountEntry | ||
2258 | 52 | |||
2259 | 53 | // current mounts that this package has created. | ||
2260 | 54 | var mounts mountEntryArray | ||
2261 | 55 | |||
2262 | 56 | // Len is part of the sort interface, required to allow sort to work | ||
2263 | 57 | // with an array of Mount objects. | ||
2264 | 58 | func (mounts mountEntryArray) Len() int { | ||
2265 | 59 | return len(mounts) | ||
2266 | 60 | } | ||
2267 | 61 | |||
2268 | 62 | // Less is part of the sort interface, required to allow sort to work | ||
2269 | 63 | // with an array of Mount objects. | ||
2270 | 64 | func (mounts mountEntryArray) Less(i, j int) bool { | ||
2271 | 65 | return mounts[i].target < mounts[j].target | ||
2272 | 66 | } | ||
2273 | 67 | |||
2274 | 68 | // Swap is part of the sort interface, required to allow sort to work | ||
2275 | 69 | // with an array of Mount objects. | ||
2276 | 70 | func (mounts mountEntryArray) Swap(i, j int) { | ||
2277 | 71 | mounts[i], mounts[j] = mounts[j], mounts[i] | ||
2278 | 72 | } | ||
2279 | 73 | |||
2280 | 74 | // removeMountByTarget removes the Mount specified by the target from | ||
2281 | 75 | // the global mounts array. | ||
2282 | 76 | func removeMountByTarget(mnts mountEntryArray, target string) (results mountEntryArray) { | ||
2283 | 77 | |||
2284 | 78 | for _, m := range mnts { | ||
2285 | 79 | if m.target != target { | ||
2286 | 80 | results = append(results, m) | ||
2287 | 81 | } | ||
2288 | 82 | } | ||
2289 | 83 | |||
2290 | 84 | return results | ||
2291 | 85 | } | ||
2292 | 86 | |||
2293 | 87 | // undoMounts unmounts all mounts this package has mounted optionally | ||
2294 | 88 | // only unmounting bind mounts and leaving all remaining mounts. | ||
2295 | 89 | func undoMounts(bindMountsOnly bool) error { | ||
2296 | 90 | |||
2297 | 91 | mountsCopy := make(mountEntryArray, len(mounts), cap(mounts)) | ||
2298 | 92 | copy(mountsCopy, mounts) | ||
2299 | 93 | |||
2300 | 94 | // reverse sort to ensure unmounts are handled in the correct | ||
2301 | 95 | // order. | ||
2302 | 96 | sort.Sort(sort.Reverse(mountsCopy)) | ||
2303 | 97 | |||
2304 | 98 | // Iterate backwards since we want a reverse-sorted list of | ||
2305 | 99 | // mounts to ensure we can unmount in order. | ||
2306 | 100 | for _, mount := range mountsCopy { | ||
2307 | 101 | if bindMountsOnly && !mount.bindMount { | ||
2308 | 102 | continue | ||
2309 | 103 | } | ||
2310 | 104 | |||
2311 | 105 | if err := unmountAndRemoveFromGlobalMountList(mount.target); err != nil { | ||
2312 | 106 | return err | ||
2313 | 107 | } | ||
2314 | 108 | } | ||
2315 | 109 | |||
2316 | 110 | return nil | ||
2317 | 111 | } | ||
2318 | 112 | |||
2319 | 113 | // FIXME: use syscall.Mount() here | ||
2320 | 114 | func mount(source, target, options string) (err error) { | ||
2321 | 115 | var args []string | ||
2322 | 116 | |||
2323 | 117 | args = append(args, "/bin/mount") | ||
2324 | 118 | if options != "" { | ||
2325 | 119 | args = append(args, fmt.Sprintf("-o%s", options)) | ||
2326 | 120 | } | ||
2327 | 121 | |||
2328 | 122 | args = append(args, source) | ||
2329 | 123 | args = append(args, target) | ||
2330 | 124 | |||
2331 | 125 | return runCommand(args...) | ||
2332 | 126 | } | ||
2333 | 127 | |||
2334 | 128 | // Mount the given directory and add it to the global mounts slice | ||
2335 | 129 | func mountAndAddToGlobalMountList(m mountEntry) (err error) { | ||
2336 | 130 | |||
2337 | 131 | err = mount(m.source, m.target, m.options) | ||
2338 | 132 | if err == nil { | ||
2339 | 133 | mounts = append(mounts, m) | ||
2340 | 134 | } | ||
2341 | 135 | |||
2342 | 136 | return err | ||
2343 | 137 | } | ||
2344 | 138 | |||
2345 | 139 | // Unmount the given directory and remove it from the global "mounts" slice | ||
2346 | 140 | func unmountAndRemoveFromGlobalMountList(target string) (err error) { | ||
2347 | 141 | err = runCommand("/bin/umount", target) | ||
2348 | 142 | if err != nil { | ||
2349 | 143 | return err | ||
2350 | 144 | |||
2351 | 145 | } | ||
2352 | 146 | |||
2353 | 147 | results := removeMountByTarget(mounts, target) | ||
2354 | 148 | |||
2355 | 149 | // Update global | ||
2356 | 150 | mounts = results | ||
2357 | 151 | |||
2358 | 152 | return nil | ||
2359 | 153 | } | ||
2360 | 154 | 0 | ||
2361 | === removed file 'partition/mount_test.go' | |||
2362 | --- partition/mount_test.go 2015-06-11 08:15:22 +0000 | |||
2363 | +++ partition/mount_test.go 1970-01-01 00:00:00 +0000 | |||
2364 | @@ -1,64 +0,0 @@ | |||
2365 | 1 | // -*- Mode: Go; indent-tabs-mode: t -*- | ||
2366 | 2 | |||
2367 | 3 | /* | ||
2368 | 4 | * Copyright (C) 2014-2015 Canonical Ltd | ||
2369 | 5 | * | ||
2370 | 6 | * This program is free software: you can redistribute it and/or modify | ||
2371 | 7 | * it under the terms of the GNU General Public License version 3 as | ||
2372 | 8 | * published by the Free Software Foundation. | ||
2373 | 9 | * | ||
2374 | 10 | * This program is distributed in the hope that it will be useful, | ||
2375 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
2376 | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
2377 | 13 | * GNU General Public License for more details. | ||
2378 | 14 | * | ||
2379 | 15 | * You should have received a copy of the GNU General Public License | ||
2380 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
2381 | 17 | * | ||
2382 | 18 | */ | ||
2383 | 19 | |||
2384 | 20 | package partition | ||
2385 | 21 | |||
2386 | 22 | import ( | ||
2387 | 23 | . "gopkg.in/check.v1" | ||
2388 | 24 | ) | ||
2389 | 25 | |||
2390 | 26 | func (s *PartitionTestSuite) TestMountEntryArray(c *C) { | ||
2391 | 27 | mea := mountEntryArray{} | ||
2392 | 28 | |||
2393 | 29 | c.Assert(mea.Len(), Equals, 0) | ||
2394 | 30 | |||
2395 | 31 | me := mountEntry{source: "/dev", | ||
2396 | 32 | target: "/dev", | ||
2397 | 33 | options: "bind", | ||
2398 | 34 | bindMount: true} | ||
2399 | 35 | |||
2400 | 36 | mea = append(mea, me) | ||
2401 | 37 | c.Assert(mea.Len(), Equals, 1) | ||
2402 | 38 | |||
2403 | 39 | me = mountEntry{source: "/foo", | ||
2404 | 40 | target: "/foo", | ||
2405 | 41 | options: "", | ||
2406 | 42 | bindMount: false} | ||
2407 | 43 | |||
2408 | 44 | mea = append(mea, me) | ||
2409 | 45 | c.Assert(mea.Len(), Equals, 2) | ||
2410 | 46 | |||
2411 | 47 | c.Assert(mea.Less(0, 1), Equals, true) | ||
2412 | 48 | c.Assert(mea.Less(1, 0), Equals, false) | ||
2413 | 49 | |||
2414 | 50 | mea.Swap(0, 1) | ||
2415 | 51 | c.Assert(mea.Less(0, 1), Equals, false) | ||
2416 | 52 | c.Assert(mea.Less(1, 0), Equals, true) | ||
2417 | 53 | |||
2418 | 54 | results := removeMountByTarget(mea, "invalid") | ||
2419 | 55 | |||
2420 | 56 | // No change expected | ||
2421 | 57 | c.Assert(results, DeepEquals, mea) | ||
2422 | 58 | |||
2423 | 59 | results = removeMountByTarget(mea, "/dev") | ||
2424 | 60 | |||
2425 | 61 | c.Assert(len(results), Equals, 1) | ||
2426 | 62 | c.Assert(results[0], Equals, mountEntry{source: "/foo", | ||
2427 | 63 | target: "/foo", options: "", bindMount: false}) | ||
2428 | 64 | } | ||
2429 | 65 | 0 | ||
2430 | === removed file 'partition/partition.go' | |||
2431 | --- partition/partition.go 2015-07-15 07:53:43 +0000 | |||
2432 | +++ partition/partition.go 1970-01-01 00:00:00 +0000 | |||
2433 | @@ -1,622 +0,0 @@ | |||
2434 | 1 | // -*- Mode: Go; indent-tabs-mode: t -*- | ||
2435 | 2 | |||
2436 | 3 | /* | ||
2437 | 4 | * Copyright (C) 2014-2015 Canonical Ltd | ||
2438 | 5 | * | ||
2439 | 6 | * This program is free software: you can redistribute it and/or modify | ||
2440 | 7 | * it under the terms of the GNU General Public License version 3 as | ||
2441 | 8 | * published by the Free Software Foundation. | ||
2442 | 9 | * | ||
2443 | 10 | * This program is distributed in the hope that it will be useful, | ||
2444 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
2445 | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
2446 | 13 | * GNU General Public License for more details. | ||
2447 | 14 | * | ||
2448 | 15 | * You should have received a copy of the GNU General Public License | ||
2449 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
2450 | 17 | * | ||
2451 | 18 | */ | ||
2452 | 19 | |||
2453 | 20 | // Package partition manipulate snappy disk partitions | ||
2454 | 21 | package partition | ||
2455 | 22 | |||
2456 | 23 | import ( | ||
2457 | 24 | "errors" | ||
2458 | 25 | "fmt" | ||
2459 | 26 | "os" | ||
2460 | 27 | "os/signal" | ||
2461 | 28 | "path/filepath" | ||
2462 | 29 | "regexp" | ||
2463 | 30 | "strings" | ||
2464 | 31 | "sync" | ||
2465 | 32 | "syscall" | ||
2466 | 33 | |||
2467 | 34 | "launchpad.net/snappy/logger" | ||
2468 | 35 | ) | ||
2469 | 36 | |||
2470 | 37 | const ( | ||
2471 | 38 | // Name of writable user data partition label as created by | ||
2472 | 39 | // ubuntu-device-flash(1). | ||
2473 | 40 | writablePartitionLabel = "writable" | ||
2474 | 41 | |||
2475 | 42 | // Name of primary root filesystem partition label as created by | ||
2476 | 43 | // ubuntu-device-flash(1). | ||
2477 | 44 | rootfsAlabel = "system-a" | ||
2478 | 45 | |||
2479 | 46 | // Name of primary root filesystem partition label as created by | ||
2480 | 47 | // ubuntu-device-flash(1). Note that this partition will | ||
2481 | 48 | // only be present if this is an A/B upgrade system. | ||
2482 | 49 | rootfsBlabel = "system-b" | ||
2483 | 50 | |||
2484 | 51 | // name of boot partition label as created by ubuntu-device-flash(1). | ||
2485 | 52 | bootPartitionLabel = "system-boot" | ||
2486 | 53 | |||
2487 | 54 | // File creation mode used when any directories are created | ||
2488 | 55 | dirMode = 0750 | ||
2489 | 56 | ) | ||
2490 | 57 | |||
2491 | 58 | var ( | ||
2492 | 59 | // ErrBootloader is returned if the bootloader can not be determined | ||
2493 | 60 | ErrBootloader = errors.New("Unable to determine bootloader") | ||
2494 | 61 | |||
2495 | 62 | // ErrPartitionDetection is returned if the partition type can not | ||
2496 | 63 | // be detected | ||
2497 | 64 | ErrPartitionDetection = errors.New("Failed to detect system type") | ||
2498 | 65 | |||
2499 | 66 | // ErrNoDualPartition is returned if you try to use a dual | ||
2500 | 67 | // partition feature on a single partition | ||
2501 | 68 | ErrNoDualPartition = errors.New("No dual partition") | ||
2502 | 69 | ) | ||
2503 | 70 | |||
2504 | 71 | // Interface provides the interface to interact with a partition | ||
2505 | 72 | type Interface interface { | ||
2506 | 73 | ToggleNextBoot() error | ||
2507 | 74 | |||
2508 | 75 | MarkBootSuccessful() error | ||
2509 | 76 | // FIXME: could we make SyncBootloaderFiles part of ToogleBootloader | ||
2510 | 77 | // to expose even less implementation details? | ||
2511 | 78 | SyncBootloaderFiles(bootAssets map[string]string) error | ||
2512 | 79 | IsNextBootOther() bool | ||
2513 | 80 | |||
2514 | 81 | // run the function f with the otherRoot mounted | ||
2515 | 82 | RunWithOther(rw MountOption, f func(otherRoot string) (err error)) (err error) | ||
2516 | 83 | } | ||
2517 | 84 | |||
2518 | 85 | // Partition is the type to interact with the partition | ||
2519 | 86 | type Partition struct { | ||
2520 | 87 | // all partitions | ||
2521 | 88 | partitions []blockDevice | ||
2522 | 89 | |||
2523 | 90 | // just root partitions | ||
2524 | 91 | roots []string | ||
2525 | 92 | } | ||
2526 | 93 | |||
2527 | 94 | type blockDevice struct { | ||
2528 | 95 | // label for partition | ||
2529 | 96 | name string | ||
2530 | 97 | |||
2531 | 98 | // the last char of the partition label | ||
2532 | 99 | shortName string | ||
2533 | 100 | |||
2534 | 101 | // full path to device on which partition exists | ||
2535 | 102 | // (for example "/dev/sda3") | ||
2536 | 103 | device string | ||
2537 | 104 | |||
2538 | 105 | // full path to disk device (for example "/dev/sda") | ||
2539 | 106 | parentName string | ||
2540 | 107 | |||
2541 | 108 | // mountpoint (or nil if not mounted) | ||
2542 | 109 | mountpoint string | ||
2543 | 110 | } | ||
2544 | 111 | |||
2545 | 112 | var once sync.Once | ||
2546 | 113 | |||
2547 | 114 | func init() { | ||
2548 | 115 | once.Do(setupSignalHandler) | ||
2549 | 116 | } | ||
2550 | 117 | |||
2551 | 118 | func signalHandler(sig os.Signal) { | ||
2552 | 119 | err := undoMounts(false) | ||
2553 | 120 | if err != nil { | ||
2554 | 121 | logger.Noticef("Failed to unmount: %v", err) | ||
2555 | 122 | } | ||
2556 | 123 | } | ||
2557 | 124 | |||
2558 | 125 | func setupSignalHandler() { | ||
2559 | 126 | ch := make(chan os.Signal, 1) | ||
2560 | 127 | |||
2561 | 128 | // add the signals we care about | ||
2562 | 129 | signal.Notify(ch, os.Interrupt) | ||
2563 | 130 | signal.Notify(ch, syscall.SIGTERM) | ||
2564 | 131 | |||
2565 | 132 | go func() { | ||
2566 | 133 | // block waiting for a signal | ||
2567 | 134 | sig := <-ch | ||
2568 | 135 | |||
2569 | 136 | // handle it | ||
2570 | 137 | signalHandler(sig) | ||
2571 | 138 | os.Exit(1) | ||
2572 | 139 | }() | ||
2573 | 140 | } | ||
2574 | 141 | |||
2575 | 142 | // Returns a list of root filesystem partition labels | ||
2576 | 143 | func rootPartitionLabels() []string { | ||
2577 | 144 | return []string{rootfsAlabel, rootfsBlabel} | ||
2578 | 145 | } | ||
2579 | 146 | |||
2580 | 147 | // Returns a list of all recognised partition labels | ||
2581 | 148 | func allPartitionLabels() []string { | ||
2582 | 149 | var labels []string | ||
2583 | 150 | |||
2584 | 151 | labels = rootPartitionLabels() | ||
2585 | 152 | labels = append(labels, bootPartitionLabel) | ||
2586 | 153 | labels = append(labels, writablePartitionLabel) | ||
2587 | 154 | |||
2588 | 155 | return labels | ||
2589 | 156 | } | ||
2590 | 157 | |||
2591 | 158 | var runLsblk = func() (out []string, err error) { | ||
2592 | 159 | output, err := runCommandWithStdout( | ||
2593 | 160 | "/bin/lsblk", | ||
2594 | 161 | "--ascii", | ||
2595 | 162 | "--output=NAME,LABEL,PKNAME,MOUNTPOINT", | ||
2596 | 163 | "--pairs") | ||
2597 | 164 | if err != nil { | ||
2598 | 165 | return out, err | ||
2599 | 166 | } | ||
2600 | 167 | |||
2601 | 168 | return strings.Split(output, "\n"), nil | ||
2602 | 169 | } | ||
2603 | 170 | |||
2604 | 171 | // Determine details of the recognised disk partitions | ||
2605 | 172 | // available on the system via lsblk | ||
2606 | 173 | func loadPartitionDetails() (partitions []blockDevice, err error) { | ||
2607 | 174 | recognised := allPartitionLabels() | ||
2608 | 175 | |||
2609 | 176 | lines, err := runLsblk() | ||
2610 | 177 | if err != nil { | ||
2611 | 178 | return partitions, err | ||
2612 | 179 | } | ||
2613 | 180 | pattern := regexp.MustCompile(`(?:[^\s"]|"(?:[^"])*")+`) | ||
2614 | 181 | |||
2615 | 182 | for _, line := range lines { | ||
2616 | 183 | fields := make(map[string]string) | ||
2617 | 184 | |||
2618 | 185 | // split the line into 'NAME="quoted value"' fields | ||
2619 | 186 | matches := pattern.FindAllString(line, -1) | ||
2620 | 187 | |||
2621 | 188 | for _, match := range matches { | ||
2622 | 189 | tmp := strings.Split(match, "=") | ||
2623 | 190 | name := tmp[0] | ||
2624 | 191 | |||
2625 | 192 | // remove quotes | ||
2626 | 193 | value := strings.Trim(tmp[1], "\"") | ||
2627 | 194 | |||
2628 | 195 | // store | ||
2629 | 196 | fields[name] = value | ||
2630 | 197 | } | ||
2631 | 198 | |||
2632 | 199 | // Look for expected partition labels | ||
2633 | 200 | name, ok := fields["LABEL"] | ||
2634 | 201 | if !ok { | ||
2635 | 202 | continue | ||
2636 | 203 | } | ||
2637 | 204 | |||
2638 | 205 | if name == "" || name == "\"\"" { | ||
2639 | 206 | continue | ||
2640 | 207 | } | ||
2641 | 208 | |||
2642 | 209 | pos := stringInSlice(recognised, name) | ||
2643 | 210 | if pos < 0 { | ||
2644 | 211 | // ignore unrecognised partitions | ||
2645 | 212 | continue | ||
2646 | 213 | } | ||
2647 | 214 | |||
2648 | 215 | // reconstruct full path to disk partition device | ||
2649 | 216 | device := fmt.Sprintf("/dev/%s", fields["NAME"]) | ||
2650 | 217 | |||
2651 | 218 | // FIXME: we should have a way to mock the "/dev" dir | ||
2652 | 219 | // or we skip this test lsblk never returns non-existing | ||
2653 | 220 | // devices | ||
2654 | 221 | /* | ||
2655 | 222 | if err := FileExists(device); err != nil { | ||
2656 | 223 | continue | ||
2657 | 224 | } | ||
2658 | 225 | */ | ||
2659 | 226 | // reconstruct full path to entire disk device | ||
2660 | 227 | disk := fmt.Sprintf("/dev/%s", fields["PKNAME"]) | ||
2661 | 228 | |||
2662 | 229 | // FIXME: we should have a way to mock the "/dev" dir | ||
2663 | 230 | // or we skip this test lsblk never returns non-existing | ||
2664 | 231 | // files | ||
2665 | 232 | /* | ||
2666 | 233 | if err := FileExists(disk); err != nil { | ||
2667 | 234 | continue | ||
2668 | 235 | } | ||
2669 | 236 | */ | ||
2670 | 237 | shortName := string(name[len(name)-1]) | ||
2671 | 238 | bd := blockDevice{ | ||
2672 | 239 | name: fields["LABEL"], | ||
2673 | 240 | shortName: shortName, | ||
2674 | 241 | device: device, | ||
2675 | 242 | mountpoint: fields["MOUNTPOINT"], | ||
2676 | 243 | parentName: disk, | ||
2677 | 244 | } | ||
2678 | 245 | |||
2679 | 246 | partitions = append(partitions, bd) | ||
2680 | 247 | } | ||
2681 | 248 | |||
2682 | 249 | return partitions, nil | ||
2683 | 250 | } | ||
2684 | 251 | |||
2685 | 252 | // New creates a new partition type | ||
2686 | 253 | func New() *Partition { | ||
2687 | 254 | p := new(Partition) | ||
2688 | 255 | |||
2689 | 256 | p.getPartitionDetails() | ||
2690 | 257 | |||
2691 | 258 | return p | ||
2692 | 259 | } | ||
2693 | 260 | |||
2694 | 261 | // RunWithOther mount the other rootfs partition, execute the | ||
2695 | 262 | // specified function and unmount "other" before returning. If "other" | ||
2696 | 263 | // is mounted read-write, /proc, /sys and /dev will also be | ||
2697 | 264 | // bind-mounted at the time the specified function is called. | ||
2698 | 265 | func (p *Partition) RunWithOther(option MountOption, f func(otherRoot string) (err error)) (err error) { | ||
2699 | 266 | dual := p.dualRootPartitions() | ||
2700 | 267 | |||
2701 | 268 | if !dual { | ||
2702 | 269 | return ErrNoDualPartition | ||
2703 | 270 | } | ||
2704 | 271 | |||
2705 | 272 | if option == RW { | ||
2706 | 273 | if err := p.remountOther(RW); err != nil { | ||
2707 | 274 | return err | ||
2708 | 275 | } | ||
2709 | 276 | |||
2710 | 277 | defer func() { | ||
2711 | 278 | // we can't reuse err here as this will override | ||
2712 | 279 | // the error value we got from calling "f()" | ||
2713 | 280 | derr := p.remountOther(RO) | ||
2714 | 281 | if derr != nil && err == nil { | ||
2715 | 282 | err = derr | ||
2716 | 283 | } | ||
2717 | 284 | }() | ||
2718 | 285 | |||
2719 | 286 | if err := p.bindmountRequiredFilesystems(); err != nil { | ||
2720 | 287 | return err | ||
2721 | 288 | } | ||
2722 | 289 | |||
2723 | 290 | defer func() { | ||
2724 | 291 | // we can't reuse err here as this will override | ||
2725 | 292 | // the error value we got from calling "f()" | ||
2726 | 293 | derr := p.unmountRequiredFilesystems() | ||
2727 | 294 | if derr != nil && err == nil { | ||
2728 | 295 | err = derr | ||
2729 | 296 | } | ||
2730 | 297 | }() | ||
2731 | 298 | } | ||
2732 | 299 | |||
2733 | 300 | err = f(mountTarget) | ||
2734 | 301 | return err | ||
2735 | 302 | } | ||
2736 | 303 | |||
2737 | 304 | // SyncBootloaderFiles syncs the bootloader files | ||
2738 | 305 | // | ||
2739 | 306 | // We need this code solve the following scenario: | ||
2740 | 307 | // | ||
2741 | 308 | // 1. start with: /boot/a/k1, /boot/b/k1 | ||
2742 | 309 | // 2. upgrade with new kernel k2: /boot/a/k1, /boot/b/k2 | ||
2743 | 310 | // 3. reboot into b, running with /boot/b/k2 | ||
2744 | 311 | // 4. new update without a changed kernel, system-a updated | ||
2745 | 312 | // | ||
2746 | 313 | // 6. reboot to system-a with /boot/a/k1 (WRONG!) | ||
2747 | 314 | // But it should be system-a with /boot/a/k2 it was just not part of | ||
2748 | 315 | // the s-i delta as we already got it. So as step (5) above we do the | ||
2749 | 316 | // SyncBootloaderFiles that copies /boot/b/k2 -> /boot/a/ | ||
2750 | 317 | // (and that is ok because we know /boot/b/k2 works) | ||
2751 | 318 | // | ||
2752 | 319 | func (p *Partition) SyncBootloaderFiles(bootAssets map[string]string) (err error) { | ||
2753 | 320 | bootloader, err := bootloader(p) | ||
2754 | 321 | if err != nil { | ||
2755 | 322 | return err | ||
2756 | 323 | } | ||
2757 | 324 | |||
2758 | 325 | return bootloader.SyncBootFiles(bootAssets) | ||
2759 | 326 | } | ||
2760 | 327 | |||
2761 | 328 | // ToggleNextBoot toggles the roofs that should be used on the next boot | ||
2762 | 329 | func (p *Partition) ToggleNextBoot() (err error) { | ||
2763 | 330 | if p.dualRootPartitions() { | ||
2764 | 331 | return p.toggleBootloaderRootfs() | ||
2765 | 332 | } | ||
2766 | 333 | return err | ||
2767 | 334 | } | ||
2768 | 335 | |||
2769 | 336 | // MarkBootSuccessful marks the boot as successful | ||
2770 | 337 | func (p *Partition) MarkBootSuccessful() (err error) { | ||
2771 | 338 | bootloader, err := bootloader(p) | ||
2772 | 339 | if err != nil { | ||
2773 | 340 | return err | ||
2774 | 341 | } | ||
2775 | 342 | |||
2776 | 343 | currentRootfs := p.rootPartition().shortName | ||
2777 | 344 | return bootloader.MarkCurrentBootSuccessful(currentRootfs) | ||
2778 | 345 | } | ||
2779 | 346 | |||
2780 | 347 | // IsNextBootOther return true if the next boot will use the other rootfs | ||
2781 | 348 | // partition. | ||
2782 | 349 | func (p *Partition) IsNextBootOther() bool { | ||
2783 | 350 | bootloader, err := bootloader(p) | ||
2784 | 351 | if err != nil { | ||
2785 | 352 | return false | ||
2786 | 353 | } | ||
2787 | 354 | |||
2788 | 355 | value, err := bootloader.GetBootVar(bootloaderBootmodeVar) | ||
2789 | 356 | if err != nil { | ||
2790 | 357 | return false | ||
2791 | 358 | } | ||
2792 | 359 | |||
2793 | 360 | if value != bootloaderBootmodeTry { | ||
2794 | 361 | return false | ||
2795 | 362 | } | ||
2796 | 363 | |||
2797 | 364 | fsname, err := bootloader.GetNextBootRootFSName() | ||
2798 | 365 | if err != nil { | ||
2799 | 366 | return false | ||
2800 | 367 | } | ||
2801 | 368 | |||
2802 | 369 | otherRootfs := p.otherRootPartition().shortName | ||
2803 | 370 | if fsname == otherRootfs { | ||
2804 | 371 | return true | ||
2805 | 372 | } | ||
2806 | 373 | |||
2807 | 374 | return false | ||
2808 | 375 | } | ||
2809 | 376 | |||
2810 | 377 | func (p *Partition) getPartitionDetails() (err error) { | ||
2811 | 378 | p.partitions, err = loadPartitionDetails() | ||
2812 | 379 | if err != nil { | ||
2813 | 380 | return err | ||
2814 | 381 | } | ||
2815 | 382 | |||
2816 | 383 | if !p.dualRootPartitions() && !p.singleRootPartition() { | ||
2817 | 384 | return ErrPartitionDetection | ||
2818 | 385 | } | ||
2819 | 386 | |||
2820 | 387 | if p.dualRootPartitions() { | ||
2821 | 388 | // XXX: this will soon be handled automatically at boot by | ||
2822 | 389 | // initramfs-tools-ubuntu-core. | ||
2823 | 390 | return p.ensureOtherMountedRO() | ||
2824 | 391 | } | ||
2825 | 392 | |||
2826 | 393 | return err | ||
2827 | 394 | } | ||
2828 | 395 | |||
2829 | 396 | // Return array of blockDevices representing available root partitions | ||
2830 | 397 | func (p *Partition) rootPartitions() (roots []blockDevice) { | ||
2831 | 398 | for _, part := range p.partitions { | ||
2832 | 399 | pos := stringInSlice(rootPartitionLabels(), part.name) | ||
2833 | 400 | if pos >= 0 { | ||
2834 | 401 | roots = append(roots, part) | ||
2835 | 402 | } | ||
2836 | 403 | } | ||
2837 | 404 | |||
2838 | 405 | return roots | ||
2839 | 406 | } | ||
2840 | 407 | |||
2841 | 408 | // Return true if system has dual root partitions configured in the | ||
2842 | 409 | // expected manner for a snappy system. | ||
2843 | 410 | func (p *Partition) dualRootPartitions() bool { | ||
2844 | 411 | return len(p.rootPartitions()) == 2 | ||
2845 | 412 | } | ||
2846 | 413 | |||
2847 | 414 | // Return true if system has a single root partition configured in the | ||
2848 | 415 | // expected manner for a snappy system. | ||
2849 | 416 | func (p *Partition) singleRootPartition() bool { | ||
2850 | 417 | return len(p.rootPartitions()) == 1 | ||
2851 | 418 | } | ||
2852 | 419 | |||
2853 | 420 | // Return pointer to blockDevice representing writable partition | ||
2854 | 421 | func (p *Partition) writablePartition() (result *blockDevice) { | ||
2855 | 422 | for _, part := range p.partitions { | ||
2856 | 423 | if part.name == writablePartitionLabel { | ||
2857 | 424 | return &part | ||
2858 | 425 | } | ||
2859 | 426 | } | ||
2860 | 427 | |||
2861 | 428 | return nil | ||
2862 | 429 | } | ||
2863 | 430 | |||
2864 | 431 | // Return pointer to blockDevice representing boot partition (if any) | ||
2865 | 432 | func (p *Partition) bootPartition() (result *blockDevice) { | ||
2866 | 433 | for _, part := range p.partitions { | ||
2867 | 434 | if part.name == bootPartitionLabel { | ||
2868 | 435 | return &part | ||
2869 | 436 | } | ||
2870 | 437 | } | ||
2871 | 438 | |||
2872 | 439 | return nil | ||
2873 | 440 | } | ||
2874 | 441 | |||
2875 | 442 | // Return pointer to blockDevice representing currently mounted root | ||
2876 | 443 | // filesystem | ||
2877 | 444 | func (p *Partition) rootPartition() (result *blockDevice) { | ||
2878 | 445 | for _, part := range p.rootPartitions() { | ||
2879 | 446 | if part.mountpoint == "/" { | ||
2880 | 447 | return &part | ||
2881 | 448 | } | ||
2882 | 449 | } | ||
2883 | 450 | |||
2884 | 451 | return nil | ||
2885 | 452 | } | ||
2886 | 453 | |||
2887 | 454 | // Return pointer to blockDevice representing the "other" root | ||
2888 | 455 | // filesystem (which is not currently mounted) | ||
2889 | 456 | func (p *Partition) otherRootPartition() (result *blockDevice) { | ||
2890 | 457 | for _, part := range p.rootPartitions() { | ||
2891 | 458 | if part.mountpoint != "/" { | ||
2892 | 459 | return &part | ||
2893 | 460 | } | ||
2894 | 461 | } | ||
2895 | 462 | |||
2896 | 463 | return nil | ||
2897 | 464 | } | ||
2898 | 465 | |||
2899 | 466 | // Mount the "other" root filesystem | ||
2900 | 467 | func (p *Partition) mountOtherRootfs(readOnly bool) (err error) { | ||
2901 | 468 | var other *blockDevice | ||
2902 | 469 | |||
2903 | 470 | if err := os.MkdirAll(mountTarget, dirMode); err != nil { | ||
2904 | 471 | return err | ||
2905 | 472 | } | ||
2906 | 473 | |||
2907 | 474 | other = p.otherRootPartition() | ||
2908 | 475 | |||
2909 | 476 | m := mountEntry{source: other.device, target: mountTarget} | ||
2910 | 477 | |||
2911 | 478 | if readOnly { | ||
2912 | 479 | m.options = "ro" | ||
2913 | 480 | err = mountAndAddToGlobalMountList(m) | ||
2914 | 481 | } else { | ||
2915 | 482 | err = fsck(m.source) | ||
2916 | 483 | if err != nil { | ||
2917 | 484 | return err | ||
2918 | 485 | } | ||
2919 | 486 | err = mountAndAddToGlobalMountList(m) | ||
2920 | 487 | } | ||
2921 | 488 | |||
2922 | 489 | return err | ||
2923 | 490 | } | ||
2924 | 491 | |||
2925 | 492 | // Create a read-only bindmount of the currently-mounted rootfs at the | ||
2926 | 493 | // specified mountpoint location (which must already exist). | ||
2927 | 494 | func (p *Partition) bindmountThisRootfsRO(target string) (err error) { | ||
2928 | 495 | return mountAndAddToGlobalMountList(mountEntry{source: "/", | ||
2929 | 496 | target: target, | ||
2930 | 497 | options: "bind,ro", | ||
2931 | 498 | bindMount: true}) | ||
2932 | 499 | } | ||
2933 | 500 | |||
2934 | 501 | // Ensure the other partition is mounted read-only. | ||
2935 | 502 | func (p *Partition) ensureOtherMountedRO() (err error) { | ||
2936 | 503 | if err = runCommand("/bin/mountpoint", mountTarget); err == nil { | ||
2937 | 504 | // already mounted | ||
2938 | 505 | return err | ||
2939 | 506 | } | ||
2940 | 507 | |||
2941 | 508 | return p.mountOtherRootfs(true) | ||
2942 | 509 | } | ||
2943 | 510 | |||
2944 | 511 | // Remount the already-mounted other partition. Whether the mount | ||
2945 | 512 | // should become writable is specified by the writable argument. | ||
2946 | 513 | // | ||
2947 | 514 | // XXX: Note that in the case where writable=true, this isn't a simple | ||
2948 | 515 | // toggle - if the partition is already mounted read-only, it needs to | ||
2949 | 516 | // be unmounted, fsck(8)'d, then (re-)mounted read-write. | ||
2950 | 517 | func (p *Partition) remountOther(option MountOption) (err error) { | ||
2951 | 518 | other := p.otherRootPartition() | ||
2952 | 519 | |||
2953 | 520 | if option == RW { | ||
2954 | 521 | // r/o -> r/w: initially r/o, so no need to fsck before | ||
2955 | 522 | // switching to r/w. | ||
2956 | 523 | err = p.unmountOtherRootfs() | ||
2957 | 524 | if err != nil { | ||
2958 | 525 | return err | ||
2959 | 526 | } | ||
2960 | 527 | |||
2961 | 528 | err = fsck(other.device) | ||
2962 | 529 | if err != nil { | ||
2963 | 530 | return err | ||
2964 | 531 | } | ||
2965 | 532 | |||
2966 | 533 | return mountAndAddToGlobalMountList(mountEntry{ | ||
2967 | 534 | source: other.device, | ||
2968 | 535 | target: mountTarget}) | ||
2969 | 536 | } | ||
2970 | 537 | // r/w -> r/o: no fsck required. | ||
2971 | 538 | return mount(other.device, mountTarget, "remount,ro") | ||
2972 | 539 | } | ||
2973 | 540 | |||
2974 | 541 | func (p *Partition) unmountOtherRootfs() (err error) { | ||
2975 | 542 | return unmountAndRemoveFromGlobalMountList(mountTarget) | ||
2976 | 543 | } | ||
2977 | 544 | |||
2978 | 545 | // The bootloader requires a few filesystems to be mounted when | ||
2979 | 546 | // run from within a chroot. | ||
2980 | 547 | func (p *Partition) bindmountRequiredFilesystems() (err error) { | ||
2981 | 548 | |||
2982 | 549 | // we always requires these | ||
2983 | 550 | requiredChrootMounts := []string{"/dev", "/proc", "/sys"} | ||
2984 | 551 | |||
2985 | 552 | // if there is a boot partition we also bind-mount it | ||
2986 | 553 | boot := p.bootPartition() | ||
2987 | 554 | if boot != nil && boot.mountpoint != "" { | ||
2988 | 555 | requiredChrootMounts = append(requiredChrootMounts, boot.mountpoint) | ||
2989 | 556 | } | ||
2990 | 557 | |||
2991 | 558 | for _, fs := range requiredChrootMounts { | ||
2992 | 559 | target := filepath.Join(mountTarget, fs) | ||
2993 | 560 | |||
2994 | 561 | err := mountAndAddToGlobalMountList(mountEntry{source: fs, | ||
2995 | 562 | target: target, | ||
2996 | 563 | options: "bind", | ||
2997 | 564 | bindMount: true}) | ||
2998 | 565 | if err != nil { | ||
2999 | 566 | return err | ||
3000 | 567 | } | ||
3001 | 568 | } | ||
3002 | 569 | |||
3003 | 570 | // Grub also requires access to both rootfs's when run from | ||
3004 | 571 | // within a chroot (to allow it to create menu entries for | ||
3005 | 572 | // both), so bindmount the real rootfs. | ||
3006 | 573 | targetInChroot := filepath.Join(mountTarget, mountTarget) | ||
3007 | 574 | |||
3008 | 575 | // FIXME: we should really remove this after the unmount | ||
3009 | 576 | |||
3010 | 577 | if err = os.MkdirAll(targetInChroot, dirMode); err != nil { | ||
3011 | 578 | return err | ||
3012 | 579 | } | ||
3013 | 580 | |||
3014 | 581 | return p.bindmountThisRootfsRO(targetInChroot) | ||
3015 | 582 | } | ||
3016 | 583 | |||
3017 | 584 | // Undo the effects of BindmountRequiredFilesystems() | ||
3018 | 585 | func (p *Partition) unmountRequiredFilesystems() (err error) { | ||
3019 | 586 | if err = undoMounts(true); err != nil { | ||
3020 | 587 | return err | ||
3021 | 588 | } | ||
3022 | 589 | |||
3023 | 590 | return nil | ||
3024 | 591 | } | ||
3025 | 592 | |||
3026 | 593 | func (p *Partition) toggleBootloaderRootfs() (err error) { | ||
3027 | 594 | |||
3028 | 595 | if !p.dualRootPartitions() { | ||
3029 | 596 | return errors.New("System is not dual root") | ||
3030 | 597 | } | ||
3031 | 598 | |||
3032 | 599 | bootloader, err := bootloader(p) | ||
3033 | 600 | if err != nil { | ||
3034 | 601 | return err | ||
3035 | 602 | } | ||
3036 | 603 | |||
3037 | 604 | // ensure we have updated kernels etc | ||
3038 | 605 | if err := bootloader.HandleAssets(); err != nil { | ||
3039 | 606 | return err | ||
3040 | 607 | } | ||
3041 | 608 | |||
3042 | 609 | otherRootfs := p.otherRootPartition().shortName | ||
3043 | 610 | return bootloader.ToggleRootFS(otherRootfs) | ||
3044 | 611 | } | ||
3045 | 612 | |||
3046 | 613 | // BootloaderDir returns the full path to the (mounted and writable) | ||
3047 | 614 | // bootloader-specific boot directory. | ||
3048 | 615 | func (p *Partition) BootloaderDir() string { | ||
3049 | 616 | bootloader, err := bootloader(p) | ||
3050 | 617 | if err != nil { | ||
3051 | 618 | return "" | ||
3052 | 619 | } | ||
3053 | 620 | |||
3054 | 621 | return bootloader.BootDir() | ||
3055 | 622 | } | ||
3056 | 623 | 0 | ||
3057 | === removed file 'partition/utils.go' | |||
3058 | --- partition/utils.go 2015-07-08 10:21:02 +0000 | |||
3059 | +++ partition/utils.go 1970-01-01 00:00:00 +0000 | |||
3060 | @@ -1,82 +0,0 @@ | |||
3061 | 1 | // -*- Mode: Go; indent-tabs-mode: t -*- | ||
3062 | 2 | |||
3063 | 3 | /* | ||
3064 | 4 | * Copyright (C) 2014-2015 Canonical Ltd | ||
3065 | 5 | * | ||
3066 | 6 | * This program is free software: you can redistribute it and/or modify | ||
3067 | 7 | * it under the terms of the GNU General Public License version 3 as | ||
3068 | 8 | * published by the Free Software Foundation. | ||
3069 | 9 | * | ||
3070 | 10 | * This program is distributed in the hope that it will be useful, | ||
3071 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
3072 | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
3073 | 13 | * GNU General Public License for more details. | ||
3074 | 14 | * | ||
3075 | 15 | * You should have received a copy of the GNU General Public License | ||
3076 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
3077 | 17 | * | ||
3078 | 18 | */ | ||
3079 | 19 | |||
3080 | 20 | package partition | ||
3081 | 21 | |||
3082 | 22 | import ( | ||
3083 | 23 | "errors" | ||
3084 | 24 | "fmt" | ||
3085 | 25 | "os/exec" | ||
3086 | 26 | "strings" | ||
3087 | 27 | ) | ||
3088 | 28 | |||
3089 | 29 | // FIXME: would it make sense to differenciate between launch errors and | ||
3090 | 30 | // exit code? (i.e. something like (returnCode, error) ?) | ||
3091 | 31 | func runCommandImpl(args ...string) (err error) { | ||
3092 | 32 | if len(args) == 0 { | ||
3093 | 33 | return errors.New("no command specified") | ||
3094 | 34 | } | ||
3095 | 35 | |||
3096 | 36 | if out, err := exec.Command(args[0], args[1:]...).CombinedOutput(); err != nil { | ||
3097 | 37 | cmdline := strings.Join(args, " ") | ||
3098 | 38 | return fmt.Errorf("Failed to run command '%s': %s (%s)", | ||
3099 | 39 | cmdline, out, err) | ||
3100 | 40 | } | ||
3101 | 41 | return nil | ||
3102 | 42 | } | ||
3103 | 43 | |||
3104 | 44 | // Run the command specified by args | ||
3105 | 45 | // This is a var instead of a function to making mocking in the tests easier | ||
3106 | 46 | var runCommand = runCommandImpl | ||
3107 | 47 | |||
3108 | 48 | // Run command specified by args and return the output | ||
3109 | 49 | func runCommandWithStdoutImpl(args ...string) (output string, err error) { | ||
3110 | 50 | if len(args) == 0 { | ||
3111 | 51 | return "", errors.New("no command specified") | ||
3112 | 52 | } | ||
3113 | 53 | |||
3114 | 54 | bytes, err := exec.Command(args[0], args[1:]...).Output() | ||
3115 | 55 | if err != nil { | ||
3116 | 56 | return "", err | ||
3117 | 57 | } | ||
3118 | 58 | |||
3119 | 59 | return string(bytes), err | ||
3120 | 60 | } | ||
3121 | 61 | |||
3122 | 62 | // This is a var instead of a function to making mocking in the tests easier | ||
3123 | 63 | var runCommandWithStdout = runCommandWithStdoutImpl | ||
3124 | 64 | |||
3125 | 65 | // Run fsck(8) on specified device. | ||
3126 | 66 | func fsck(device string) (err error) { | ||
3127 | 67 | return runCommand( | ||
3128 | 68 | "/sbin/fsck", | ||
3129 | 69 | "-M", // Paranoia - don't fsck if already mounted | ||
3130 | 70 | "-av", device) | ||
3131 | 71 | } | ||
3132 | 72 | |||
3133 | 73 | // Returns the position of the string in the given slice or -1 if its not found | ||
3134 | 74 | func stringInSlice(slice []string, value string) int { | ||
3135 | 75 | for i, s := range slice { | ||
3136 | 76 | if s == value { | ||
3137 | 77 | return i | ||
3138 | 78 | } | ||
3139 | 79 | } | ||
3140 | 80 | |||
3141 | 81 | return -1 | ||
3142 | 82 | } | ||
3143 | 83 | 0 | ||
3144 | === removed file 'partition/utils_test.go' | |||
3145 | --- partition/utils_test.go 2015-06-02 20:46:07 +0000 | |||
3146 | +++ partition/utils_test.go 1970-01-01 00:00:00 +0000 | |||
3147 | @@ -1,47 +0,0 @@ | |||
3148 | 1 | // -*- Mode: Go; indent-tabs-mode: t -*- | ||
3149 | 2 | |||
3150 | 3 | /* | ||
3151 | 4 | * Copyright (C) 2014-2015 Canonical Ltd | ||
3152 | 5 | * | ||
3153 | 6 | * This program is free software: you can redistribute it and/or modify | ||
3154 | 7 | * it under the terms of the GNU General Public License version 3 as | ||
3155 | 8 | * published by the Free Software Foundation. | ||
3156 | 9 | * | ||
3157 | 10 | * This program is distributed in the hope that it will be useful, | ||
3158 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
3159 | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
3160 | 13 | * GNU General Public License for more details. | ||
3161 | 14 | * | ||
3162 | 15 | * You should have received a copy of the GNU General Public License | ||
3163 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
3164 | 17 | * | ||
3165 | 18 | */ | ||
3166 | 19 | |||
3167 | 20 | package partition | ||
3168 | 21 | |||
3169 | 22 | import ( | ||
3170 | 23 | . "gopkg.in/check.v1" | ||
3171 | 24 | ) | ||
3172 | 25 | |||
3173 | 26 | type UtilsTestSuite struct { | ||
3174 | 27 | } | ||
3175 | 28 | |||
3176 | 29 | var _ = Suite(&UtilsTestSuite{}) | ||
3177 | 30 | |||
3178 | 31 | func (s *UtilsTestSuite) SetUpTest(c *C) { | ||
3179 | 32 | } | ||
3180 | 33 | |||
3181 | 34 | func (s *UtilsTestSuite) TestRunCommand(c *C) { | ||
3182 | 35 | err := runCommandImpl("false") | ||
3183 | 36 | c.Assert(err, NotNil) | ||
3184 | 37 | |||
3185 | 38 | err = runCommandImpl("no-such-command") | ||
3186 | 39 | c.Assert(err, NotNil) | ||
3187 | 40 | } | ||
3188 | 41 | |||
3189 | 42 | func (s *UtilsTestSuite) TestRunCommandWithStdout(c *C) { | ||
3190 | 43 | runCommandWithStdout = runCommandWithStdoutImpl | ||
3191 | 44 | output, err := runCommandWithStdout("sh", "-c", "printf 'foo\nbar'") | ||
3192 | 45 | c.Assert(err, IsNil) | ||
3193 | 46 | c.Assert(output, DeepEquals, "foo\nbar") | ||
3194 | 47 | } | ||
3195 | 48 | 0 | ||
3196 | === modified file 'pkg/types.go' | |||
3197 | --- pkg/types.go 2015-05-19 19:34:13 +0000 | |||
3198 | +++ pkg/types.go 2015-09-03 08:14:56 +0000 | |||
3199 | @@ -34,6 +34,8 @@ | |||
3200 | 34 | TypeCore Type = "core" | 34 | TypeCore Type = "core" |
3201 | 35 | TypeFramework Type = "framework" | 35 | TypeFramework Type = "framework" |
3202 | 36 | TypeOem Type = "oem" | 36 | TypeOem Type = "oem" |
3203 | 37 | TypeOS Type = "os" | ||
3204 | 38 | TypeKernel Type = "kernel" | ||
3205 | 37 | ) | 39 | ) |
3206 | 38 | 40 | ||
3207 | 39 | // MarshalJSON returns *m as the JSON encoding of m. | 41 | // MarshalJSON returns *m as the JSON encoding of m. |
3208 | 40 | 42 | ||
3209 | === modified file 'snappy/click.go' | |||
3210 | --- snappy/click.go 2015-08-19 15:21:16 +0000 | |||
3211 | +++ snappy/click.go 2015-09-03 08:14:56 +0000 | |||
3212 | @@ -718,7 +718,8 @@ | |||
3213 | 718 | if err != nil { | 718 | if err != nil { |
3214 | 719 | return "", err | 719 | return "", err |
3215 | 720 | } | 720 | } |
3217 | 721 | defer part.deb.Close() | 721 | // FIXME: NewSnapPartFromSnapFile is ugly that it needs this close |
3218 | 722 | defer part.debClose() | ||
3219 | 722 | 723 | ||
3220 | 723 | return part.Install(inter, flags) | 724 | return part.Install(inter, flags) |
3221 | 724 | } | 725 | } |
3222 | 725 | 726 | ||
3223 | === modified file 'snappy/dirs.go' | |||
3224 | --- snappy/dirs.go 2015-06-30 12:36:00 +0000 | |||
3225 | +++ snappy/dirs.go 2015-09-03 08:14:56 +0000 | |||
3226 | @@ -27,6 +27,8 @@ | |||
3227 | 27 | 27 | ||
3228 | 28 | snapAppsDir string | 28 | snapAppsDir string |
3229 | 29 | snapOemDir string | 29 | snapOemDir string |
3230 | 30 | snapOsDir string | ||
3231 | 31 | snapKernelDir string | ||
3232 | 30 | snapDataDir string | 32 | snapDataDir string |
3233 | 31 | snapDataHomeGlob string | 33 | snapDataHomeGlob string |
3234 | 32 | snapAppArmorDir string | 34 | snapAppArmorDir string |
3235 | @@ -53,6 +55,8 @@ | |||
3236 | 53 | 55 | ||
3237 | 54 | snapAppsDir = filepath.Join(rootdir, "/apps") | 56 | snapAppsDir = filepath.Join(rootdir, "/apps") |
3238 | 55 | snapOemDir = filepath.Join(rootdir, "/oem") | 57 | snapOemDir = filepath.Join(rootdir, "/oem") |
3239 | 58 | snapOsDir = filepath.Join(rootdir, "/os") | ||
3240 | 59 | snapKernelDir = filepath.Join(rootdir, "/kernel") | ||
3241 | 56 | snapDataDir = filepath.Join(rootdir, "/var/lib/apps") | 60 | snapDataDir = filepath.Join(rootdir, "/var/lib/apps") |
3242 | 57 | snapDataHomeGlob = filepath.Join(rootdir, "/home/*/apps/") | 61 | snapDataHomeGlob = filepath.Join(rootdir, "/home/*/apps/") |
3243 | 58 | snapAppArmorDir = filepath.Join(rootdir, "/var/lib/apparmor/clicks") | 62 | snapAppArmorDir = filepath.Join(rootdir, "/var/lib/apparmor/clicks") |
3244 | 59 | 63 | ||
3245 | === modified file 'snappy/install.go' | |||
3246 | --- snappy/install.go 2015-06-11 09:38:18 +0000 | |||
3247 | +++ snappy/install.go 2015-09-03 08:14:56 +0000 | |||
3248 | @@ -24,8 +24,8 @@ | |||
3249 | 24 | "os" | 24 | "os" |
3250 | 25 | "sort" | 25 | "sort" |
3251 | 26 | 26 | ||
3252 | 27 | "launchpad.net/snappy/bootloader" | ||
3253 | 27 | "launchpad.net/snappy/logger" | 28 | "launchpad.net/snappy/logger" |
3254 | 28 | "launchpad.net/snappy/partition" | ||
3255 | 29 | "launchpad.net/snappy/progress" | 29 | "launchpad.net/snappy/progress" |
3256 | 30 | "launchpad.net/snappy/provisioning" | 30 | "launchpad.net/snappy/provisioning" |
3257 | 31 | ) | 31 | ) |
3258 | @@ -97,7 +97,7 @@ | |||
3259 | 97 | // FIXME: this is terrible, we really need a single | 97 | // FIXME: this is terrible, we really need a single |
3260 | 98 | // bootloader dir like /boot or /boot/loader | 98 | // bootloader dir like /boot or /boot/loader |
3261 | 99 | // instead of having to query the partition code | 99 | // instead of having to query the partition code |
3263 | 100 | if provisioning.InDeveloperMode(partition.BootloaderDir()) { | 100 | if provisioning.InDeveloperMode(bootloader.Dir()) { |
3264 | 101 | flags |= AllowUnauthenticated | 101 | flags |= AllowUnauthenticated |
3265 | 102 | } | 102 | } |
3266 | 103 | 103 | ||
3267 | 104 | 104 | ||
3268 | === modified file 'snappy/install_test.go' | |||
3269 | --- snappy/install_test.go 2015-06-29 20:21:38 +0000 | |||
3270 | +++ snappy/install_test.go 2015-09-03 08:14:56 +0000 | |||
3271 | @@ -30,7 +30,6 @@ | |||
3272 | 30 | "path/filepath" | 30 | "path/filepath" |
3273 | 31 | 31 | ||
3274 | 32 | . "gopkg.in/check.v1" | 32 | . "gopkg.in/check.v1" |
3275 | 33 | "launchpad.net/snappy/partition" | ||
3276 | 34 | "launchpad.net/snappy/progress" | 33 | "launchpad.net/snappy/progress" |
3277 | 35 | ) | 34 | ) |
3278 | 36 | 35 | ||
3279 | @@ -172,94 +171,3 @@ | |||
3280 | 172 | _, err = Install("hello-app.potato", 0, ag) | 171 | _, err = Install("hello-app.potato", 0, ag) |
3281 | 173 | c.Assert(err, ErrorMatches, ".*"+ErrPackageNameAlreadyInstalled.Error()) | 172 | c.Assert(err, ErrorMatches, ".*"+ErrPackageNameAlreadyInstalled.Error()) |
3282 | 174 | } | 173 | } |
3283 | 175 | |||
3284 | 176 | func (s *SnapTestSuite) TestUpdate(c *C) { | ||
3285 | 177 | snapPackagev1 := makeTestSnapPackage(c, "name: foo\nversion: 1\nvendor: foo") | ||
3286 | 178 | name, err := Install(snapPackagev1, AllowUnauthenticated|DoInstallGC, &progress.NullProgress{}) | ||
3287 | 179 | c.Assert(err, IsNil) | ||
3288 | 180 | c.Assert(name, Equals, "foo") | ||
3289 | 181 | |||
3290 | 182 | snapPackagev2 := makeTestSnapPackage(c, "name: foo\nversion: 2\nvendor: foo") | ||
3291 | 183 | |||
3292 | 184 | snapR, err := os.Open(snapPackagev2) | ||
3293 | 185 | c.Assert(err, IsNil) | ||
3294 | 186 | defer snapR.Close() | ||
3295 | 187 | |||
3296 | 188 | // details | ||
3297 | 189 | var dlURL, iconURL string | ||
3298 | 190 | mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
3299 | 191 | switch r.URL.Path { | ||
3300 | 192 | case "/details/foo": | ||
3301 | 193 | io.WriteString(w, `{ | ||
3302 | 194 | "package_name": "foo", | ||
3303 | 195 | "version": "2", | ||
3304 | 196 | "origin": "sideload", | ||
3305 | 197 | "anon_download_url": "`+dlURL+`", | ||
3306 | 198 | "icon_url": "`+iconURL+`" | ||
3307 | 199 | }`) | ||
3308 | 200 | case "/dl": | ||
3309 | 201 | snapR.Seek(0, 0) | ||
3310 | 202 | io.Copy(w, snapR) | ||
3311 | 203 | case "/icon": | ||
3312 | 204 | fmt.Fprintf(w, "") | ||
3313 | 205 | default: | ||
3314 | 206 | panic("unexpected url path: " + r.URL.Path) | ||
3315 | 207 | } | ||
3316 | 208 | })) | ||
3317 | 209 | c.Assert(mockServer, NotNil) | ||
3318 | 210 | defer mockServer.Close() | ||
3319 | 211 | |||
3320 | 212 | dlURL = mockServer.URL + "/dl" | ||
3321 | 213 | iconURL = mockServer.URL + "/icon" | ||
3322 | 214 | |||
3323 | 215 | storeDetailsURI, err = url.Parse(mockServer.URL + "/details/") | ||
3324 | 216 | c.Assert(err, IsNil) | ||
3325 | 217 | |||
3326 | 218 | // bulk | ||
3327 | 219 | mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
3328 | 220 | io.WriteString(w, `[{ | ||
3329 | 221 | "package_name": "foo", | ||
3330 | 222 | "version": "2", | ||
3331 | 223 | "origin": "sideload", | ||
3332 | 224 | "anon_download_url": "`+dlURL+`", | ||
3333 | 225 | "icon_url": "`+iconURL+`" | ||
3334 | 226 | }]`) | ||
3335 | 227 | })) | ||
3336 | 228 | |||
3337 | 229 | storeBulkURI, err = url.Parse(mockServer.URL) | ||
3338 | 230 | c.Assert(err, IsNil) | ||
3339 | 231 | |||
3340 | 232 | c.Assert(mockServer, NotNil) | ||
3341 | 233 | defer mockServer.Close() | ||
3342 | 234 | |||
3343 | 235 | // system image | ||
3344 | 236 | newPartition = func() (p partition.Interface) { | ||
3345 | 237 | return new(MockPartition) | ||
3346 | 238 | } | ||
3347 | 239 | defer func() { newPartition = newPartitionImpl }() | ||
3348 | 240 | |||
3349 | 241 | tempdir := c.MkDir() | ||
3350 | 242 | systemImageRoot = tempdir | ||
3351 | 243 | |||
3352 | 244 | makeFakeSystemImageChannelConfig(c, filepath.Join(tempdir, systemImageChannelConfig), "1") | ||
3353 | 245 | // setup fake /other partition | ||
3354 | 246 | makeFakeSystemImageChannelConfig(c, filepath.Join(tempdir, "other", systemImageChannelConfig), "2") | ||
3355 | 247 | |||
3356 | 248 | siServer := runMockSystemImageWebServer() | ||
3357 | 249 | defer siServer.Close() | ||
3358 | 250 | |||
3359 | 251 | mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
3360 | 252 | io.WriteString(w, fmt.Sprintf(mockSystemImageIndexJSONTemplate, "1")) | ||
3361 | 253 | })) | ||
3362 | 254 | c.Assert(mockServer, NotNil) | ||
3363 | 255 | defer mockServer.Close() | ||
3364 | 256 | |||
3365 | 257 | systemImageServer = mockServer.URL | ||
3366 | 258 | |||
3367 | 259 | // the test | ||
3368 | 260 | updates, err := Update(0, &progress.NullProgress{}) | ||
3369 | 261 | c.Assert(err, IsNil) | ||
3370 | 262 | c.Assert(updates, HasLen, 1) | ||
3371 | 263 | c.Check(updates[0].Name(), Equals, "foo") | ||
3372 | 264 | c.Check(updates[0].Version(), Equals, "2") | ||
3373 | 265 | } | ||
3374 | 266 | 174 | ||
3375 | === added file 'snappy/kernel.go' | |||
3376 | --- snappy/kernel.go 1970-01-01 00:00:00 +0000 | |||
3377 | +++ snappy/kernel.go 2015-09-03 08:14:56 +0000 | |||
3378 | @@ -0,0 +1,103 @@ | |||
3379 | 1 | // -*- Mode: Go; indent-tabs-mode: t -*- | ||
3380 | 2 | |||
3381 | 3 | /* | ||
3382 | 4 | * Copyright (C) 2014-2015 Canonical Ltd | ||
3383 | 5 | * | ||
3384 | 6 | * This program is free software: you can redistribute it and/or modify | ||
3385 | 7 | * it under the terms of the GNU General Public License version 3 as | ||
3386 | 8 | * published by the Free Software Foundation. | ||
3387 | 9 | * | ||
3388 | 10 | * This program is distributed in the hope that it will be useful, | ||
3389 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
3390 | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
3391 | 13 | * GNU General Public License for more details. | ||
3392 | 14 | * | ||
3393 | 15 | * You should have received a copy of the GNU General Public License | ||
3394 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
3395 | 17 | * | ||
3396 | 18 | */ | ||
3397 | 19 | |||
3398 | 20 | package snappy | ||
3399 | 21 | |||
3400 | 22 | import ( | ||
3401 | 23 | "os" | ||
3402 | 24 | "path/filepath" | ||
3403 | 25 | |||
3404 | 26 | "launchpad.net/snappy/bootloader" | ||
3405 | 27 | "launchpad.net/snappy/helpers" | ||
3406 | 28 | ) | ||
3407 | 29 | |||
3408 | 30 | type KernelSnap struct { | ||
3409 | 31 | SnapPart | ||
3410 | 32 | } | ||
3411 | 33 | |||
3412 | 34 | func (s *KernelSnap) NeedsReboot() bool { | ||
3413 | 35 | snappyKernel, _ := bootloader.GetBootVar("snappy_kernel") | ||
3414 | 36 | if !s.IsActive() && snappyKernel == s.Version() { | ||
3415 | 37 | return true | ||
3416 | 38 | } | ||
3417 | 39 | |||
3418 | 40 | return false | ||
3419 | 41 | } | ||
3420 | 42 | |||
3421 | 43 | /* | ||
3422 | 44 | func (s *KernelSnap) dir() string { | ||
3423 | 45 | fullName := s.SnapPart.m.qualifiedName(s.origin) | ||
3424 | 46 | return filepath.Join(snapKernelDir, fullName, s.m.Version) | ||
3425 | 47 | } | ||
3426 | 48 | */ | ||
3427 | 49 | |||
3428 | 50 | func (s *KernelSnap) activate(inhibitHooks bool, inter interacter) error { | ||
3429 | 51 | if err := s.SnapPart.activate(inhibitHooks, inter); err != nil { | ||
3430 | 52 | return err | ||
3431 | 53 | } | ||
3432 | 54 | |||
3433 | 55 | if err := bootloader.SetBootVar("snappy_kernel", s.Version()); err != nil { | ||
3434 | 56 | return err | ||
3435 | 57 | } | ||
3436 | 58 | |||
3437 | 59 | return bootloader.SetBootVar("snappy_mode", "try") | ||
3438 | 60 | } | ||
3439 | 61 | |||
3440 | 62 | func unpackKernel(s *SnapPart) error { | ||
3441 | 63 | // FIXME: do we need the globalRootDir here? | ||
3442 | 64 | if err := s.deb.Unpack(s.basedir); err != nil { | ||
3443 | 65 | return err | ||
3444 | 66 | } | ||
3445 | 67 | |||
3446 | 68 | // FIXME: create special unpack that diverts kernel/initrd/dtbs | ||
3447 | 69 | // to the right place, this will currently copy the files | ||
3448 | 70 | // (duplicate data) | ||
3449 | 71 | bootdir := bootloader.Dir() | ||
3450 | 72 | if err := os.MkdirAll(filepath.Join(bootdir, s.Version()), 0755); err != nil { | ||
3451 | 73 | return err | ||
3452 | 74 | } | ||
3453 | 75 | if s.m.Kernel != "" { | ||
3454 | 76 | src := filepath.Join(s.basedir, s.m.Kernel) | ||
3455 | 77 | dst := filepath.Join(bootdir, s.Version(), normalizeKernelInitrdName(s.m.Kernel)) | ||
3456 | 78 | if err := helpers.CopyFile(src, dst, helpers.CopyFlagDefault); err != nil { | ||
3457 | 79 | return err | ||
3458 | 80 | } | ||
3459 | 81 | } | ||
3460 | 82 | if s.m.Initrd != "" { | ||
3461 | 83 | src := filepath.Join(s.basedir, s.m.Initrd) | ||
3462 | 84 | dst := filepath.Join(bootdir, s.Version(), normalizeKernelInitrdName(s.m.Initrd)) | ||
3463 | 85 | if err := helpers.CopyFile(src, dst, helpers.CopyFlagDefault); err != nil { | ||
3464 | 86 | return err | ||
3465 | 87 | } | ||
3466 | 88 | } | ||
3467 | 89 | if s.m.Dtbs != "" { | ||
3468 | 90 | src := filepath.Join(s.basedir, s.m.Dtbs) | ||
3469 | 91 | dst := filepath.Join(bootdir, s.Version(), s.m.Dtbs) | ||
3470 | 92 | // FIXME: a simple "cp -a" is what we need here | ||
3471 | 93 | if err := helpers.RSyncWithDelete(src, dst); err != nil { | ||
3472 | 94 | return err | ||
3473 | 95 | } | ||
3474 | 96 | } | ||
3475 | 97 | |||
3476 | 98 | if err := bootloader.SetBootVar("snappy_kernel", s.Version()); err != nil { | ||
3477 | 99 | return err | ||
3478 | 100 | } | ||
3479 | 101 | |||
3480 | 102 | return bootloader.SetBootVar("snappy_mode", "trial") | ||
3481 | 103 | } | ||
3482 | 0 | 104 | ||
3483 | === modified file 'snappy/oem.go' | |||
3484 | --- snappy/oem.go 2015-07-01 14:48:33 +0000 | |||
3485 | +++ snappy/oem.go 2015-09-03 08:14:56 +0000 | |||
3486 | @@ -291,3 +291,14 @@ | |||
3487 | 291 | 291 | ||
3488 | 292 | return nil | 292 | return nil |
3489 | 293 | } | 293 | } |
3490 | 294 | |||
3491 | 295 | type OemSnap struct { | ||
3492 | 296 | SnapPart | ||
3493 | 297 | } | ||
3494 | 298 | |||
3495 | 299 | /* | ||
3496 | 300 | func (s *OemSnap) dir() string { | ||
3497 | 301 | fullName := s.SnapPart.m.qualifiedName(s.origin) | ||
3498 | 302 | return filepath.Join(snapOemDir, fullName, s.m.Version) | ||
3499 | 303 | } | ||
3500 | 304 | */ | ||
3501 | 294 | 305 | ||
3502 | === added file 'snappy/os.go' | |||
3503 | --- snappy/os.go 1970-01-01 00:00:00 +0000 | |||
3504 | +++ snappy/os.go 2015-09-03 08:14:56 +0000 | |||
3505 | @@ -0,0 +1,68 @@ | |||
3506 | 1 | // -*- Mode: Go; indent-tabs-mode: t -*- | ||
3507 | 2 | |||
3508 | 3 | /* | ||
3509 | 4 | * Copyright (C) 2014-2015 Canonical Ltd | ||
3510 | 5 | * | ||
3511 | 6 | * This program is free software: you can redistribute it and/or modify | ||
3512 | 7 | * it under the terms of the GNU General Public License version 3 as | ||
3513 | 8 | * published by the Free Software Foundation. | ||
3514 | 9 | * | ||
3515 | 10 | * This program is distributed in the hope that it will be useful, | ||
3516 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
3517 | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
3518 | 13 | * GNU General Public License for more details. | ||
3519 | 14 | * | ||
3520 | 15 | * You should have received a copy of the GNU General Public License | ||
3521 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
3522 | 17 | * | ||
3523 | 18 | */ | ||
3524 | 19 | |||
3525 | 20 | package snappy | ||
3526 | 21 | |||
3527 | 22 | import ( | ||
3528 | 23 | "launchpad.net/snappy/bootloader" | ||
3529 | 24 | ) | ||
3530 | 25 | |||
3531 | 26 | type OsSnap struct { | ||
3532 | 27 | SnapPart | ||
3533 | 28 | } | ||
3534 | 29 | |||
3535 | 30 | func (s *OsSnap) NeedsReboot() bool { | ||
3536 | 31 | snappyOS, _ := bootloader.GetBootVar("snappy_os") | ||
3537 | 32 | if !s.IsActive() && snappyOS == s.Version() { | ||
3538 | 33 | return true | ||
3539 | 34 | } | ||
3540 | 35 | |||
3541 | 36 | return false | ||
3542 | 37 | } | ||
3543 | 38 | |||
3544 | 39 | /* | ||
3545 | 40 | func (s *OsSnap) dir() string { | ||
3546 | 41 | fullName := s.SnapPart.m.qualifiedName(s.origin) | ||
3547 | 42 | return filepath.Join(snapOsDir, fullName, s.m.Version) | ||
3548 | 43 | } | ||
3549 | 44 | */ | ||
3550 | 45 | |||
3551 | 46 | func (s *OsSnap) activate(inhibitHooks bool, inter interacter) error { | ||
3552 | 47 | if err := s.SnapPart.activate(inhibitHooks, inter); err != nil { | ||
3553 | 48 | return err | ||
3554 | 49 | } | ||
3555 | 50 | if err := bootloader.SetBootVar("snappy_os", s.Version()); err != nil { | ||
3556 | 51 | return err | ||
3557 | 52 | } | ||
3558 | 53 | |||
3559 | 54 | return bootloader.SetBootVar("snappy_mode", "try") | ||
3560 | 55 | } | ||
3561 | 56 | |||
3562 | 57 | func unpackOS(s *SnapPart) error { | ||
3563 | 58 | // FIXME: do we need the globalRootDir here? | ||
3564 | 59 | if err := s.deb.Unpack(s.basedir); err != nil { | ||
3565 | 60 | return err | ||
3566 | 61 | } | ||
3567 | 62 | |||
3568 | 63 | if err := bootloader.SetBootVar("snappy_os", s.Version()); err != nil { | ||
3569 | 64 | return err | ||
3570 | 65 | } | ||
3571 | 66 | |||
3572 | 67 | return bootloader.SetBootVar("snappy_mode", "trial") | ||
3573 | 68 | } | ||
3574 | 0 | 69 | ||
3575 | === modified file 'snappy/parts.go' | |||
3576 | --- snappy/parts.go 2015-07-23 11:05:37 +0000 | |||
3577 | +++ snappy/parts.go 2015-09-03 08:14:56 +0000 | |||
3578 | @@ -134,14 +134,10 @@ | |||
3579 | 134 | m := new(MetaRepository) | 134 | m := new(MetaRepository) |
3580 | 135 | m.all = []Repository{} | 135 | m.all = []Repository{} |
3581 | 136 | 136 | ||
3590 | 137 | if repo := NewSystemImageRepository(); repo != nil { | 137 | for _, d := range []string{snapAppsDir, snapOemDir, snapOsDir, snapKernelDir} { |
3591 | 138 | m.all = append(m.all, repo) | 138 | if repo := NewLocalSnapRepository(d); repo != nil { |
3592 | 139 | } | 139 | m.all = append(m.all, repo) |
3593 | 140 | if repo := NewLocalSnapRepository(snapAppsDir); repo != nil { | 140 | } |
3586 | 141 | m.all = append(m.all, repo) | ||
3587 | 142 | } | ||
3588 | 143 | if repo := NewLocalSnapRepository(snapOemDir); repo != nil { | ||
3589 | 144 | m.all = append(m.all, repo) | ||
3594 | 145 | } | 141 | } |
3595 | 146 | 142 | ||
3596 | 147 | return m | 143 | return m |
3597 | 148 | 144 | ||
3598 | === modified file 'snappy/purge.go' | |||
3599 | --- snappy/purge.go 2015-05-29 12:08:46 +0000 | |||
3600 | +++ snappy/purge.go 2015-09-03 08:14:56 +0000 | |||
3601 | @@ -47,7 +47,7 @@ | |||
3602 | 47 | 47 | ||
3603 | 48 | purgeActive := flags&DoPurgeActive != 0 | 48 | purgeActive := flags&DoPurgeActive != 0 |
3604 | 49 | 49 | ||
3606 | 50 | var active []*SnapPart | 50 | var active []Snap |
3607 | 51 | 51 | ||
3608 | 52 | for _, datadir := range datadirs { | 52 | for _, datadir := range datadirs { |
3609 | 53 | yamlPath := filepath.Join(snapAppsDir, datadir.QualifiedName(), datadir.Version, "meta", "package.yaml") | 53 | yamlPath := filepath.Join(snapAppsDir, datadir.QualifiedName(), datadir.Version, "meta", "package.yaml") |
3610 | 54 | 54 | ||
3611 | === modified file 'snappy/purge_test.go' | |||
3612 | --- snappy/purge_test.go 2015-06-09 19:11:54 +0000 | |||
3613 | +++ snappy/purge_test.go 2015-09-03 08:14:56 +0000 | |||
3614 | @@ -57,7 +57,7 @@ | |||
3615 | 57 | c.Check(inter.notified, HasLen, 0) | 57 | c.Check(inter.notified, HasLen, 0) |
3616 | 58 | } | 58 | } |
3617 | 59 | 59 | ||
3619 | 60 | func (s *purgeSuite) mkpkg(c *C, args ...string) (dataDir string, part *SnapPart) { | 60 | func (s *purgeSuite) mkpkg(c *C, args ...string) (dataDir string, part Snap) { |
3620 | 61 | version := "1.10" | 61 | version := "1.10" |
3621 | 62 | extra := "" | 62 | extra := "" |
3622 | 63 | switch len(args) { | 63 | switch len(args) { |
3623 | 64 | 64 | ||
3624 | === modified file 'snappy/snapp.go' | |||
3625 | --- snappy/snapp.go 2015-08-19 15:21:16 +0000 | |||
3626 | +++ snappy/snapp.go 2015-09-03 08:14:56 +0000 | |||
3627 | @@ -38,6 +38,7 @@ | |||
3628 | 38 | 38 | ||
3629 | 39 | "gopkg.in/yaml.v2" | 39 | "gopkg.in/yaml.v2" |
3630 | 40 | 40 | ||
3631 | 41 | "launchpad.net/snappy/bootloader" | ||
3632 | 41 | "launchpad.net/snappy/clickdeb" | 42 | "launchpad.net/snappy/clickdeb" |
3633 | 42 | "launchpad.net/snappy/helpers" | 43 | "launchpad.net/snappy/helpers" |
3634 | 43 | "launchpad.net/snappy/logger" | 44 | "launchpad.net/snappy/logger" |
3635 | @@ -198,6 +199,16 @@ | |||
3636 | 198 | return nil | 199 | return nil |
3637 | 199 | } | 200 | } |
3638 | 200 | 201 | ||
3639 | 202 | type Snap interface { | ||
3640 | 203 | Part | ||
3641 | 204 | activate(inhibitHooks bool, inter interacter) error | ||
3642 | 205 | deactivate(inhibitHooks bool, inter interacter) error | ||
3643 | 206 | remove(inter interacter) error | ||
3644 | 207 | dir() string | ||
3645 | 208 | ServiceYamls() []ServiceYaml | ||
3646 | 209 | debClose() error | ||
3647 | 210 | } | ||
3648 | 211 | |||
3649 | 201 | // TODO split into payloads per package type composing the common | 212 | // TODO split into payloads per package type composing the common |
3650 | 202 | // elements for all snaps. | 213 | // elements for all snaps. |
3651 | 203 | type packageYaml struct { | 214 | type packageYaml struct { |
3652 | @@ -228,6 +239,11 @@ | |||
3653 | 228 | 239 | ||
3654 | 229 | ExplicitLicenseAgreement bool `yaml:"explicit-license-agreement,omitempty"` | 240 | ExplicitLicenseAgreement bool `yaml:"explicit-license-agreement,omitempty"` |
3655 | 230 | LicenseVersion string `yaml:"license-version,omitempty"` | 241 | LicenseVersion string `yaml:"license-version,omitempty"` |
3656 | 242 | |||
3657 | 243 | // FIXME: move into a special kernel struct | ||
3658 | 244 | Kernel string `yaml:"kernel,omitempty"` | ||
3659 | 245 | Initrd string `yaml:"initrd,omitempty"` | ||
3660 | 246 | Dtbs string `yaml:"dtbs,omitempty"` | ||
3661 | 231 | } | 247 | } |
3662 | 232 | 248 | ||
3663 | 233 | type remoteSnap struct { | 249 | type remoteSnap struct { |
3664 | @@ -362,6 +378,12 @@ | |||
3665 | 362 | return m.Name + "." + origin | 378 | return m.Name + "." + origin |
3666 | 363 | } | 379 | } |
3667 | 364 | 380 | ||
3668 | 381 | // noramlizeAssetName transforms like "vmlinuz-4.1.0" -> "vmlinuz" | ||
3669 | 382 | func normalizeKernelInitrdName(name string) string { | ||
3670 | 383 | name = filepath.Base(name) | ||
3671 | 384 | return strings.SplitN(name, "-", 2)[0] | ||
3672 | 385 | } | ||
3673 | 386 | |||
3674 | 365 | func (m *packageYaml) checkForNameClashes() error { | 387 | func (m *packageYaml) checkForNameClashes() error { |
3675 | 366 | d := make(map[string]struct{}) | 388 | d := make(map[string]struct{}) |
3676 | 367 | for _, bin := range m.Binaries { | 389 | for _, bin := range m.Binaries { |
3677 | @@ -528,7 +550,7 @@ | |||
3678 | 528 | } | 550 | } |
3679 | 529 | 551 | ||
3680 | 530 | // NewInstalledSnapPart returns a new SnapPart from the given yamlPath | 552 | // NewInstalledSnapPart returns a new SnapPart from the given yamlPath |
3682 | 531 | func NewInstalledSnapPart(yamlPath, origin string) (*SnapPart, error) { | 553 | func NewInstalledSnapPart(yamlPath, origin string) (Snap, error) { |
3683 | 532 | m, err := parsePackageYamlFile(yamlPath) | 554 | m, err := parsePackageYamlFile(yamlPath) |
3684 | 533 | if err != nil { | 555 | if err != nil { |
3685 | 534 | return nil, err | 556 | return nil, err |
3686 | @@ -546,7 +568,7 @@ | |||
3687 | 546 | // NewSnapPartFromSnapFile loads a snap from the given (clickdeb) snap file. | 568 | // NewSnapPartFromSnapFile loads a snap from the given (clickdeb) snap file. |
3688 | 547 | // Caller should call Close on the clickdeb. | 569 | // Caller should call Close on the clickdeb. |
3689 | 548 | // TODO: expose that Close. | 570 | // TODO: expose that Close. |
3691 | 549 | func NewSnapPartFromSnapFile(snapFile string, origin string, unauthOk bool) (*SnapPart, error) { | 571 | func NewSnapPartFromSnapFile(snapFile string, origin string, unauthOk bool) (Snap, error) { |
3692 | 550 | if err := clickdeb.Verify(snapFile, unauthOk); err != nil { | 572 | if err := clickdeb.Verify(snapFile, unauthOk); err != nil { |
3693 | 551 | return nil, err | 573 | return nil, err |
3694 | 552 | } | 574 | } |
3695 | @@ -566,10 +588,15 @@ | |||
3696 | 566 | return nil, err | 588 | return nil, err |
3697 | 567 | } | 589 | } |
3698 | 568 | 590 | ||
3699 | 591 | // FIMXE: move all into {App,Kernel,Os}Snap.dir() | ||
3700 | 569 | targetDir := snapAppsDir | 592 | targetDir := snapAppsDir |
3703 | 570 | // the "oem" parts are special | 593 | switch m.Type { |
3704 | 571 | if m.Type == pkg.TypeOem { | 594 | case pkg.TypeOem: |
3705 | 572 | targetDir = snapOemDir | 595 | targetDir = snapOemDir |
3706 | 596 | case pkg.TypeOS: | ||
3707 | 597 | targetDir = snapOsDir | ||
3708 | 598 | case pkg.TypeKernel: | ||
3709 | 599 | targetDir = snapKernelDir | ||
3710 | 573 | } | 600 | } |
3711 | 574 | 601 | ||
3712 | 575 | if origin == sideloadedOrigin { | 602 | if origin == sideloadedOrigin { |
3713 | @@ -579,12 +606,22 @@ | |||
3714 | 579 | fullName := m.qualifiedName(origin) | 606 | fullName := m.qualifiedName(origin) |
3715 | 580 | instDir := filepath.Join(targetDir, fullName, m.Version) | 607 | instDir := filepath.Join(targetDir, fullName, m.Version) |
3716 | 581 | 608 | ||
3718 | 582 | return &SnapPart{ | 609 | baseSnap := &SnapPart{ |
3719 | 583 | basedir: instDir, | 610 | basedir: instDir, |
3720 | 584 | origin: origin, | 611 | origin: origin, |
3721 | 585 | m: m, | 612 | m: m, |
3722 | 586 | deb: d, | 613 | deb: d, |
3724 | 587 | }, nil | 614 | } |
3725 | 615 | switch m.Type { | ||
3726 | 616 | case pkg.TypeOem: | ||
3727 | 617 | return &OemSnap{SnapPart: *baseSnap}, nil | ||
3728 | 618 | case pkg.TypeOS: | ||
3729 | 619 | return &OsSnap{SnapPart: *baseSnap}, nil | ||
3730 | 620 | case pkg.TypeKernel: | ||
3731 | 621 | return &KernelSnap{SnapPart: *baseSnap}, nil | ||
3732 | 622 | } | ||
3733 | 623 | |||
3734 | 624 | return baseSnap, nil | ||
3735 | 588 | } | 625 | } |
3736 | 589 | 626 | ||
3737 | 590 | // NewSnapPartFromYaml returns a new SnapPart from the given *packageYaml at yamlPath | 627 | // NewSnapPartFromYaml returns a new SnapPart from the given *packageYaml at yamlPath |
3738 | @@ -650,6 +687,10 @@ | |||
3739 | 650 | return part, nil | 687 | return part, nil |
3740 | 651 | } | 688 | } |
3741 | 652 | 689 | ||
3742 | 690 | func (s *SnapPart) dir() string { | ||
3743 | 691 | return s.basedir | ||
3744 | 692 | } | ||
3745 | 693 | |||
3746 | 653 | // Type returns the type of the SnapPart (app, oem, ...) | 694 | // Type returns the type of the SnapPart (app, oem, ...) |
3747 | 654 | func (s *SnapPart) Type() pkg.Type { | 695 | func (s *SnapPart) Type() pkg.Type { |
3748 | 655 | if s.m.Type != "" { | 696 | if s.m.Type != "" { |
3749 | @@ -665,6 +706,11 @@ | |||
3750 | 665 | return s.m.Name | 706 | return s.m.Name |
3751 | 666 | } | 707 | } |
3752 | 667 | 708 | ||
3753 | 709 | // FIXME: super ugly | ||
3754 | 710 | func (s *SnapPart) debClose() error { | ||
3755 | 711 | return s.deb.Close() | ||
3756 | 712 | } | ||
3757 | 713 | |||
3758 | 668 | // Version returns the version | 714 | // Version returns the version |
3759 | 669 | func (s *SnapPart) Version() string { | 715 | func (s *SnapPart) Version() string { |
3760 | 670 | if s.basedir != "" { | 716 | if s.basedir != "" { |
3761 | @@ -802,7 +848,7 @@ | |||
3762 | 802 | fullName := QualifiedName(s) | 848 | fullName := QualifiedName(s) |
3763 | 803 | dataDir := filepath.Join(snapDataDir, fullName, s.Version()) | 849 | dataDir := filepath.Join(snapDataDir, fullName, s.Version()) |
3764 | 804 | 850 | ||
3766 | 805 | var oldPart *SnapPart | 851 | var oldPart Snap |
3767 | 806 | if currentActiveDir, _ := filepath.EvalSymlinks(filepath.Join(s.basedir, "..", "current")); currentActiveDir != "" { | 852 | if currentActiveDir, _ := filepath.EvalSymlinks(filepath.Join(s.basedir, "..", "current")); currentActiveDir != "" { |
3768 | 807 | oldPart, err = NewInstalledSnapPart(filepath.Join(currentActiveDir, "meta", "package.yaml"), s.origin) | 853 | oldPart, err = NewInstalledSnapPart(filepath.Join(currentActiveDir, "meta", "package.yaml"), s.origin) |
3769 | 808 | if err != nil { | 854 | if err != nil { |
3770 | @@ -824,10 +870,21 @@ | |||
3771 | 824 | } | 870 | } |
3772 | 825 | }() | 871 | }() |
3773 | 826 | 872 | ||
3778 | 827 | // we need to call the external helper so that we can reliable drop | 873 | // FIXME: so ugly, what we need is |
3779 | 828 | // privs | 874 | // "dynamic dispatch of overridden methods" which go makes hard |
3780 | 829 | if err := s.deb.UnpackWithDropPrivs(s.basedir, globalRootDir); err != nil { | 875 | switch s.Type() { |
3781 | 830 | return "", err | 876 | case pkg.TypeOS: |
3782 | 877 | if err := unpackOS(s); err != nil { | ||
3783 | 878 | return "", err | ||
3784 | 879 | } | ||
3785 | 880 | case pkg.TypeKernel: | ||
3786 | 881 | if err := unpackKernel(s); err != nil { | ||
3787 | 882 | return "", err | ||
3788 | 883 | } | ||
3789 | 884 | default: | ||
3790 | 885 | if err := s.deb.UnpackWithDropPrivs(s.basedir, globalRootDir); err != nil { | ||
3791 | 886 | return "", err | ||
3792 | 887 | } | ||
3793 | 831 | } | 888 | } |
3794 | 832 | 889 | ||
3795 | 833 | // legacy, the hooks (e.g. apparmor) need this. Once we converted | 890 | // legacy, the hooks (e.g. apparmor) need this. Once we converted |
3796 | @@ -1128,6 +1185,14 @@ | |||
3797 | 1128 | 1185 | ||
3798 | 1129 | // NeedsReboot returns true if the snap becomes active on the next reboot | 1186 | // NeedsReboot returns true if the snap becomes active on the next reboot |
3799 | 1130 | func (s *SnapPart) NeedsReboot() bool { | 1187 | func (s *SnapPart) NeedsReboot() bool { |
3800 | 1188 | // FIXME: needs to become SnapKernel | ||
3801 | 1189 | if s.m.Type == pkg.TypeKernel { | ||
3802 | 1190 | snappyKernel, _ := bootloader.GetBootVar("snappy_kernel") | ||
3803 | 1191 | if !s.IsActive() && snappyKernel == s.Version() { | ||
3804 | 1192 | return true | ||
3805 | 1193 | } | ||
3806 | 1194 | } | ||
3807 | 1195 | |||
3808 | 1131 | return false | 1196 | return false |
3809 | 1132 | } | 1197 | } |
3810 | 1133 | 1198 | ||
3811 | @@ -1266,10 +1331,10 @@ | |||
3812 | 1266 | } | 1331 | } |
3813 | 1267 | 1332 | ||
3814 | 1268 | // RefreshDependentsSecurity refreshes the security policies of dependent snaps | 1333 | // RefreshDependentsSecurity refreshes the security policies of dependent snaps |
3816 | 1269 | func (s *SnapPart) RefreshDependentsSecurity(oldPart *SnapPart, inter interacter) (err error) { | 1334 | func (s *SnapPart) RefreshDependentsSecurity(oldPart Snap, inter interacter) (err error) { |
3817 | 1270 | oldBaseDir := "" | 1335 | oldBaseDir := "" |
3818 | 1271 | if oldPart != nil { | 1336 | if oldPart != nil { |
3820 | 1272 | oldBaseDir = oldPart.basedir | 1337 | oldBaseDir = oldPart.(*SnapPart).basedir |
3821 | 1273 | } | 1338 | } |
3822 | 1274 | upPol, upTpl := policy.AppArmorDelta(oldBaseDir, s.basedir, s.Name()+"_") | 1339 | upPol, upTpl := policy.AppArmorDelta(oldBaseDir, s.basedir, s.Name()+"_") |
3823 | 1275 | 1340 | ||
3824 | @@ -1878,7 +1943,7 @@ | |||
3825 | 1878 | // The returned environment contains additional SNAP_* variables that | 1943 | // The returned environment contains additional SNAP_* variables that |
3826 | 1879 | // are required when calling a meta/hook/ script and that will override | 1944 | // are required when calling a meta/hook/ script and that will override |
3827 | 1880 | // any already existing SNAP_* variables in os.Environment() | 1945 | // any already existing SNAP_* variables in os.Environment() |
3829 | 1881 | func makeSnapHookEnv(part *SnapPart) (env []string) { | 1946 | func makeSnapHookEnv(part Snap) (env []string) { |
3830 | 1882 | desc := struct { | 1947 | desc := struct { |
3831 | 1883 | AppName string | 1948 | AppName string |
3832 | 1884 | AppArch string | 1949 | AppArch string |
3833 | @@ -1889,7 +1954,7 @@ | |||
3834 | 1889 | }{ | 1954 | }{ |
3835 | 1890 | part.Name(), | 1955 | part.Name(), |
3836 | 1891 | helpers.UbuntuArchitecture(), | 1956 | helpers.UbuntuArchitecture(), |
3838 | 1892 | part.basedir, | 1957 | part.dir(), |
3839 | 1893 | part.Version(), | 1958 | part.Version(), |
3840 | 1894 | QualifiedName(part), | 1959 | QualifiedName(part), |
3841 | 1895 | part.Origin(), | 1960 | part.Origin(), |
3842 | 1896 | 1961 | ||
3843 | === modified file 'snappy/snapp_test.go' | |||
3844 | --- snappy/snapp_test.go 2015-07-23 11:05:37 +0000 | |||
3845 | +++ snappy/snapp_test.go 2015-09-03 08:14:56 +0000 | |||
3846 | @@ -29,10 +29,10 @@ | |||
3847 | 29 | "os" | 29 | "os" |
3848 | 30 | "path/filepath" | 30 | "path/filepath" |
3849 | 31 | "strings" | 31 | "strings" |
3850 | 32 | "testing" | ||
3851 | 32 | 33 | ||
3852 | 33 | "launchpad.net/snappy/clickdeb" | 34 | "launchpad.net/snappy/clickdeb" |
3853 | 34 | "launchpad.net/snappy/helpers" | 35 | "launchpad.net/snappy/helpers" |
3854 | 35 | "launchpad.net/snappy/partition" | ||
3855 | 36 | "launchpad.net/snappy/pkg" | 36 | "launchpad.net/snappy/pkg" |
3856 | 37 | "launchpad.net/snappy/policy" | 37 | "launchpad.net/snappy/policy" |
3857 | 38 | "launchpad.net/snappy/release" | 38 | "launchpad.net/snappy/release" |
3858 | @@ -41,6 +41,9 @@ | |||
3859 | 41 | . "gopkg.in/check.v1" | 41 | . "gopkg.in/check.v1" |
3860 | 42 | ) | 42 | ) |
3861 | 43 | 43 | ||
3862 | 44 | // Hook up check.v1 into the "go test" runner | ||
3863 | 45 | func Test(t *testing.T) { TestingT(t) } | ||
3864 | 46 | |||
3865 | 44 | type SnapTestSuite struct { | 47 | type SnapTestSuite struct { |
3866 | 45 | tempdir string | 48 | tempdir string |
3867 | 46 | clickhook string | 49 | clickhook string |
3868 | @@ -54,9 +57,6 @@ | |||
3869 | 54 | aaClickHookCmd = "/bin/true" | 57 | aaClickHookCmd = "/bin/true" |
3870 | 55 | s.secbase = policy.SecBase | 58 | s.secbase = policy.SecBase |
3871 | 56 | s.tempdir = c.MkDir() | 59 | s.tempdir = c.MkDir() |
3872 | 57 | newPartition = func() (p partition.Interface) { | ||
3873 | 58 | return new(MockPartition) | ||
3874 | 59 | } | ||
3875 | 60 | 60 | ||
3876 | 61 | SetRootDir(s.tempdir) | 61 | SetRootDir(s.tempdir) |
3877 | 62 | policy.SecBase = filepath.Join(s.tempdir, "security") | 62 | policy.SecBase = filepath.Join(s.tempdir, "security") |
3878 | @@ -96,9 +96,6 @@ | |||
3879 | 96 | c.Assert(err, IsNil) | 96 | c.Assert(err, IsNil) |
3880 | 97 | 97 | ||
3881 | 98 | runScFilterGen = mockRunScFilterGen | 98 | runScFilterGen = mockRunScFilterGen |
3882 | 99 | |||
3883 | 100 | // ensure we do not look at the system | ||
3884 | 101 | systemImageRoot = s.tempdir | ||
3885 | 102 | } | 99 | } |
3886 | 103 | 100 | ||
3887 | 104 | func (s *SnapTestSuite) TearDownTest(c *C) { | 101 | func (s *SnapTestSuite) TearDownTest(c *C) { |
3888 | @@ -153,11 +150,11 @@ | |||
3889 | 153 | c.Assert(services[0].Name, Equals, "svc1") | 150 | c.Assert(services[0].Name, Equals, "svc1") |
3890 | 154 | 151 | ||
3891 | 155 | // ensure we get valid Date() | 152 | // ensure we get valid Date() |
3893 | 156 | st, err := os.Stat(snap.basedir) | 153 | st, err := os.Stat(snap.dir()) |
3894 | 157 | c.Assert(err, IsNil) | 154 | c.Assert(err, IsNil) |
3895 | 158 | c.Assert(snap.Date(), Equals, st.ModTime()) | 155 | c.Assert(snap.Date(), Equals, st.ModTime()) |
3896 | 159 | 156 | ||
3898 | 160 | c.Assert(snap.basedir, Equals, filepath.Join(s.tempdir, "apps", helloAppComposedName, "1.10")) | 157 | c.Assert(snap.dir(), Equals, filepath.Join(s.tempdir, "apps", helloAppComposedName, "1.10")) |
3899 | 161 | c.Assert(snap.InstalledSize(), Not(Equals), -1) | 158 | c.Assert(snap.InstalledSize(), Not(Equals), -1) |
3900 | 162 | } | 159 | } |
3901 | 163 | 160 | ||
3902 | @@ -827,11 +824,11 @@ | |||
3903 | 827 | c.Assert(services[1].Description, Equals, "Service #2") | 824 | c.Assert(services[1].Description, Equals, "Service #2") |
3904 | 828 | 825 | ||
3905 | 829 | // ensure we get valid Date() | 826 | // ensure we get valid Date() |
3907 | 830 | st, err := os.Stat(snap.basedir) | 827 | st, err := os.Stat(snap.dir()) |
3908 | 831 | c.Assert(err, IsNil) | 828 | c.Assert(err, IsNil) |
3909 | 832 | c.Assert(snap.Date(), Equals, st.ModTime()) | 829 | c.Assert(snap.Date(), Equals, st.ModTime()) |
3910 | 833 | 830 | ||
3912 | 834 | c.Assert(snap.basedir, Equals, filepath.Join(s.tempdir, "apps", helloAppComposedName, "1.10")) | 831 | c.Assert(snap.dir(), Equals, filepath.Join(s.tempdir, "apps", helloAppComposedName, "1.10")) |
3913 | 835 | c.Assert(snap.InstalledSize(), Not(Equals), -1) | 832 | c.Assert(snap.InstalledSize(), Not(Equals), -1) |
3914 | 836 | } | 833 | } |
3915 | 837 | 834 | ||
3916 | 838 | 835 | ||
3917 | === removed file 'snappy/systemimage.go' | |||
3918 | --- snappy/systemimage.go 2015-07-24 12:03:30 +0000 | |||
3919 | +++ snappy/systemimage.go 1970-01-01 00:00:00 +0000 | |||
3920 | @@ -1,518 +0,0 @@ | |||
3921 | 1 | // -*- Mode: Go; indent-tabs-mode: t -*- | ||
3922 | 2 | |||
3923 | 3 | /* | ||
3924 | 4 | * Copyright (C) 2014-2015 Canonical Ltd | ||
3925 | 5 | * | ||
3926 | 6 | * This program is free software: you can redistribute it and/or modify | ||
3927 | 7 | * it under the terms of the GNU General Public License version 3 as | ||
3928 | 8 | * published by the Free Software Foundation. | ||
3929 | 9 | * | ||
3930 | 10 | * This program is distributed in the hope that it will be useful, | ||
3931 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
3932 | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
3933 | 13 | * GNU General Public License for more details. | ||
3934 | 14 | * | ||
3935 | 15 | * You should have received a copy of the GNU General Public License | ||
3936 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
3937 | 17 | * | ||
3938 | 18 | */ | ||
3939 | 19 | |||
3940 | 20 | package snappy | ||
3941 | 21 | |||
3942 | 22 | import ( | ||
3943 | 23 | "crypto/sha512" | ||
3944 | 24 | "encoding/hex" | ||
3945 | 25 | "fmt" | ||
3946 | 26 | "os" | ||
3947 | 27 | "path/filepath" | ||
3948 | 28 | "strings" | ||
3949 | 29 | "time" | ||
3950 | 30 | |||
3951 | 31 | "github.com/mvo5/goconfigparser" | ||
3952 | 32 | |||
3953 | 33 | "launchpad.net/snappy/coreconfig" | ||
3954 | 34 | "launchpad.net/snappy/helpers" | ||
3955 | 35 | "launchpad.net/snappy/logger" | ||
3956 | 36 | "launchpad.net/snappy/partition" | ||
3957 | 37 | "launchpad.net/snappy/pkg" | ||
3958 | 38 | "launchpad.net/snappy/progress" | ||
3959 | 39 | "launchpad.net/snappy/provisioning" | ||
3960 | 40 | ) | ||
3961 | 41 | |||
3962 | 42 | const ( | ||
3963 | 43 | systemImagePartName = "ubuntu-core" | ||
3964 | 44 | systemImagePartOrigin = "ubuntu" | ||
3965 | 45 | systemImagePartVendor = "Canonical Ltd." | ||
3966 | 46 | |||
3967 | 47 | // location of the channel config on the filesystem. | ||
3968 | 48 | // | ||
3969 | 49 | // This file specifies the s-i version installed on the rootfs | ||
3970 | 50 | // and hence s-i updates this file on every update applied to | ||
3971 | 51 | // the rootfs (by unpacking file "version-$version.tar.xz"). | ||
3972 | 52 | systemImageChannelConfig = "/etc/system-image/config.d/01_channel.ini" | ||
3973 | 53 | |||
3974 | 54 | // location of the client config. | ||
3975 | 55 | // | ||
3976 | 56 | // The full path to this file needs to be passed to | ||
3977 | 57 | // systemImageCli when querying a different rootfs. | ||
3978 | 58 | systemImageClientConfig = "/etc/system-image/config.d/00_default.ini" | ||
3979 | 59 | ) | ||
3980 | 60 | |||
3981 | 61 | var ( | ||
3982 | 62 | // the system-image-cli binary | ||
3983 | 63 | systemImageCli = "system-image-cli" | ||
3984 | 64 | ) | ||
3985 | 65 | |||
3986 | 66 | // This is the root directory of the filesystem. Its only useful to | ||
3987 | 67 | // change when writing tests | ||
3988 | 68 | var systemImageRoot = "/" | ||
3989 | 69 | |||
3990 | 70 | // will replace newPartition() to return a mockPartition | ||
3991 | 71 | var newPartition = newPartitionImpl | ||
3992 | 72 | |||
3993 | 73 | func newPartitionImpl() (p partition.Interface) { | ||
3994 | 74 | return partition.New() | ||
3995 | 75 | } | ||
3996 | 76 | |||
3997 | 77 | // SystemImagePart represents a "core" snap that is managed via the SystemImage | ||
3998 | 78 | // client | ||
3999 | 79 | type SystemImagePart struct { | ||
4000 | 80 | version string | ||
4001 | 81 | versionDetails string | ||
4002 | 82 | channelName string | ||
4003 | 83 | lastUpdate time.Time | ||
4004 | 84 | |||
4005 | 85 | isInstalled bool | ||
4006 | 86 | isActive bool | ||
4007 | 87 | |||
4008 | 88 | updateSize int64 | ||
4009 | 89 | |||
4010 | 90 | partition partition.Interface | ||
4011 | 91 | } | ||
4012 | 92 | |||
4013 | 93 | // Type returns pkg.TypeCore for this snap | ||
4014 | 94 | func (s *SystemImagePart) Type() pkg.Type { | ||
4015 | 95 | return pkg.TypeCore | ||
4016 | 96 | } | ||
4017 | 97 | |||
4018 | 98 | // Name returns the name | ||
4019 | 99 | func (s *SystemImagePart) Name() string { | ||
4020 | 100 | return systemImagePartName | ||
4021 | 101 | } | ||
4022 | 102 | |||
4023 | 103 | // Origin returns the origin ("ubuntu") | ||
4024 | 104 | func (s *SystemImagePart) Origin() string { | ||
4025 | 105 | return systemImagePartOrigin | ||
4026 | 106 | } | ||
4027 | 107 | |||
4028 | 108 | // Vendor returns the vendor ("Canonical Ltd.") | ||
4029 | 109 | func (s *SystemImagePart) Vendor() string { | ||
4030 | 110 | return systemImagePartVendor | ||
4031 | 111 | } | ||
4032 | 112 | |||
4033 | 113 | // Version returns the version | ||
4034 | 114 | func (s *SystemImagePart) Version() string { | ||
4035 | 115 | return s.version | ||
4036 | 116 | } | ||
4037 | 117 | |||
4038 | 118 | // Description returns the description | ||
4039 | 119 | func (s *SystemImagePart) Description() string { | ||
4040 | 120 | return "ubuntu-core description" | ||
4041 | 121 | } | ||
4042 | 122 | |||
4043 | 123 | // Hash returns the hash | ||
4044 | 124 | func (s *SystemImagePart) Hash() string { | ||
4045 | 125 | hasher := sha512.New() | ||
4046 | 126 | hasher.Write([]byte(s.versionDetails)) | ||
4047 | 127 | hexdigest := hex.EncodeToString(hasher.Sum(nil)) | ||
4048 | 128 | |||
4049 | 129 | return hexdigest | ||
4050 | 130 | } | ||
4051 | 131 | |||
4052 | 132 | // IsActive returns true if the snap is active | ||
4053 | 133 | func (s *SystemImagePart) IsActive() bool { | ||
4054 | 134 | return s.isActive | ||
4055 | 135 | } | ||
4056 | 136 | |||
4057 | 137 | // IsInstalled returns true if the snap is installed | ||
4058 | 138 | func (s *SystemImagePart) IsInstalled() bool { | ||
4059 | 139 | return s.isInstalled | ||
4060 | 140 | } | ||
4061 | 141 | |||
4062 | 142 | // InstalledSize returns the size of the installed snap | ||
4063 | 143 | func (s *SystemImagePart) InstalledSize() int64 { | ||
4064 | 144 | return -1 | ||
4065 | 145 | } | ||
4066 | 146 | |||
4067 | 147 | // DownloadSize returns the dowload size | ||
4068 | 148 | func (s *SystemImagePart) DownloadSize() int64 { | ||
4069 | 149 | return s.updateSize | ||
4070 | 150 | } | ||
4071 | 151 | |||
4072 | 152 | // Date returns the last update date | ||
4073 | 153 | func (s *SystemImagePart) Date() time.Time { | ||
4074 | 154 | return s.lastUpdate | ||
4075 | 155 | } | ||
4076 | 156 | |||
4077 | 157 | // SetActive sets the snap active | ||
4078 | 158 | func (s *SystemImagePart) SetActive(pb progress.Meter) (err error) { | ||
4079 | 159 | isNextBootOther := s.partition.IsNextBootOther() | ||
4080 | 160 | // active and no switch scheduled -> nothing to do | ||
4081 | 161 | if s.IsActive() && !isNextBootOther { | ||
4082 | 162 | return nil | ||
4083 | 163 | } | ||
4084 | 164 | // not currently active but switch scheduled already -> nothing to do | ||
4085 | 165 | if !s.IsActive() && isNextBootOther { | ||
4086 | 166 | return nil | ||
4087 | 167 | } | ||
4088 | 168 | |||
4089 | 169 | return s.partition.ToggleNextBoot() | ||
4090 | 170 | } | ||
4091 | 171 | |||
4092 | 172 | // override in tests | ||
4093 | 173 | var bootloaderDir = bootloaderDirImpl | ||
4094 | 174 | |||
4095 | 175 | func bootloaderDirImpl() string { | ||
4096 | 176 | return partition.BootloaderDir() | ||
4097 | 177 | } | ||
4098 | 178 | |||
4099 | 179 | // Install installs the snap | ||
4100 | 180 | func (s *SystemImagePart) Install(pb progress.Meter, flags InstallFlags) (name string, err error) { | ||
4101 | 181 | if provisioning.IsSideLoaded(bootloaderDir()) { | ||
4102 | 182 | return "", ErrSideLoaded | ||
4103 | 183 | } | ||
4104 | 184 | |||
4105 | 185 | if pb != nil { | ||
4106 | 186 | // ensure the progress finishes when we are done | ||
4107 | 187 | defer func() { | ||
4108 | 188 | pb.Finished() | ||
4109 | 189 | }() | ||
4110 | 190 | } | ||
4111 | 191 | |||
4112 | 192 | // Ensure there is always a kernel + initrd to boot with, even | ||
4113 | 193 | // if the update does not provide new versions. | ||
4114 | 194 | if s.needsBootAssetSync() { | ||
4115 | 195 | if pb != nil { | ||
4116 | 196 | pb.Notify("Syncing boot files") | ||
4117 | 197 | } | ||
4118 | 198 | err = s.partition.SyncBootloaderFiles(bootAssetFilePaths()) | ||
4119 | 199 | if err != nil { | ||
4120 | 200 | return "", err | ||
4121 | 201 | } | ||
4122 | 202 | } | ||
4123 | 203 | |||
4124 | 204 | // find out what config file to use, the other partition may be | ||
4125 | 205 | // empty so we need to fallback to the current one if it is | ||
4126 | 206 | configFile := systemImageClientConfig | ||
4127 | 207 | err = s.partition.RunWithOther(partition.RO, func(otherRoot string) (err error) { | ||
4128 | 208 | // XXX: Note that systemImageDownloadUpdate() requires | ||
4129 | 209 | // the s-i _client_ config file whereas otherIsEmpty() | ||
4130 | 210 | // checks the s-i _channel_ config file. | ||
4131 | 211 | otherConfigFile := filepath.Join(systemImageRoot, otherRoot, systemImageClientConfig) | ||
4132 | 212 | if !otherIsEmpty(otherRoot) && helpers.FileExists(otherConfigFile) { | ||
4133 | 213 | configFile = otherConfigFile | ||
4134 | 214 | } | ||
4135 | 215 | |||
4136 | 216 | // NOTE: we need to pass the config dir here | ||
4137 | 217 | configDir := filepath.Dir(configFile) | ||
4138 | 218 | return systemImageDownloadUpdate(configDir, pb) | ||
4139 | 219 | }) | ||
4140 | 220 | if err != nil { | ||
4141 | 221 | return "", err | ||
4142 | 222 | } | ||
4143 | 223 | |||
4144 | 224 | // Check that the final system state is as expected. | ||
4145 | 225 | if err = s.verifyUpgradeWasApplied(); err != nil { | ||
4146 | 226 | return "", err | ||
4147 | 227 | } | ||
4148 | 228 | |||
4149 | 229 | // XXX: ToggleNextBoot() calls handleAssets() (but not SyncBootloader | ||
4150 | 230 | // files :/) - handleAssets() may copy kernel/initramfs to the | ||
4151 | 231 | // sync mounted /boot/uboot, so its very slow, tell the user | ||
4152 | 232 | // at least that something is going on | ||
4153 | 233 | if pb != nil { | ||
4154 | 234 | pb.Notify("Updating boot files") | ||
4155 | 235 | } | ||
4156 | 236 | if err = s.partition.ToggleNextBoot(); err != nil { | ||
4157 | 237 | return "", err | ||
4158 | 238 | } | ||
4159 | 239 | return systemImagePartName, nil | ||
4160 | 240 | } | ||
4161 | 241 | |||
4162 | 242 | // Ensure the expected version update was applied to the expected partition. | ||
4163 | 243 | func (s *SystemImagePart) verifyUpgradeWasApplied() error { | ||
4164 | 244 | // The upgrade has now been applied, so check that the expected | ||
4165 | 245 | // update was applied by comparing "self" (which is the newest | ||
4166 | 246 | // system-image revision with that installed on the other | ||
4167 | 247 | // partition. | ||
4168 | 248 | |||
4169 | 249 | // Determine the latest installed part. | ||
4170 | 250 | latestPart := makeOtherPart(s.partition) | ||
4171 | 251 | if latestPart == nil { | ||
4172 | 252 | // If there is no other part, this system must be a | ||
4173 | 253 | // single rootfs one, so re-query current to find the | ||
4174 | 254 | // latest installed part. | ||
4175 | 255 | latestPart = makeCurrentPart(s.partition) | ||
4176 | 256 | } | ||
4177 | 257 | |||
4178 | 258 | if latestPart == nil { | ||
4179 | 259 | return &ErrUpgradeVerificationFailed{ | ||
4180 | 260 | msg: "could not find latest installed partition", | ||
4181 | 261 | } | ||
4182 | 262 | } | ||
4183 | 263 | |||
4184 | 264 | if s.version != latestPart.Version() { | ||
4185 | 265 | return &ErrUpgradeVerificationFailed{ | ||
4186 | 266 | msg: fmt.Sprintf("found %q but expected %q", latestPart.Version(), s.version), | ||
4187 | 267 | } | ||
4188 | 268 | } | ||
4189 | 269 | |||
4190 | 270 | return nil | ||
4191 | 271 | } | ||
4192 | 272 | |||
4193 | 273 | // Uninstall can not be used for "core" snaps | ||
4194 | 274 | func (s *SystemImagePart) Uninstall(progress.Meter) error { | ||
4195 | 275 | return ErrPackageNotRemovable | ||
4196 | 276 | } | ||
4197 | 277 | |||
4198 | 278 | // Config is used to to configure the snap | ||
4199 | 279 | func (s *SystemImagePart) Config(configuration []byte) (newConfig string, err error) { | ||
4200 | 280 | if cfg := string(configuration); cfg != "" { | ||
4201 | 281 | return coreconfig.Set(cfg) | ||
4202 | 282 | } | ||
4203 | 283 | |||
4204 | 284 | return coreconfig.Get() | ||
4205 | 285 | } | ||
4206 | 286 | |||
4207 | 287 | // NeedsReboot returns true if the snap becomes active on the next reboot | ||
4208 | 288 | func (s *SystemImagePart) NeedsReboot() bool { | ||
4209 | 289 | |||
4210 | 290 | if !s.IsActive() && s.partition.IsNextBootOther() { | ||
4211 | 291 | return true | ||
4212 | 292 | } | ||
4213 | 293 | |||
4214 | 294 | return false | ||
4215 | 295 | } | ||
4216 | 296 | |||
4217 | 297 | // MarkBootSuccessful marks the *currently* booted rootfs as "good" | ||
4218 | 298 | // (it booted :) | ||
4219 | 299 | // Note: Not part of the Part interface. | ||
4220 | 300 | func (s *SystemImagePart) MarkBootSuccessful() (err error) { | ||
4221 | 301 | |||
4222 | 302 | return s.partition.MarkBootSuccessful() | ||
4223 | 303 | } | ||
4224 | 304 | |||
4225 | 305 | // Channel returns the system-image-server channel used | ||
4226 | 306 | func (s *SystemImagePart) Channel() string { | ||
4227 | 307 | return s.channelName | ||
4228 | 308 | } | ||
4229 | 309 | |||
4230 | 310 | // Icon returns the icon path | ||
4231 | 311 | func (s *SystemImagePart) Icon() string { | ||
4232 | 312 | return "" | ||
4233 | 313 | } | ||
4234 | 314 | |||
4235 | 315 | // Frameworks returns the list of frameworks needed by the snap | ||
4236 | 316 | func (s *SystemImagePart) Frameworks() ([]string, error) { | ||
4237 | 317 | // system image parts can't depend on frameworks. | ||
4238 | 318 | return nil, nil | ||
4239 | 319 | } | ||
4240 | 320 | |||
4241 | 321 | // SystemImageRepository is the type used for the system-image-server | ||
4242 | 322 | type SystemImageRepository struct { | ||
4243 | 323 | partition partition.Interface | ||
4244 | 324 | } | ||
4245 | 325 | |||
4246 | 326 | // NewSystemImageRepository returns a new SystemImageRepository | ||
4247 | 327 | func NewSystemImageRepository() *SystemImageRepository { | ||
4248 | 328 | return &SystemImageRepository{partition: newPartition()} | ||
4249 | 329 | } | ||
4250 | 330 | |||
4251 | 331 | func makePartFromSystemImageConfigFile(p partition.Interface, channelIniPath string, isActive bool) (part Part, err error) { | ||
4252 | 332 | cfg := goconfigparser.New() | ||
4253 | 333 | f, err := os.Open(channelIniPath) | ||
4254 | 334 | if err != nil { | ||
4255 | 335 | return nil, err | ||
4256 | 336 | } | ||
4257 | 337 | defer f.Close() | ||
4258 | 338 | err = cfg.Read(f) | ||
4259 | 339 | if err != nil { | ||
4260 | 340 | logger.Noticef("Can not parse config %q: %v", channelIniPath, err) | ||
4261 | 341 | return nil, err | ||
4262 | 342 | } | ||
4263 | 343 | st, err := os.Stat(channelIniPath) | ||
4264 | 344 | if err != nil { | ||
4265 | 345 | logger.Noticef("Can not stat %q: %v", channelIniPath, err) | ||
4266 | 346 | return nil, err | ||
4267 | 347 | } | ||
4268 | 348 | |||
4269 | 349 | currentBuildNumber, err := cfg.Get("service", "build_number") | ||
4270 | 350 | versionDetails, err := cfg.Get("service", "version_detail") | ||
4271 | 351 | channelName, err := cfg.Get("service", "channel") | ||
4272 | 352 | return &SystemImagePart{ | ||
4273 | 353 | isActive: isActive, | ||
4274 | 354 | isInstalled: true, | ||
4275 | 355 | version: currentBuildNumber, | ||
4276 | 356 | versionDetails: versionDetails, | ||
4277 | 357 | channelName: channelName, | ||
4278 | 358 | lastUpdate: st.ModTime(), | ||
4279 | 359 | partition: p}, err | ||
4280 | 360 | } | ||
4281 | 361 | |||
4282 | 362 | // Returns the part associated with the current rootfs | ||
4283 | 363 | func makeCurrentPart(p partition.Interface) Part { | ||
4284 | 364 | configFile := filepath.Join(systemImageRoot, systemImageChannelConfig) | ||
4285 | 365 | part, err := makePartFromSystemImageConfigFile(p, configFile, true) | ||
4286 | 366 | if err != nil { | ||
4287 | 367 | return nil | ||
4288 | 368 | } | ||
4289 | 369 | return part | ||
4290 | 370 | } | ||
4291 | 371 | |||
4292 | 372 | // otherIsEmpty returns true if the rootfs path specified should be | ||
4293 | 373 | // considered empty. | ||
4294 | 374 | // | ||
4295 | 375 | // Note that the rootfs _may_ not actually be strictly empty, but it | ||
4296 | 376 | // must be considered empty if it is incomplete. | ||
4297 | 377 | // | ||
4298 | 378 | // This function encapsulates the heuristics to determine if the rootfs | ||
4299 | 379 | // is complete. | ||
4300 | 380 | func otherIsEmpty(root string) bool { | ||
4301 | 381 | configFile := filepath.Join(systemImageRoot, root, systemImageChannelConfig) | ||
4302 | 382 | |||
4303 | 383 | st, err := os.Stat(configFile) | ||
4304 | 384 | |||
4305 | 385 | if err == nil && st.Size() > 0 { | ||
4306 | 386 | // the channel config file exists and has a "reasonable" | ||
4307 | 387 | // size. The upgrade therefore completed successfully, | ||
4308 | 388 | // so consider the rootfs complete. | ||
4309 | 389 | return false | ||
4310 | 390 | } | ||
4311 | 391 | |||
4312 | 392 | // The upgrader pre-creates the s-i channel config file as a | ||
4313 | 393 | // zero-sized file when "other" is first provisioned. | ||
4314 | 394 | // | ||
4315 | 395 | // So if this file either does not exist, or has a size of zero | ||
4316 | 396 | // (indicating the upgrader failed to complete the s-i unpack on | ||
4317 | 397 | // a previous boot [which would have made configFile >0 bytes]), | ||
4318 | 398 | // the other partition is considered empty. | ||
4319 | 399 | |||
4320 | 400 | return true | ||
4321 | 401 | } | ||
4322 | 402 | |||
4323 | 403 | // Returns the part associated with the other rootfs (if any) | ||
4324 | 404 | func makeOtherPart(p partition.Interface) Part { | ||
4325 | 405 | var part Part | ||
4326 | 406 | err := p.RunWithOther(partition.RO, func(otherRoot string) (err error) { | ||
4327 | 407 | if otherIsEmpty(otherRoot) { | ||
4328 | 408 | return nil | ||
4329 | 409 | } | ||
4330 | 410 | |||
4331 | 411 | configFile := filepath.Join(systemImageRoot, otherRoot, systemImageChannelConfig) | ||
4332 | 412 | part, err = makePartFromSystemImageConfigFile(p, configFile, false) | ||
4333 | 413 | if err != nil { | ||
4334 | 414 | logger.Noticef("Can not make system-image part for %q: %v", configFile, err) | ||
4335 | 415 | } | ||
4336 | 416 | return err | ||
4337 | 417 | }) | ||
4338 | 418 | if err == partition.ErrNoDualPartition { | ||
4339 | 419 | return nil | ||
4340 | 420 | } | ||
4341 | 421 | return part | ||
4342 | 422 | } | ||
4343 | 423 | |||
4344 | 424 | // Description describes the repository | ||
4345 | 425 | func (s *SystemImageRepository) Description() string { | ||
4346 | 426 | return "SystemImageRepository" | ||
4347 | 427 | } | ||
4348 | 428 | |||
4349 | 429 | // Search searches the SystemImageRepository for the given terms | ||
4350 | 430 | func (s *SystemImageRepository) Search(terms string) (versions []Part, err error) { | ||
4351 | 431 | if strings.Contains(terms, systemImagePartName) { | ||
4352 | 432 | part := makeCurrentPart(s.partition) | ||
4353 | 433 | versions = append(versions, part) | ||
4354 | 434 | } | ||
4355 | 435 | return versions, err | ||
4356 | 436 | } | ||
4357 | 437 | |||
4358 | 438 | // Details returns details for the given snap | ||
4359 | 439 | func (s *SystemImageRepository) Details(snapName string) (versions []Part, err error) { | ||
4360 | 440 | if snapName == systemImagePartName { | ||
4361 | 441 | part := makeCurrentPart(s.partition) | ||
4362 | 442 | versions = append(versions, part) | ||
4363 | 443 | } | ||
4364 | 444 | return versions, err | ||
4365 | 445 | } | ||
4366 | 446 | |||
4367 | 447 | // Updates returns the available updates | ||
4368 | 448 | func (s *SystemImageRepository) Updates() (parts []Part, err error) { | ||
4369 | 449 | configFile := filepath.Join(systemImageRoot, systemImageChannelConfig) | ||
4370 | 450 | updateStatus, err := systemImageClientCheckForUpdates(configFile) | ||
4371 | 451 | |||
4372 | 452 | current := makeCurrentPart(s.partition) | ||
4373 | 453 | // no VersionCompare here because the channel provides a "order" and | ||
4374 | 454 | // that may go backwards when switching channels(?) | ||
4375 | 455 | if current.Version() != updateStatus.targetVersion { | ||
4376 | 456 | parts = append(parts, &SystemImagePart{ | ||
4377 | 457 | version: updateStatus.targetVersion, | ||
4378 | 458 | versionDetails: updateStatus.targetVersionDetails, | ||
4379 | 459 | lastUpdate: updateStatus.lastUpdate, | ||
4380 | 460 | updateSize: updateStatus.updateSize, | ||
4381 | 461 | channelName: current.(*SystemImagePart).channelName, | ||
4382 | 462 | partition: s.partition}) | ||
4383 | 463 | } | ||
4384 | 464 | |||
4385 | 465 | return parts, err | ||
4386 | 466 | } | ||
4387 | 467 | |||
4388 | 468 | // Installed returns the installed snaps from this repository | ||
4389 | 469 | func (s *SystemImageRepository) Installed() (parts []Part, err error) { | ||
4390 | 470 | // current partition | ||
4391 | 471 | curr := makeCurrentPart(s.partition) | ||
4392 | 472 | if curr != nil { | ||
4393 | 473 | parts = append(parts, curr) | ||
4394 | 474 | } | ||
4395 | 475 | |||
4396 | 476 | // other partition | ||
4397 | 477 | other := makeOtherPart(s.partition) | ||
4398 | 478 | if other != nil { | ||
4399 | 479 | parts = append(parts, other) | ||
4400 | 480 | } | ||
4401 | 481 | |||
4402 | 482 | return parts, err | ||
4403 | 483 | } | ||
4404 | 484 | |||
4405 | 485 | // needsSync determines if syncing boot assets is required | ||
4406 | 486 | func (s *SystemImagePart) needsBootAssetSync() bool { | ||
4407 | 487 | // current partition | ||
4408 | 488 | curr := makeCurrentPart(s.partition) | ||
4409 | 489 | if curr == nil { | ||
4410 | 490 | // this should never ever happen | ||
4411 | 491 | panic("current part does not exist") | ||
4412 | 492 | } | ||
4413 | 493 | |||
4414 | 494 | // other partition | ||
4415 | 495 | other := makeOtherPart(s.partition) | ||
4416 | 496 | if other == nil { | ||
4417 | 497 | return true | ||
4418 | 498 | } | ||
4419 | 499 | |||
4420 | 500 | // the idea here is that a channel change on the other | ||
4421 | 501 | // partition always triggers a full image download so | ||
4422 | 502 | // there is no need for syncing the assets (because the | ||
4423 | 503 | // kernel is included in the full image already) | ||
4424 | 504 | // | ||
4425 | 505 | // FIXME: its not entirely clear if this is true, there | ||
4426 | 506 | // is no mechanism to switch channels right now and all | ||
4427 | 507 | // the tests always switch both partitions | ||
4428 | 508 | if curr.Channel() != other.Channel() { | ||
4429 | 509 | return false | ||
4430 | 510 | } | ||
4431 | 511 | |||
4432 | 512 | // if the other version is already a higher version number | ||
4433 | 513 | // than the current one it means all the kernel updates | ||
4434 | 514 | // has happend already and we do not need to sync the | ||
4435 | 515 | // bootloader files, see: | ||
4436 | 516 | // https://bugs.launchpad.net/snappy/+bug/1474125 | ||
4437 | 517 | return VersionCompare(curr.Version(), other.Version()) > 0 | ||
4438 | 518 | } | ||
4439 | 519 | 0 | ||
4440 | === removed file 'snappy/systemimage_native.go' | |||
4441 | --- snappy/systemimage_native.go 2015-07-02 20:57:18 +0000 | |||
4442 | +++ snappy/systemimage_native.go 1970-01-01 00:00:00 +0000 | |||
4443 | @@ -1,211 +0,0 @@ | |||
4444 | 1 | // -*- Mode: Go; indent-tabs-mode: t -*- | ||
4445 | 2 | |||
4446 | 3 | /* | ||
4447 | 4 | * Copyright (C) 2014-2015 Canonical Ltd | ||
4448 | 5 | * | ||
4449 | 6 | * This program is free software: you can redistribute it and/or modify | ||
4450 | 7 | * it under the terms of the GNU General Public License version 3 as | ||
4451 | 8 | * published by the Free Software Foundation. | ||
4452 | 9 | * | ||
4453 | 10 | * This program is distributed in the hope that it will be useful, | ||
4454 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
4455 | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
4456 | 13 | * GNU General Public License for more details. | ||
4457 | 14 | * | ||
4458 | 15 | * You should have received a copy of the GNU General Public License | ||
4459 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
4460 | 17 | * | ||
4461 | 18 | */ | ||
4462 | 19 | |||
4463 | 20 | package snappy | ||
4464 | 21 | |||
4465 | 22 | import ( | ||
4466 | 23 | "bufio" | ||
4467 | 24 | "encoding/json" | ||
4468 | 25 | "fmt" | ||
4469 | 26 | "io" | ||
4470 | 27 | "io/ioutil" | ||
4471 | 28 | "net/http" | ||
4472 | 29 | "os" | ||
4473 | 30 | "os/exec" | ||
4474 | 31 | "path" | ||
4475 | 32 | "strings" | ||
4476 | 33 | "time" | ||
4477 | 34 | |||
4478 | 35 | "github.com/mvo5/goconfigparser" | ||
4479 | 36 | |||
4480 | 37 | "launchpad.net/snappy/helpers" | ||
4481 | 38 | "launchpad.net/snappy/progress" | ||
4482 | 39 | ) | ||
4483 | 40 | |||
4484 | 41 | var systemImageServer = "https://system-image.ubuntu.com/" | ||
4485 | 42 | |||
4486 | 43 | type updateStatus struct { | ||
4487 | 44 | targetVersion string | ||
4488 | 45 | targetVersionDetails string | ||
4489 | 46 | updateSize int64 | ||
4490 | 47 | lastUpdate time.Time | ||
4491 | 48 | } | ||
4492 | 49 | |||
4493 | 50 | type channelImage struct { | ||
4494 | 51 | Descripton string `json:"description,omitempty"` | ||
4495 | 52 | Type string `json:"type, omitempty"` | ||
4496 | 53 | Version int `json:"version, omitempty"` | ||
4497 | 54 | VersionDetails string `json:"version_detail, omitempty"` | ||
4498 | 55 | Files []channelImageFiles `json:"files"` | ||
4499 | 56 | } | ||
4500 | 57 | |||
4501 | 58 | type channelImageFiles struct { | ||
4502 | 59 | Size int64 `json:"size"` | ||
4503 | 60 | } | ||
4504 | 61 | |||
4505 | 62 | type channelImageGlobal struct { | ||
4506 | 63 | GeneratedAt string `json:"generated_at"` | ||
4507 | 64 | } | ||
4508 | 65 | |||
4509 | 66 | type channelJSON struct { | ||
4510 | 67 | Global channelImageGlobal `json:"global"` | ||
4511 | 68 | Images []channelImage `json:"images"` | ||
4512 | 69 | } | ||
4513 | 70 | |||
4514 | 71 | func systemImageClientCheckForUpdates(configFile string) (us updateStatus, err error) { | ||
4515 | 72 | cfg := goconfigparser.New() | ||
4516 | 73 | if err := cfg.ReadFile(configFile); err != nil { | ||
4517 | 74 | return us, err | ||
4518 | 75 | } | ||
4519 | 76 | channel, _ := cfg.Get("service", "channel") | ||
4520 | 77 | device, _ := cfg.Get("service", "device") | ||
4521 | 78 | |||
4522 | 79 | indexURL := systemImageServer + "/" + path.Join(channel, device, "index.json") | ||
4523 | 80 | |||
4524 | 81 | resp, err := http.Get(indexURL) | ||
4525 | 82 | if err != nil { | ||
4526 | 83 | return us, err | ||
4527 | 84 | } | ||
4528 | 85 | defer resp.Body.Close() | ||
4529 | 86 | |||
4530 | 87 | if resp.StatusCode != 200 { | ||
4531 | 88 | return us, fmt.Errorf("systemImageDbusProxy: unexpected http statusCode %v for %s", resp.StatusCode, indexURL) | ||
4532 | 89 | } | ||
4533 | 90 | |||
4534 | 91 | // and decode json | ||
4535 | 92 | var channelData channelJSON | ||
4536 | 93 | dec := json.NewDecoder(resp.Body) | ||
4537 | 94 | if err := dec.Decode(&channelData); err != nil { | ||
4538 | 95 | return us, err | ||
4539 | 96 | } | ||
4540 | 97 | |||
4541 | 98 | // global property | ||
4542 | 99 | us.lastUpdate, _ = time.Parse("Mon Jan 2 15:04:05 MST 2006", channelData.Global.GeneratedAt) | ||
4543 | 100 | |||
4544 | 101 | // FIXME: find latest image of type "full" here | ||
4545 | 102 | latestImage := channelData.Images[len(channelData.Images)-1] | ||
4546 | 103 | us.targetVersion = fmt.Sprintf("%d", latestImage.Version) | ||
4547 | 104 | us.targetVersionDetails = latestImage.VersionDetails | ||
4548 | 105 | |||
4549 | 106 | // FIXME: this is not accurate right now as it does not take | ||
4550 | 107 | // the deltas into account | ||
4551 | 108 | for _, f := range latestImage.Files { | ||
4552 | 109 | us.updateSize += f.Size | ||
4553 | 110 | } | ||
4554 | 111 | |||
4555 | 112 | return us, nil | ||
4556 | 113 | } | ||
4557 | 114 | |||
4558 | 115 | type genericJSON struct { | ||
4559 | 116 | Type string `json:"type, omitempty"` | ||
4560 | 117 | Message string `json:"msg, omitempty"` | ||
4561 | 118 | Now float64 `json:"now, omitempty"` | ||
4562 | 119 | Total float64 `json:"total, omitempty"` | ||
4563 | 120 | } | ||
4564 | 121 | |||
4565 | 122 | func parseSIProgress(pb progress.Meter, stdout io.Reader) error { | ||
4566 | 123 | if pb == nil { | ||
4567 | 124 | pb = &progress.NullProgress{} | ||
4568 | 125 | } | ||
4569 | 126 | |||
4570 | 127 | scanner := bufio.NewScanner(stdout) | ||
4571 | 128 | // s-i is funny, total changes during the runs | ||
4572 | 129 | total := 0.0 | ||
4573 | 130 | pb.Start("ubuntu-core", 100) | ||
4574 | 131 | |||
4575 | 132 | for scanner.Scan() { | ||
4576 | 133 | if os.Getenv("SNAPPY_DEBUG") != "" { | ||
4577 | 134 | fmt.Println(scanner.Text()) | ||
4578 | 135 | } | ||
4579 | 136 | |||
4580 | 137 | jsonStream := strings.NewReader(scanner.Text()) | ||
4581 | 138 | dec := json.NewDecoder(jsonStream) | ||
4582 | 139 | var genericData genericJSON | ||
4583 | 140 | if err := dec.Decode(&genericData); err != nil { | ||
4584 | 141 | // we ignore invalid json here and continue | ||
4585 | 142 | // the parsing if s-i-cli or ubuntu-core-upgrader | ||
4586 | 143 | // output something unexpected (like stray debug | ||
4587 | 144 | // output or whatnot) | ||
4588 | 145 | continue | ||
4589 | 146 | } | ||
4590 | 147 | |||
4591 | 148 | switch { | ||
4592 | 149 | case genericData.Type == "spinner": | ||
4593 | 150 | pb.Spin(genericData.Message) | ||
4594 | 151 | case genericData.Type == "error": | ||
4595 | 152 | return fmt.Errorf("error from %s: %s", systemImageCli, genericData.Message) | ||
4596 | 153 | case genericData.Type == "progress": | ||
4597 | 154 | if total != genericData.Total { | ||
4598 | 155 | total = genericData.Total | ||
4599 | 156 | pb.SetTotal(total) | ||
4600 | 157 | } | ||
4601 | 158 | pb.Set(genericData.Now) | ||
4602 | 159 | } | ||
4603 | 160 | } | ||
4604 | 161 | // ugly: avoid Spin() artifacts | ||
4605 | 162 | pb.Notify("\nApply done") | ||
4606 | 163 | |||
4607 | 164 | if err := scanner.Err(); err != nil { | ||
4608 | 165 | return err | ||
4609 | 166 | } | ||
4610 | 167 | |||
4611 | 168 | return nil | ||
4612 | 169 | } | ||
4613 | 170 | |||
4614 | 171 | func systemImageDownloadUpdate(configDir string, pb progress.Meter) (err error) { | ||
4615 | 172 | cmd := exec.Command(systemImageCli, "--progress", "json", "-C", configDir) | ||
4616 | 173 | |||
4617 | 174 | // collect progress over stdout pipe if we want progress | ||
4618 | 175 | var stdout io.Reader | ||
4619 | 176 | stdout, err = cmd.StdoutPipe() | ||
4620 | 177 | if err != nil { | ||
4621 | 178 | return err | ||
4622 | 179 | } | ||
4623 | 180 | |||
4624 | 181 | // collect error message (traceback etc in a separate goroutine) | ||
4625 | 182 | stderr, err := cmd.StderrPipe() | ||
4626 | 183 | if err != nil { | ||
4627 | 184 | return err | ||
4628 | 185 | } | ||
4629 | 186 | stderrCh := make(chan []byte) | ||
4630 | 187 | go func() { | ||
4631 | 188 | stderrContent, _ := ioutil.ReadAll(stderr) | ||
4632 | 189 | stderrCh <- stderrContent | ||
4633 | 190 | }() | ||
4634 | 191 | |||
4635 | 192 | // run it | ||
4636 | 193 | if err := cmd.Start(); err != nil { | ||
4637 | 194 | return err | ||
4638 | 195 | } | ||
4639 | 196 | |||
4640 | 197 | // and parse progress synchronously | ||
4641 | 198 | if err := parseSIProgress(pb, stdout); err != nil { | ||
4642 | 199 | return err | ||
4643 | 200 | } | ||
4644 | 201 | |||
4645 | 202 | // we need to read all of stderr *before* calling cmd.Wait() to avoid | ||
4646 | 203 | // a race, see docs for "os/exec:func (*Cmd) StdoutPipe" | ||
4647 | 204 | stderrContent := <-stderrCh | ||
4648 | 205 | if err := cmd.Wait(); err != nil { | ||
4649 | 206 | retCode, _ := helpers.ExitCode(err) | ||
4650 | 207 | return fmt.Errorf("%s failed with return code %v: %s", systemImageCli, retCode, string(stderrContent)) | ||
4651 | 208 | } | ||
4652 | 209 | |||
4653 | 210 | return err | ||
4654 | 211 | } | ||
4655 | 212 | 0 | ||
4656 | === removed file 'snappy/systemimage_native_test.go' | |||
4657 | --- snappy/systemimage_native_test.go 2015-06-02 20:46:07 +0000 | |||
4658 | +++ snappy/systemimage_native_test.go 1970-01-01 00:00:00 +0000 | |||
4659 | @@ -1,123 +0,0 @@ | |||
4660 | 1 | // -*- Mode: Go; indent-tabs-mode: t -*- | ||
4661 | 2 | |||
4662 | 3 | /* | ||
4663 | 4 | * Copyright (C) 2014-2015 Canonical Ltd | ||
4664 | 5 | * | ||
4665 | 6 | * This program is free software: you can redistribute it and/or modify | ||
4666 | 7 | * it under the terms of the GNU General Public License version 3 as | ||
4667 | 8 | * published by the Free Software Foundation. | ||
4668 | 9 | * | ||
4669 | 10 | * This program is distributed in the hope that it will be useful, | ||
4670 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
4671 | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
4672 | 13 | * GNU General Public License for more details. | ||
4673 | 14 | * | ||
4674 | 15 | * You should have received a copy of the GNU General Public License | ||
4675 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
4676 | 17 | * | ||
4677 | 18 | */ | ||
4678 | 19 | |||
4679 | 20 | package snappy | ||
4680 | 21 | |||
4681 | 22 | import ( | ||
4682 | 23 | "fmt" | ||
4683 | 24 | "io" | ||
4684 | 25 | "net/http" | ||
4685 | 26 | "net/http/httptest" | ||
4686 | 27 | "path/filepath" | ||
4687 | 28 | "time" | ||
4688 | 29 | |||
4689 | 30 | . "gopkg.in/check.v1" | ||
4690 | 31 | ) | ||
4691 | 32 | |||
4692 | 33 | /* acquired via: | ||
4693 | 34 | curl https://system-image.ubuntu.com/ubuntu-core/devel/generic_armhf/index.json | ||
4694 | 35 | */ | ||
4695 | 36 | const mockSystemImageIndexJSONTemplate = `{ | ||
4696 | 37 | "global": { | ||
4697 | 38 | "generated_at": "Thu Feb 19 18:26:23 UTC 2015" | ||
4698 | 39 | }, | ||
4699 | 40 | "images": [ | ||
4700 | 41 | { | ||
4701 | 42 | "description": ",version=1", | ||
4702 | 43 | "files": [ | ||
4703 | 44 | { | ||
4704 | 45 | "checksum": "3869be55a95db880862fe3dc3f5d643101736f674e9970a2099a883bd2bd2367", | ||
4705 | 46 | "order": 0, | ||
4706 | 47 | "path": "/pool/ubuntu-3f8ef532557aaec57264565b9948734bfe01f2a39886c816b0f69ae19e25f903.tar.xz", | ||
4707 | 48 | "signature": "/pool/ubuntu-3f8ef532557aaec57264565b9948734bfe01f2a39886c816b0f69ae19e25f903.tar.xz.asc", | ||
4708 | 49 | "size": 71081252 | ||
4709 | 50 | }, | ||
4710 | 51 | { | ||
4711 | 52 | "checksum": "f1a5ae7b4264e045ad58710a427168c02732e29130e5619dd2a82bbc8781b7c0", | ||
4712 | 53 | "order": 1, | ||
4713 | 54 | "path": "/pool/device-bf0a49e75a3deb99855f186906f17d7668e2dcc3a0b38f5feade3e6f7c75e9b8.tar.xz", | ||
4714 | 55 | "signature": "/pool/device-bf0a49e75a3deb99855f186906f17d7668e2dcc3a0b38f5feade3e6f7c75e9b8.tar.xz.asc", | ||
4715 | 56 | "size": 52589508 | ||
4716 | 57 | }, | ||
4717 | 58 | { | ||
4718 | 59 | "checksum": "a84fb49b505a797e1ddd6811d8cf79b5fe2a021198cd221e022318026dfb5417", | ||
4719 | 60 | "order": 2, | ||
4720 | 61 | "path": "/ubuntu-core/devel/generic_armhf/version-1.tar.xz", | ||
4721 | 62 | "signature": "/ubuntu-core/devel/generic_armhf/version-1.tar.xz.asc", | ||
4722 | 63 | "size": 328 | ||
4723 | 64 | } | ||
4724 | 65 | ], | ||
4725 | 66 | "type": "full", | ||
4726 | 67 | "version": 1, | ||
4727 | 68 | "version_detail": ",version=1" | ||
4728 | 69 | }, | ||
4729 | 70 | { | ||
4730 | 71 | "description": ",version=2", | ||
4731 | 72 | "files": [ | ||
4732 | 73 | { | ||
4733 | 74 | "checksum": "8be1d2c82a6d785089de91febf70aa7aa790c23fae4f2e02c5dc4dfc343d005a", | ||
4734 | 75 | "order": 0, | ||
4735 | 76 | "path": "/pool/ubuntu-0e6f7a24f941a2fbe27c922b5158da9ab177afaa851c28c002a22f4166f3ec01.tar.xz", | ||
4736 | 77 | "signature": "/pool/ubuntu-0e6f7a24f941a2fbe27c922b5158da9ab177afaa851c28c002a22f4166f3ec01.tar.xz.asc", | ||
4737 | 78 | "size": 70576648 | ||
4738 | 79 | }, | ||
4739 | 80 | { | ||
4740 | 81 | "checksum": "f1a5ae7b4264e045ad58710a427168c02732e29130e5619dd2a82bbc8781b7c0", | ||
4741 | 82 | "order": 1, | ||
4742 | 83 | "path": "/pool/device-bf0a49e75a3deb99855f186906f17d7668e2dcc3a0b38f5feade3e6f7c75e9b8.tar.xz", | ||
4743 | 84 | "signature": "/pool/device-bf0a49e75a3deb99855f186906f17d7668e2dcc3a0b38f5feade3e6f7c75e9b8.tar.xz.asc", | ||
4744 | 85 | "size": 52589508 | ||
4745 | 86 | }, | ||
4746 | 87 | { | ||
4747 | 88 | "checksum": "242f0198b4bd0b63c943e7ff7eb46889753c725cb5b127a7bb76600bcecee544", | ||
4748 | 89 | "order": 2, | ||
4749 | 90 | "path": "/ubuntu-core/devel/generic_armhf/version-2.tar.xz", | ||
4750 | 91 | "signature": "/ubuntu-core/devel/generic_armhf/version-2.tar.xz.asc", | ||
4751 | 92 | "size": 332 | ||
4752 | 93 | } | ||
4753 | 94 | ], | ||
4754 | 95 | "type": "full", | ||
4755 | 96 | "version": %s, | ||
4756 | 97 | "version_detail": ",version=2" | ||
4757 | 98 | } | ||
4758 | 99 | ] | ||
4759 | 100 | }` | ||
4760 | 101 | |||
4761 | 102 | var mockSystemImageIndexJSON = fmt.Sprintf(mockSystemImageIndexJSONTemplate, "2") | ||
4762 | 103 | |||
4763 | 104 | func runMockSystemImageWebServer() *httptest.Server { | ||
4764 | 105 | mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
4765 | 106 | io.WriteString(w, mockSystemImageIndexJSON) | ||
4766 | 107 | })) | ||
4767 | 108 | if mockServer == nil { | ||
4768 | 109 | return nil | ||
4769 | 110 | } | ||
4770 | 111 | systemImageServer = mockServer.URL | ||
4771 | 112 | return mockServer | ||
4772 | 113 | } | ||
4773 | 114 | |||
4774 | 115 | func (s *SITestSuite) TestCheckForUpdates(c *C) { | ||
4775 | 116 | mockConfigFile := filepath.Join(c.MkDir(), "channel.init") | ||
4776 | 117 | makeFakeSystemImageChannelConfig(c, mockConfigFile, "1") | ||
4777 | 118 | updateStatus, err := systemImageClientCheckForUpdates(mockConfigFile) | ||
4778 | 119 | c.Assert(err, IsNil) | ||
4779 | 120 | c.Assert(updateStatus.targetVersion, Equals, "2") | ||
4780 | 121 | c.Assert(updateStatus.targetVersionDetails, Equals, ",version=2") | ||
4781 | 122 | c.Assert(updateStatus.lastUpdate, Equals, time.Date(2015, 02, 19, 18, 26, 23, 0, time.UTC)) | ||
4782 | 123 | } | ||
4783 | 124 | 0 | ||
4784 | === removed file 'snappy/systemimage_test.go' | |||
4785 | --- snappy/systemimage_test.go 2015-07-15 07:53:43 +0000 | |||
4786 | +++ snappy/systemimage_test.go 1970-01-01 00:00:00 +0000 | |||
4787 | @@ -1,486 +0,0 @@ | |||
4788 | 1 | // -*- Mode: Go; indent-tabs-mode: t -*- | ||
4789 | 2 | |||
4790 | 3 | /* | ||
4791 | 4 | * Copyright (C) 2014-2015 Canonical Ltd | ||
4792 | 5 | * | ||
4793 | 6 | * This program is free software: you can redistribute it and/or modify | ||
4794 | 7 | * it under the terms of the GNU General Public License version 3 as | ||
4795 | 8 | * published by the Free Software Foundation. | ||
4796 | 9 | * | ||
4797 | 10 | * This program is distributed in the hope that it will be useful, | ||
4798 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
4799 | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
4800 | 13 | * GNU General Public License for more details. | ||
4801 | 14 | * | ||
4802 | 15 | * You should have received a copy of the GNU General Public License | ||
4803 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
4804 | 17 | * | ||
4805 | 18 | */ | ||
4806 | 19 | |||
4807 | 20 | package snappy | ||
4808 | 21 | |||
4809 | 22 | import ( | ||
4810 | 23 | "fmt" | ||
4811 | 24 | "io/ioutil" | ||
4812 | 25 | "net/http/httptest" | ||
4813 | 26 | "os" | ||
4814 | 27 | "path/filepath" | ||
4815 | 28 | "strings" | ||
4816 | 29 | "testing" | ||
4817 | 30 | |||
4818 | 31 | "launchpad.net/snappy/partition" | ||
4819 | 32 | "launchpad.net/snappy/provisioning" | ||
4820 | 33 | |||
4821 | 34 | . "gopkg.in/check.v1" | ||
4822 | 35 | ) | ||
4823 | 36 | |||
4824 | 37 | // Hook up check.v1 into the "go test" runner | ||
4825 | 38 | func Test(t *testing.T) { TestingT(t) } | ||
4826 | 39 | |||
4827 | 40 | type SITestSuite struct { | ||
4828 | 41 | systemImage *SystemImageRepository | ||
4829 | 42 | mockSystemImageWebServer *httptest.Server | ||
4830 | 43 | } | ||
4831 | 44 | |||
4832 | 45 | var _ = Suite(&SITestSuite{}) | ||
4833 | 46 | |||
4834 | 47 | func (s *SITestSuite) SetUpTest(c *C) { | ||
4835 | 48 | newPartition = func() (p partition.Interface) { | ||
4836 | 49 | return new(MockPartition) | ||
4837 | 50 | } | ||
4838 | 51 | |||
4839 | 52 | s.systemImage = NewSystemImageRepository() | ||
4840 | 53 | c.Assert(s, NotNil) | ||
4841 | 54 | // setup alternative root for system image | ||
4842 | 55 | tempdir := c.MkDir() | ||
4843 | 56 | systemImageRoot = tempdir | ||
4844 | 57 | |||
4845 | 58 | makeFakeSystemImageChannelConfig(c, filepath.Join(tempdir, systemImageChannelConfig), "1") | ||
4846 | 59 | // setup fake /other partition | ||
4847 | 60 | makeFakeSystemImageChannelConfig(c, filepath.Join(tempdir, "other", systemImageChannelConfig), "0") | ||
4848 | 61 | |||
4849 | 62 | // run test webserver instead of talking to the real one | ||
4850 | 63 | // | ||
4851 | 64 | // The mock webserver versions "1" and "2" | ||
4852 | 65 | s.mockSystemImageWebServer = runMockSystemImageWebServer() | ||
4853 | 66 | c.Assert(s.mockSystemImageWebServer, NotNil) | ||
4854 | 67 | |||
4855 | 68 | // create mock system-image-cli | ||
4856 | 69 | systemImageCli = makeMockSystemImageCli(c, tempdir) | ||
4857 | 70 | } | ||
4858 | 71 | |||
4859 | 72 | func (s *SITestSuite) TearDownTest(c *C) { | ||
4860 | 73 | s.mockSystemImageWebServer.Close() | ||
4861 | 74 | systemImageRoot = "/" | ||
4862 | 75 | bootloaderDir = bootloaderDirImpl | ||
4863 | 76 | } | ||
4864 | 77 | |||
4865 | 78 | func makeMockSystemImageCli(c *C, tempdir string) string { | ||
4866 | 79 | s := `#!/bin/sh | ||
4867 | 80 | |||
4868 | 81 | printf '{"type": "progress", "now": 20, "total":100}\n' | ||
4869 | 82 | printf '{"type": "progress", "now": 40, "total":100}\n' | ||
4870 | 83 | printf '{"type": "progress", "now": 60, "total":100}\n' | ||
4871 | 84 | printf '{"type": "progress", "now": 80, "total":100}\n' | ||
4872 | 85 | printf '{"type": "progress", "now": 100, "total":100}\n' | ||
4873 | 86 | printf '{"type": "spinner", "msg": "Applying"}\n' | ||
4874 | 87 | ` | ||
4875 | 88 | mockScript := filepath.Join(tempdir, "system-image-cli") | ||
4876 | 89 | err := ioutil.WriteFile(mockScript, []byte(s), 0755) | ||
4877 | 90 | c.Assert(err, IsNil) | ||
4878 | 91 | |||
4879 | 92 | return mockScript | ||
4880 | 93 | } | ||
4881 | 94 | |||
4882 | 95 | func makeFakeSystemImageChannelConfig(c *C, cfgPath, buildNumber string) { | ||
4883 | 96 | os.MkdirAll(filepath.Dir(cfgPath), 0775) | ||
4884 | 97 | f, err := os.OpenFile(cfgPath, os.O_CREATE|os.O_RDWR, 0664) | ||
4885 | 98 | c.Assert(err, IsNil) | ||
4886 | 99 | defer f.Close() | ||
4887 | 100 | f.Write([]byte(fmt.Sprintf(` | ||
4888 | 101 | [service] | ||
4889 | 102 | base: system-image.ubuntu.com | ||
4890 | 103 | http_port: 80 | ||
4891 | 104 | https_port: 443 | ||
4892 | 105 | channel: ubuntu-core/devel-proposed | ||
4893 | 106 | device: generic_amd64 | ||
4894 | 107 | build_number: %s | ||
4895 | 108 | version_detail: ubuntu=20141206,raw-device=20141206,version=77 | ||
4896 | 109 | `, buildNumber))) | ||
4897 | 110 | } | ||
4898 | 111 | |||
4899 | 112 | func (s *SITestSuite) TestTestInstalled(c *C) { | ||
4900 | 113 | // whats installed | ||
4901 | 114 | parts, err := s.systemImage.Installed() | ||
4902 | 115 | c.Assert(err, IsNil) | ||
4903 | 116 | // we have one active and one inactive | ||
4904 | 117 | c.Assert(parts, HasLen, 2) | ||
4905 | 118 | c.Assert(parts[0].Name(), Equals, systemImagePartName) | ||
4906 | 119 | c.Assert(parts[0].Origin(), Equals, systemImagePartOrigin) | ||
4907 | 120 | c.Assert(parts[0].Vendor(), Equals, systemImagePartVendor) | ||
4908 | 121 | c.Assert(parts[0].Version(), Equals, "1") | ||
4909 | 122 | c.Assert(parts[0].Hash(), Equals, "e09c13f68fccef3b2fe0f5c8ff5c61acf2173b170b1f2a3646487147690b0970ef6f2c555d7bcb072035f29ee4ea66a6df7f6bb320d358d3a7d78a0c37a8a549") | ||
4910 | 123 | c.Assert(parts[0].IsActive(), Equals, true) | ||
4911 | 124 | c.Assert(parts[0].Channel(), Equals, "ubuntu-core/devel-proposed") | ||
4912 | 125 | |||
4913 | 126 | // second partition is not active and has a different version | ||
4914 | 127 | c.Assert(parts[1].IsActive(), Equals, false) | ||
4915 | 128 | c.Assert(parts[1].Version(), Equals, "0") | ||
4916 | 129 | } | ||
4917 | 130 | |||
4918 | 131 | func (s *SITestSuite) TestUpdateNoUpdate(c *C) { | ||
4919 | 132 | mockSystemImageIndexJSON = fmt.Sprintf(mockSystemImageIndexJSONTemplate, "1") | ||
4920 | 133 | parts, err := s.systemImage.Updates() | ||
4921 | 134 | c.Assert(err, IsNil) | ||
4922 | 135 | c.Assert(parts, HasLen, 0) | ||
4923 | 136 | } | ||
4924 | 137 | |||
4925 | 138 | func (s *SITestSuite) TestUpdateHasUpdate(c *C) { | ||
4926 | 139 | // add a update | ||
4927 | 140 | mockSystemImageIndexJSON = fmt.Sprintf(mockSystemImageIndexJSONTemplate, "2") | ||
4928 | 141 | parts, err := s.systemImage.Updates() | ||
4929 | 142 | c.Assert(err, IsNil) | ||
4930 | 143 | c.Assert(parts, HasLen, 1) | ||
4931 | 144 | c.Assert(parts[0].Name(), Equals, "ubuntu-core") | ||
4932 | 145 | c.Assert(parts[0].Version(), Equals, "2") | ||
4933 | 146 | c.Assert(parts[0].DownloadSize(), Equals, int64(123166488)) | ||
4934 | 147 | } | ||
4935 | 148 | |||
4936 | 149 | type MockPartition struct { | ||
4937 | 150 | toggleNextBootCalled bool | ||
4938 | 151 | markBootSuccessfulCalled bool | ||
4939 | 152 | syncBootloaderFilesCalled bool | ||
4940 | 153 | } | ||
4941 | 154 | |||
4942 | 155 | func (p *MockPartition) ToggleNextBoot() error { | ||
4943 | 156 | p.toggleNextBootCalled = true | ||
4944 | 157 | return nil | ||
4945 | 158 | } | ||
4946 | 159 | |||
4947 | 160 | func (p *MockPartition) MarkBootSuccessful() error { | ||
4948 | 161 | p.markBootSuccessfulCalled = true | ||
4949 | 162 | return nil | ||
4950 | 163 | } | ||
4951 | 164 | func (p *MockPartition) SyncBootloaderFiles(map[string]string) error { | ||
4952 | 165 | p.syncBootloaderFilesCalled = true | ||
4953 | 166 | return nil | ||
4954 | 167 | } | ||
4955 | 168 | func (p *MockPartition) IsNextBootOther() bool { | ||
4956 | 169 | return false | ||
4957 | 170 | } | ||
4958 | 171 | |||
4959 | 172 | func (p *MockPartition) RunWithOther(option partition.MountOption, f func(otherRoot string) (err error)) (err error) { | ||
4960 | 173 | return f("/other") | ||
4961 | 174 | } | ||
4962 | 175 | |||
4963 | 176 | // used by GetBootLoaderDir(), used to test sideload logic. | ||
4964 | 177 | var tempBootDir string | ||
4965 | 178 | |||
4966 | 179 | func (p *MockPartition) BootloaderDir() string { | ||
4967 | 180 | return tempBootDir | ||
4968 | 181 | } | ||
4969 | 182 | |||
4970 | 183 | func (s *SITestSuite) TestSystemImagePartInstallUpdatesPartition(c *C) { | ||
4971 | 184 | // FIXME: ideally we would change the version to "2" as a side-effect | ||
4972 | 185 | // of calling sp.Install() we need to update it because the | ||
4973 | 186 | // sp.Install() will verify that it got applied | ||
4974 | 187 | makeFakeSystemImageChannelConfig(c, filepath.Join(systemImageRoot, "other", systemImageChannelConfig), "2") | ||
4975 | 188 | |||
4976 | 189 | // add a update | ||
4977 | 190 | mockSystemImageIndexJSON = fmt.Sprintf(mockSystemImageIndexJSONTemplate, "2") | ||
4978 | 191 | parts, err := s.systemImage.Updates() | ||
4979 | 192 | c.Assert(err, IsNil) | ||
4980 | 193 | |||
4981 | 194 | sp := parts[0].(*SystemImagePart) | ||
4982 | 195 | mockPartition := MockPartition{} | ||
4983 | 196 | sp.partition = &mockPartition | ||
4984 | 197 | |||
4985 | 198 | pb := &MockProgressMeter{} | ||
4986 | 199 | // do the install | ||
4987 | 200 | _, err = sp.Install(pb, 0) | ||
4988 | 201 | c.Assert(err, IsNil) | ||
4989 | 202 | c.Assert(mockPartition.toggleNextBootCalled, Equals, true) | ||
4990 | 203 | c.Assert(pb.total, Equals, 100.0) | ||
4991 | 204 | c.Assert(pb.spin, Equals, true) | ||
4992 | 205 | c.Assert(pb.spinMsg, Equals, "Applying") | ||
4993 | 206 | c.Assert(pb.finished, Equals, true) | ||
4994 | 207 | c.Assert(pb.progress, DeepEquals, []float64{20.0, 40.0, 60.0, 80.0, 100.0}) | ||
4995 | 208 | } | ||
4996 | 209 | |||
4997 | 210 | func (s *SITestSuite) TestSystemImagePartInstallUpdatesBroken(c *C) { | ||
4998 | 211 | // fake a broken upgrade | ||
4999 | 212 | scriptContent := `#!/bin/sh | ||
5000 | 213 | printf '{"type": "error", "msg": "some error msg"}\n' |
The diff has been truncated for viewing.