Add freebsd-update conf, add doFileExist, getCurrentRcKeyValue and addRcKeyValue, fix zfsGetDatasetByMountpoint and executeCommand output

This commit is contained in:
yo 2024-09-22 15:10:58 +02:00
parent 900e1939f3
commit 2507f10b59

View File

@ -25,7 +25,88 @@ const (
// Maximum thread qty for start/stop
gMaxThreads = 4
gDefaultsJson = ` {
fbsdUpdateConfig = `# $FreeBSD$
# Trusted keyprint. Changing this is a Bad Idea unless you've received
# a PGP-signed email from <security-officer@FreeBSD.org> telling you to
# change it and explaining why.
KeyPrint 800651ef4b4c71c27e60786d7b487188970f4b4169cc055784e21eb71d410cc5
# Server or server pool from which to fetch updates. You can change
# this to point at a specific server if you want, but in most cases
# using a "nearby" server won't provide a measurable improvement in
# performance.
ServerName update.FreeBSD.org
# Components of the base system which should be kept updated.
Components world
# Example for updating the userland and the kernel source code only:
# Components src/base src/sys world
# Paths which start with anything matching an entry in an IgnorePaths
# statement will be ignored.
IgnorePaths
# Paths which start with anything matching an entry in an IDSIgnorePaths
# statement will be ignored by "freebsd-update IDS".
IDSIgnorePaths /usr/share/man/cat
IDSIgnorePaths /usr/share/man/whatis
IDSIgnorePaths /var/db/locate.database
IDSIgnorePaths /var/log
# Paths which start with anything matching an entry in an UpdateIfUnmodified
# statement will only be updated if the contents of the file have not been
# modified by the user (unless changes are merged; see below).
UpdateIfUnmodified /etc/ /var/ /root/ /.cshrc /.profile
# When upgrading to a new FreeBSD release, files which match MergeChanges
# will have any local changes merged into the version from the new release.
MergeChanges /etc/
### Default configuration options:
# Directory in which to store downloaded updates and temporary
# files used by FreeBSD Update.
WorkDir /iocage/freebsd-update
# Destination to send output of "freebsd-update cron" if an error
# occurs or updates have been downloaded.
# MailTo root
# Is FreeBSD Update allowed to create new files?
# AllowAdd yes
# Is FreeBSD Update allowed to delete files?
# AllowDelete yes
# If the user has modified file ownership, permissions, or flags, should
# FreeBSD Update retain this modified metadata when installing a new version
# of that file?
# KeepModifiedMetadata yes
# When upgrading between releases, should the list of Components be
# read strictly (StrictComponents yes) or merely as a list of components
# which *might* be installed of which FreeBSD Update should figure out
# which actually are installed and upgrade those (StrictComponents no)?
StrictComponents yes
# When installing a new kernel perform a backup of the old one first
# so it is possible to boot the old kernel in case of problems.
BackupKernel no
# If BackupKernel is enabled, the backup kernel is saved to this
# directory.
# BackupKernelDir /boot/kernel.old
# When backing up a kernel also back up debug symbol files?
BackupKernelSymbolFiles no
# Create a new boot environment when installing patches
CreateBootEnv no
`
gDefaultsJson = `{
"CONFIG_VERSION": "27",
"allow_chflags": 0,
"allow_mlock": 0,
@ -79,9 +160,9 @@ const (
"exec_timeout": "60",
"host_domainname": "none",
"host_time": 1,
"hostid": "36353536-3135-5a43-4a34-313130315a56",
"hostid": "TO-BE-REPLACED-WITH-HOSTID",
"hostid_strict_check": 0,
"interfaces": "vnet0:bridge0",
"interfaces": "vnet0:TO-BE-REPLACED-WITH-BRIDGE",
"ip4": "new",
"ip4_addr": "none",
"ip4_saddrsel": 1,
@ -157,7 +238,7 @@ const (
"wallclock": "off",
"writebps": "off",
"writeiops": "off"
}
}
`
)
@ -362,7 +443,7 @@ func executeCommand(cmdline string) (string, error) {
out, err = exec.Command(cmd[0]).CombinedOutput()
}
return string(out), err
return strings.TrimSuffix(string(out), "\n"), err
}
/* From iocage:
@ -812,7 +893,9 @@ func zfsCreateDataset(dataset, mountpoint, compression string) error {
}
// Return dataset name for a given mountpoint
func zfsGetDatasetByMountpoint(mountpoint string) (string, error) {
cmd := fmt.Sprintf("zfs list -p -r -H -o name %s", mountpoint)
// We dont want no recursivity
//cmd := fmt.Sprintf("zfs list -p -r -H -o name %s", mountpoint)
cmd := fmt.Sprintf("zfs list -p -H -o name %s", mountpoint)
out, err := executeCommand(cmd)
if err != nil {
return "", errors.New(fmt.Sprintf("%v; command returned \"%s\"", err, out))
@ -866,6 +949,17 @@ func getPermissions(path string) (os.FileInfo, error) {
return os.Stat(path)
}
func doFileExist(filePath string) (bool, error) {
if _, err := os.Stat(filePath); err != nil {
if errors.Is(err, os.ErrNotExist) {
return false, nil
} else {
return false, err
}
}
return true, nil
}
/*****************************************************************************
*
* rc.conf management
@ -900,6 +994,43 @@ func disableRcKey(rcconfpath string, key string) error {
return nil
}
// returns no error if rc key does not exist
func getCurrentRcKeyValue(rcconfpath string, key string) (string, error) {
cmd := "/usr/sbin/sysrc -a"
kvs, err := executeCommand(cmd)
if err != nil {
return "", err
}
for _, kv := range strings.Split(string(kvs), "\n") {
fmt.Printf("%s\n", kv)
if strings.HasPrefix(kv, fmt.Sprintf("%s:", key)) {
return strings.TrimPrefix(strings.Join(strings.Split(kv, ":")[1:], ":"), " "), nil
}
}
return "", nil
}
// Add a value to current existing key value
func addRcKeyValue(rcconfpath string, key string, value string) error {
var nv string
cv, err := getCurrentRcKeyValue(rcconfpath, key)
if err != nil {
return err
}
if len(cv) > 0 {
log.Debugf("Current value of %s: %s\n", key, cv)
nv = fmt.Sprintf("\"%s %s\"", cv, value)
} else {
nv = fmt.Sprintf("\"%s\"", value)
}
cmd := fmt.Sprintf("/usr/sbin/sysrc -f %s %s=%s", rcconfpath, key, nv)
_, err = executeCommand(cmd)
if err != nil {
return err
}
return nil
}
/*****************************************************************************
* Parse an fstab file, returning an array of Mount
*****************************************************************************/