Lyrical part
Who among us in childhood didn’t make out toys to see what was inside. I was no exception. Only a few years passed, and Linux became one of the toys. From the abstract desire to “break and see” it was formalized into several very specific tasks, one of which was to find some starting point - the minimal thing that would load and give the shell. A couple of times I started collecting lfs, but I still did not get to the end. I read several articles about the development of embedded, but there everything was too serious and grown-up: after the proposals to build the environment for cross-compilation for various architectures and remote debugging turned off the Internet in a panic. I reviewed a few mini-livecd, but they all were either megadrevne (kernel 2.4 and below) or necessarily with graphics and DE, which did not fit into my idea of the ideal minidist for experiments.
And not so long ago, I accidentally found out that what I was looking for was called initramfs and was under my nose.
HOWTO part
Let us set ourselves the task of getting a small, cozy and, preferably, useful distribution package with something, without making special efforts. By and large, we will need a boot loader, a kernel, and an initramfs image. The easiest way to get them is to pull them out of the ready-made distribution. In order to make it at least somewhat useful, we put in there a busybox and configure several network services. I will show how to do this with CentOS, but in principle, any one will do. So let's get started:
Extract the initrd somewhere:
mkdir /root/myimage
cd /root/myimage
gzip -d < /boot/initrd-2.6.18-128.el5 | cpio --extract --verbose --make-directories --no-absolute-filenames
Install and copy busybox to our minilinux (busybox does not link to anything, so feel free to copy)
yum install busybox
cp -a /sbin/busybox /root/myimage/bin
We leave in init only the most necessary minimum: we create procfs and sysfs, the main device files, load the necessary drivers (for my network I needed to copy them from / lib of the main distribution) and run a couple of services. Here is:
/ init
')
#!/bin/nash
mount -t proc /proc /proc
mount -t sysfs /sys /sys
mount -o mode=0755 -t tmpfs /dev /dev
mkdir /dev/pts
mount -t devpts -o gid=5,mode=620 /dev/pts /dev/pts
mkdir /dev/shm
mknod /dev/null c 1 3
mknod /dev/zero c 1 5
mknod /dev/systty c 4 0
mknod /dev/tty c 5 0
mknod /dev/console c 5 1
mknod /dev/ptmx c 5 2
mknod /dev/rtc c 10 135
mknod /dev/tty0 c 4 0
mkblkdevs
insmod /lib/libphy.ko
insmod /lib/tg3.ko
mkblkdevs
echo WELCOME!!!
/bin/busybox ln -s /bin/busybox /bin/ash
/bin/busybox ln -s /bin/busybox /bin/telnetd
/bin/busybox ifconfig eth0 192.168.1.1 netmask 255.255.255.0
/bin/busybox httpd
/bin/busybox inetd /etc/inetd.conf
/bin/ash
/ etc / passwd
root:x:0:0:root:/:/bin/ash
/etc/inetd.conf
23 stream tcp nowait root /bin/telnetd telnetd -l /bin/ash
We collect initramfs and return to the place:
find . | cpio -H newc --create --verbose | gzip -9 > /boot/initrd-2.6.18-128.el5
Voila Our minilinux is ready. You can demolish everything except the / boot directory and reboot. But for greater reliability, we transfer it to a USB flash drive:
Suppose a flash drive is defined by the kernel as / dev / sdb. There is only one fat section on it. With fdisk we will make it bootable.
fdisk /dev/sdb
We check if there is an asterisk in the boot column. If not, press a, and then w.
Mount the flash drive somewhere and copy / boot to it
mount /dev/sdb1 /mnt
cp -a /boot /mnt
umount /dev/sdb1
Using grub, overwrite the boot sector of the flash drive
grub
>find (hd1,0)/boot/grub/stage2
>root (hd1,0)
>setup (hd0)
>quit
here I made the assumption that grub "saw" the flash drive as disk 2. If this is not the case, find will give an error (will not find / boot). See the result of find (hd2,0) ... find (hd3,0) ..., etc., until you find the USB flash drive.
Total
Well that's all. Reboot, turn on the boot from removable media in the BIOS and get the command line prompt of our minilinux. We are also trying to connect to the 192.168.1.1 telnet client and open it in the browser
192.168.1.1/init. If someone is inspired by the theme of creating their own small distribution kit, here are some directions for further research:
- See utilbox busybox. There are many useful network services: http, dhcp, tftp, smtp, tod, inetd. From tar + gzip + ftpput you get a great “unattended backup”. There is also a lot of utilities for working with FS, which will allow you to create your own rescue cd.
- Identify more devices. As you probably noticed, I threw out all the disk and fs drivers from the init-script. ABOUT!!! And by the way, hotplug and USB drivers are also disabled, so if you have a usb keyboard, first of all, put them back in place.
I hope this article will be useful to someone.