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,139 +25,220 @@ const (
// Maximum thread qty for start/stop // Maximum thread qty for start/stop
gMaxThreads = 4 gMaxThreads = 4
gDefaultsJson = ` { fbsdUpdateConfig = `# $FreeBSD$
"CONFIG_VERSION": "27",
"allow_chflags": 0, # Trusted keyprint. Changing this is a Bad Idea unless you've received
"allow_mlock": 0, # a PGP-signed email from <security-officer@FreeBSD.org> telling you to
"allow_mount": 0, # change it and explaining why.
"allow_mount_devfs": 0, KeyPrint 800651ef4b4c71c27e60786d7b487188970f4b4169cc055784e21eb71d410cc5
"allow_mount_fusefs": 0,
"allow_mount_nullfs": 0, # Server or server pool from which to fetch updates. You can change
"allow_mount_procfs": 0, # this to point at a specific server if you want, but in most cases
"allow_mount_tmpfs": 0, # using a "nearby" server won't provide a measurable improvement in
"allow_mount_zfs": 0, # performance.
"allow_quotas": 0, ServerName update.FreeBSD.org
"allow_raw_sockets": 0,
"allow_set_hostname": 1, # Components of the base system which should be kept updated.
"allow_socket_af": 0, Components world
"allow_sysvipc": 0,
"allow_tun": 0, # Example for updating the userland and the kernel source code only:
"allow_vmm": 0, # Components src/base src/sys world
"assign_localhost": 0,
"available": "readonly", # Paths which start with anything matching an entry in an IgnorePaths
"basejail": 0, # statement will be ignored.
"boot": 0, IgnorePaths
"bpf": 0,
"children_max": "0", # Paths which start with anything matching an entry in an IDSIgnorePaths
"comment": "none", # statement will be ignored by "freebsd-update IDS".
"compression": "lz4", IDSIgnorePaths /usr/share/man/cat
"compressratio": "readonly", IDSIgnorePaths /usr/share/man/whatis
"coredumpsize": "off", IDSIgnorePaths /var/db/locate.database
"count": "1", IDSIgnorePaths /var/log
"cpuset": "off",
"cputime": "off", # Paths which start with anything matching an entry in an UpdateIfUnmodified
"datasize": "off", # statement will only be updated if the contents of the file have not been
"dedup": "off", # modified by the user (unless changes are merged; see below).
"defaultrouter": "auto", UpdateIfUnmodified /etc/ /var/ /root/ /.cshrc /.profile
"defaultrouter6": "auto",
"depends": "none", # When upgrading to a new FreeBSD release, files which match MergeChanges
"devfs_ruleset": "4", # will have any local changes merged into the version from the new release.
"dhcp": 0, MergeChanges /etc/
"enforce_statfs": "2",
"exec_clean": 1, ### Default configuration options:
"exec_created": "/usr/bin/true",
"exec_fib": "0", # Directory in which to store downloaded updates and temporary
"exec_jail_user": "root", # files used by FreeBSD Update.
"exec_poststart": "/usr/bin/true", WorkDir /iocage/freebsd-update
"exec_poststop": "/usr/bin/true",
"exec_prestart": "/usr/bin/true", # Destination to send output of "freebsd-update cron" if an error
"exec_prestop": "/usr/bin/true", # occurs or updates have been downloaded.
"exec_start": "/bin/sh /etc/rc", # MailTo root
"exec_stop": "/bin/sh /etc/rc.shutdown",
"exec_system_jail_user": "0", # Is FreeBSD Update allowed to create new files?
"exec_system_user": "root", # AllowAdd yes
"exec_timeout": "60",
"host_domainname": "none", # Is FreeBSD Update allowed to delete files?
"host_time": 1, # AllowDelete yes
"hostid": "36353536-3135-5a43-4a34-313130315a56",
"hostid_strict_check": 0, # If the user has modified file ownership, permissions, or flags, should
"interfaces": "vnet0:bridge0", # FreeBSD Update retain this modified metadata when installing a new version
"ip4": "new", # of that file?
"ip4_addr": "none", # KeepModifiedMetadata yes
"ip4_saddrsel": 1,
"ip6": "new", # When upgrading between releases, should the list of Components be
"ip6_addr": "none", # read strictly (StrictComponents yes) or merely as a list of components
"ip6_saddrsel": 1, # which *might* be installed of which FreeBSD Update should figure out
"ip_hostname": 0, # which actually are installed and upgrade those (StrictComponents no)?
"jail_zfs": 0, StrictComponents yes
"jail_zfs_mountpoint": "none",
"last_started": "none", # When installing a new kernel perform a backup of the old one first
"localhost_ip": "none", # so it is possible to boot the old kernel in case of problems.
"login_flags": "-f root", BackupKernel no
"mac_prefix": "2c44fd",
"maxproc": "off", # If BackupKernel is enabled, the backup kernel is saved to this
"memorylocked": "off", # directory.
"memoryuse": "off", # BackupKernelDir /boot/kernel.old
"min_dyn_devfs_ruleset": "1000",
"mount_devfs": 1, # When backing up a kernel also back up debug symbol files?
"mount_fdescfs": 1, BackupKernelSymbolFiles no
"mount_linprocfs": 0,
"mount_procfs": 0, # Create a new boot environment when installing patches
"mountpoint": "readonly", CreateBootEnv no
"msgqqueued": "off", `
"msgqsize": "off",
"nat": 0, gDefaultsJson = `{
"nat_backend": "ipfw", "CONFIG_VERSION": "27",
"nat_forwards": "none", "allow_chflags": 0,
"nat_interface": "none", "allow_mlock": 0,
"nat_prefix": "172.16", "allow_mount": 0,
"nmsgq": "off", "allow_mount_devfs": 0,
"notes": "none", "allow_mount_fusefs": 0,
"nsem": "off", "allow_mount_nullfs": 0,
"nsemop": "off", "allow_mount_procfs": 0,
"nshm": "off", "allow_mount_tmpfs": 0,
"nthr": "off", "allow_mount_zfs": 0,
"openfiles": "off", "allow_quotas": 0,
"origin": "readonly", "allow_raw_sockets": 0,
"owner": "root", "allow_set_hostname": 1,
"pcpu": "off", "allow_socket_af": 0,
"plugin_name": "none", "allow_sysvipc": 0,
"plugin_repository": "none", "allow_tun": 0,
"priority": "99", "allow_vmm": 0,
"pseudoterminals": "off", "assign_localhost": 0,
"quota": "none", "available": "readonly",
"readbps": "off", "basejail": 0,
"readiops": "off", "boot": 0,
"reservation": "none", "bpf": 0,
"resolver": "/etc/resolv.conf", "children_max": "0",
"rlimits": "off", "comment": "none",
"rtsold": 0, "compression": "lz4",
"securelevel": "2", "compressratio": "readonly",
"shmsize": "off", "coredumpsize": "off",
"stacksize": "off", "count": "1",
"stop_timeout": "30", "cpuset": "off",
"swapuse": "off", "cputime": "off",
"sync_state": "none", "datasize": "off",
"sync_target": "none", "dedup": "off",
"sync_tgt_zpool": "none", "defaultrouter": "auto",
"sysvmsg": "new", "defaultrouter6": "auto",
"sysvsem": "new", "depends": "none",
"sysvshm": "new", "devfs_ruleset": "4",
"template": 0, "dhcp": 0,
"type": "jail", "enforce_statfs": "2",
"used": "readonly", "exec_clean": 1,
"vmemoryuse": "off", "exec_created": "/usr/bin/true",
"vnet": 0, "exec_fib": "0",
"vnet0_mac": "none", "exec_jail_user": "root",
"vnet1_mac": "none", "exec_poststart": "/usr/bin/true",
"vnet2_mac": "none", "exec_poststop": "/usr/bin/true",
"vnet3_mac": "none", "exec_prestart": "/usr/bin/true",
"vnet_default_interface": "auto", "exec_prestop": "/usr/bin/true",
"vnet_interfaces": "none", "exec_start": "/bin/sh /etc/rc",
"wallclock": "off", "exec_stop": "/bin/sh /etc/rc.shutdown",
"writebps": "off", "exec_system_jail_user": "0",
"writeiops": "off" "exec_system_user": "root",
} "exec_timeout": "60",
"host_domainname": "none",
"host_time": 1,
"hostid": "TO-BE-REPLACED-WITH-HOSTID",
"hostid_strict_check": 0,
"interfaces": "vnet0:TO-BE-REPLACED-WITH-BRIDGE",
"ip4": "new",
"ip4_addr": "none",
"ip4_saddrsel": 1,
"ip6": "new",
"ip6_addr": "none",
"ip6_saddrsel": 1,
"ip_hostname": 0,
"jail_zfs": 0,
"jail_zfs_mountpoint": "none",
"last_started": "none",
"localhost_ip": "none",
"login_flags": "-f root",
"mac_prefix": "2c44fd",
"maxproc": "off",
"memorylocked": "off",
"memoryuse": "off",
"min_dyn_devfs_ruleset": "1000",
"mount_devfs": 1,
"mount_fdescfs": 1,
"mount_linprocfs": 0,
"mount_procfs": 0,
"mountpoint": "readonly",
"msgqqueued": "off",
"msgqsize": "off",
"nat": 0,
"nat_backend": "ipfw",
"nat_forwards": "none",
"nat_interface": "none",
"nat_prefix": "172.16",
"nmsgq": "off",
"notes": "none",
"nsem": "off",
"nsemop": "off",
"nshm": "off",
"nthr": "off",
"openfiles": "off",
"origin": "readonly",
"owner": "root",
"pcpu": "off",
"plugin_name": "none",
"plugin_repository": "none",
"priority": "99",
"pseudoterminals": "off",
"quota": "none",
"readbps": "off",
"readiops": "off",
"reservation": "none",
"resolver": "/etc/resolv.conf",
"rlimits": "off",
"rtsold": 0,
"securelevel": "2",
"shmsize": "off",
"stacksize": "off",
"stop_timeout": "30",
"swapuse": "off",
"sync_state": "none",
"sync_target": "none",
"sync_tgt_zpool": "none",
"sysvmsg": "new",
"sysvsem": "new",
"sysvshm": "new",
"template": 0,
"type": "jail",
"used": "readonly",
"vmemoryuse": "off",
"vnet": 0,
"vnet0_mac": "none",
"vnet1_mac": "none",
"vnet2_mac": "none",
"vnet3_mac": "none",
"vnet_default_interface": "auto",
"vnet_interfaces": "none",
"wallclock": "off",
"writebps": "off",
"writeiops": "off"
}
` `
) )
@ -362,7 +443,7 @@ func executeCommand(cmdline string) (string, error) {
out, err = exec.Command(cmd[0]).CombinedOutput() out, err = exec.Command(cmd[0]).CombinedOutput()
} }
return string(out), err return strings.TrimSuffix(string(out), "\n"), err
} }
/* From iocage: /* From iocage:
@ -812,7 +893,9 @@ func zfsCreateDataset(dataset, mountpoint, compression string) error {
} }
// Return dataset name for a given mountpoint // Return dataset name for a given mountpoint
func zfsGetDatasetByMountpoint(mountpoint string) (string, error) { 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) out, err := executeCommand(cmd)
if err != nil { if err != nil {
return "", errors.New(fmt.Sprintf("%v; command returned \"%s\"", err, out)) 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) 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 * rc.conf management
@ -900,6 +994,43 @@ func disableRcKey(rcconfpath string, key string) error {
return nil 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 * Parse an fstab file, returning an array of Mount
*****************************************************************************/ *****************************************************************************/