If you need to create a Linux distribution, this tutorial will clarify the important issues while giving a fast view on the creation processes.
This article focuses on Linux From Scratch and Ubuntu Live as ways to create your custom Linux distribution. LFS (Linux From Scratch) is a great method for advanced users, while Ubuntu Live is good for inexperienced ones.
The steps and commands shown are not meant to be functional but to portray the difficulty level of each process.
Linux From Scratch
LFS (Linux From Scratch) is the most popular tool to create custom Linux distributions from source. Creating your own Linux distribution may include some advantages (and disadvantages too). Among the advantages, during the process, you will learn a lot on Linux. You will know how Linux modules interact and how to customize the system.
The operating system size is also an advantage depending on your hardware resources or the use that you want to give the system.
On their website LFS, developers say that they created a web server to work with Apache on 5 mb size. Together with Gentoo Linux, Linux From Scratch is the most flexible way to set up a Linux system. Building it is pretty simple and the process is described step by step. We will only show the initial steps to portray an idea of the difficulty and a link to the official documentation from the building stage.
To get started with Linux From Scratch, you need to create first a partition (minimum 3 GB due compilation process). It is also recommendable to create a swap partition or to share your existing one (for instructions on partitioning, check Partitioning hard disks under Debian/Ubuntu and resizing partitions).
Once you create the partition, create the $LFS variable by running the following command:
You can check it by running the following command:
Create the directory as shown in the following:
Note: Replace <sdX> for your partition.
Then, run for the swap partition:
You need to download the packages to a specific directory called LFS/sources. To create the directory, run the following command:
Make it writable and sticky.
Download and save within the directory all the packages from
https://www.linuxfromscratch.org/lfs/view/development/chapter03/packages.html
And from https://www.linuxfromscratch.org/lfs/view/development/chapter03/patches.html
Alternatively, you can use the wget-list: https://www.linuxfromscratch.org/lfs/view/development/wget-list
Create the tools directory and symbolic link by running the following commnand:
Then, run the following command:
Run the following commands to create the lfs user, giving it rights on the tools and sources directories. Type the password when requested as shown in the following screenshot:
sudo useradd -s /bin/bash -g lfs -m -k /dev/null lfs
sudo passwd lfs
sudo chown -v lfs $LFS/tools
sudo chown -v lfs $LFS/sources
sudo su - lfs
Log in as the lfs user and run the following command:
Then, execute the following:
Finally, type the following command:
Create a new bashrc by running the following command:
umask 022
LFS=/mnt/lfs
LC_ALL=POSIX
LFS_TGT=$(uname -m)-lfs-linux-gnu
PATH=/tools/bin:/bin:/usr/bin
export LFS LC_ALL LFS_TGT PATH
EOF
Then, run the following command:
set MAKEFLAGS='-j 2'
Then, you can save the tools to start building your Linux distribution by following the steps at https://www.linuxfromscratch.org/lfs/view/development/chapter05/introduction.html.
After finishing, change the tools directory ownership by running the following command:
The official documentation to build your distribution, prepare the kernel. The base software can be found here. It is a sequence of steps that you need to create a customized distribution. The following steps are:
- Preparing a Virtual Kernel File Systems
- Package Management
- Entering the Chroot Environment
- Creating Directories
- Creating Essential Files and Symlinks
- Installing additional software Listed Here
- Stripping Again
- Cleaning Up
To start customizing the system, visit the following chapters:
- LFS-Bootscripts-20220723
- How Do These Bootscripts Work?
- Creating setlock script
- Configuring the Linux Console
- Configuring the sysklogd Script
- Creating the /etc/inputrc File
- The Bash Shell Startup Files
- Device and Module Handling on an LFS System
- Creating Custom Symlinks to Devices
- Configuring the localnet Script
- Customizing the /etc/hosts File
- General network customization
- Creating the /etc/fstab File
- Installation of the kernel
Finally, install GRUB using GRUB to set up the boot process and follow the steps before rebooting for the first time.
Creating Your Own Ubuntu Based Linux Distribution
Creating a customized Linux based on Ubuntu is pretty easy. It can be done very fast. It is extremely simple when compared with the Linux From Scratch but it is not flexible at all. You’ll be able to add the software and customize the background and some details. But the basic customizations like the applications menu edition are not supported.
Add the following command:
--variant=minbase
bionic
$HOME/live-ubuntu-from-scratch/chroot
Set the mount points.
sudo mount --bind /run $HOME/live-ubuntu-from-scratch/chroot/run
sudo chroot $HOME/live-ubuntu-from-scratch/chroot
mount none -t proc /proc
mount none -t sysfs /sys
mount none -t devpts /dev/pts
export HOME=/root
export LC_ALL=C
echo "ubuntu-fs-live" > /etc/hostname
Then, you’ll need to update the repositories and run the apt update using the following command:
dbus-uuidgen > /etc/machine-id
ln -fs /etc/machine-id /var/lib/dbus/machine-id
dpkg-divert --local --rename --add /sbin/initctl
ln -s /bin/true /sbin/initctl
apt-get install -y ubuntu-standard casper lupin-casper discover laptop-detect os-prober network-manager resolvconf net-tools wireless-tools wpagui locales linux-generic
When prompting the configuration screens like GRUB, press ENTER to default choices to continue. Then, run the following command:
Install any X Window Manager that you want.
Add any additional software that you want in your distribution. Then, run the following:
sudo apt-get install -y code
Select your locales and reconfigure the resolv.conf and network manager.
sudo dpkg-reconfigure resolv.conf
sudo dpkg-reconfigure network-manager
Then, run the following:
rm /sbin/initctl
apt-get clean
rm -rf /tmp/* ~/.bash_history
Unmount all the filesystems.
umount /sys
umount /dev/pts
export HISTSIZE=0exit
sudo umount $HOME/live-ubuntu-from-scratch/chroot/dev
sudo umount $HOME/live-ubuntu-from-scratch/chroot/run
Create the directories and copy the kernel and binaries.
mkdir -p image/{casper,isolinux,install}
sudo cp chroot/boot/vmlinuz-**-**-generic image/casper/vmlinuz
sudo cp chroot/boot/initrd.img-**-**-generic image/casper/initrd
sudo cp chroot/boot/memtest86+.bin image/install/memtest86+
wget --progress=dot https://www.memtest86.com/downloads/memtest86-usb.zip -O image/install/memtest86-usb.zipunzip -p image/install/memtest86-usb.zip memtest86-usb.img > image/install/memtest86rm image/install/memtest86-usb.zip
To finish, setup GRUB by executing the following:
sudo mksquashfs chroot image/casper/filesystem.squashfs
printf $(sudo du -sx --block-size=1 chroot | cut -f1) > image/casper/filesystem.size
cd $HOME/live-ubuntu-from-scratch
cd $HOME/live-ubuntu-from-scratch/image
grub-mkstandalone --format=x86_64-efi --output=isolinux/bootx64.efi --locales="" --fonts="" "boot/grub/grub.cfg=isolinux/grub.cfg"
grub-mkstandalone --format=i386-pc --output=isolinux/core.img --install-modules="linux16 linux normal iso9660 biosdisk memdisk search tar ls" --modules="linux16 linux normal iso9660 biosdisk search" --locales="" --fonts="" "boot/grub/grub.cfg=isolinux/grub.cfg"
cat /usr/lib/grub/i386-pc/cdboot.img isolinux/core.img > isolinux/bios.img
sudo /bin/bash -c "(find . -type f -print0 | xargs -0 md5sum | grep -v "./md5sum.txt" > md5sum.txt)"
sudo xorriso -as mkisofs -iso-level 3 -full-iso9660-filenames -volid "<YourDistroName>" -eltorito-boot boot/grub/bios.img -no-emul-boot -boot-load-size 4 -boot-info-table --eltorito-catalog boot/grub/boot.cat
--grub2-boot-info --grub2-mbr /usr/lib/grub/i386-pc/boot_hybrid.img -eltorito-alt-boot -e EFI/efiboot.img -no-emul-boot -append_partition 2 0xef isolinux/efiboot.img -output "../<yourdistronam>.iso" -graft-points "." /boot/grub/bios.img=isolinux/bios.img /EFI/efiboot.img=isolinux/efiboot.img
As you can see, the process with Ubuntu Live is a lot faster and easier. But it is not more than an Ubuntu distribution with light customizations in contrast to Linux From Scratch which is fully customizable.
Conclusion
As you can see, creating a Linux distribution may be a very hard task, but a good goal to get a full understanding on how Linux systems work. This happens especially with the first described method based on Linux from Scratch. The Ubuntu alternative is also a good starting point for the new users who are looking for an Ubuntu based custom Linux distribution. The LFS process is messy; do not give up before errors which are common when getting started. After all, you are creating an operating system.
We hope that you found this tutorial useful. Keep following LinuxHint for additional tips and updates on Linux and networking.