how to create a new partition in linux

Satish Tiwary's picture

Creating and deleting new partitions in linux a very normal practice. In this post we will be going through simple steps to create a new partition in linux, format it and mount it at your required mount point.

This article will also take you through the steps to delete your required partition, view your hard drive geometry,display existing partition table,display the UUID of a partition and many more about partition creation ,view,help, partitioning command line help and troubleshooting.

The tools used in this tutorial are very as mentioned below.

  • fdisk
  • mke2fs
  • mount
  • partprobe
  • tune2fs
  • blkid
  • parted
  • mkfs
  • fsck


Step1:(Device identification)

First check the partition table using fdisk command.

Using Fdisk command one can identify his device that whether it is your internal Hard disk or external hard disk.

/dev/sd(a,b,c)----------->SCSI

/dev/hd(a,b,c)----------->IDE



#fdisk -l

[root@myvm1 ~]# fdisk -l

Disk /dev/sda: 26.8 GB, 26843545600 bytes
255 heads, 63 sectors/track, 3263 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1           6       48163+  83  Linux
/dev/sda2               7         515     4088542+  83  Linux
/dev/sda3             516        3133    21029085   83  Linux
/dev/sda4            3134        3263     1044225    5  Extended
/dev/sda5            3134        3263     1044193+  82  Linux swap / Solaris

 

we have seen that already partition up to /dev/sda5 has been created.
So now we have to create a new partion that will start from /dev/sda6

 

Before Partition we need to know about our hard disk in use. We use here some commands to know the detail information of hard disk we are using and we are going to partitioning.

[root@satish ~]# lspci|grep -i ide
00:1f.1 IDE interface: Intel Corporation 82801G (ICH7 Family) IDE Controller (rev 01)
00:1f.2 IDE interface: Intel Corporation 82801GB/GR/GH (ICH7 Family) SATA IDE Controller (rev 01)


 

[root@satish ~]# cat /proc/scsi/scsi
Attached devices:
Host: scsi0 Channel: 00 Id: 00 Lun: 00
  Vendor: ATA      Model: ST3160215AS      Rev: 4.AA    
------>> My hard Disk
  Type:   Direct-Access                    ANSI SCSI revision: 05
Host: scsi4 Channel: 00 Id: 00 Lun: 00
  Vendor: Kingston Model: DataTraveler G2  Rev: 1.00    ---->> My usb Device
  Type:   Direct-Access                    ANSI SCSI revision: 02
Host: scsi8 Channel: 00 Id: 00 Lun: 00
  Vendor: HUAWEI   Model: Mass Storage     Rev: 2.31    ---->> My HUWAEI USb Modem
  Type:   CD-ROM                           ANSI SCSI revision: 02
Host: scsi9 Channel: 00 Id: 00 Lun: 00
  Vendor: HUAWEI   Model: MMC Storage      Rev: 2.31      
  Type:   Direct-Access                    ANSI SCSI revision: 02




Step2: See how to create a new Partiton using fdisk tool.(Partitioning Device)

We do partition or you can say disks are partitioned to make a separate File System according our need.

#fdisk /dev/sda

[root@myvm1 ~]# fdisk /dev/sda

The number of cylinders for this disk is set to 3263.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help):

 

note: here we have taken sda because fdisk -l show us our hard disk is sda type not hda or hdb.


:n      this will create a new partition

:l      this will create a logical partition

:       just press enter to take default
        cylinder value.
:+2000M this mean we want to create a partion of size 2gb approx ie 2000Mb.

:w      write the changes and save it and exit

 

Step3:Why we use partprobe in linux?

If You don't want to reboot your linux system for making update for the changes you have made in the partition table above just use partprobe command.


#partprobe



Step4:How to make a file system in Linux?

Mkfs or mke2fs command is used to create file system in linux.

So Create an ext3 file system.(Make File System so that it get recognized by OS)

#mke2fs -j /dev/sda6

or

#mkfs -t ext3 /dev/sda6

or

#mkfs.ext3 /dev/sda6

This will format the /dev/sda6 partition  and create journal file system ext3 which can be regognised by our Linux operation System
.

 

step5:Mount point

Now make a new directory and mount /dev/sda6 newly created partion on it.

mountpoint are  directories where file systems are mapped.

 

#mkdir /new

#mount /dev/sda6 /new

Now above command will mount /dev/sda6 on /new directory.
 So now whatever you write in new directory will be saved in newly created partition /dev/sda6



Step6:To verify whether the filesystem has been mounted or not.


#df -h

 this will show you clearly that your /dev/sda6 is mounted on /new directory



Step7:Make File system permanent after reboot.

To make this change exist after reboot or to make it permanent we have to make a entry in /etc/fstab file.

 

#vim /etc/fstab
/dev/sda6     /new     ext3    defaults  0 0
 

save and exit
 the file

 and thats it.
 You have created a new partion /dev/sda6 and now its working.

 

How tO DELETE this Partion?

stepwise Explanation of partition deletion.



Step1:
First unmount the partition and remove the entry from /etc/fstab u have made above


#umount /dev/sda6


Step2:
Then use fdisk command to delete the partition

 

#fdisk /dev/sda

:d    here d is used to delete the partion

:6     it means delete the partion /dev/sda6

:w     save the abve changes and exit




Step3: update change without a reboot.

#partprobe


Step4:Now check the partition table whether it is updated or not.


#fdisk -l

You will find /dev/sda6 has been deleted.


How to see the filesystem type creation command exists for your system?

 

[root@localhost sbin]# cd /sbin/mk
mkbootdisk   mke2fs       mkfs.ext2    mkfs.vfat    mkzonedb     
mkdosfs      mkfs         mkfs.ext3    mkinitrd     
mkdumprd     mkfs.cramfs  mkfs.msdos   mkswap   

 

How to See your Hard Disk Geometry?

 

[root@localhost ~]# fdisk -v
fdisk (util-linux 2.13-pre7)

 

[root@localhost ~]# parted /dev/sda print

Model: ATA ST3160215AS (scsi)
Disk /dev/sda: 160GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type      File system  Flags
 1      1049kB  106MB   105MB   primary   ntfs         boot
 2      106MB   31.5GB  31.4GB  primary   ntfs              
 3      31.5GB  94.4GB  62.9GB  primary   ntfs              
 4      94.4GB  160GB   65.7GB  extended                    
 5      94.4GB  155GB   60.8GB  logical   ext3              

Information: Don't forget to update /etc/fstab, if necessary.

 

How to Display UUID of a partition?

 

[root@localhost ~]# blkid /dev/sda5
/dev/sda5: LABEL="/1" UUID="b8b36258-6c3f-43d9-9c4b-063070945c5c" TYPE="ext3" SEC_TYPE="ext2"

 

How to see the size of existing file system?

[root@localhost ~]# fdisk -s /dev/sda
156290904


 

 

How to create a vfat filesystem in linux?

 

View the existing aprtition table first by using fdisk -l command.

[root@localhost ~]# fdisk -l

Disk /dev/sda: 160.0 GB, 160041885696 bytes
255 heads, 63 sectors/track, 19457 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      102400    7  HPFS/NTFS
Partition 1 does not end on cylinder boundary.
/dev/sda2              13        3825    30617600    7  HPFS/NTFS
/dev/sda3            3825       11474    61440000    7  HPFS/NTFS
/dev/sda4           11475       19457    64123447+   5  Extended
/dev/sda5           11475       18868    59392273+  83  Linux
You have new mail in /var/spool/mail/root

 

Now apply fdisk command to create a new partition.


[root@localhost ~]# fdisk /dev/sda

The number of cylinders for this disk is set to 19457.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): n
First cylinder (18869-19457, default 18869):
Using default value 18869
Last cylinder or +size or +sizeM or +sizeK (18869-19457, default 19457): +100M

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.

 

 

Now use partprobe command to update the partition table without a system reboot.


[root@localhost ~]# partprobe

 

 

Now check whether your system support vfat file system or not ?


[root@localhost ~]# mkfs
mkfs         mkfs.cramfs  mkfs.ext2    mkfs.ext3    mkfs.msdos   mkfs.vfat

 


Now Create vfat partition using mkfs.vfat command.

 

Format /dev/sda6 using mkfs.vfat command.


[root@localhost ~]# mkfs.vfat /dev/sda6
mkfs.vfat 2.11 (12 Mar 2005)

 

Create a mount point for newly created partition i.e for /dev/sda6 partition.


[root@localhost ~]# mkdir /newone

 

Now mount the newly created partition using mount command.


[root@localhost ~]# mount /dev/sda6 /newone/

 

List or check whether partion got mounted or not by using df -h command.


[root@localhost ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda5              55G   19G   34G  36% /
tmpfs                 502M     0  502M   0% /dev/shm
/dev/sda6             102M     0  102M   0% /newone

 

 

Check whether your partition created or not?


[root@localhost ~]# parted /dev/sda print

Model: ATA ST3160215AS (scsi)
Disk /dev/sda: 160GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type      File system  Flags
 1      1049kB  106MB   105MB   primary   ntfs         boot
 2      106MB   31.5GB  31.4GB  primary   ntfs              
 3      31.5GB  94.4GB  62.9GB  primary   ntfs              
 4      94.4GB  160GB   65.7GB  extended                    
 5      94.4GB  155GB   60.8GB  logical   ext3              
 6      155GB   155GB   107MB   logical   fat16             

Information: Don't forget to update /etc/fstab, if necessary.

 

[root@localhost ~]# file -sL /dev/sda6
/dev/sda6: x86 boot sector, mkdosfs boot message display, code offset 0x3c, OEM-ID " mkdosfs", sectors/cluster 4, root entries 512, Media descriptor 0xf8, sectors/FAT 204, heads 255, sectors 208782 (volumes > 32 MB) , serial number 0x517ba11a, label: "           ", FAT (16 bit)

 

How to check the File System Type of your created partition?

 

root@localhost ~]# fsck -N /dev/sda6
fsck 1.39 (29-May-2006)
[/sbin/fsck.vfat (1) -- /dev/sda6] fsck.vfat /dev/sda6


 

Check whether any error or not on newly created file system using fsck command.

 

[root@localhost ~]# fsck /dev/sda6
fsck 1.39 (29-May-2006)
dosfsck 2.11, 12 Mar 2005, FAT32, LFN
/dev/sda6: 0 files, 0/52085 clusters

 

if errors are found on the filesystem fcsk will run.

 

How to create an ext2 Filesystem.

[root@satish ~]# fdisk /dev/sda

The number of cylinders for this disk is set to 4341414.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): n
First cylinder (4096001-4341414, default 4096001):
Using default value 4096001
Last cylinder or +size or +sizeM or +sizeK (4096001-4341414, default 4341414): +1G

Command (m for help): p

Disk /dev/sda: 160.0 GB, 160041885696 bytes
18 heads, 4 sectors/track, 4341414 cylinders
Units = cylinders of 72 * 512 = 36864 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1              29      995584    35840000    7  HPFS/NTFS
/dev/sda2   *      995585     3299584    82944000    7  HPFS/NTFS
/dev/sda3         3299585     3697806    14335992   83  Linux
/dev/sda4         3896918     4341414    16001892    5  Extended
/dev/sda5         3896918     4096000     7166986   83  Linux
/dev/sda6         4096001     4123128      976606   83  Linux

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.

[root@satish ~]# partprobe

[root@satish ~]# mkfs.ext2 /dev/sda6

 

Now Mount  this ext2 filesystem.

first create a directory and then mount it.

[root@satish ~]# mkdir /shivangi

[root@satish ~]# mount /dev/sda6 /shivangi/

 

Now check the mounted filesystem.

 

[root@satish ~]# df -Th /dev/sda6
Filesystem    Type    Size  Used Avail Use% Mounted on
/dev/sda6     ext2    939M  1.2M  890M   1% /shivangi

you can see in above output that filesystem type for /dev/sda6 is ext2 and is mounted on /shivangi.

 

Now Convert this ext2 filesystem to ext3 filesystem.

Upgrading of File system /dev/sda6 here.

  • First unmount the mounted filesystem.
  • then convert is or upgrade it using tune2fs command.
  • then mount it back.
  • then check the filesystem type uding df command.
[root@satish ~]# umount /dev/sda6

[root@satish ~]# tune2fs -j /dev/sda6
tune2fs 1.39 (29-May-2006)
Creating journal inode: done
This filesystem will be automatically checked every 35 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

[root@satish ~]# mount /dev/sda6 /shivangi/

[root@satish ~]# df -Th
Filesystem    Type    Size  Used Avail Use% Mounted on
/dev/sda3     ext3     14G  6.3G  6.4G  50% /
/dev/sda5     ext3    6.7G  2.8G  3.6G  44% /var
tmpfs        tmpfs   1010M     0 1010M   0% /dev/shm
/dev/sda6     ext3    939M   18M  874M   2% /shivangi

 

Check for bad blocks on newly created partition /dev/sda6 here.

 

[root@satish ~]# mke2fs -c /dev/sda6


Forcefully create a filesystem on a mounted device.

[root@satish ~]# mke2fs -F /dev/sda6


 

 

Rate this article: 
Average: 3.4 (707 votes)

Comments

Hello Admin,

Thanks for explaining with snapshots. Actually I am Solaris Admin & learning this Linux. I understood very neatly.

good job, easy practical of any one persons

This is just awesome and so practical! Good job guys!

Hi Sarath,

I have seen that sometime partprobe working but sometimes its not working.
what is the reason behind this ? How can we overcome this? Any other command can be used ?

Hi Sharath,

sometimes we get below message in fdisk.

Partition 1 does not end on cylinder boundary

What does it mean and how can we create partition so shat we should not get such message. Is this message we get only for partitions that we create at OS installatin time ?

Regards,
Gorakhnath

GOOD JOB VERY EASY TO UNDERSTAND.

It was very clearly explained amazing work!!

GOOD JOB VERY EASY TO UNDERSTAND... :)

hi friend i tried to create pvcreate but it threw the error the device dev/sdb3 doesnt exist or ignored by filtering type of error help me please

Sarath Pillai's picture

Sometimes partition table type can also cause such issues. Could you paste the parted command output for that disk (/dev/sdb)?

hi sharath,

it was very clearly explained amazing work. the great job and good platform.

thanks
jaya

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.