github.com/rigado/snapd@v2.42.5-go-mod+incompatible/bootloader/grub.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2014-2015 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package bootloader 21 22 import ( 23 "os" 24 "path/filepath" 25 26 "github.com/snapcore/snapd/bootloader/grubenv" 27 "github.com/snapcore/snapd/osutil" 28 "github.com/snapcore/snapd/snap" 29 ) 30 31 type grub struct { 32 rootdir string 33 } 34 35 // newGrub create a new Grub bootloader object 36 func newGrub(rootdir string) Bootloader { 37 g := &grub{rootdir: rootdir} 38 if !osutil.FileExists(g.ConfigFile()) { 39 return nil 40 } 41 42 return g 43 } 44 45 func (g *grub) Name() string { 46 return "grub" 47 } 48 49 func (g *grub) setRootDir(rootdir string) { 50 g.rootdir = rootdir 51 } 52 53 func (g *grub) dir() string { 54 if g.rootdir == "" { 55 panic("internal error: unset rootdir") 56 } 57 return filepath.Join(g.rootdir, "/boot/grub") 58 } 59 60 func (g *grub) ConfigFile() string { 61 return filepath.Join(g.dir(), "grub.cfg") 62 } 63 64 func (g *grub) envFile() string { 65 return filepath.Join(g.dir(), "grubenv") 66 } 67 68 func (g *grub) GetBootVars(names ...string) (map[string]string, error) { 69 out := make(map[string]string) 70 71 env := grubenv.NewEnv(g.envFile()) 72 if err := env.Load(); err != nil { 73 return nil, err 74 } 75 76 for _, name := range names { 77 out[name] = env.Get(name) 78 } 79 80 return out, nil 81 } 82 83 func (g *grub) SetBootVars(values map[string]string) error { 84 env := grubenv.NewEnv(g.envFile()) 85 if err := env.Load(); err != nil && !os.IsNotExist(err) { 86 return err 87 } 88 for k, v := range values { 89 env.Set(k, v) 90 } 91 return env.Save() 92 } 93 94 func (g *grub) ExtractKernelAssets(s snap.PlaceInfo, snapf snap.Container) error { 95 // XXX: should we use "kernel.yaml" for this? 96 if _, err := snapf.ReadFile("meta/force-kernel-extraction"); err == nil { 97 return extractKernelAssetsToBootDir(g.dir(), s, snapf) 98 } 99 return nil 100 } 101 102 func (g *grub) RemoveKernelAssets(s snap.PlaceInfo) error { 103 return removeKernelAssetsFromBootDir(g.dir(), s) 104 }