<?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>DEV Community: ChienPang Lee</title>
    <description>The latest articles on DEV Community by ChienPang Lee (@chienpang_lee_f5d1ce793ef).</description>
    <link>https://dev.to/chienpang_lee_f5d1ce793ef</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%2F3911331%2F04428831-4267-49d6-bbee-dfe150680abe.png</url>
      <title>DEV Community: ChienPang Lee</title>
      <link>https://dev.to/chienpang_lee_f5d1ce793ef</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chienpang_lee_f5d1ce793ef"/>
    <language>en</language>
    <item>
      <title>Small footprint of a power editor in initramfs</title>
      <dc:creator>ChienPang Lee</dc:creator>
      <pubDate>Mon, 04 May 2026 11:15:17 +0000</pubDate>
      <link>https://dev.to/chienpang_lee_f5d1ce793ef/small-footprint-of-a-power-editor-in-initramfs-5gkc</link>
      <guid>https://dev.to/chienpang_lee_f5d1ce793ef/small-footprint-of-a-power-editor-in-initramfs-5gkc</guid>
      <description>&lt;p&gt;Have you ever found yourself trapped in a restricted root filesystem where every megabyte has to be weighed? For most people, this is a rarity, but when working with &lt;strong&gt;initramfs&lt;/strong&gt;—the transient filesystem used by the Linux kernel during the boot process—space is everything.&lt;/p&gt;

&lt;p&gt;A text mode editor is a vital tool for navigating filesystem, debugging problems and changing configurations. However, as a dedicated &lt;strong&gt;Emacs&lt;/strong&gt; user, the standard lightweight alternatives like &lt;code&gt;vi&lt;/code&gt;, &lt;code&gt;nano&lt;/code&gt;, or &lt;code&gt;pico&lt;/code&gt; is not in the favored list. In such an extreme environment where I from time to time have to handle critical boot issues, a working, light, and powerful editor is to me not just a vital tool but a luxury.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Challenge: Why Not &lt;code&gt;emacs-nox&lt;/code&gt;?
&lt;/h3&gt;

&lt;p&gt;Navigating a filesystem without a proper editor is like living in a read-only world. While &lt;code&gt;echo&lt;/code&gt; redirection and &lt;code&gt;sed&lt;/code&gt; can handle simple tasks, complex configuration changes become a painful exercise in precision.&lt;/p&gt;

&lt;p&gt;Normally, I would reach for &lt;code&gt;emacs-nox&lt;/code&gt; (the full Emacs experience without the X11 overhead). But in an initramfs environment, adding &lt;code&gt;emacs-nox&lt;/code&gt; and its dependencies can balloon the image size by over 100MB. For a system that needs to be lean and fast, that’s simply not an option.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Solution: &lt;code&gt;mg&lt;/code&gt; (Micro GNU Emacs)
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;mg&lt;/code&gt;&lt;/strong&gt; is a microscopic editor that maintains Emacs keybindings while remaining incredibly lightweight. On a standard Ubuntu system, it’s a tiny footprint:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# apt show mg
Package: mg
Version: 20230501-1
Priority: optional
Section: universe/editors
Origin: Ubuntu
Maintainer: Ubuntu Developers &amp;lt;ubuntu-devel-discuss@lists.ubuntu.com&amp;gt;
Original-Maintainer: Harald Dunkel &amp;lt;harri@afaics.de&amp;gt;
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 273 kB
Provides: editor
Depends: libbsd0 (&amp;gt;= 0.5.0), libc6 (&amp;gt;= 2.38), libtinfo6 (&amp;gt;= 6)
Homepage: https://homepage.boetes.org/software/mg/
Download-Size: 121 kB
APT-Manual-Installed: yes
APT-Sources: http://mirror.math.princeton.edu/pub/ubuntu noble/universe amd64 Packages
Description: microscopic GNU Emacs-style editor
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On a standard Linux operating system like Ubuntu, it is as simple as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;root@frad:~/Workspace/# &lt;span class="c"&gt;# apt-get install mg&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nonetheless, we are dealing with a mimimized distroless rootfilesystem that doen't come with package managers of any sort. To get mg to work inside mini rootfs, assuming you're on a Ubuntu, install mg with the above. After installation, the binary can be located with&lt;/p&gt;

&lt;p&gt;All that is still on host system. To get mg working in initramfs, we have to perform a manual port.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Binary Placement
&lt;/h2&gt;

&lt;p&gt;First, we locate the binary on the host and copy it into our target initramfs directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;root@frad:~/Workspace/# &lt;span class="c"&gt;# which mg&lt;/span&gt;
/usr/bin/mg
root@frad:~/Workspace/# &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; &lt;span class="nt"&gt;-D&lt;/span&gt; &lt;span class="nt"&gt;-m&lt;/span&gt; 0755 /usr/bin/mg ~/Workspace/img/mini_initramfs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let take a peek how it'd work inside the rootfs&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;root@frad:~/Workspace/# &lt;span class="nb"&gt;chroot&lt;/span&gt; ~/Workspace/img/mini_initramfs
/ &lt;span class="c"&gt;# echo $PATH&lt;/span&gt;
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
/ &lt;span class="c"&gt;# mg&lt;/span&gt;
/bin/sh: mg: not found
/ &lt;span class="c"&gt;# ls -lt /bin/mg&lt;/span&gt;
&lt;span class="nt"&gt;-rwxr-xr-x&lt;/span&gt;    1 0        0           231080 Apr 20 06:48 /bin/mg
/ &lt;span class="c"&gt;#&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Invoking md command inside the mini initramfs came back with an error that "mg" was not found. Further commands confirmed that the binary was there and path was also correct. The installed mg turned out to be a dynamically-linked binary so the shared libraries also need to be copied.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Resolving Dependencies
&lt;/h2&gt;

&lt;p&gt;To fix this, we use ldd to identify the shared library dependencies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;root@frad:~/Workspace/# ldd /usr/bin/mg
    libtinfo.so.6 &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; /lib/x86_64-linux-gnu/libtinfo.so.6
    libbsd.so.0 &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; /lib/x86_64-linux-gnu/libbsd.so.0
    libc.so.6 &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; /lib/x86_64-linux-gnu/libc.so.6
    /lib64/ld-linux-x86-64.so.2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We must replicate this structure inside our initramfs:&lt;/p&gt;

&lt;h1&gt;
  
  
  Create directory structure and symlinks
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;root@frad:~/Workspace/# &lt;span class="nb"&gt;mkdir&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; ~/Workspace/img/mini_initramfs/usr/lib/x86_64-linux-gnu
root@frad:~/Workspace/# &lt;span class="nb"&gt;ln&lt;/span&gt; &lt;span class="nt"&gt;-sf&lt;/span&gt; usr/lib ~/Workspace/img/mini_initramfs/lib
root@frad:~/Workspace/# &lt;span class="nb"&gt;ln&lt;/span&gt; &lt;span class="nt"&gt;-sf&lt;/span&gt; ../lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 ~/Workspace/img/mini_initramfs/lib64/
root@frad:~/Workspace/# ldd /usr/bin/mg | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="s2"&gt;"=&amp;gt;"&lt;/span&gt; | &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'{print $3}'&lt;/span&gt; | xargs &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="nb"&gt;cp&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt; ~/Workspace/img/mini_initramfs&lt;span class="o"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3: Solving the "Terminal Panic"
&lt;/h2&gt;

&lt;p&gt;After this, give it another try!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;root@frad:~/Workspace/# &lt;span class="nb"&gt;chroot&lt;/span&gt; ~/Workspace/img/mini_initramfs
/ &lt;span class="c"&gt;# mg&lt;/span&gt;
panic: Terminal setup failed
/ &lt;span class="c"&gt;# echo $TERM&lt;/span&gt;
xterm
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;mg was launched with previous errors gone. But there appeared to be a new error about terminal panic. The terminal is "xterm". With some more digging it pointed to missing data/db of xterm capabilities. As such, I copied xterm pertinent data into rootfs&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;/bin/bash ~/Workspace/scripts/install ~/Workspace/img/mini_initramfs /usr/share/terminfo/x/xterm usr/share/terminfo/x

root@frad:~/Workspace/# &lt;span class="nb"&gt;chroot&lt;/span&gt; ~/Workspace/img/mini_initramfs
/ &lt;span class="c"&gt;# ls -lt /usr/share/terminfo/x/&lt;/span&gt;
total 4
&lt;span class="nt"&gt;-rw-r--r--&lt;/span&gt;    1 0        0             3991 Apr  8  2024 xterm
/ &lt;span class="c"&gt;# mg&lt;/span&gt;
/ &lt;span class="c"&gt;#&lt;/span&gt;
/ &lt;span class="c"&gt;# ls -lt /lib/x86_64-linux-gnu/ /lib64&lt;/span&gt;
lrwxrwxrwx    1 0        0                9 Apr 20 08:19 /lib64 -&amp;gt; usr/lib64

/lib/x86_64-linux-gnu/:
total 2652
&lt;span class="nt"&gt;-rwxr-xr-x&lt;/span&gt;    1 0        0           236616 Apr 20 08:19 ld-linux-x86-64.so.2
&lt;span class="nt"&gt;-rwxr-xr-x&lt;/span&gt;    1 0        0             3632 Apr 20 08:19 ld-linux-x86-64.so.2.debug
&lt;span class="nt"&gt;-rw-r--r--&lt;/span&gt;    1 0        0            80888 Apr 20 08:19 libbsd.so.0
&lt;span class="nt"&gt;-rwxr-xr-x&lt;/span&gt;    1 0        0          2125328 Apr 20 08:19 libc.so.6
&lt;span class="nt"&gt;-rw-r--r--&lt;/span&gt;    1 0        0            55536 Apr 20 08:19 libmd.so.0
&lt;span class="nt"&gt;-rw-r--r--&lt;/span&gt;    1 0        0           208328 Apr 20 08:19 libtinfo.so.6
/ &lt;span class="c"&gt;# du -hsc /lib/x86_64-linux-gnu/ /lib64 /bin/mg&lt;/span&gt;
2.6M    /lib/x86_64-linux-gnu/
0   /lib64
228.0K  /bin/mg
2.8M    total
/ &lt;span class="c"&gt;# mg /lib/x86_64-linux-gnu/&lt;/span&gt;
/ &lt;span class="c"&gt;#&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The Result
&lt;/h2&gt;

&lt;p&gt;And there we go: a functional, Emacs-style editor with small footprint (2.8M in total) in a distroless, customized and size-critical filesystem. I'd play a mission-critical role when it comes to future trouble-shooting during bootstrapping, expecially when kernel panics.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnv9p44yh8ena9cspjbf2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnv9p44yh8ena9cspjbf2.png" alt=" "&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>initramfs</category>
      <category>emacs</category>
      <category>linux</category>
      <category>kernel</category>
    </item>
  </channel>
</rss>
