github.com/ethanhsieh/snapd@v0.0.0-20210615102523-3db9b8e4edc5/cmd/snap/cmd_debug_bootvars.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2019 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 main
    21  
    22  import (
    23  	"errors"
    24  
    25  	"github.com/jessevdk/go-flags"
    26  
    27  	"github.com/snapcore/snapd/boot"
    28  	"github.com/snapcore/snapd/i18n"
    29  	"github.com/snapcore/snapd/release"
    30  )
    31  
    32  type cmdBootvarsGet struct {
    33  	UC20    bool   `long:"uc20"`
    34  	RootDir string `long:"root-dir"`
    35  }
    36  
    37  type cmdBootvarsSet struct {
    38  	RootDir    string `long:"root-dir"`
    39  	Recovery   bool   `long:"recovery"`
    40  	Positional struct {
    41  		VarEqValue []string `positional-arg-name:"<var-eq-value>" required:"1"`
    42  	} `positional-args:"yes" required:"yes"`
    43  }
    44  
    45  func init() {
    46  	cmdGet := addDebugCommand("boot-vars",
    47  		"(internal) obtain the snapd boot variables",
    48  		"(internal) obtain the snapd boot variables",
    49  		func() flags.Commander {
    50  			return &cmdBootvarsGet{}
    51  		}, map[string]string{
    52  			"uc20":     i18n.G("Whether to use uc20 boot vars or not"),
    53  			"root-dir": i18n.G("Root directory to look for boot variables in"),
    54  		}, nil)
    55  
    56  	cmdSet := addDebugCommand("set-boot-vars",
    57  		"(internal) set snapd boot variables",
    58  		"(internal) set snapd boot variables",
    59  		func() flags.Commander {
    60  			return &cmdBootvarsSet{}
    61  		}, map[string]string{
    62  			"root-dir": i18n.G("Root directory to look for boot variables in (implies UC20)"),
    63  			"recovery": i18n.G("Manipulate the recovery bootloader (implies UC20)"),
    64  		}, nil)
    65  
    66  	if release.OnClassic {
    67  		cmdGet.hidden = true
    68  		cmdSet.hidden = true
    69  	}
    70  }
    71  
    72  func (x *cmdBootvarsGet) Execute(args []string) error {
    73  	if release.OnClassic {
    74  		return errors.New(`the "boot-vars" command is not available on classic systems`)
    75  	}
    76  	return boot.DebugDumpBootVars(Stdout, x.RootDir, x.UC20)
    77  }
    78  
    79  func (x *cmdBootvarsSet) Execute(args []string) error {
    80  	if release.OnClassic {
    81  		return errors.New(`the "boot-vars" command is not available on classic systems`)
    82  	}
    83  	return boot.DebugSetBootVars(x.RootDir, x.Recovery, x.Positional.VarEqValue)
    84  }