From 259c3ee1e048007bb14149279cd2ad100b4ff521 Mon Sep 17 00:00:00 2001 From: yo Date: Sun, 22 Sep 2024 15:10:58 +0200 Subject: [PATCH] Add freebsd-update conf, add doFileExist, getCurrentRcKeyValue and addRcKeyValue, fix zfsGetDatasetByMountpoint and executeCommand output --- cmd/utils.go | 405 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 268 insertions(+), 137 deletions(-) diff --git a/cmd/utils.go b/cmd/utils.go index 6b5746e..c44680f 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -24,140 +24,221 @@ const ( ifconfigipv4re = `inet[[:space:]](` + ipv4re + `)` // Maximum thread qty for start/stop gMaxThreads = 4 - - gDefaultsJson = ` { - "CONFIG_VERSION": "27", - "allow_chflags": 0, - "allow_mlock": 0, - "allow_mount": 0, - "allow_mount_devfs": 0, - "allow_mount_fusefs": 0, - "allow_mount_nullfs": 0, - "allow_mount_procfs": 0, - "allow_mount_tmpfs": 0, - "allow_mount_zfs": 0, - "allow_quotas": 0, - "allow_raw_sockets": 0, - "allow_set_hostname": 1, - "allow_socket_af": 0, - "allow_sysvipc": 0, - "allow_tun": 0, - "allow_vmm": 0, - "assign_localhost": 0, - "available": "readonly", - "basejail": 0, - "boot": 0, - "bpf": 0, - "children_max": "0", - "comment": "none", - "compression": "lz4", - "compressratio": "readonly", - "coredumpsize": "off", - "count": "1", - "cpuset": "off", - "cputime": "off", - "datasize": "off", - "dedup": "off", - "defaultrouter": "auto", - "defaultrouter6": "auto", - "depends": "none", - "devfs_ruleset": "4", - "dhcp": 0, - "enforce_statfs": "2", - "exec_clean": 1, - "exec_created": "/usr/bin/true", - "exec_fib": "0", - "exec_jail_user": "root", - "exec_poststart": "/usr/bin/true", - "exec_poststop": "/usr/bin/true", - "exec_prestart": "/usr/bin/true", - "exec_prestop": "/usr/bin/true", - "exec_start": "/bin/sh /etc/rc", - "exec_stop": "/bin/sh /etc/rc.shutdown", - "exec_system_jail_user": "0", - "exec_system_user": "root", - "exec_timeout": "60", - "host_domainname": "none", - "host_time": 1, - "hostid": "36353536-3135-5a43-4a34-313130315a56", - "hostid_strict_check": 0, - "interfaces": "vnet0:bridge0", - "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" - } + + fbsdUpdateConfig = `# $FreeBSD$ + +# Trusted keyprint. Changing this is a Bad Idea unless you've received +# a PGP-signed email from 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, + "allow_mount": 0, + "allow_mount_devfs": 0, + "allow_mount_fusefs": 0, + "allow_mount_nullfs": 0, + "allow_mount_procfs": 0, + "allow_mount_tmpfs": 0, + "allow_mount_zfs": 0, + "allow_quotas": 0, + "allow_raw_sockets": 0, + "allow_set_hostname": 1, + "allow_socket_af": 0, + "allow_sysvipc": 0, + "allow_tun": 0, + "allow_vmm": 0, + "assign_localhost": 0, + "available": "readonly", + "basejail": 0, + "boot": 0, + "bpf": 0, + "children_max": "0", + "comment": "none", + "compression": "lz4", + "compressratio": "readonly", + "coredumpsize": "off", + "count": "1", + "cpuset": "off", + "cputime": "off", + "datasize": "off", + "dedup": "off", + "defaultrouter": "auto", + "defaultrouter6": "auto", + "depends": "none", + "devfs_ruleset": "4", + "dhcp": 0, + "enforce_statfs": "2", + "exec_clean": 1, + "exec_created": "/usr/bin/true", + "exec_fib": "0", + "exec_jail_user": "root", + "exec_poststart": "/usr/bin/true", + "exec_poststop": "/usr/bin/true", + "exec_prestart": "/usr/bin/true", + "exec_prestop": "/usr/bin/true", + "exec_start": "/bin/sh /etc/rc", + "exec_stop": "/bin/sh /etc/rc.shutdown", + "exec_system_jail_user": "0", + "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() } - return string(out), err + return strings.TrimSuffix(string(out), "\n"), err } /* From iocage: @@ -814,7 +895,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)) @@ -868,6 +951,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 @@ -893,7 +987,7 @@ func disableRcKey(rcconfpath string, key string) error { return err } } - + cmd = fmt.Sprintf("/usr/sbin/sysrc -f %s -x %s", rcconfpath, key) _, err = executeCommand(cmd) if err != nil { @@ -902,6 +996,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 *****************************************************************************/