Monday 6 June 2011

List, create, delete and set partition type for primary, extended, and logical partitions

As with many things in Linux, there are many ways of performing the tasks needed to master this objective. In general you will either use fdisk or parted. 

List Partitions: 

fdisk -l - will provide a list of partitions for all devices in the system

fdisk -l /dev/sda - will list all partitions for disk sda

Alternatively, you can use parted, like this:

parted - once inside the parted shell, type:

print all

In order to list the partitions in a single device, once inside the parted shell, simply type:
print
If you want to change the device you are operating in, you can either invoke add the device to the parted command, e.g.

parted /dev/sdb

or while inside the parted shell:

select /dev/sdb

Note, that you could run the commands outside the parted command shell, like this: parted -l

Create Partitions:

First we'll use fdisk, note that this is for a blank disk, if the disk already contains partitions then there will be limitations, e.g. if you already have 4 primary partitions you'll only be able to create an extended partition.
  1. Start fdisk (note that you can type m for help) - fdisk /dev/sdb  or fdisk -u /dev/sdb for sizes in sectors instead of cylinders
  2. Type n and you'll get the following options
  3.    Command action    
      e   extended   
             p   primary partition (1-4)  
  4.  Type p
  5. Type 1, to make this the first partition
  6. The next option is where to start your partition, I normally accept the default, so I hit Enter
  7. The last option is where to end your partition. If you want just one partition accept the default, otherwise just select your partition size, e.g. say you want a 1 GB partition, you need type +1G
  8. type w, to write your changes to the partition table and exit fdisk.
You'll have to wait until the following objective  Create, mount, unmount and use ext2, ext3 and ext4 file systems to learn how to use the partition you have just created.

Now, let's turn to parted.
  1. Start parted (note that you can type help for help)- parted /dev/sdb
  2. Type makepart
  3. You'll get a choice of primary or extended, type primary
  4. Select the file type, e.g. ext4
  5. Select the start for the partition, note that unlike fdisk, you have to type something here, e.g. 1 for the partition to start at the beginning of the disk
  6. Select the end of the partition
  7. You might get a warning that the partition is not best aligned for performance, if you set the start at 0.
  8. Exit parted.
  9. Alternatively, you can just type parted /dev/sdb primary ext4 1 1000M from the command shell
Note, that due to limitations in the MBR, it is only possible to have four primary partitions. If more partitions are needed, then an extended and logical partition(s) need to be created. In essence, an extended partition is created and inside the extended partition, logical partitions can be created. These can then be used in a similar way to primary partitions.

Using parted only, and assuming that the disk is 8 GB and has a 1 GB primary partition:
  1. First make an extended partition - parted /dev/sdb mkpart extended 1000M 8000M
  2. Then make a logical partition - parted /dev/sdb mkpart logical 1001M 4000M
  3. output from: parted /dev/sdb print
Model: VMware Virtual disk (scsi)
Disk /dev/sdb: 8590MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type      File system  Flags
 1      1049kB  1000MB  999MB   primary   ext4
 2      1000MB  8000MB  6999MB  extended               lba
 5      1001MB  4000MB  2999MB  logical
Note how the logical partition has a number of 5

Delete partitions:

For fdisk.
  1. fdisk /dev/sdb
  2. Type d
  3. Type the number of the partition you want to delete
  4. Type w
For parted.
  1.  parted /dev/sdb
  2. rm n - where n is the number of the partition you want to delete.
  3. Alternatively, parted /dev/sdb rm n - where n is the number of the partition you want to delete.
Set partition type:

This is relatively simple for fdisk.
  1. fdisk /dev/sdb
  2. Type t
  3. Type the partition number you want to use
  4. If you know the code, then type it, otherwise type L to get a list of partition codes
  5. Let's say we want to make it a swap partition, so type 82
  6. Type w
I'm not 100% sure how to do this with parted.

You can do parted /dev/sdb 5 set lvm on, to set the lvm label on, but I don't know how to change the partition type to say VMWare VMFS.

No comments:

Post a Comment