github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/overlord/hookstate/ctlcmd/system_mode.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2021 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 ctlcmd
    21  
    22  import (
    23  	"fmt"
    24  
    25  	"gopkg.in/yaml.v2"
    26  
    27  	"github.com/snapcore/snapd/i18n"
    28  	"github.com/snapcore/snapd/overlord/devicestate"
    29  	"github.com/snapcore/snapd/strutil"
    30  )
    31  
    32  type systemModeCommand struct {
    33  	baseCommand
    34  }
    35  
    36  var shortSystemModeHelp = i18n.G("Get the current system mode and associated details")
    37  
    38  var longSystemModeHelp = i18n.G(`
    39  The system-mode command returns information about the device's current system mode.
    40  
    41  This information includes the mode itself and whether the model snaps have been installed from the seed (seed-loaded). The system mode is either run, recover, or install.
    42  
    43  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.
    44  
    45  The output is in YAML format. Example output:
    46      $ snapctl system-mode
    47      system-mode: install
    48      seed-loaded: true
    49      factory: true
    50  `)
    51  
    52  func init() {
    53  	addCommand("system-mode", shortSystemModeHelp, longSystemModeHelp, func() command { return &systemModeCommand{} })
    54  }
    55  
    56  var devicestateSystemModeInfoFromState = devicestate.SystemModeInfoFromState
    57  
    58  type systemModeResult struct {
    59  	SystemMode string `yaml:"system-mode,omitempty"`
    60  	Seeded     bool   `yaml:"seed-loaded"`
    61  	Factory    bool   `yaml:"factory,omitempty"`
    62  }
    63  
    64  func (c *systemModeCommand) Execute(args []string) error {
    65  	context := c.context()
    66  	if context == nil {
    67  		return fmt.Errorf("cannot run system-mode without a context")
    68  	}
    69  
    70  	st := context.State()
    71  	st.Lock()
    72  	defer st.Unlock()
    73  
    74  	smi, err := devicestateSystemModeInfoFromState(st)
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	res := systemModeResult{
    80  		SystemMode: smi.Mode,
    81  		Seeded:     smi.Seeded,
    82  	}
    83  	if strutil.ListContains(smi.BootFlags, "factory") {
    84  		res.Factory = true
    85  	}
    86  
    87  	b, err := yaml.Marshal(res)
    88  	if err != nil {
    89  		return err
    90  	}
    91  
    92  	c.printf("%s", string(b))
    93  
    94  	return nil
    95  }