interview question and answer

November 23, 2012

how to create partitions using fdisk and different filesystem like ext2,ext3 and ext4 in Linux


Disk Partitioning & Managing Partitions
When we format a computer hard drive, we will lose everything that is on the drive. Therefore, it is very important to back up anything you might later want.To format a secondary drive, we  need root access. Linux allows only 4 primary partitions.

on an IDE drive, the first drive is called hda, and the partitions are shown as hda1, hda2 . . . . etc. etc. Your second drive is called hdb.On an IDE drive you can have up to 63 partitions, 3 primary and 60 logical ( contained in one extended partition )
On a SCSI drive, the first drive is called sda, the partitions are sda1, sda2 . . The second drive is called sdb.On an IDE drive you can have up to 63 partitions, 3 primary and 60 logical ( contained in one extended partition )
An extended partition is the only kind of partition that can have multiple partitions inside. Think of it like a box that contains other boxes, the logical partitions. The extended partition can't store anything, it's just a holder for logical partitions.

The extended partitions is a way to get around the fact you can only have four primary partitions on a drive. You can put lots of logical partitions inside it.

We can see all the drives attached to your system by typing the command "ls /dev/hd*" or "ls /dev/sd*", depending on which type (IDE, SATA and so forth) the drives are. On the example system, the result of this command looks like "/dev/hda /dev/hda1 /dev/hda2 /dev/hdb /dev/hdb1". The operating system is installed on hda, which has two partitions (hda1 and hda2), and there is one partition on hdb and hdb1.

Steps for Creating partition using fdisk command : -
Step:1  To list available drives on you machine type:
# fdisk -l
Choose the drive you want to make changes to and engage it using fdisk:
# fdisk /dev/sdc
replace the "sdc" with the drive you want to edit.
The basic fdisk commands you need are:
o                              m - print help
o                              p - print the partition table
o                              n - create a new partition
o                              d - delete a partition
o                              q - quit without saving changes
- write the new partition table and exit
Step:2  Enter "p" to see the partition table of the drive. The first line of output from the "p" command will also tell you the size of the drive. This is a good way to double-check that you are working with the correct drive.
Step:3 Type "n" and hit "Enter." Then press "p" to create a primary partition. It asks you for a partition number; enter "1." Now you are asked which cylinder the partition should start at. The beginning of the drive is the default, so just hit "Enter." Then, you are asked for the last cylinder. The end of the drive is the default, so you can just press "Enter" again.
Step:4 Now you are back at fdisk's command prompt. Use the "p" command to check the partition table. You should now see your new partition at the bottom of the output.


Step:5   Now  we need to set the filesystem type for your new partition with the "t" command. We are asked for the Hex code of the filesystem you wish to use. We will use the standard Linux ext2 filesystem, which is "83." If you are doing something special and know of a particular filesystem that you need to use, you can press "L" to see all the codes, which are one or two characters made up of the numbers 0 to 9 and the letters a to f.
Step:6  Now just issue the "w" command to write your new partition table and exit fdisk

Step:7  # partprobe      (command used to force the kernel to re-read the new partition table)
To Delete Partitions using fdisk command
> 
Let us as suppose  that we want to remove a partition from /dev/hdb disk. Type the following command:

Step:1 # fdisk /dev/hdb
Now type p command to list partition:
Command (m for help): p

Step:2 Now let us say you want to delete /dev/hdb3 (3rd partition). Type the d command to delete a partition:

Command (m for help):
 d
Partition number (1-4):
 3
Step:3 It will prompt you for the partition number. Type 3:
Verify that partition deleted:
Command (m for help): p
Now save the changes and exit to shell prompt. Type the w command:
Command (m for help): w
Reboot the system OR run  partprobe  command
Formatting the Partitions
To use the partition we need to format the partitions using the different filesystem. We can format the partitions using either mkfs or mke2fs command.
# mkfs.ext2 /dev/sdb1            (ext2 filesystem)
# mkfs.ext3 /dev/sdb1            (ext3 filesystem)
# mkfs.ext4  /dev/sdb1            (ext4 filesystem)
# mkfs.vfat /dev/sdb1              (DOS filesystem)
Mount the new disk using mount command
First create a mount point /data and use mount command to mount /dev/sdb1, enter:

# mkdir /data
# mount /dev/sdb1 /data
# df -H
Update /etc/fstab file
Open /etc/fstab file, enter:

# vi /etc/fstab
Append as follows:
/dev/sdb1         /data         ext3         defaults         1         2

Save and close the file.
Brief Description about the File system:-

Ext2
  1.           Ext2 stands for second extended file system.
  2.             It was introduced in 1993. Developed by Rémy Card.
  3.            This was developed to overcome the limitation of the original ext file system.
  4.             Ext2 does not have journaling feature.
  5.            On flash drives, usb drives, ext2 is recommended, as it doesn’t need to do the over head of journaling.

                Maximum individual file size can be from 16 GB to 2 TB
Overall ext2 file system size can be from 2 TB to 32 TB

Ext3
  1.                               Ext3 stands for third extended file system.
  2.                               It was introduced in 2001. Developed by Stephen Tweedie.
  3.                             Starting from Linux Kernel 2.4.15 ext3 was available.
  4.                             The main benefit of ext3 is that it allows journaling.
  5.                            Journaling has a dedicated area in the file system, where all the changes are tracked.      When the system crashes, the possibility of file system corruption is less because of journaling.
  6.                           Maximum individual file size can be from 16 GB to 2 TB
  7.                            Overall ext3 file system size can be from 2 TB to 32 TB

                                         There are three types of journaling available in ext3 file system.
                                                 Journal – Metadata and content are saved in the journal.
                                             Ordered – Only metadata is saved in the journal. Metadata are journaled only after writing                 
                                        the content to disk. This is the default.
                                            Writeback – Only metadata is saved in the journal. Metadata might be journaled either 
                                           before or after the content is written to the disk.
                        You can convert a ext2 file system to ext3 file system directly (without backup/restore).

Ext4
  1.                               Ext4 stands for fourth extended file system.
  2.                             It was introduced in 2008.
  3.                            Starting from Linux Kernel 2.6.19 ext4 was available.
  4.                          Supports huge individual file size and overall file system size.
  5.                           Maximum individual file size can be from 16 GB to 16 TB                         Overall maximum ext4 file    system size is 1 EB (exabyte). 1 EB = 1024 PB (petabyte). 1 PB = 1024 TB (terabyte).
  6.                            Directory can contain a maximum of 64,000 subdirectories (as opposed to 32,000 in ext3)
  7.                          You can also mount an existing ext3 fs as ext4 fs (without having to upgrade it).
  8.                            Several other new features are introduced in ext4: multiblock allocation, delayed allocation, journal checksum. fast fsck, etc. All you need to know is that these new features have improved the performance and reliability of the filesystem when compared to ext3.

In ext4, you also have the option of turning the journaling feature “off”.

No comments: