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
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2019 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 main
import (
"fmt"
"io"
"os"
"path/filepath"
"github.com/jessevdk/go-flags"
"github.com/snapcore/snapd/image/preseed"
// for SanitizePlugsSlots
"github.com/snapcore/snapd/interfaces/builtin"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/snap"
)
const (
shortHelp = "Prerun the first boot seeding of snaps in an image filesystem chroot with a snapd seed."
longHelp = `
The snap-preseed command takes a directory containing an image, including seed
snaps (at /var/lib/snapd/seed), and runs through the snapd first-boot process
up to hook execution. No boot actions unrelated to snapd are performed.
It creates systemd units for seeded snaps, makes any connections, and generates
security profiles. The image is updated and consequently optimised to reduce
first-boot startup time`
)
type options struct {
Reset bool `long:"reset"`
ResetChroot bool `long:"reset-chroot" hidden:"1"`
PreseedSignKey string `long:"preseed-sign-key"`
AppArmorFeaturesDir string `long:"apparmor-features-dir"`
SysfsOverlay string `long:"sysfs-overlay"`
}
var (
osGetuid = os.Getuid
// unused currently, left in place for consistency for when it is needed
// Stdout io.Writer = os.Stdout
Stderr io.Writer = os.Stderr
preseedCore20 = preseed.Core20
preseedClassic = preseed.Classic
preseedClassicReset = preseed.ClassicReset
preseedResetPreseededChroot = preseed.ResetPreseededChroot
opts options
)
func Parser() *flags.Parser {
opts = options{}
parser := flags.NewParser(&opts, flags.HelpFlag|flags.PassDoubleDash|flags.PassAfterNonOption)
parser.ShortDescription = shortHelp
parser.LongDescription = longHelp
return parser
}
func probeCore20ImageDir(dir string) bool {
sysDir := filepath.Join(dir, "system-seed")
_, isDir, _ := osutil.DirExists(sysDir)
return isDir
}
func main() {
parser := Parser()
if err := run(parser, os.Args[1:]); err != nil {
fmt.Fprintf(Stderr, "error: %v\n", err)
os.Exit(1)
}
}
func run(parser *flags.Parser, args []string) (err error) {
// real validation of plugs and slots; needs to be set
// for processing of seeds with gadget because of readInfo().
snap.SanitizePlugsSlots = builtin.SanitizePlugsSlots
rest, err := parser.ParseArgs(args)
if err != nil {
return err
}
if osGetuid() != 0 {
return fmt.Errorf("must be run as root")
}
var chrootDir string
if opts.ResetChroot {
chrootDir = "/"
} else {
if len(rest) == 0 {
return fmt.Errorf("need chroot path as argument")
}
chrootDir, err = filepath.Abs(rest[0])
if err != nil {
return err
}
// safety check
if chrootDir == "/" {
return fmt.Errorf("cannot run snap-preseed against /")
}
}
if probeCore20ImageDir(chrootDir) {
if opts.Reset || opts.ResetChroot {
return fmt.Errorf("cannot snap-preseed --reset for Ubuntu Core")
}
coreOpts := &preseed.CoreOptions{
PrepareImageDir: chrootDir,
PreseedSignKey: opts.PreseedSignKey,
AppArmorKernelFeaturesDir: opts.AppArmorFeaturesDir,
SysfsOverlay: opts.SysfsOverlay,
}
return preseedCore20(coreOpts)
}
if opts.ResetChroot {
return preseedResetPreseededChroot(chrootDir)
}
if opts.Reset {
return preseedClassicReset(chrootDir)
}
return preseedClassic(chrootDir)
}
|