File: system_mode.go

package info (click to toggle)
snapd 2.72-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 80,412 kB
  • sloc: sh: 16,506; ansic: 16,211; python: 11,213; makefile: 1,919; exp: 190; awk: 58; xml: 22
file content (107 lines) | stat: -rw-r--r-- 3,030 bytes parent folder | download
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
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2021 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 ctlcmd

import (
	"gopkg.in/yaml.v2"

	"github.com/snapcore/snapd/i18n"
	"github.com/snapcore/snapd/overlord/devicestate"
	"github.com/snapcore/snapd/overlord/fdestate"
	"github.com/snapcore/snapd/strutil"
)

type systemModeCommand struct {
	baseCommand
}

var shortSystemModeHelp = i18n.G("Get the current system mode and associated details")

var longSystemModeHelp = i18n.G(`
The system-mode command returns information about the device's current system mode.

This information includes the mode itself, whether the model snaps have been installed from the seed (seed-loaded), and whether Full Disk Encryption (FDE) is enabled. The system mode is either run, recover, or install.

Retrieved information can also include "factory mode" details: 'factory: true' declares whether the device booted an image flagged as for factory use. This flag can be set for convenience when building the image. No security sensitive decisions should be based on this bit alone.

The output is in YAML format. Example output:
    $ snapctl system-mode
    system-mode: install
    seed-loaded: true
    factory: true
    storage-encrypted: managed
`)

func init() {
	addCommand("system-mode", shortSystemModeHelp, longSystemModeHelp, func() command { return &systemModeCommand{} })
}

var (
	devicestateSystemModeInfoFromState = devicestate.SystemModeInfoFromState
	fdestateSystemEncryptedFromState   = fdestate.SystemEncryptedFromState
)

type systemModeResult struct {
	SystemMode       string `yaml:"system-mode,omitempty"`
	Seeded           bool   `yaml:"seed-loaded"`
	Factory          bool   `yaml:"factory,omitempty"`
	StorageEncrypted string `yaml:"storage-encrypted,omitempty"`
}

func (c *systemModeCommand) Execute(args []string) error {
	context, err := c.ensureContext()
	if err != nil {
		return err
	}

	st := context.State()
	st.Lock()
	defer st.Unlock()

	smi, err := devicestateSystemModeInfoFromState(st)
	if err != nil {
		return err
	}

	encrypted, err := fdestateSystemEncryptedFromState(st)
	if err != nil {
		return err
	}

	res := systemModeResult{
		SystemMode: smi.Mode,
		Seeded:     smi.Seeded,
	}
	if strutil.ListContains(smi.BootFlags, "factory") {
		res.Factory = true
	}
	if encrypted {
		res.StorageEncrypted = "managed"
	}

	b, err := yaml.Marshal(res)
	if err != nil {
		return err
	}

	c.printf("%s", string(b))

	return nil
}