forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernel_os.go
203 lines (174 loc) · 4.95 KB
/
kernel_os.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* 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 boot
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/partition"
"github.com/snapcore/snapd/release"
"github.com/snapcore/snapd/snap"
)
// RemoveKernelAssets removes the unpacked kernel/initrd for the given
// kernel snap.
func RemoveKernelAssets(s snap.PlaceInfo) error {
bootloader, err := partition.FindBootloader()
if err != nil {
return fmt.Errorf("no not remove kernel assets: %s", err)
}
// remove the kernel blob
blobName := filepath.Base(s.MountFile())
dstDir := filepath.Join(bootloader.Dir(), blobName)
if err := os.RemoveAll(dstDir); err != nil {
return err
}
return nil
}
func copyAll(src, dst string) error {
if output, err := exec.Command("cp", "-aLv", src, dst).CombinedOutput(); err != nil {
return fmt.Errorf("cannot copy %q -> %q: %s (%s)", src, dst, err, output)
}
return nil
}
// ExtractKernelAssets extracts kernel/initrd/dtb data from the given
// kernel snap, if required, to a versioned bootloader directory so
// that the bootloader can use it.
func ExtractKernelAssets(s *snap.Info, snapf snap.Container) error {
if s.Type != snap.TypeKernel {
return fmt.Errorf("cannot extract kernel assets from snap type %q", s.Type)
}
bootloader, err := partition.FindBootloader()
if err != nil {
return fmt.Errorf("cannot extract kernel assets: %s", err)
}
if bootloader.Name() == "grub" {
return nil
}
// now do the kernel specific bits
blobName := filepath.Base(s.MountFile())
dstDir := filepath.Join(bootloader.Dir(), blobName)
if err := os.MkdirAll(dstDir, 0755); err != nil {
return err
}
dir, err := os.Open(dstDir)
if err != nil {
return err
}
defer dir.Close()
for _, src := range []string{"kernel.img", "initrd.img"} {
if err := snapf.Unpack(src, dstDir); err != nil {
return err
}
if err := dir.Sync(); err != nil {
return err
}
}
if err := snapf.Unpack("dtbs/*", dstDir); err != nil {
return err
}
return dir.Sync()
}
// SetNextBoot will schedule the given OS or kernel snap to be used in
// the next boot
func SetNextBoot(s *snap.Info) error {
if release.OnClassic {
return nil
}
if s.Type != snap.TypeOS && s.Type != snap.TypeKernel {
return nil
}
bootloader, err := partition.FindBootloader()
if err != nil {
return fmt.Errorf("cannot set next boot: %s", err)
}
var nextBoot, goodBoot string
switch s.Type {
case snap.TypeOS:
nextBoot = "snap_try_core"
goodBoot = "snap_core"
case snap.TypeKernel:
nextBoot = "snap_try_kernel"
goodBoot = "snap_kernel"
}
blobName := filepath.Base(s.MountFile())
// check if we actually need to do anything, i.e. the exact same
// kernel/core revision got installed again (e.g. firstboot)
m, err := bootloader.GetBootVars(goodBoot)
if err != nil {
return err
}
if m[goodBoot] == blobName {
return nil
}
return bootloader.SetBootVars(map[string]string{
nextBoot: blobName,
"snap_mode": "try",
})
}
// KernelOrOsRebootRequired returns whether a reboot is required to swith to the given OS or kernel snap.
func KernelOrOsRebootRequired(s *snap.Info) bool {
if s.Type != snap.TypeKernel && s.Type != snap.TypeOS {
return false
}
bootloader, err := partition.FindBootloader()
if err != nil {
logger.Noticef("cannot get boot settings: %s", err)
return false
}
var nextBoot, goodBoot string
switch s.Type {
case snap.TypeKernel:
nextBoot = "snap_try_kernel"
goodBoot = "snap_kernel"
case snap.TypeOS:
nextBoot = "snap_try_core"
goodBoot = "snap_core"
}
m, err := bootloader.GetBootVars(nextBoot, goodBoot)
if err != nil {
return false
}
squashfsName := filepath.Base(s.MountFile())
if m[nextBoot] == squashfsName && m[goodBoot] != m[nextBoot] {
return true
}
return false
}
// InUse checks if the given name/revision is used in the
// boot environment
func InUse(name string, rev snap.Revision) bool {
bootloader, err := partition.FindBootloader()
if err != nil {
logger.Noticef("cannot get boot settings: %s", err)
return false
}
bootVars, err := bootloader.GetBootVars("snap_kernel", "snap_try_kernel", "snap_core", "snap_try_core")
if err != nil {
logger.Noticef("cannot get boot vars: %s", err)
return false
}
snapFile := filepath.Base(snap.MountFile(name, rev))
for _, bootVar := range bootVars {
if bootVar == snapFile {
return true
}
}
return false
}