Shashikant shah

Saturday, 22 September 2012

Parted Command

The parted utility is an alternative to fdisk. It can perform all the operations of fdisk, but it has additional functionality, such as resizing and copying partitions and creating file systems. The utility is called from the "root" user, passing the disk device as a parameter. Typing help at the prompt lists the commands available.
For additional information on a specific command, type help followed by the command.
(parted) help mklabel  
mklabel,mktable LABEL-TYPE create a new disklabel(partition table)

LABEL-TYPE is one of:aix,amiga,bsd,dvh,gpt,mac,msdos,pc98,sunloop
(parted)
When working with a new disk, the first thing we need to do is label the disk using the mklabel command displayed above. Oddly enough, we have to use the label type of "msdos" for Linux partitions.
(parted) mklabel msdos
Warning: The existing disk label on /dev/sdb will be destroyed and all data on
this disk will be lost. Do you want to continue?
Yes/No? Yes
(parted)
To check the free space on the drive using the print free command.
(parted) print free
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type  File system  Flags
        32.3kB  10.7GB  10.7GB        Free Space

(parted)

Create a Partition

To create a new partition use the mkpart command. If the FS-TYPE parameter is specified, the system ID of the partition is set accordingly, but no file system is created. The following example uses mkpart to create a 2G partition with no file system on it.
(parted) mkpart
Partition type?  primary/extended? primary
File system type?  [ext2]? ext4
Start? 1MB
End? 2GB
(parted) print free
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
        32.3kB  1049kB  1016kB           Free Space
 1      1049kB  2000MB  1999MB  primary
        2000MB  10.7GB  8738MB           Free Space

(parted)
Notice the partition starts at "1MB". This is to leave space for the master boot record.
The mkpartfs command allows you to create a new partition with a file system, but when you try to use it you get a warning that the file system creation is not robust. For this reason, it should probably be avoided.

Delete a Partition

The rm command is used to delete a partition. It accepts the partition number as a parameter. The following example deletes the partition we just created.
(parted) print free
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
        32.3kB  1049kB  1016kB           Free Space
 1      1049kB  2000MB  1999MB  primary
        2000MB  10.7GB  8738MB           Free Space

(parted) rm 1
(parted) print free
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type  File system  Flags
        32.3kB  10.7GB  10.7GB        Free Space

(parted)

Boot Partitions

Boot partitions are created in a similar manner to regular partitions, but the boot flag is set using the toggle or set command after the partition is created, as shown below.
(parted) mkpart
Partition type?  primary/extended? primary
File system type?  [ext2]? ext4
Start? 1MG
End? 500MB
(parted) toggle 1 boot
(parted) print free
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
        32.3kB  1049kB  1016kB           Free Space
 1      1049kB  500MB   499MB   primary               boot
        500MB   10.7GB  10.2GB           Free Space

(parted)

Swap Partitions

Swap partitions are created in a similar manner to regular partitions, but the file system type is set to linux-swap. The example below creates a 2G swap partition after the boot partition created previously.
(parted) mkpart
Partition type?  primary/extended? primary
File system type?  [ext2]? linux-swap
Start? 500MB
End? 2.5GB
(parted) print free
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
        32.3kB  1049kB  1016kB           Free Space
 1      1049kB  500MB   499MB   primary               boot
 2      500MB   2500MB  2000MB  primary
        2500MB  10.7GB  8238MB           Free Space

(parted)

LVM Partitions

LVM partitions are created in a similar manner to regular partitions, but lvm flag is set using the toggle or set command after the partition is created, as shown below.
(parted) mkpart
Partition type?  primary/extended? primary
File system type?  [ext2]? ext4
Start? 2.5GB
End? 10.7GB
(parted) toggle 3 lvm
(parted) print free
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
        32.3kB  1049kB  1016kB           Free Space
 1      1049kB  500MB   499MB   primary               boot
 2      500MB   2500MB  2000MB  primary
 3      2500MB  10.7GB  8238MB  primary               lvm

(parted)

No comments:

Post a Comment