6

I am experimenting with Yocto project for generating custom Linux images for my embedded devices.

I have a requirement to add a persistent custom kernel parameter to /etc/sysctl.conf of the generated image.

i.e.

kernel.core_pipe_limit = 1

/etc/sysctl.conf is generated by procps package that comes with Yocto base system (meta/recipes-extended/procps/procps/sysctl.conf). However, I believe editing the sysctl.conf in the base system is not the recommended approach.

I am using a new layer for defining my custom configurations. I hope there is a way to apply a patch to a base package via a custom layer after deploying the base layer.

How can I do this?


I am aware how to persistently change a kernel variable by updating /etc/sysctl.conf (or, preferably, /etc/sysctl.d/xxx.conf). My question is, how to generate the Linux image with the necessary update applied?

Anubis
  • 6,995
  • 14
  • 56
  • 87
  • 1
    Don't you have `/etc/sysctl.d/` ? Check what your `sysctl --system` does. – KamilCuk Jul 17 '19 at 08:08
  • Yes there is `/etc/sysctl.d`. But what is the correct way to add an entry even there, in the final image built by Yocto ? – Anubis Jul 17 '19 at 08:16

3 Answers3

5

You can add something like this in image recipe or local.conf:

set_kernel_opt(){
    mkdir -p ${IMAGE_ROOTFS}/etc/sysctl.d
    echo 'kernel.core_pipe_limit = 1' > ${IMAGE_ROOTFS}/etc/sysctl.d/kernel_core_pipe_limit.conf
}

ROOTFS_POSTPROCESS_COMMAND += "set_kernel_opt;"

If you want to override /etc/sysctl.conf file, you can create a meta-custom/recipes-extended/procps/procps_%.bbappend file with:

FILESEXTRAPATHS_prepend := "${THISDIR}/files:"

Then create a folder meta-custom/recipes-extended/procps/files and copy your custom sysctl.conf file in it.


Finally you can create a meta-custom/recipe-custom/custom-config/custom-config.bb recipe with:

LICENSE = "CLOSED"

SRC_URI = " \
   file://kernel_core_pipe_limit.conf \
"

PV = "1.0"

S = "${WORKDIR}"

inherit allarch

do_install() {
    install -d ${D}${sysconfdir}/sysctl.d
    install -m 0644 ${B}/kernel_core_pipe_limit.conf ${D}${sysconfdir}/sysctl.d/
}

do_configure[noexec] = "1"
do_compile[noexec] = "1"

And copy your kernel_core_pipe_limit.conf in meta-custom/recipe-custom/custom-config/files/

Nayfe
  • 2,130
  • 13
  • 18
2

The answer up there are wrong in my opinion. There is already a recipe providing sysctl.conf. It is procps. What you need to do is override the default configuration with a bbappend. More about append files on the online Yocto documention

Create a procps folder, procps_%.bbappend and systctl.conf in recipes-extended in your layer such as

meta-my-layer/recipes-extended/
└── procps
    ├── files
    │   └── sysctl.conf
    └── procps_%.bbappend

procps_%.bbappend:

FILESEXTRAPATHS_prepend := "${THISDIR}/files:"`

(example of) sysctl.conf:

fs.protected_hardlinks = 1
fs.protected_symlinks = 1

In case you want to keep default configuration and append to it, you only need a do_install_append step with an echo appending your text.

David Bensoussan
  • 2,887
  • 2
  • 38
  • 55
1

Just create a file with .conf extension under /etc/sysctl.d.

echo 'kernel.core_pipe_limit = 1' > /etc/sysctl.d/bla_bla_change_kernel_core_pipe_limit.conf

From man sysctl:

 --system
              Load settings from all system configuration files. Files are
              read from directories in the following list in given order
              from top to bottom.  Once a file of a given filename is
              loaded, any file of the same name in subsequent directories is
              ignored.
              /run/sysctl.d/*.conf
              /etc/sysctl.d/*.conf
              /usr/local/lib/sysctl.d/*.conf
              /usr/lib/sysctl.d/*.conf
              /lib/sysctl.d/*.conf
              /etc/sysctl.conf

The sysctl --system should be called on system startup. On systems with systemd this is done via systemd-sysctl.service service. Thus it should load all the /etc/sysctl.d. The syntax is the same as /etc/sysct.conf syntax files.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Thank you for the answer. But how can I relate this to my exact problem? How to make `Yocto` do this? i.e. create the custom Linux image with `sysctl.conf` (or `sysctl.d/xxx.conf`) properly updated. – Anubis Jul 17 '19 at 08:24