forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernel.go
179 lines (155 loc) · 4.39 KB
/
kernel.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
package snappy
// -*- 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/>.
*
*/
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/ubuntu-core/snappy/partition"
"github.com/ubuntu-core/snappy/pkg"
"github.com/ubuntu-core/snappy/pkg/squashfs"
"github.com/ubuntu-core/snappy/progress"
)
// dropVersionSuffix drops the kernel/initrd version suffix,
// e.g. "vmlinuz-4.1.0" -> "vmlinuz".
func dropVersionSuffix(name string) string {
name = filepath.Base(name)
return strings.SplitN(name, "-", 2)[0]
}
// override in tests
var bootloaderDir = partition.BootloaderDir
// removeKernelAssets removes the unpacked kernel/initrd for the given
// kernel snap
func removeKernelAssets(s *SnapPart, inter interacter) error {
if s.m.Type != pkg.TypeKernel {
return fmt.Errorf("can not remove kernel assets from snap type %q", s.Type())
}
// remove the kernel blob
blobName := filepath.Base(squashfs.BlobPath(s.basedir))
dstDir := filepath.Join(bootloaderDir(), blobName)
if err := os.RemoveAll(dstDir); err != nil {
return err
}
return nil
}
// extractKernelAssets extracts kernel/initrd/dtb data from the given
// SnapPart to a versionized bootloader directory so that the bootloader
// can use it.
func extractKernelAssets(s *SnapPart, inter progress.Meter, flags InstallFlags) error {
if s.m.Type != pkg.TypeKernel {
return fmt.Errorf("can not extract kernel assets from snap type %q", s.Type())
}
// check if we are on a "grub" system. if so, no need to unpack
// the kernel
if oem, err := getGadget(); err == nil {
if oem.Gadget.Hardware.Bootloader == "grub" {
return nil
}
}
// FIXME: feels wrong to use the basedir here, need something better
//
// now do the kernel specific bits
blobName := filepath.Base(squashfs.BlobPath(s.basedir))
dstDir := filepath.Join(bootloaderDir(), 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{s.m.Kernel, s.m.Initrd} {
if src == "" {
continue
}
if err := s.deb.Unpack(src, dstDir); err != nil {
return err
}
src = filepath.Join(dstDir, src)
dst := filepath.Join(dstDir, dropVersionSuffix(src))
if err := os.Rename(src, dst); err != nil {
return err
}
if err := dir.Sync(); err != nil {
return err
}
}
if s.m.Dtbs != "" {
src := filepath.Join(s.m.Dtbs, "*")
dst := dstDir
if err := s.deb.Unpack(src, dst); err != nil {
return err
}
}
return dir.Sync()
}
// used in the unit tests
var setBootVar = partition.SetBootVar
// setNextBoot will schedule the given os or kernel snap to be used in
// the next boot
func setNextBoot(s *SnapPart) error {
if s.m.Type != pkg.TypeOS && s.m.Type != pkg.TypeKernel {
return nil
}
var bootvar string
switch s.m.Type {
case pkg.TypeOS:
bootvar = "snappy_os"
case pkg.TypeKernel:
bootvar = "snappy_kernel"
}
blobName := filepath.Base(squashfs.BlobPath(s.basedir))
if err := setBootVar(bootvar, blobName); err != nil {
return err
}
if err := setBootVar("snappy_mode", "try"); err != nil {
return err
}
return nil
}
// used in the unit tests
var getBootVar = partition.GetBootVar
func kernelOrOsRebootRequired(s *SnapPart) bool {
if s.m.Type != pkg.TypeKernel && s.m.Type != pkg.TypeOS {
return false
}
var nextBoot, goodBoot string
switch s.m.Type {
case pkg.TypeKernel:
nextBoot = "snappy_kernel"
goodBoot = "snappy_good_kernel"
case pkg.TypeOS:
nextBoot = "snappy_os"
goodBoot = "snappy_good_os"
}
nextBootVer, err := getBootVar(nextBoot)
if err != nil {
return false
}
goodBootVer, err := getBootVar(goodBoot)
if err != nil {
return false
}
squashfsName := filepath.Base(stripGlobalRootDir(squashfs.BlobPath(s.basedir)))
if nextBootVer == squashfsName && goodBootVer != nextBootVer {
return true
}
return false
}