Tag Archive : linux

/ linux

Linux Kernel Packaging

December 9, 2015 | Article | No Comments

If you wish to install linux kernel without doing compilation stage, you can always use prebuilt binary package of kernel image. Just take the package to the target machine and install it. The package you use is built by someone else. So if you know how to build Linux kernel from source, how can you make a package from it?

For creating the build package, while building kernel, instead of giving ‘make’ command alone do one of following command. These commands will creates bzImage and modules and then pack them into a package file. The available commands are:

make rpm-pkg
make binrpm-pkg
make deb-pkg
make tar-pkg
make rargz-pkg
make tarbz2-pkg

Creating Hidden Partition – Linux Edition

December 9, 2015 | Article | No Comments

You have some confidential stuffs that no one should read / watch? You need to keep your data save?

There are many things you can do to protect your data. One could mention encryption, to encrypt your data to unreadable data so just you who know the key can access it. Another trick is setting the permission of file or folder, or maybe get the confusing hierarchy of data. Oh, and not to mention if you are using Windows you can use Windows feature to hide your data.  Well, that’s nice but let me tell you cool trick: hidden partition.

This article will discuss about Hidden Partition, and the trick from Linux perspective.

What is Hidden Partition?

Like implied by the name, hidden partition is a partition from disk or other storage media that hidden or invisible from system.

So, what’s so cool about hidden partition?

You can still use tricks like things we have mentioned above, but above all the partition we are playing with are invisible to the system. Imagine, how could (common) people do to your privacy if they can’t find the partition where you hide your stuffs. It’s like implementing hidden files/folders but in higher level: partition.

When people don’t aware there are some hidden partition, how can they browse or manipulate through it? Yes, that’s the point!

Theory Behind

Partition is just an area in hard disk which is defined by start on certain sector and end on certain sector. Data on the disk are written to a small allocation unit called block. Each block has fix size, for example 4kB. A file system is a way to store, read, write, and delete data effectively. There are many filesystems, but we won’t cover it in detail. Some popular file systems are: FAT, NTFS, EXT series (2/3/4), HFS, ZFS, XFS, ReiserFS, etc.

Next, the big question is: how we know or aware of a partition?

For every storage media, there are a certain area in the front of disk. This area is called MBR or Master Boot Records. It is like a table which records what partition exists on the disk. The important parts is they records the beginning and ending of a partition.

Hidden partition is as simple as removing the entry of a partition from the MBR.

Wait! If we delete the entry, doesn’t that means we are removing a partition? Yes! However, the only missing is the entry, while the data are still exists on the partition area. In fact, unless you do a format, the file system is still accessible when you know where it is.

A normal people, even a system, won’t aware if there is a hidden partition unless you told there is. Your computer won’t notify you partition which doesn’t have record on MBR. And this is why it’s called hidden partition (or at least I called it as that)

Experiment

Now, we will experiment on hidden partition. As suggested by the title, we will use Linux. In this article I use:

  1. Slackware64 14.0
  2. SD Card of 4GB as storage media

The Slackware is only proof of concept. You can use any linux available, no need to install Slackware just for this article.

The Slackware is running on Lenovo Ideapad z480.

The SD Card serves purpose as storage media which we will play with. It has 4GB of storage in total and divided into three partition.Using fdisk

fdisk -l

got the list of storage media attached to my machine. In this case, the SD card is detected as /dev/sdb and the following is the partition list as the result of fdisk.

Disk /dev/sdb: 4008 MB, 4008706048 bytes
124 heads, 62 sectors/track, 1018 cylinders, total 7829504 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048      206847      102400    b  W95 FAT32
/dev/sdb2          206848     7500000     3646576+  83  Linux
/dev/sdb3         7501824     7829503      163840    7  HPFS/NTFS/exFAT

OK, we have plenty information there. Let’s focus on the partition informations. The three partitions has differents file system. Suppose we have format each of them to their respective filesystem.

Now in this article we will make the second partition (/dev/sdb2) hidden.

To remove a partition, it is as simple as remove the entry from the MBR. We can use tool such as fdisk to accomplish this.

fdisk /dev/sdb

And you will get to SD card. Type ‘d’ and enter number 2 (our partition /dev/sdb2 is on number 2). Once you done, write the progress to the disk.

Command (m for help): d
Partition number (1-4): 2
Partition 2 is deleted

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

You can verify by invoking this command again:

fdisk -l

It should be like this:

Disk /dev/sdb: 4008 MB, 4008706048 bytes
124 heads, 62 sectors/track, 1018 cylinders, total 7829504 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048      206847      102400    b  W95 FAT32
/dev/sdb3         7501824     7829503      163840    7  HPFS/NTFS/exFAT

Now, see the gap? The first partition started from block 2048 and end on 206847. The third partition is from 7501824 to 7829503. Where’s the partition from 206848 to 7500000 like we see before? It’s hiding right now.

Now, test to mount it.

mount /dev/sdb2 /mnt/

And we get error! Well, not like this is unexpected. But how can we access the partition if we can’t mount it?

Hey! Who said we can’t mount it? We can but there is a trick to do that.

Actually, when we said mount a partition, mount will check whether a special device (/dev/sdb2 in this case) exists. If it is, mount will read the table to find where is the area of that partition. We got the area by start and end, right? And that’s how we will mount it. We will provide the start and the end, manually.

But, the start and end are not in term of sector or block. The partition are known as n bytes of offset from the start. Recall the location of partition 2, it is started from block 206848 to 7500000. Now see how bigh is each block. From the result of fdisk we know that each block is 512 bytes in size. Now, just multiply the block with 512 to get the size. Actually, we only need the start so calculate the value. If you are too lazy, you can skip it 😛

Now, to mount a hidden partition we need to invoke mount in special manner. We can’t access non existing /dev/sdb2, but we can still access /dev/sdb. So here is how I access the hidden partition

mount -o loop,offset=$((206848*512)) /dev/sdb /mnt/

ps: I’m lazy.

And, you now get the partition mounted on /mnt/

To unmount it, do following:

umount /mnt/

Isn’t that easy? 😀

This article is similar to this article, except it use fdisk to partitioning.

When write image to CD card for installation of Raspberry Pi, the usable size will only be the size of the image. That means the rest of the space will be waste. On some distribution such as Fedora Remix or Raspbian Wheezy, when final configuration begin the distro run some script to automatically resize and fill the SD card. However not all distro does that. Therefore we need to do manual resize.

In this article we will discuss about how to manually resize SD card on Slackware. Alhtough using Slackware, you can also use other Linux, but we won’t cover that. This article describes activities relating partitions. Incorrectly following instruction is likely to corrupt your system, so please be careful.

On this article I use:

  1. Slackware64 14.0
  2. SD card with Arch Linux ARM  (2GB of 8GB occupied)

Preparation

Insert the SD card to our machine. Make sure it is now mounted. We will use partition tool to resize the partition.

Following on from the instructions above, keep the newly-written SD card in the card reader, but unmounted. We’ll use the fdisk tool to edit partition record on MBR and other tools to resize the partitions.

Show partition information to find our SD card. Look for a partition that matches the roughly the size of your distribution image. On Arch Linux it should be around 2GB. For example, it is detected as /dev/sdb5. Then unmount that partition. Those can be done by invoking:

mount
umount /dev/sdb5

Note that in this example case, root partition of Arch Linux is on extended partition.

Resizing

Now use fdisk with root privileges. You can do sudo if you are on sudoer group or use super user account.

Welcome to fdisk (util-linux 2.21.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help):

You are asked for command, let’s print what partition there by type ‘p’ character and enter it. Here is my result:

Disk /dev/sdb: 7952 MB, 7952400384 bytes
64 heads, 32 sectors/track, 7584 cylinders, total 15532032 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00057540

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048      186367       92160    c  W95 FAT32 (LBA)
/dev/sdb2          186368     3667967     1740800    5  Extended
/dev/sdb5          188416     3667967     1739776   83  Linux

Command (m for help):

This shows how my SD card was formatted after writing the image.I have 7584 cylinders with total 155352032 sectores.

Notice that nothing use the area from the end of partition /dev/sdb5 (3667967) to the card’s maximum (15532032).

Partition 1 is the boot partition. Nothing to do here, let’s leave that alone. Partition 5 is the root partition which located on the extended partition. This is the partition we will fill in. Some OS versions will have a Partition for swap space, which needs to be moved to the end of the card.

Now grow the root partition. This involves removing the partition, re-creating it, then using resize2fs to grow the filesystem to fill the partition. It won’t destroy any data.

We know that partition 5 (/dev/sdb5) is logical partition and located on Extended partition area. It means we have to remove both partition before creating new one.

First press d to delete the partition 5.

Command (m for help): d
Partition number (1-5): 5
Partition 5 is deleted

Next, press d again to delete the partition 2

Command (m for help): d
Partition number (1-5): 2
Partition 2 is deleted

Next we create them. But now we do it in reverse order.

First create partition 2. This will be extended partition. Because our partition is the last partition, we can omit the value and use all the remain area. In my case, I will start from sector 186368 and end it to the 15532031.

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): e
Partition number (1-4, default 2): 
Using default value 2
First sector (186368-15532031, default 186368): 
Using default value 186368
Last sector, +sectors or +size{K,M,G} (186368-15532031, default 15532031): 
Using default value 15532031
Partition 2 of type Extended and of size 7.3 GiB is set

Now we have extended partition.

Next, create the partition 5. This partition should be built on top of partition 2.

Command (m for help): n
Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): l
Adding logical partition 5
First sector (188416-15532031, default 188416): 
Using default value 188416
Last sector, +sectors or +size{K,M,G} (188416-15532031, default 15532031): 
Using default value 15532031
Partition 5 of type Linux and of size 7.3 GiB is set

Now write the changes back to the disk by entering ‘w’.

Next clean and resize the root partition. We will do a filesystem check:

e2fsck -f /dev/sdb5

That command will allow it to add lost-and-found.

resize2fs /dev/sdb5

And that’s it, we can enjoy Raspberry again 😀

Arch Linux Pacage Manager Command List

December 9, 2015 | Article | No Comments

Here is the list of useful commands for pacman. Pacman is a package manager for Arch Linux.

Whether you use x86, amd64, or ARM architecture, the command should remain same.

Update Package List

The repository is consists of many package. To update the package list, we do:

pacman -Syy

Upgrade Packages

This will update all packages on the system.

pacman -Suy

Perform Full Upgrade

This is necessary if some libraries are missing to recompile system properly.

pacman -Syyu

Install New Package

Install specific package.

pacman -S $PACKAGE

where $PACKAGE is the package name you want to install. For example: xorg-server

Install package groups

pacman -Sg $PACKAGEGROUP

where $PACKAGE is the package group. For example: gnome.

Remove Package

Remove package, normally.

pacman -R $PACKAGE

where $PACKAGE is the package name you want to remove. This will remove a single package and leaving all of its dependencies installed.

If you want to remove a package and its dependencies which are note required by any other package, do following:

pacman -Rs $PACKAGE

If you want to remove a package, its dependencies, and all of the dependent (packages depend on it)

pacman -Rsc $PACKAGE

Search for Package

Will search the package database on given keyword

pacman -Ss $KEYWORD

where $KEYWORD is the keyword, can be parts of package name, description, etc.

List of Package Installed

List of packages

pacman -Q

List the package installed with given keyword

pacman -Q | grep $KEYWORD

List the packages installed as dependencies but not used anymore.

pacman -Qdt

List the packages installed, and not needed by other packages (not bein depended on)

pacman -Qet

List of files installed with package (content of package)

pacman -Ql $PACKAGE

Remote Access Raspberry Pi Using Only X Client

December 9, 2015 | Article | No Comments

There are some ways for accessing Raspberry Pi: access Pi directly or indirectly. You might at least try one of thus method for connecting your Pi to your PC.

The direct access involving access without any medium. You connect screen (LCD screen, for example), keyboards, mice, etc to your Pi. This way, your Raspberry Pi act as normal computer. You operate it like you operate your PC. This method has a drawback, whenever you want to use Pi you must provide appropriate input and output device (keyboard, mouse, screen).

Another way to access Pi is using indirect access. This method involving other medium to access Pi, technically a network. In this term, your Pi is connect to a network and you can access the Pi over the network. SSH and VNC are the good example of this. This method demand us to provide good network connection if you want a good connection.

Accessing Pi over network is good, you don’t need other peripheral / device such as keyboard, mouse, or LCD screen. If you want to do thing graphically you can use VNC (Virtual Network Computing). However, using VNC is somehow slow because the application and desktop environment are rendered both on your Pi and your desktop. VNC also require better network as it sends picture / screen image over time.

There is a better solution for this problem. Instead of rendering the graphic on Pi, why don’t we render it on our local computer? This way, Pi only send us minimal packet. Using this method, we can also use keyboard and mouse on our local computer, just like indirect access do.

In this article we will discuss about other way to remote accessing your desktop without VNC. We will use X.org which is used for Linux & Unix OS for GUI. For this article I use:

  1. Slackware64 14.0 on PC
  2. Raspbian Wheezy on Raspberry Pi

This article is written for Linux. If you use Windows, go here.

X Window System – Server & Client Architecture

X Window is originated at the Massachussetts Institute of Technology (MIT) In 1984. X11 is the system-level software infrastructure for the windowing GUI on Linux, *BSD, and other UNIX-like Operating System. It is designed to handle both local display, as well as across the network displays. X Window is a computer software system and network protocol that provides a basis for graphical user interfaces (GUIs) and rich input device capability for networked computers. It creates a hardware abstraction layer where software is written to use a generalized set of commands, allowing for device independence and reuse of programs on any computer that implements X.

X was designed from the beginning to be network-centric. It adopts a “client-server” model.

In the X model, the “X server” runs on the computer that has the keyboard, monitor, and mouse attached. The server’s responsibility includes tasks such as managing the display, handling input from the keyboard and mouse, and other input or output devices (i.e., a “tablet” can be used as an input device, and a video projector may be an alternative output device). Each X application (such as XTerm or Firefox) is a “client”. A client sends messages to the server such as “Please draw a window at these coordinates”, and the server sends back messages such as “The user just clicked on the OK button”.

In a home or small office environment, the X server and the X clients commonly run on the same computer. However, it is perfectly possible to run the X server on a less powerful desktop computer, and run X applications (the clients) on, say, the powerful and expensive machine that serves the office. In this scenario the communication between the X client and server takes place over the network.

This confuses some people, because the X terminology is exactly backward to what they expect. They expect the “X server” to be the big powerful machine down the hall, and the “X client” to be the machine on their desk.

It is important to remember that the X server is the machine with the monitor and keyboard, and the X clients are the programs that display the windows.

Preparations

As stated before, the X window is using client/server model. Our communication would be on top of secure connection. Specifically, SSH (Secure SHell) connection are chosen to transport data (as a tunneling). Therefore, we should configure SSH server properly.

Now on your Raspberry Pi, configure SSH server. Obviously, you MUST install your SSH server program. Edit following file /etc/ssh/ssh_config, make sure these lines are not commented.

ForwardAgent yes
ForwardX11 yes
ForwardX11Trusted yes

Now open /etc/ssh/sshd_config and edit the file so it has following line:

X11Forwarding yes

Restart the SSH Server.

Remote Display

Running X on your local machine, you should have more than 1 tty (teletype). Go to tty 1 (or any from 2 to 6) and you would see a black screen with login message there. Login with your account.

Next we need to make an X server. This server is another X server running on our local machine. This X server will manage our local screen output. We use this server to display anything from our X client, which is raspberry pi.

export DISPLAY=:1

Now, the easiest way is to do is running X on display :1.

X :1 &; xterm

You should be brought to new tty with a blank screen and a “window” there.

Now connect to Raspberry Pi using SSH.

ssh -X [email protected]_ip_address

user is the username we will use and the raspberry_ip_address is the Pi’s IP address. Make sure you can access and you can login to Pi.

Once you are logged in, execute this to start the X.

lxsession &
lxpanel &

Now, your tty should be drawn to your Raspbian’s LXDE.

When you want to back to your original desktop, press CTRL+ALT+F7. When you want to back to Pi’s desktop, press CTRL+ALT+F8.

Drawbacks

This method require more resource on local computer as your local computer run 2 X Server at same time.

You should also note that you can only do swap display to change between these two display. Using VNC, you can display the content of Pi display as a window.

Linux Boot Process – Init

December 9, 2015 | Article | No Comments

In Unix-based OS, init (short for initialization) is the first process started during booting of the computer system. Init is a daemon process that continues running until the system is shutdown. It performs many things and also served as the ‘mother’ of processes.

Once init starts, “swapper” part on kernel goes into the sleep state, into an idle loop and rest of the bootup process is taken up by init process. The old init systems such as SysV-style and BSD-style is based on a single process which load the application serially using script. A newer system such as system and upstart can init system in parallel to speed up the process.

SysV-style

System V init is one of init system used by major Linux distribution. SysV init examines the /etc/inittab file for an :initdefault: entry, which defines any default runlevel. If there is no default runlevel, then init dumps the user to a system console for manual entry of a runlevel.

Runlevels

States in which system is running at. It describe certain states of a machine, characterized by the processes run. There are generally eight runlevels, three of which are standard:

  • Runlevel 0 : Halt
  • Runlevel 1 : Single user mode (aka. S or s)
  • Runlevel 6 : Reboot

Aside from these, every Unix and Unix-like system treats runlevels a little differently. The commond denominator, the /etc/inittab file, defines what each runlevel does in a given system. For example, there are runlevels such as:

  • multiuser mode
  • multiuser mode with networking support
  • GUI mode

BSD-Style

BSD init runs the initialization shell script located in /etc/rc, then launches getty on text-based terminals or a windowing system such as X on graphical terminals under the control of /etc/ttys. There are no runlevels; the /etc/rc file determines what programs are run by init. The advantage of this system is that it is simple and easy to edit manually. However, new software added to the system may require changes to existing files that risk producing an unbootable system. To mitigate against this, BSD variants have long supported a site-specific /etc/rc.local file that is run in a sub-shell near the end of the boot sequence.

Unlike System V’s script ordering, which is derived from the filename of each script, this system uses explicit dependency tags placed within each script. The order in which scripts are executed is determined by the rcorder script based on the requirements stated in these tags.

Slackware use this.

systemd

System management daemon designed exclusively for the Linux kernel. Like init, systemd is a daemon that manages other daemons.

Upstart

Event-based replacement for traditional init daemon.

Other

These are alternatives used to replace Sys-V and BSD-style init:

  1. BootScripts
  2. busybox-init
  3. DEMONS
  4. eINIT
  5. Initng
  6. launchd
  7. Mudur
  8. runit
  9. s6
  10. Service Management Facility
  11. SystemStarter

Linux Boot Process – Kernel

December 9, 2015 | Article | No Comments

In order for a computer to successfully boot into device we know, a computer should pass some process which we refer as boot process. The BIOS, operating system, and harware components must all be working properly. Failure of any of these three elemens will likely result in a failed boot sequence.

After a boot loader does it jobs (load the kernel) it will terminate itself and the control will be passed to the kernel.

When a Linux kernel is loaded and ran, it is actually running two process for completely load kernel to memory. First one is bootstrapping, and second one is the process in the kernel itself.

In earlier phase kernel will use a bootstrap which is assembly code added to the image. When bzImge is loaded , the code of bootstrap starts executing. The tasks of bootstrap are,

  1. Forcing cpu to go into protected mode. In protected mode we have 4 GB of virtual address space or virtual addressing enabled.
  2. Enabling the MMU (Memory Management Unit) . MMU has got address translation ability , mapping between virtual addresses and physical addresses. CPU contains MMU instruction set.
  3. Decompressing the linux kernel. You can see “Decompressing Linux….” Messages on console at this point.
  4. Calling “start_kernel” function after that. start_kernel is similar to main function. kernel image starts running and initializes all the devices , schedulers, console etc. . You can see lot of text flying on the screen related to this. kernel boots up from here.

When bootstrapping done, the kernel take the process. Kernel does:

  1. Kernel initialize CPU and performs the “Bogo MIPS” calculation. Though BIOS has already intialized CPU but kernel does not depend upon BIOS initialization. It will re-initialize the CPU again. It will create one structure and stores all the CPU details in it. After that it performs “Bogo MIPS”calculation. Using “Bogo MIPS” calculation kernel estimates the cpu speed. At bootup time, you can see the message ” Bogo MIPS is …..” .
  2. Kernel re-initializes parallel ports, serial ports and all the on-board controllers like USB, IDE, SCSI, Serial, parallel, timers, RTC etc. These startup drivers are also called BSP drivers or Board Support Drivers.
  3. Interrupts will be enabled. All the data structures required to hold the interrupts including interrupt descriptor table will be setup , i.e. which device is using which interrupt, IRQ details will be filled.

  4. File system required to read from this disk, /boot , /root , /etc , /home etc. is initialized. File system must be loaded now so that can now start using disk.

  5. After this loader will be started. Loader starts a process that is registered in the PCB with PID ‘0’ . Name of this process is swapper.

  6. “swapper” will start a thread called “init” thread. “init” is a kernel thread. “init” kernel thread will look into /bin and look for a program whose name is “init”. This kernel thread is told for where to look for init process via boot parameter in bootloader configuration file. If kernel thread doesn’t find init there , it becomes a reason for kernel panic and the booting stops there. This may also happen if file system is not initialized . “init” thread looks into /bin and starts executing init process. init’s parent is “init” thread. /bin/init is registered as PID 1 and this is the first user space process.

Kernel has loaded, but the booting process has not stopped yet.

Linux Boot Process – Boot Loader

December 9, 2015 | Article | 2 Comments

In order for a computer to successfully boot into device we know, a computer should pass some process which we refer as boot process. The BIOS, operating system, and harware components must all be working properly. Failure of any of these three elemens will likely result in a failed boot sequence.

The first sector of a boot device is called as sector ’0′ or the Master Boot Record (MBR) . BIOS can read only sector ‘0’, it can’t read any other sector. They are programmed to read the first 512 bytes of disk or the Master Boot Record section. It has three components: primary boot loader info in first 446 bytes, partition table in next 64 bytes, MBR validation check in last 2 bytes.

To load Operating System, a special program called boot loader written here. Boot loader even helps us to boot more than one operating systems on the machine (called chaining).

The infamous boot loaders for Linux are GRUB and LILO. GRUB (Grand Unified Boot loader) is used by default on majority of Linux distributions. GRUB is capable of booting DOS, Windows, Linux, and BSD operating system. The LILO (Linux Loader) is an old boot loader, used by several distribution.

When BIOS finished initialized the system, it will jump to Master Boot Record and loads the boot loader from there.

Boot loader does:

  1. Bring the kernel (and all the kernel needs to bootstrap) into memory
  2. Provide kernel with the information it needs to work correctly
  3. Switch to an environment that the kernel will like
  4. Transfer control to the kernel.

Boot loaders can be divided into some categorizes by the design: single stage boot loader, two stage boot loader, and mixed stage boot loader.

A single stage boot loader is a boot loader which consists of a single file that is loaded entirely by the BIOS. This image performs the steps described above to start the kernel. However, on x86 the boot loader is limited to the 512 bytes.

We can take an entire dump/backup of MBR in Linux, using dd utility:

dd if=/dev/sda of=mbr bs=512 count=1

When the above command executed, it will dump the contents of our first sector which of 512 byte to a file named mbr. The bs stands for block size. The count means to dump only the first sector. So the overall command will dump only first sector (512 bytes) of disk /dev/sda.

As discussed before, the MBR has a structure in which it has partition table. We can see the partition table information from our MBR backup file as show below:

file mbr

A two stage boot loader actually consists of two boot loaders after each other. The first being small which then load the second one. The second one can then contain all the code needed for loading kernel.

Mixed boot loader is a boot loader similar to two stage boot loader. it splits the bootloader in two parts, where the first half (512 bytes) can load the rest. This can be achieved by inserting a ‘512-bytes’ break in the ASM code, making sure the rest of the loader is put after the bootsector.

In Linux, after boot loader is loaded into memory it looks into /boot directory and loads the kernel image. Note here, that it is boot loader that expects all the files and kernel image needed for booting under /boot directory. This is the reason why kernel image and all boot related files are kept under /boot directory.

For GRUB, it has three stages of bootloading:

  1. GRUB stage 1
  2. GRUB stage 1.5
  3. GRUB stage 2

The hard disk sectors are counted from 0 to the last sector. The first sector contains the GRUB stage 1. Normally partitions will not start before sector 63 hence we have sectors from 1 to 63 free. This space is used for storing GRUB stage 1.5. The free space between MBR and the beginning of the partition is called as MBR GAP. This space contains the drivers for reading file system. So the GRUB stage 1 will load grub stage 1.5 to the RAM and will pas the control to it.

Now grub stage 1.5 will load the file system drivers and once the file system drivers are loaded, it can now access /boot/grub/grub.conf file which contains other details about kernel path and initrd path etc.

Once boot loader loads kernel image to memory , the control is passed to kernel image .

Linux Boot Process – BIOS

December 9, 2015 | Article | 1 Comment

In order for a computer to successfully boot into device we know, a computer should pass some process which we refer as boot process. The BIOS, operating system, and harware components must all be working properly. Failure of any of these three elemens will likely result in a failed boot sequence.

BIOS is a abbreviation of Basic Input Output. It is a firmware, software written into special chip on motherboard.

Whenever a machine is powered on , the 1st program to run is BIOS. Primary jobs of BIOS are:

  • POST : Performs the Power On Self Test, or POST , to test the peripherals if they are functioning properly at hardware level.
  • CPU Initialization : BIOS checks the clock speed of CPU and will put the CPU on realmode. At the time of power on , cpu will be in real mode. In real mode cpu works on direct physical addresses only.
  • Memory Probing : BIOS checks for machine’s available primary memory(RAM).
  • Basic Devices Initialization : BIOS initializes only those devices which are required to carry out the boot process. For example boot process requires basic I/O devices . So BIOS initializes standard input device and standard output device. Apart from basic I/O it also initializessome controllers like PCI controller, IDE controller. IDE controller are needed because without IDE controllers floppy disks / hard disks cant be read or written.
  • Creating Memory Map – BIOS creates real mode memory map or real mode address space. This memory map is always architecture specific. It expects the kernel image to be loaded directly at those addresses. That is why we build the kernel image ( bzImage ) according to the machine’s architecture. The bzImage carries real mode , architecture specific addresses so that it can find those direct addresses and can be loaded there.
  • Loading MBR – The last job of BIOS is to find the boot device and to jump to its sector ‘0’ ( boot sector ). BIOS has got access to only sector ’0′ of a boot device. Whatever BIOS finds in sector ‘0’ , it loads that in real mode address space.

The BIOS job has finished and the control goes to whatever code written on the disk boot sector.

Linux Kernel Configuration

December 9, 2015 | Article | No Comments

Having Linux kernel source code allow us to open possibility to customize / configure the kernel as you wish. Main kernel image is built as a static image, and some part of linux kernel image is also built as loadable modules (device driver). The image is always resident in memory while some services are added to the kernel at runtime in the form of modules. Complete kernel image constitute static kernel image plus run-time loadable modules. Linux kernel configuration allows you to decide , which feature you want to include in linux kernel image , which feature you do not want to include or which service you want to make as a loadable kernel module etc.

This article will try to cover all important ones for configuring Linux kernel.

Text Based Configuration

1. Interactive Configuration

Kernel has provide an interactive text based kernel configuration utility using Makefile. If you use ‘make config’, it will configure itself. In fact, it is useful for quite experienced kernel developers and not for the beginners. The result of this command is shown below:

# make config

scripts/kconfig/conf arch/x86/Kconfig

*
* Linux Kernel Configuration
*
*
* General setup
*
Prompt for development and/or incomplete code/drivers (EXPERIMENTAL) [N/y/?] y

Local version – append to kernel release (LOCALVERSION) [] y

Automatically append version information to the version string (LOCALVERSION_AUTO) [N/y/?] y

Kernel compression mode

> 1. Gzip (KERNEL_GZIP)

  2. Bzip2 (KERNEL_BZIP2)

  3. LZMA (KERNEL_LZMA)

  4. LZO (KERNEL_LZO)

choice[1-4?]: 1

Support for paging of anonymous memory (swap) (SWAP) [N/y/?] y

System V IPC (SYSVIPC) [N/y/?] y

BSD Process Accounting (BSD_PROCESS_ACCT) [N/y/?] y

  BSD Process Accounting version 3 file format (BSD_PROCESS_ACCT_V3) [N/y/?] (NEW) y

*

* RCU Subsystem

*

RCU Implementation

> 1. Tree-based hierarchical RCU (TREE_RCU)

  2. UP-only small-memory-footprint RCU (TINY_RCU)

choice[1-2]: 1

Enable tracing for RCU (RCU_TRACE) [N/y/?] y

Tree-based hierarchical RCU fanout value (RCU_FANOUT) [32]

This command starts an interactive kernel configuration script. Once invoked, it will continuously prompts for user input at every step . User can enter any one of the four options:

  1. Yes – For building the particular feature with kernel’s static image.
  2. No – For not including the feature at all in the kernel image
  3. Module – For build this feature as loadable kernel module.
  4. ? – For help, gives brief description on the feature.

Script prompts for each and every option and expects the input for each of the options. There are so many options available so it is very tedious and time consuming way. Now you can guess the reason why it not a preferred way to configure linux kernel.

2. Predefined Configuration Target

Tell the Makefile to use predefined configuration target. There are some predefined configuration, which are: defconfig, oldconfig, randconfig, allmodconfig, allyesconfig, allnoconfig.

2.1 defconfig

Use default configuration to configure kernel.

make defconfig

It’s easy and you would not expecting much other than default parts.

This utility generates default configuration for linux kernel. This default configuration is based on the configuration options preferred and used by linux kernel maintainers on their own machines. Once the task done, you can view and modify the configuration as you wish.

2.2 oldconfig

Technically speaking, it updates the current kernel configuration by using the current .config file and prompting for any new options that have been added to the kernel.

make oldconfig

If you think your old configuration is fit to your condition, you can always use this. Like defconfig, you can view and modify the configuration once the configuration finished.

There is also a variant to this, silentoldconfig, which prints nothing to screen except a question.

2.3 randconfig

Generates a new kernel configuration with random answers to all of the different option.

make randconfig

2.4 allmodconfig

Generates a new kernel configuration in which modules are enabled whenever possible.

make allmodconfig

2.6 allyesconfig

Generates a new kernel configuration with all options set to yes.

make allyesconfig

2.7 allnoconfig

enerates a new kernel configuration with all options set to no.

make allnoconfig

Console Based Configuration

The very popular way of configuring kernel. Still using command line interface, but it’s at least graphically on terminal (using ncurses).

make menuconfig

It will opens a graphical console in which all kernel configuration options are arranged into categories. These categories are further categorized into subcategories. Just browse through and keep on selecting options until you reach the specific configuration option you need to modify.

This method is easier than the methods we have covered so far.

If you think you have it enough, you can select < Exit > to exit the configuration. You can also exit without doing any modification. A configuration, .config file, will be created / updated.

GUI Based Configuration

1. X11 Based Configuration

Using help of X window system.

make xconfig

Make xconfig is X11-based graphical configuration utility. It arranges all the configuration options into left and right pane. You can easily browse through and select different options easily.

2. GTK Based Configuration

Using GTK+ based system. Make sure you have GTK libraries installed.

make gconfig

Other

Other method to configure kernel. It doesn’t use Makefile like other methods we have discussed, but instead we directly modify the configuration file. Configuration is saved as a file “.config” without quote.

The configuration file is located in the root of the kernel source tree. It consists of some lines, usually in the format VARIABLE=value

The variable are the kernel options where the value can be y, n and other value defined for that options.

Social media & sharing icons powered by UltimatelySocial