github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/overlord/configstate/configcore/timezone.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2020 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 configcore
    21  
    22  import (
    23  	"fmt"
    24  	"os"
    25  	"os/exec"
    26  	"path/filepath"
    27  	"regexp"
    28  
    29  	"github.com/snapcore/snapd/osutil"
    30  	"github.com/snapcore/snapd/overlord/configstate/config"
    31  )
    32  
    33  func init() {
    34  	// add supported configuration of this module
    35  	supportedConfigurations["core.system.timezone"] = true
    36  }
    37  
    38  var validTimezone = regexp.MustCompile(`^[a-zA-Z0-9+_-]+(/[a-zA-Z0-9+_-]+)?(/[a-zA-Z0-9+_-]+)?$`).MatchString
    39  
    40  func validateTimezoneSettings(tr config.ConfGetter) error {
    41  	timezone, err := coreCfg(tr, "system.timezone")
    42  	if err != nil {
    43  		return err
    44  	}
    45  	if timezone == "" {
    46  		return nil
    47  	}
    48  	if !validTimezone(timezone) {
    49  		return fmt.Errorf("cannot set timezone %q: name not valid", timezone)
    50  	}
    51  
    52  	return nil
    53  }
    54  
    55  func handleTimezoneConfiguration(tr config.ConfGetter, opts *fsOnlyContext) error {
    56  	// TODO: convert to "virtual" configuration nodes once we have support
    57  	// for this. The current code is not ideal because if one calls
    58  	// `snap get system system.hostname` the answer can be ""
    59  	// when not set via snap set.
    60  	//
    61  	// It will also override any hostname on the next `snap set` run
    62  	// that was written not using `snap set system system.hostname`.
    63  	timezone, err := coreCfg(tr, "system.timezone")
    64  	if err != nil {
    65  		return nil
    66  	}
    67  	// nothing to do
    68  	if timezone == "" {
    69  		return nil
    70  	}
    71  	// runtime system
    72  	if opts == nil {
    73  		output, err := exec.Command("timedatectl", "set-timezone", timezone).CombinedOutput()
    74  		if err != nil {
    75  			return fmt.Errorf("cannot set timezone: %v", osutil.OutputErr(output, err))
    76  		}
    77  	} else {
    78  		// On the UC16/UC18/UC20 images the file /etc/hostname is a
    79  		// symlink to /etc/writable/hostname. The /etc/hostname is
    80  		// not part of the "writable-path" so we must set the file
    81  		// in /etc/writable here for this to work.
    82  		localtimePath := filepath.Join(opts.RootDir, "/etc/writable/localtime")
    83  		if err := os.MkdirAll(filepath.Dir(localtimePath), 0755); err != nil {
    84  			return err
    85  		}
    86  		if err := os.Symlink(filepath.Join("/usr/share/zoneinfo", timezone), localtimePath); err != nil {
    87  			return err
    88  		}
    89  		timezonePath := filepath.Join(opts.RootDir, "/etc/writable/timezone")
    90  		if err := osutil.AtomicWriteFile(timezonePath, []byte(timezone+"\n"), 0644, 0); err != nil {
    91  			return fmt.Errorf("cannot write timezone: %v", err)
    92  		}
    93  	}
    94  
    95  	return nil
    96  }