<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Kamraj</title>
    <description>The latest articles on Forem by Kamraj (@kamraj).</description>
    <link>https://forem.com/kamraj</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F832332%2F201afcce-ea4b-4001-88b4-fe28f4bb9467.jpg</url>
      <title>Forem: Kamraj</title>
      <link>https://forem.com/kamraj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/kamraj"/>
    <language>en</language>
    <item>
      <title>Create "Container" without Docker</title>
      <dc:creator>Kamraj</dc:creator>
      <pubDate>Tue, 19 Mar 2024 16:59:06 +0000</pubDate>
      <link>https://forem.com/kamraj/create-container-without-docker-256o</link>
      <guid>https://forem.com/kamraj/create-container-without-docker-256o</guid>
      <description>&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; I am not a Container anatomy specialist or a Linux expert. This is my experiment with containers in order to understand them.&lt;/p&gt;

&lt;p&gt;I am on a learning path to understand how containers work internally. Well, the answer is Namespaces, Cgroups, chroot and of course a filesystem.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Namespaces&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Linux namespaces are a feature of the Linux kernel that allows the isolation and virtualisation of system resources between different processes. This means that processes within different namespaces can have their own independent view of the system's resources, such as process IDs, network interfaces, file systems, and other system resources&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cgroups&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Cgroups are a fundamental technology in Linux containerization, allowing the isolation and management of resources among containers. For example, when a Docker container is created, it is assigned its own cgroup, and resources are allocated according to the limits set by the container configuration.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Chroot&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;chroot is a Unix and Linux system call and command-line tool that changes the root directory of a running process and its children to a new location in the filesystem. This effectively creates an isolated filesystem environment for that process, separate from the main filesystem hierarchy. &lt;/p&gt;

&lt;p&gt;So basically Containers are chroot on steroids &lt;/p&gt;

&lt;p&gt;Let's try creating our own.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create Namespaces:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;unshare --uts --pid --net --mount --ipc --fork
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Setup Cgroups:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir /sys/fs/cgroup/Example/
echo "200000 1000000" &amp;gt; /sys/fs/cgroup/Example/tasks/cpu.max
echo "$$" &amp;gt; /sys/fs/cgroup/Example/tasks/cgroup.procs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First line creates a new cgroup, second one assigns cpu quota&lt;br&gt;
Third line attaches current shell to the cgroup of container&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Setup container's root filesystem:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;debootstrap focal ./ubuntu-rootfs http://archive.ubuntu.com/ubuntu/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This installs a basic Debian or Ubuntu base system into a directory on an existing and running system.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Mount and chroot into container's filesystem:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mount -t proc none ./ubuntu-rootfs/proc
mount -t sysfs none ./ubuntu-rootfs/sys
mount -o bind /dev ./ubuntu-rootfs/dev
chroot ./ubuntu-rootfs /bin/bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These commands are used to set up a chroot environment, where you can operate within the directory &lt;code&gt;./ubuntu-rootfs&lt;/code&gt; as if it were the root of the filesystem. They mount various virtual filesystems and device files that a typical Linux system requires for normal operation. Here's what each command does:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;mount -t proc none ./ubuntu-rootfs/proc&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;mount&lt;/code&gt; is the command used to mount filesystems.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-t proc&lt;/code&gt; specifies the type of filesystem to mount, which in this case is &lt;code&gt;proc&lt;/code&gt;. The &lt;code&gt;proc&lt;/code&gt; filesystem is a virtual filesystem that provides access to kernel and process information.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;none&lt;/code&gt; is used here since the &lt;code&gt;proc&lt;/code&gt; filesystem does not correspond to a physical device.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;./ubuntu-rootfs/proc&lt;/code&gt; is the directory where the &lt;code&gt;proc&lt;/code&gt; filesystem will be mounted. It is the &lt;code&gt;/proc&lt;/code&gt; directory within your chroot environment.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This command mounts the &lt;code&gt;proc&lt;/code&gt; filesystem into your &lt;code&gt;chroot&lt;/code&gt; environment, which is necessary for processes within the chroot to get information about the system and running processes.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;mount -t sysfs none ./ubuntu-rootfs/sys&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-t sysfs&lt;/code&gt; specifies that the &lt;code&gt;sysfs&lt;/code&gt; filesystem type is to be mounted. &lt;code&gt;sysfs&lt;/code&gt; is a virtual filesystem that provides a hierarchy of system and hardware information.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;none&lt;/code&gt; is used here as well since &lt;code&gt;sysfs&lt;/code&gt; is also virtual.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;./ubuntu-rootfs/sys&lt;/code&gt; is the mount point within the chroot.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This command is similar to the previous but mounts the &lt;code&gt;sysfs&lt;/code&gt; filesystem, which is necessary for interacting with system and hardware information.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;mount -o bind /dev ./ubuntu-rootfs/dev&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-o bind&lt;/code&gt; is an option to perform a bind mount. A bind mount creates a mirror of a directory or mount point to some other location.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;/dev&lt;/code&gt; is the source directory that contains device nodes and interfaces that the kernel provides.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;./ubuntu-rootfs/dev&lt;/code&gt; is the target directory where the device interfaces will be available within the chroot.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This command mounts the &lt;code&gt;/dev&lt;/code&gt; directory into the chroot environment's &lt;code&gt;/dev&lt;/code&gt; directory, allowing access to the device files from within the chroot. Device files are needed, for example, to access hard drives, input devices, etc.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;chroot ./ubuntu-rootfs /bin/bash&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;chroot&lt;/code&gt; changes the root directory for the session, or for the command specified, to a new location.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;./ubuntu-rootfs&lt;/code&gt; is the new root directory where the chroot environment has been prepared.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;/bin/bash&lt;/code&gt; is the command to run in the chroot environment, in this case, the Bash shell.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This command enters the chroot environment. Once within the chroot, the user will be operating as if &lt;code&gt;./ubuntu-rootfs&lt;/code&gt; were the root (&lt;code&gt;/&lt;/code&gt;) of the filesystem, and will start an interactive Bash shell.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Well, we are done, you can run any commands that you want to run as we are now inside the "Container" and the environment is entirely isolated and processes inside it don't interact with the processes outside chroot&lt;/p&gt;

&lt;p&gt;To get out of chroot, just press &lt;code&gt;Ctrl + d&lt;/code&gt; and you will be out of it&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;References&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/managing_monitoring_and_updating_the_kernel/using-cgroups-v2-to-control-distribution-of-cpu-time-for-applications_managing-monitoring-and-updating-the-kernel#preparing-the-cgroup-for-distribution-of-cpu-time_using-cgroups-v2-to-control-distribution-of-cpu-time-for-applications"&gt;https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/managing_monitoring_and_updating_the_kernel/using-cgroups-v2-to-control-distribution-of-cpu-time-for-applications_managing-monitoring-and-updating-the-kernel#preparing-the-cgroup-for-distribution-of-cpu-time_using-cgroups-v2-to-control-distribution-of-cpu-time-for-applications&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://akashrajpurohit.com/blog/build-your-own-docker-with-linux-namespaces-cgroups-and-chroot-handson-guide/"&gt;https://akashrajpurohit.com/blog/build-your-own-docker-with-linux-namespaces-cgroups-and-chroot-handson-guide/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;The series by DataDog --&amp;gt; &lt;a href="https://securitylabs.datadoghq.com/articles/container-security-fundamentals-part-1/"&gt;https://securitylabs.datadoghq.com/articles/container-security-fundamentals-part-1/&lt;/a&gt;
&lt;a href="https://securitylabs.datadoghq.com/articles/container-security-fundamentals-part-2/"&gt;https://securitylabs.datadoghq.com/articles/container-security-fundamentals-part-2/&lt;/a&gt;
&lt;a href="https://securitylabs.datadoghq.com/articles/container-security-fundamentals-part-3/"&gt;https://securitylabs.datadoghq.com/articles/container-security-fundamentals-part-3/&lt;/a&gt;
&lt;a href="https://securitylabs.datadoghq.com/articles/container-security-fundamentals-part-4/"&gt;https://securitylabs.datadoghq.com/articles/container-security-fundamentals-part-4/&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>containers</category>
      <category>cgroups</category>
      <category>namespaces</category>
      <category>linux</category>
    </item>
  </channel>
</rss>
