Manage your disks with Parted on Linux

diskWhile there is a GUI front-end to parted on Linux (GParted), I use cmdline parted and fdisk to handle my disks most of the time. In this article I will explain how to partition a fresh disk using parted. Straight to business with a new pen drive (operations are exactly the same as hard disk) and as root (you can also use sudo).

Once you connect the drive to Linux you have to know which is the correct device node:

# dmesg|tail -f
[16005.985619] sd 8:0:0:0: [sdb] Write Protect is off
[16005.985623] sd 8:0:0:0: [sdb] Mode Sense: 43 00 00 00
[16005.986328] sd 8:0:0:0: [sdb] No Caching mode page found
[16005.986332] sd 8:0:0:0: [sdb] Assuming drive cache: write through
[16005.989478] sd 8:0:0:0: [sdb] No Caching mode page found
[16005.989482] sd 8:0:0:0: [sdb] Assuming drive cache: write through
[16005.991658] sdb: unknown partition table
[16005.993343] sd 8:0:0:0: [sdb] No Caching mode page found
[16005.993357] sd 8:0:0:0: [sdb] Assuming drive cache: write through
[16005.993360] sd 8:0:0:0: [sdb] Attached SCSI removable disk

So it’s detected as sdb. You can also use fdisk:

# fdisk -l

Disk /dev/sdb: 16.2 GB, 16219373568 bytes
64 heads, 32 sectors/track, 15468 cylinders, total 31678464 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

Disk /dev/sdb doesn't contain a valid partition table

Get it ready using parted. Comments in bold:

# parted /dev/sdb
GNU Parted 2.3
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.

(parted) help //see what commands are supported
align-check TYPE N check partition N for TYPE(min|opt) alignment
check NUMBER do a simple check on the file system
cp [FROM-DEVICE] FROM-NUMBER TO-NUMBER copy file system to another partition
help [COMMAND] print general help, or help on COMMAND
mklabel,mktable LABEL-TYPE create a new disklabel (partition table)
mkfs NUMBER FS-TYPE make a FS-TYPE file system on partition NUMBER
mkpart PART-TYPE [FS-TYPE] START END make a partition
mkpartfs PART-TYPE FS-TYPE START END make a partition with a file system
move NUMBER START END move partition NUMBER
name NUMBER NAME name partition NUMBER as NAME
print [devices|free|list,all|NUMBER] display the partition table, available devices, free space, all found partitions, or a particular partition
quit exit program
rescue START END rescue a lost partition near START and END
resize NUMBER START END resize partition NUMBER and its file system
rm NUMBER delete partition NUMBER
select DEVICE choose the device to edit
set NUMBER FLAG STATE change the FLAG on partition NUMBER
toggle [NUMBER [FLAG]] toggle the state of FLAG on partition NUMBER
unit UNIT set the default unit to UNIT
version display the version number and copyright information of GNU Parted

(parted) mklabel //choose the disk partitioning scheme. msdos and gpt are common
New disk label type? //double TAB shows the options below
aix amiga bsd dvh gpt loop mac msdos pc98 sun
New disk label type? msdos

(parted) mkpart //create a new partition
Partition type? primary/extended? p
File system type? [ext2]? //double TAB for options
affs0 affs4 amufs amufs3 apfs2 ext3 freebsd-ufs hp-ufs linux-swap(old) reiserfs zfs
affs1 affs5 amufs0 amufs4 asfs ext4 hfs jfs linux-swap(v0) sun-ufs
affs2 affs6 amufs1 amufs5 btrfs fat16 hfs+ linux-swap linux-swap(v1) swsusp
affs3 affs7 amufs2 apfs1 ext2 fat32 hfsx linux-swap(new) ntfs xfs
File system type? [ext2]? ntfs
Start? 1 //start the first partition at 1 MB
End? 4000 //upto 4000 MB

(parted) mkpart
Partition type? primary/extended? p
File system type? [ext2]? ext4
Start? 4000 //we ended first partition at 4000. parted adjusts automatically! 
End? -1 //-1 means the last sector of the disk. We are using all the available space

(parted) p //check the partition details on the drive
Model: JetFlash Transcend 16GB (scsi)
Disk /dev/sdb: 16.2GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system Flags
1 1049kB 4000MB 3999MB primary ntfs
2 4000MB 16.2GB 12.2GB primary

(parted) quit //all done!                                                             
Information: You may need to update /etc/fstab.

Now you have a disk with one ntfs and one ext4 partition. Well! Not yet! Parted just set the information about the filesystem types (ntfs and ext4) in the MBR. You have to format the partitions to start using them.

# mkntfs -fc 4096 /dev/sdb1 //ntfs filesystem, cluster size 4KB (common for NTFS)
# mkfs.ext4 /dev/sdb2 //ext4 filesystem

That’s it! The pen drive is ready with a 4GB NTFS partition and a 12.2GB EXT4 partition. And do you know that (specifically in case of a pen drive) though both of them will be automatically detected on any Linux distro, Windows 7 will only find the first NTFS partition. Even reformatting won’t help. The reason is that Windows 7 can detect only one partition on pen drives and thinks that’s the size of the pen drive as well.

Parted is very powerful and supports many more operations. Check out man parted.

One thought on “Manage your disks with Parted on Linux”


Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s