
Creating an operating system based on the linux kernel. From scratch
Sooner or later, every Linux user thinks about creating his own distribution kit. Some argue that you can "customize everything for yourself." Others complain that among the distributions already presented in the Branch there is no ideal. And they supposedly have superconceptual ideas for their own system. Why did I start all this psychology? In order to immediately block the oxygen beginners playing with Linux, who have nothing to do. If you are already thinking about creating an OS, think to the end. So,
I want to create a Linux based OS.
I warn you right away: there would be an eighteenth century, all those who choose another developed distribution (and, God forbid, the popular ...) would be waiting for the gallows to base their future system. The post is about creating a system from scratch, which means that we will not touch all sorts of Slax and Linux Mint.
Step 1. Selecting the media
There are few options: either your OS starts from a LiveCD, or from a hard disk, or from a flash device. I’ll make a reservation right away: I won’t say a word about the hard drive in a post, because it’s much more convenient to create a flexible distribution kit from the “I carry everything with you” series, or a locked distribution on an optical disk. If you learn how to create a LiveCD or LiveUSB system, there will be no problem installing it on your hard drive.
Just in case, prepare a blank USB flash drive, CD-ROM, and finally install Virtualbox.
Step 2. Compiling the kernel
Regarding the release of the third Linux kernel, this step inspires further development ... So, we need the kernel sources. Every user knows that they can be obtained at kernel.org. By no means, hear ?, never screw an extraneous kernel compiled not by you to your system!

Since my laziness went wild, I created the / linuxkernel folder and unpacked the archive with the sources there. Logging in as root, I did the following:
In principle, the kernel can be configured in three ways: make config (dialog configuration), make menuconfig (pseudographic configuration via ncurses), and also make xconfig (graphical configuration). The bottom line is that make config will ruin your mood for a long time, because he will ask all possible questions on all aspects of all topics. The problem with make xconfig is not encountered by everyone, but I have met it and it does. If you feel the urge to do through X, sort it out yourself. The best option is make menuconfig. This thing will open you a pseudographic interface through which you can configure the kernel in your own way. Stuck requires the ncurses library, which is easy to install.

Basically, if your brain understands Linux at least as much as you can figure out how to configure it. This process is interesting, there are really many options, and the help, although in English, but still pleases with its accessibility and simplicity.
However, you still have to send. Go to File Systems ---> and put the necessary stars. The letter M means that the support of a driver is carried out by connecting an external module to the kernel (I hate them!). We will also need isofs support for reading disks. File Systems ---> CD-ROM / DVD Filesystems ---> ISO 9660 CDROM file system support. You can still support the ancient Dos systems.

The smart Mandriva developers forgot to allow File systems ---> DOS / FAT / NT Filesystems ---> NTFS write support, and on one of their distributions I was tormented with access to the old Windows partition.
Take a look at Processor type and features ---> Processor family, I was recommended to choose Pentium-MMX.
Still rummage in Device Drivers, it is useful. For fun, you can pick it all up and compile a kernel weighing> 50 MB.
Further. The kernel after loading itself should load, in fact, the system. Either from compiled files (used in embedded systems), from a CPIO archive compressed by something, or from Initrd. Here you are not DOS, here you will not be able to immediately refer to some init'ovy file in the root directory of the drive or flash drive.It actually works outThis is wrong, even though on the Internet there is already a lot of controversy over this issue. In our system we will use initrd, as it is convenient, and does not cause foul language from third-party developers, unlike the CPIO archive.
Oh yes, compile the kernel with the command
If you have x86, you will find it at / linuxkernel / arch / x86 / boot / bzImage.
For harsh Chelyabinsk programmers, you can use Cross-compiling ...
Creating Ramdisk.
Now we need initrd with the simplest shell installed there. We will use busybox, because this nyasha can do anything. The way we will steal from Roberto de Leo, the creator of Movix (I would even begin to respect him if it were not for the transcendental love for Perl):
That's it, now we have a Ramdisk with a capacity of 5 MB. It is possible and more, just not necessary. Unlike Thomas Matejisek, I am not going to stuff initrd modules in Squashfs compressed with LZMA. All that is needed will be compiled with the kernel. Yes, this is not very logical and correct, but the trouble is a hundred times smaller. And especially for those who condemn this approach, you can enable the modularity option in the kernel: Enable loadable module support.
Our Ramdisk mounted in / distro has such a folder, lost + found. This is because we formatted it in ext2. In no case can you delete it, although it is unlikely to help here, the image is somehow fixed. We would have to busybox first ...
Install Busybox
That's why such cool projects have such lousy sites? Although ... it doesn’t matter if the sources are downloaded and successfully unpacked into the / busybox folder.

You can configure busybox in the same way: If you still do not understand what it is, I will explain. Busybox replaces tons of UNIX applications stored in the / bin, / sbin, / usr / bin, / usr / sbin folders. Instead, only one application is created: / bin / busybox, and a bunch of links are created to it in the above folders. Install busybox with the following command: Busybox will also create / sbin / init files and / linuxrc for some reason so that your system starts up correctly. But not all the necessary folders have been created. So we finish everything with our hands and create: If you forgot something, remember, because These directories are hard to forget.

Everything would be fine, but busybox only requires libraries that need to be copied to our distribution kit. It is very easy to find out which ones:
The program will show us the libraries required for our shell. I say right away: linux gate is created by the kernel and cannot be copied.
When copying libraries, you can cut off debugging information (as Roberto advises):
We make Linux from Linux
We need to create several system text files:
We need / etc / inittab. I will surprise you: at the beginning of life, the system does not even know what Root is. We even have a nameless user, but a file of system-wide low-level features (ONF) should be present. The pilot contents of the file are as follows: The next file is / etc / fstab. This is a table that describes what and where to mount at boot.
Useless thing! We need to mount proc, otherwise nothing will work at all, so in the file we write:
For mount we also need the / etc / mtab file. Create it and leave it blank.
But mount will do everything necessary only when we explicitly ask for it. And we will ask in the same boot file /etc/rc.d/rc.S (rc.d - folder). We politely ask: We also need a profile file (b) (a) sh, here there is generally expanse for imagination. We create the file / etc / profile and fill it with the following: You will also need the file / etc / shell, which indicates that there is a shell: That's all. You can write our Ramdisk to a file. Creating a bootable flash drive
The "finish line" of our small development. We take a flash drive, insert it, format it in vfat (it is possible in ext, too, but do not forget that not all Windows users have shot themselves yet).
On the flash drive, create the boot folder, in it the initrd and kernel folders.
Copy the compressed Ramdisk from the / os folder to the boot / initrd folder on the USB flash drive, call it “main.gz”. From the folder with the kernel sources, copy bzImage to the boot / kernel folder on the USB flash drive, call it “main.lk”. We get the Syslinux bootloader files (on the Internet, or from another distribution: it doesn’t matter here), namely syslinux.bin, syslinux.boot, syslinux.cfg. Copy them to the root directory of our flash drive. In the syslinux.cfg file we write something similar: Thus, we supported the custom initrd and the kernel, which, experimentally, can be connected to our distribution.
We will find out which device in the system is our flash drive (you can run mount without parameters and see). This is either / dev / sdb1, or / dev / sdc1, or / dev / sdd1. It is necessary to unmount the USB flash drive before starting the installation.
Install syslinux (if there is no package in the system, apt-get install syslinux):
The ldlinux.sys file should appear in the root directory of the flash drive. If it is, then syslinux.bin, syslinux.boot are no longer needed.

How to configure the BIOS to boot from a flash drive, I will not tell you - it's easy. I can only say that it is very convenient to create a folder / boot / initrd / init, in which you can mount / boot / initrd / main, for further work with it. Just do not forget to expand and compress it with gzip.
That's it.
Like I just told you how to create a Linux system from scratch. Easy, right? Then you can edit the script / sbin / init, because you still have a lot of work to do! You will need to write a script to mount the flash drive, which makes chroot to the root directory. Otherwise, you will have to work with ReadOnly partition, the size of 5 MB. But this is a completely different story.
Davis Unnx T , Bed and .
For the
uneducated : Thomas Matejisek is the creator of Slax and Linux Live Scripts.
Roberto de Leo is the creator of Movix.
I want to create a Linux based OS.
I warn you right away: there would be an eighteenth century, all those who choose another developed distribution (and, God forbid, the popular ...) would be waiting for the gallows to base their future system. The post is about creating a system from scratch, which means that we will not touch all sorts of Slax and Linux Mint.
Step 1. Selecting the media
There are few options: either your OS starts from a LiveCD, or from a hard disk, or from a flash device. I’ll make a reservation right away: I won’t say a word about the hard drive in a post, because it’s much more convenient to create a flexible distribution kit from the “I carry everything with you” series, or a locked distribution on an optical disk. If you learn how to create a LiveCD or LiveUSB system, there will be no problem installing it on your hard drive.
Just in case, prepare a blank USB flash drive, CD-ROM, and finally install Virtualbox.
Step 2. Compiling the kernel
Regarding the release of the third Linux kernel, this step inspires further development ... So, we need the kernel sources. Every user knows that they can be obtained at kernel.org. By no means, hear ?, never screw an extraneous kernel compiled not by you to your system!

Since my laziness went wild, I created the / linuxkernel folder and unpacked the archive with the sources there. Logging in as root, I did the following:
cd /linuxkernel
make menuconfig
In principle, the kernel can be configured in three ways: make config (dialog configuration), make menuconfig (pseudographic configuration via ncurses), and also make xconfig (graphical configuration). The bottom line is that make config will ruin your mood for a long time, because he will ask all possible questions on all aspects of all topics. The problem with make xconfig is not encountered by everyone, but I have met it and it does. If you feel the urge to do through X, sort it out yourself. The best option is make menuconfig. This thing will open you a pseudographic interface through which you can configure the kernel in your own way. Stuck requires the ncurses library, which is easy to install.

Basically, if your brain understands Linux at least as much as you can figure out how to configure it. This process is interesting, there are really many options, and the help, although in English, but still pleases with its accessibility and simplicity.
However, you still have to send. Go to File Systems ---> and put the necessary stars. The letter M means that the support of a driver is carried out by connecting an external module to the kernel (I hate them!). We will also need isofs support for reading disks. File Systems ---> CD-ROM / DVD Filesystems ---> ISO 9660 CDROM file system support. You can still support the ancient Dos systems.

The smart Mandriva developers forgot to allow File systems ---> DOS / FAT / NT Filesystems ---> NTFS write support, and on one of their distributions I was tormented with access to the old Windows partition.
Take a look at Processor type and features ---> Processor family, I was recommended to choose Pentium-MMX.
Still rummage in Device Drivers, it is useful. For fun, you can pick it all up and compile a kernel weighing> 50 MB.
Further. The kernel after loading itself should load, in fact, the system. Either from compiled files (used in embedded systems), from a CPIO archive compressed by something, or from Initrd. Here you are not DOS, here you will not be able to immediately refer to some init'ovy file in the root directory of the drive or flash drive.
Oh yes, compile the kernel with the command
make bzImage
If you have x86, you will find it at / linuxkernel / arch / x86 / boot / bzImage.
For harsh Chelyabinsk programmers, you can use Cross-compiling ...
Creating Ramdisk.
Now we need initrd with the simplest shell installed there. We will use busybox, because this nyasha can do anything. The way we will steal from Roberto de Leo, the creator of Movix (I would even begin to respect him if it were not for the transcendental love for Perl):
dd if=/dev/zero of=/dev/ram0 bs=1k count=5000 - Создаем Ramdisk в оперативной памяти нашего компьютера.
mke2fs -m0 /dev/ram0 5000 - Форматируем Ramdisk в системе Ext2
mkdir /distro - Создаем папку
mount /dev/ram0 /distro - Монтируем в папку /distro
That's it, now we have a Ramdisk with a capacity of 5 MB. It is possible and more, just not necessary. Unlike Thomas Matejisek, I am not going to stuff initrd modules in Squashfs compressed with LZMA. All that is needed will be compiled with the kernel. Yes, this is not very logical and correct, but the trouble is a hundred times smaller. And especially for those who condemn this approach, you can enable the modularity option in the kernel: Enable loadable module support.
Our Ramdisk mounted in / distro has such a folder, lost + found. This is because we formatted it in ext2. In no case can you delete it, although it is unlikely to help here, the image is somehow fixed. We would have to busybox first ...
Install Busybox
That's why such cool projects have such lousy sites? Although ... it doesn’t matter if the sources are downloaded and successfully unpacked into the / busybox folder.

You can configure busybox in the same way: If you still do not understand what it is, I will explain. Busybox replaces tons of UNIX applications stored in the / bin, / sbin, / usr / bin, / usr / sbin folders. Instead, only one application is created: / bin / busybox, and a bunch of links are created to it in the above folders. Install busybox with the following command: Busybox will also create / sbin / init files and / linuxrc for some reason so that your system starts up correctly. But not all the necessary folders have been created. So we finish everything with our hands and create: If you forgot something, remember, because These directories are hard to forget.
cd /busybox
make menuconfig

make CONFIG_PREFIX=/distro install
/distro/etc
/distro/lib
/distro/dev
/distro/mnt
distro/proc
/distro/root
/distro/tmp
/distro/root
Everything would be fine, but busybox only requires libraries that need to be copied to our distribution kit. It is very easy to find out which ones:
ldd /distro/bin/busybox
The program will show us the libraries required for our shell. I say right away: linux gate is created by the kernel and cannot be copied.
When copying libraries, you can cut off debugging information (as Roberto advises):
objcopy --strip-debug откуда куда
We make Linux from Linux
We need to create several system text files:
We need / etc / inittab. I will surprise you: at the beginning of life, the system does not even know what Root is. We even have a nameless user, but a file of system-wide low-level features (ONF) should be present. The pilot contents of the file are as follows: The next file is / etc / fstab. This is a table that describes what and where to mount at boot.
::sysinit:/etc/rc.d/rc.S
# Запустить оболочку в консоли.
::respawn:-/bin/sh
# Перезагрузка по нажатии на Ctrl+Alt+Del.
::ctrlaltdel:/sbin/reboot
# Команды, выполняемые перед выключением и перезагрузкой.
::shutdown:/sbin/swapoff -a >/dev/null 2>&1
::shutdown:/bin/umount -a -r >/dev/null 2>&1
none /proc proc defaults 0 0
For mount we also need the / etc / mtab file. Create it and leave it blank.
But mount will do everything necessary only when we explicitly ask for it. And we will ask in the same boot file /etc/rc.d/rc.S (rc.d - folder). We politely ask: We also need a profile file (b) (a) sh, here there is generally expanse for imagination. We create the file / etc / profile and fill it with the following: You will also need the file / etc / shell, which indicates that there is a shell: That's all. You can write our Ramdisk to a file. Creating a bootable flash drive
#!/bin/ash
/bin/mount -av -t nonfs
PATH="$PATH:/bin:/sbin:/usr/bin:/usr/sbin:"
LESS=-MM
TERM=linux
HOME=/root
PS1='> '
PS2='> '
ignoreeof=10
export PATH DISPLAY LESS TERM PS1 PS2 HOME ignoreeof
/bin/sh
/bin/ash
/bin/bash
mkdir /os - папка для "готового".
umount /dev/ram0 - размонтируем кусочек оперативной памяти.
dd if=/dev/ram0 of=/os/initrd bs=1k count=5000 - создаем файл.
gzip /os/initrd - сжимаем файл initrd
The "finish line" of our small development. We take a flash drive, insert it, format it in vfat (it is possible in ext, too, but do not forget that not all Windows users have shot themselves yet).
On the flash drive, create the boot folder, in it the initrd and kernel folders.
Copy the compressed Ramdisk from the / os folder to the boot / initrd folder on the USB flash drive, call it “main.gz”. From the folder with the kernel sources, copy bzImage to the boot / kernel folder on the USB flash drive, call it “main.lk”. We get the Syslinux bootloader files (on the Internet, or from another distribution: it doesn’t matter here), namely syslinux.bin, syslinux.boot, syslinux.cfg. Copy them to the root directory of our flash drive. In the syslinux.cfg file we write something similar: Thus, we supported the custom initrd and the kernel, which, experimentally, can be connected to our distribution.
default mm
prompt 1
timeout 100
label mm
kernel /boot/kernel/main.lk
append initrd=/boot/initrd/main.gz load_ramdisk=1 ramdisk_size=5000 rw root=/dev/ram0
label mc
kernel /boot/kernel/main.lk
append initrd=/boot/initrd/custom.gz load_ramdisk=1 ramdisk_size=5000 rw root=/dev/ram0
label cm
kernel /boot/kernel/custom.lk
append initrd=/boot/initrd/main.gz load_ramdisk=1 ramdisk_size=5000 rw root=/dev/ram0
label cc
kernel /boot/kernel/custom.lk
append initrd=/boot/initrd/custom.gz load_ramdisk=1 ramdisk_size=5000 rw root=/dev/ram0
label hd
localboot 0x80
We will find out which device in the system is our flash drive (you can run mount without parameters and see). This is either / dev / sdb1, or / dev / sdc1, or / dev / sdd1. It is necessary to unmount the USB flash drive before starting the installation.
Install syslinux (if there is no package in the system, apt-get install syslinux):
syslinux -d путь_к_устройству
The ldlinux.sys file should appear in the root directory of the flash drive. If it is, then syslinux.bin, syslinux.boot are no longer needed.

How to configure the BIOS to boot from a flash drive, I will not tell you - it's easy. I can only say that it is very convenient to create a folder / boot / initrd / init, in which you can mount / boot / initrd / main, for further work with it. Just do not forget to expand and compress it with gzip.
That's it.
Like I just told you how to create a Linux system from scratch. Easy, right? Then you can edit the script / sbin / init, because you still have a lot of work to do! You will need to write a script to mount the flash drive, which makes chroot to the root directory. Otherwise, you will have to work with ReadOnly partition, the size of 5 MB. But this is a completely different story.
Davis Unnx T , Bed and .
For the
uneducated : Thomas Matejisek is the creator of Slax and Linux Live Scripts.
Roberto de Leo is the creator of Movix.