HostOnNet Blog

Partitioning with parted

Looking for Linux Server Admin or WordPress Expert? We can help.

parted is a command line program to partition hard disks.

Viewing Partition

To view partition on a hard disk, use

parted /dev/DEV_NAME print

Example

I will use /dev/sdc as device name.

root@hon-pc-01:~# parted /dev/sdc print
Model: ATA ST31000528AS (scsi)
Disk /dev/sdc: 1000GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start  End  Size  Type  File system  Flags

root@hon-pc-01:~# 

From the result, we identify model of the hard disk as ST31000528AS and Disk size 1000 GB.

Partition table type is msdos. Disk have no partition.

Creating Partition

Before creating partition, you need to know how much free disk space you have, this can be found with command

parted /dev/DEV_NAME print free

Example

root@hon-pc-01:~# parted /dev/sdc print free
Model: ATA ST31000528AS (scsi)
Disk /dev/sdc: 1000GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

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

root@hon-pc-01:~# 

Lets create a partition with

parted /dev/DEV_NAME mkpart

Example

root@hon-pc-01:~# parted /dev/sdc mkpart
Partition type?  primary/extended? primary                                
File system type?  [ext2]? ext4                                           
Start? 1M                                                                 
End? 100%                                                                 
Information: You may need to update /etc/fstab.

root@hon-pc-01:~#                                                         

Now view the partition table with command

parted /dev/DEV_NAME print

Example

root@hon-pc-01:~# parted /dev/sdc print
Model: ATA ST31000528AS (scsi)
Disk /dev/sdc: 1000GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  1000GB  1000GB  primary  ext4

root@hon-pc-01:~# 

We have one partition, it will be refereed as /dev/sdc1

Formatting Partition

Before you can use a partition, you need to format it. This can be done with command

mkfs.ext4 /dev/DEV_NAME

Example

root@hon-pc-01:~# mkfs.ext4 /dev/sdc1
mke2fs 1.42.13 (17-May-2015)
/dev/sdc1 contains a ext4 file system
	last mounted on Tue Aug  9 22:21:57 2016
Proceed anyway? (y,n) y
Creating filesystem with 244190208 4k blocks and 61054976 inodes
Filesystem UUID: 159684b2-2788-47c2-bc05-2aaf6866ff1c
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
	4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 
	102400000, 214990848

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done     

root@hon-pc-01:~# 

Mounting a partition

You need to mount a filesystem to use it. First create a mount point, here we will create /backup

mkdir /backup

Now we can mount /dev/sdc1 to /backup folder using command

mount /dev/sdc1 /backup

Lets verify with df -h

root@hon-pc-01:~# df -h | grep  sdc
/dev/sdc1       917G   72M  871G   1% /backup
root@hon-pc-01:~# 

Using /etc/fstab

Running mount command every time you want to use a filesystem is not practical. Linux provide /etc/fstab to auto mount filesystems.

To mount this disk, we need to edit /etc/fstab

vi /etc/fstab

Add

/dev/sdc1 /backup auto nosuid,nodev,nofail 0 0

The problem with this is if you change Hard disk cable, the name of device changes. So best way is to use Device UUID instead of Device name. To find UUID of a device, use command

blkid

Example

root@hon-pc-01:~# blkid | grep sdc
/dev/sdc1: UUID="159684b2-2788-47c2-bc05-2aaf6866ff1c" TYPE="ext4" PARTUUID="1bd91bd8-01"
root@hon-pc-01:~# 

In this case, UUID is 159684b2-2788-47c2-bc05-2aaf6866ff1c

To mount a disk by UUID, add following line to /etc/fstab

/dev/disk/by-uuid/159684b2-2788-47c2-bc05-2aaf6866ff1c /backup auto nosuid,nodev,nofail 0 0

To verify Disk can mount from /etc/fstab, first unmount the disk with

umount /dev/sdc1

Now run

mount -a

This will mount all disks specified in /etc/fstab file. Verify /dev/sdc1 is mounted

root@hon-pc-01:~# df -h | grep sdc1
/dev/sdc1       917G   72M  871G   1% /backup
root@hon-pc-01:~# 

Posted in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.