Friday, June 5, 2015

Tagged Under: ,

Create your own linux based operating system Using Linux Kernel from Scratch

By: sachin On: 9:58 PM
  • Share The Gag
  • Create your own operating system from scratch

    Creating your own custom operating system, is a dream for many of us , But now it has come true. Creating your own operating system using the linux kernel as the base has been explained in this article.
    To create your own operating system you need to have the basic idea of the linux commands , which you will be using to perform the operations in the terminal.

    What is initrd

    INITRD is initial boot disk, means this is the one which helps all the other modules of an operating system to load. this is loaded as part of the kernel loading procedure, let us stop the explanation on initrd for the time being.

    Basic knowledge of linux / bash commands

    cd : Change directory
    ls : List the files in the current directory
    mkdir : Makes a directory
    cp : Used to copy / move a file
    rm : To refove a file / directory
    sudo : This is to access the super user permision

    Prerequisites for building OS  

    You need to download tiny core Linux  kernel , here we used tiny core since it is lite ( Download ).
    The required files are downloaded by you ? if yes proceed

    Procedure to create your own custom Linux Operating System

    Initial setup
    All the files are not allowed to extract in your local filesystem so here we are creating a separate file system , initially we create a empty binary file using dd command , later on we willl make it a file system so we can handle all types of files
    dd if=/dev/zero of=output.fliename bs=1024 count=5000000
    This is the command to create a empty binary file.
    Explination of above command
    Here ‘ if ‘ is the input file that is /dev/zero/ this is the input file which writes zero to the output file. ‘ of ‘ is the path and name the output file , ‘ bs’ is the block size that is the size of each block which is 1024 bytes then the count means the total size of the empty binart file here we have given 500MB ( 500000).
    Once we set up the empty binary file the next step is to make it a file system this is achieved using another command
    mke2fs -F -m 0 -b 1024 sactest 500000
    mke2fs is a linux command to turn a binary block into a filesystem,
    Here -F stands forthis forces the system to turn the specified binary file into file system even if it is not one,
    -m removes the reserved 5% space for the superuser
    -b is the block size
    The next is the path and name of the empty binary file we had just created using dd command , next is the file size.
    The last step in this procedure of initial set up is to mount your newly created file system so you can perform all the operations here
    sudo mount sactest sac -t ext4
    The above command will mount your filesystem inside a mount point sac with ext4 format
    This completes your initial step to set up the environment.

    Creating your initrd image
    Here we basically need to create a initrd image.
    Initially we create a directory of your choice of name i have created a directory named  ‘ my_new ‘

    Then we need to create certain no of files and folders inside this as per the linux standards.
    Step 1 : Create a folder named  ” bin ” ,this is where all the execttable binaries has to be placed , you can compile your own c or c++ cprogram and paste it here and directly run it from terminal just by typing its filename.
    for the sake of easy usage we need to copy following necessary files like busybox for the standard linux functioning.
    If you dont have busybox binary download it ( Download busybox )
    NOTE : This is a 64 bit version the kernel to be choose should also be 64 bit one which will be used in later sessions
    ( learn more on busybox )
    use the command ” busybox ” in your current running system will give you list of the functions busybox provides , now you need to create the link manually for each of the command inside the ” bin ” folder where you have the busy box binary
    Syntax : ln -s busybox command_name
     Example : ln -s busybox cd cd
    the above example creates a link to busybox for cd command and redirects the direct entry of ” cd ” command to ” busybox cd ”
    This is quite time consuming , complete doing this.
    Step 2 : Create a folder named ” etc ” inside which you need to create your fstab file ( a text file ) Paste following command and save it ( The explination is very huje so better let us skip for the time being )
    # /etc/fstab
    proc /proc proc defaults 0 0
    sysfs /sys sysfs defaults 0 0
    devpts /dev/pts devpts defaults 0 0
    tmpfs /dev/shm tmpfs defaults 0 0this will create several mount points.
    Step 3 :Create a folder ” lib ” inside which all the dependent liberaries has to be placed ( i personally recomend to copy and paste this from the tiny core linux ).
    Step 4 : Create an empty folder proc this stores all the runtime files ( in linux all the process are considered as files ).
    Step 5 : You need to create a link named ” sbin ” which points towards the ” bin ” folder .
    ln -s bin sbin
    Step 6 : Create a ” sys ” folder also and leave it empty.
    Step 7 : Create a floder named ” dev ” and you need to copy and paste the following into it , such as “tty1 , tty , tty2 , tty3 , console , null ” . These files are available in your current running linux filesystem inside ” dev ” , you cannot just copy and paste these files as they need root permision and copy them using ” cp ” command.
    Syntax : sudo cp source_address Destination_address
    Example : sudo cp /dev/tty /Desktop/sac/dev/
    Step 8: Final step is to create a ” init ” file and paste the following .
    #!/bin/ash
    mount -t proc /proc /proc
    mount -t sysfs none /sys
    /bin/ash
    Explination : the above code sinppet is a init script which performs the following actions
    1 : mounts all the processess in proc directory
    2 : mounts file system
    3 : Shifts control to ash ( link BASH )
    You are almost done with this.
    You need to change the chmod of the init script to execuitable using the following command
    chmod +x (pathname)/init
    replace the (pathname) with the apth to init script
    Now just you need to do is to make the compressed image files of all these
    Change the current directory inside the folder in which you have created all these sub directories and execute the following command
    find . | cpio –create –format=’newc’ > ../sac
    here we use find command and the output is piped to cpio utility which creates a cpio image if all the output of find command and then is saved in “sac” file name in the succeeding directory( Note : ../sac)
    Once you do this next is to make the zip image of this so use gzip tool to make its zip
    gzip -9 ./sac
    this will make the zip achieve in compression level 9 and name it as ” sac ”
    You are done with second step too, This completes the init creation, Further things are very simple and is nothing hard at all.
     Creating your bootable OS Pendrive
    This is actually an exploit of the softwares, We use Unitbootin a windows application , using which we will create a bootable image of some other linux version in the pendrive so there eill be a bootable pendrive  with us,We use WINE tool for doing this using which we can run lite windows apps on ubuntu or other Linux flavors.
    Copy and paste the init achive you have created into the pendrive
    Copy and paste the vmlinuz which is already been downloaded in the beginning ( Download now )
    The only thing left is to insert new mount point , This can be done by adding a new label in the ” /boot/syslinyx.cfg. ” or the ” isolinux.cfg ” , Based on bootloader installed this may change, You are done, For this part please check our video which will be uploaded very soon.

    This completes the session on how to build / create your own operating system using Linux kernel

    External Reference : Click here

    0 comments:

    Post a Comment