def update_kernel_settings(): lots_and_lots = pow(1024, 4) # 1 TB sysctl_settings = {'kernel.shmmax': lots_and_lots, 'kernel.shmall': lots_and_lots} sysctl.create(yaml.dump(sysctl_settings), '/etc/sysctl.d/50-postgresql.conf') reactive.set_state('postgresql.cluster.kernel_settings.set')
def start_quagga(): ch_core_sysctl.create("{'net.ipv4.ip_forward': 1}", '/etc/sysctl.d/42-charm-quagga.conf') ch_core_templating.render('daemons', '/etc/quagga/daemons', {}, owner='quagga', group='quagga', perms=0o640) ch_core_templating.render('zebra.conf', '/etc/quagga/zebra.conf', {}, owner='quagga', group='quagga', perms=0o640) ch_core_templating.render('bgpd.conf', '/etc/quagga/bgpd.conf', {}, owner='quagga', group='quagga', perms=0o640) quagga.restart_services() # Perform basic BGP configuration quagga.vtysh([ 'conf t', 'router bgp {}'.format(quagga.get_asn()), 'bgp router-id {}'.format(ch_core.hookenv.unit_get('private-address')), 'redistribute connected', 'exit', 'exit', 'write', ]) ch_core.hookenv.status_set('active', 'Ready (AS Number {})'.format(quagga.get_asn())) reactive.set_state('quagga.started')
def test_create_invalid_argument(self, mock_open): """Test create sysctl with an invalid argument""" _file = MagicMock(spec=io.FileIO) mock_open.return_value = _file create('{"kernel.max_pid": 1337 xxxx', "/etc/sysctl.d/test-sysctl.conf") self.log.assert_called_with( 'Error parsing YAML sysctl_dict: {"kernel.max_pid": 1337 xxxx', level='ERROR')
def test_create_raises(self, mock_open): """CalledProcessErrors are propagated for non-container machines.""" _file = MagicMock(spec=io.FileIO) mock_open.return_value = _file self.is_container.return_value = False self.check_call.side_effect = CalledProcessError(1, 'sysctl') with self.assertRaises(CalledProcessError): create('{"kernel.max_pid": 1337}', "/etc/sysctl.d/test-sysctl.conf")
def check_for_sysctl() -> Result: """ :return: """ if config.changed("sysctl"): config_path = os.path.join(os.sep, "etc", "sysctl.d", "50-gluster-charm.conf") sysctl_dict = config("sysctl") if sysctl_dict is not None: sysctl.create(sysctl_dict, config_path) return Ok(())
def test_create_raises_container(self, mock_open): """CalledProcessErrors are logged for containers.""" _file = MagicMock(spec=io.FileIO) mock_open.return_value = _file self.is_container.return_value = True self.check_call.side_effect = CalledProcessError(1, 'sysctl', 'foo') create('{"kernel.max_pid": 1337}', "/etc/sysctl.d/test-sysctl.conf") self.log.assert_called_with( 'Error setting some sysctl keys in this container: foo', level='WARNING')
def prepare_hugepages_kernel_mode(): p_1g = _get_hp_options("kernel-hugepages-1g") p_2m = _get_hp_options("kernel-hugepages-2m") if p_1g == 0 and p_2m == 0: log("No hugepages set for kernel mode. Skip configuring.") return sysctl_file = '/etc/sysctl.d/10-contrail-hugepage.conf' # use prefix 60- because of # https://bugs.launchpad.net/curtin/+bug/1527664 cfg_file = '/etc/default/grub.d/60-contrail-agent.cfg' if p_1g == 0: _del_hp_fstab_mount('1G') if _remove_file(cfg_file): log("grub config file is present but 1G disabled - update grub") check_call(["update-grub"]) log("Allocate {} x {} hugepages via sysctl".format(p_2m, '2MB')) sysctl.create(yaml.dump({'vm.nr_hugepages': p_2m}), sysctl_file) _add_hp_fstab_mount('2M') return # remove sysctl file as hugepages be allocated via kernel args _remove_file(sysctl_file) # 1gb avalable only on boot time, so change kernel boot options boot_opts = "default_hugepagesz=1G hugepagesz=1G hugepages={}".format(p_1g) _add_hp_fstab_mount('1G', mount=False) if p_2m != 0: boot_opts += " hugepagesz=2M hugepages={}".format(p_2m) _add_hp_fstab_mount('2M', mount=False) else: _del_hp_fstab_mount('2M') log("Update grub config for hugepages: {}".format(boot_opts)) mkdir('/etc/default/grub.d', perms=0o744) new_content = 'GRUB_CMDLINE_LINUX_DEFAULT="$GRUB_CMDLINE_LINUX_DEFAULT {}"'.format( boot_opts) try: old_content = check_output(['cat', cfg_file]).decode('UTF-8') log("Old kernel boot paramters: {}".format(old_content)) if old_content == new_content: log("Kernel boot parameters are not changed") return except Exception: pass log("New kernel boot paramters: {}".format(new_content)) write_file(cfg_file, new_content, perms=0o644) check_call(["update-grub"])
def test_create(self, mock_open): """Test create sysctl method""" _file = MagicMock(spec=io.FileIO) mock_open.return_value = _file create('{"kernel.max_pid": 1337}', "/etc/sysctl.d/test-sysctl.conf") _file.__enter__().write.assert_called_with("kernel.max_pid=1337\n") self.log.assert_called_with( "Updating sysctl_file: /etc/sysctl.d/test-sysctl.conf" " values: {'kernel.max_pid': 1337}", level='DEBUG') self.check_call.assert_called_with( ["sysctl", "-p", "/etc/sysctl.d/test-sysctl.conf"])
def hugepage_support(user, group='hugetlb', nr_hugepages=256, max_map_count=65536, mnt_point='/run/hugepages/kvm', pagesize='2MB', mount=True, set_shmmax=False): """Enable hugepages on system. Args: user (str) -- Username to allow access to hugepages to group (str) -- Group name to own hugepages nr_hugepages (int) -- Number of pages to reserve max_map_count (int) -- Number of Virtual Memory Areas a process can own mnt_point (str) -- Directory to mount hugepages on pagesize (str) -- Size of hugepages mount (bool) -- Whether to Mount hugepages """ group_info = add_group(group) gid = group_info.gr_gid add_user_to_group(user, group) if max_map_count < 2 * nr_hugepages: max_map_count = 2 * nr_hugepages sysctl_settings = { 'vm.nr_hugepages': nr_hugepages, 'vm.max_map_count': max_map_count, 'vm.hugetlb_shm_group': gid, } if set_shmmax: shmmax_current = int(check_output(['sysctl', '-n', 'kernel.shmmax'])) shmmax_minsize = bytes_from_string(pagesize) * nr_hugepages if shmmax_minsize > shmmax_current: sysctl_settings['kernel.shmmax'] = shmmax_minsize sysctl.create(yaml.dump(sysctl_settings), '/etc/sysctl.d/10-hugepage.conf') mkdir(mnt_point, owner='root', group='root', perms=0o755, force=False) lfstab = fstab.Fstab() fstab_entry = lfstab.get_entry_by_attr('mountpoint', mnt_point) if fstab_entry: lfstab.remove_entry(fstab_entry) entry = lfstab.Entry('nodev', mnt_point, 'hugetlbfs', 'mode=1770,gid={},pagesize={}'.format(gid, pagesize), 0, 0) lfstab.add_entry(entry) if mount: fstab_mount(mnt_point)
def configure_hugepages(): if not config["dpdk"]: return pages = get_hugepages() if not pages: return map_max = pages * 2 if map_max < 65536: map_max = 65536 options = {"vm.nr_hugepages": pages, "vm.max_map_count": map_max, "vm.hugetlb_shm_group": 0} sysctl.create(yaml.dump(options), "/etc/sysctl.d/10-hugepage.conf") check_call(["sysctl", "-w", "vm.nr_hugepages={}".format(pages)]) check_call(["sysctl", "-w", "vm.max_map_count={}".format(map_max)]) check_call(["sysctl", "-w", "vm.hugetlb_shm_group=0".format(pages)])
def prepare_hugepages_kernel_mode(): p_1g = _get_hp_options("kernel-hugepages-1g") p_2m = _get_hp_options("kernel-hugepages-2m") if p_1g == 0 and p_2m == 0: log("No hugepages set for kernel mode. Skip configuring.") return sysctl_file = '/etc/sysctl.d/10-contrail-hugepage.conf' if p_1g == 0: log("Allocate {} x {} hugepages via sysctl".format(p_2m, '2MB')) sysctl.create(yaml.dump({'vm.nr_hugepages': p_2m}), sysctl_file) _add_hp_fstab_mount('2M') return os.remove(sysctl_file) # 1gb avalable only on boot time, so change kernel boot options boot_opts = "default_hugepagesz=1G hugepagesz=1G hugepages={}".format(p_1g) _add_hp_fstab_mount('1G') if p_2m != 0: boot_opts += " hugepagesz=2M hugepages={}".format(p_2m) _add_hp_fstab_mount('2M') log("Update grub config for hugepages: {}".format(boot_opts)) mkdir('/etc/default/grub.d', perms=0o744) new_content = 'GRUB_CMDLINE_LINUX_DEFAULT="$GRUB_CMDLINE_LINUX_DEFAULT {}"'.format( boot_opts) cfg_file = '/etc/default/grub.d/50-contrail-agent.cfg' try: old_content = check_output(['cat', cfg_file]).decode('UTF-8') log("Old kernel boot paramters: {}".format(old_content)) if old_content == new_content: log("Kernel boot parameters are not changed") return except: pass log("New kernel boot paramters: {}".format(new_content)) write_file(cfg_file, new_content, perms=0o644) check_call(["update-grub"])
def update_kernel_settings(): lots_and_lots = pow(1024, 4) # 1 TB sysctl_settings = {"kernel.shmmax": lots_and_lots, "kernel.shmall": lots_and_lots} sysctl.create(yaml.dump(sysctl_settings), "/etc/sysctl.d/50-postgresql.conf") reactive.set_state("postgresql.cluster.kernel_settings.set")
def set_sysctls(): ch_core_sysctl.create(hookenv.config('sysctls'), '/etc/sysctl.d/43-charm-sysctl.conf')
def configure_crashes(): mkdir("/var/crashes", perms=0o755, force=True) options = {"kernel.core_pattern": "/var/crashes/core.%e.%p.%h.%t"} sysctl.create(yaml.dump(options), "/etc/sysctl.d/10-core-pattern.conf")