forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provisioning.go
136 lines (116 loc) · 3.37 KB
/
provisioning.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* Copyright (C) 2014-2015 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package provisioning
import (
"fmt"
"io/ioutil"
"path/filepath"
"time"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/partition"
"gopkg.in/yaml.v2"
)
const (
// InstallYamlFile is the name of the file created by
// ubuntu-device-flash(1), created at system installation time,
// that contains metadata on the installation.
//
// XXX: Public for ubuntu-device-flash(1)
InstallYamlFile = "install.yaml"
)
var (
// simplify testing
findBootloader = partition.FindBootloader
)
// ErrNoInstallYaml is emitted when InstallYamlFile does not exist.
type ErrNoInstallYaml struct {
origErr error
}
func (e *ErrNoInstallYaml) Error() string {
return fmt.Sprintf("failed to read provisioning data: %s", e.origErr)
}
// InstallMeta encapsulates the metadata for a system install.
type InstallMeta struct {
Timestamp time.Time
}
// InstallTool encapsulates metadata on the tool used to create the
// system image.
type InstallTool struct {
Name string
Path string
Version string
}
// InstallOptions summarises the options used when creating the system image.
type InstallOptions struct {
Size int64 `yaml:"size"`
SizeUnit string `yaml:"size-unit"`
Output string
Channel string
DevicePart string `yaml:"device-part,omitempty"`
Gadget string
OS string
Kernel string
DeveloperMode bool `yaml:"developer-mode,omitempty"`
}
// InstallYaml represents 'InstallYamlFile'
//
// XXX: Public for ubuntu-device-flash
type InstallYaml struct {
InstallMeta `yaml:"meta"`
InstallTool `yaml:"tool"`
InstallOptions `yaml:"options"`
}
func parseInstallYaml(path string) (*InstallYaml, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, &ErrNoInstallYaml{origErr: err}
}
return parseInstallYamlData(data)
}
func parseInstallYamlData(yamlData []byte) (*InstallYaml, error) {
var i InstallYaml
err := yaml.Unmarshal(yamlData, &i)
if err != nil {
logger.Noticef("Cannot parse install.yaml %q", yamlData)
return nil, err
}
return &i, nil
}
// InDeveloperMode returns true if the image was build with --developer-mode
func InDeveloperMode() bool {
// FIXME: this is a bit terrible, we really need a single
// bootloader dir like /boot or /boot/loader
// instead of having to query the partition code
bootloader, err := findBootloader()
if err != nil {
// can only happy on systems like ubuntu classic
// that are not full snappy systems
return false
}
file := filepath.Join(bootloader.Dir(), InstallYamlFile)
if !osutil.FileExists(file) {
// no idea
return false
}
InstallYaml, err := parseInstallYaml(file)
if err != nil {
// no idea
return false
}
return InstallYaml.InstallOptions.DeveloperMode
}