What is exec?

As opposed to fork or other means that creates a new process, exec will replace the process that calls it.

Wikipedia:

In computing, exec is a functionality of an operating system that runs an executable file in the context of an already existing process, replacing the previous executable. This act is also referred to as an overlay. It is especially important in Unix-like systems, although exists elsewhere. As a new process is not created, the process identifier (PID) does not change, but the machine code, data, heap, and stack of the process are replaced by those of the new program.

exec is implemented in several programming languages. In Linux, we have an exec system call as well as a bash builtin.

type exec
exec is a shell builtin

Let’s do a simple example to understand what it means to replace the calling process. Continue reading “What is exec?”

What is exec?

How does Linux kernel know to load init?

The kernel is hard coded to load init. From Kernel source init/main.c:

if (!try_to_run_init_process("/sbin/init") ||
        !try_to_run_init_process("/etc/init") ||
        !try_to_run_init_process("/bin/init") ||
        !try_to_run_init_process("/bin/sh"))
        return 0;

If you have a Kernel source locally:

root [ /sources/linux-4.7.2 ]# find ./ -name main.c | xargs -IA grep -H run_init_process A
./init/main.c:static int run_init_process(const char *init_filename)
./init/main.c:static int try_to_run_init_process(const char *init_filename)
./init/main.c:  ret = run_init_process(init_filename);
./init/main.c:      ret = run_init_process(ramdisk_execute_command);
./init/main.c:      ret = run_init_process(execute_command);
./init/main.c:  if (!try_to_run_init_process("/sbin/init") ||
./init/main.c:      !try_to_run_init_process("/etc/init") ||
./init/main.c:      !try_to_run_init_process("/bin/init") ||
./init/main.c:      !try_to_run_init_process("/bin/sh"))

If systemd is used, init is symlinked to systemd.

[root@localhost ~]# ll /sbin/init 
lrwxrwxrwx. 1 root root 22 Dec 21 03:54 /sbin/init -> ../lib/systemd/systemd
How does Linux kernel know to load init?

Cannot grep /proc, it thinks it is a binary file

[root@localhost ~]# grep systemd /proc/*/cmdline
Binary file /proc/1/cmdline matches
Binary file /proc/461/cmdline matches
Binary file /proc/478/cmdline matches
Binary file /proc/602/cmdline matches
Binary file /proc/610/cmdline matches
Binary file /proc/self/cmdline matches

Solution: Delete the NUL character.

cat  /proc/*/cmdline | tr -d '\000'  | grep systemd
Cannot grep /proc, it thinks it is a binary file

Mapping interfaces in a guest domain to the OVS interface

Read Correlating OVS and Guest Domain Interfaces.

I have been using a combination of virsh domiflist domain_name and ovs-vsctl to map VM interfaces to OVS interface in an AHV environment.

From Scott’s post, I learned these new commands which are new to me.

ovs-vsctl list interface vnetX
ovs-vsctl get interface vnetX external_ids

Mapping interfaces in a guest domain to the OVS interface