The fdisk
command is a text-based utility for viewing and managing hard disk partitions on Linux. It’s one of the most powerful tools you can use to manage partitions.
Many drives come with a single partition already set up, but all storage devices are just treated as a mass of unallocated, free space when they contain no partitions. To actually set up a file system and save any files to the drive, the drive needs a partition.
Partitions are necessary because you can’t just start writing files to a blank drive. You must first create at least one container with a file system. After creating a partition, the partition is formatted with a file system — like the NTFS file system on Windows drives, FAT32 file system for removable drives, HFS+ file system on Mac computers, or the ext4 file system on Linux. Files are then written to that file system on the partition.
The sudo fdisk -l commands lists the partitions on your system.
sudo fdisk -l
Disk /dev/xvda: 100 GiB, 107374182400 bytes, 209715200 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
Disklabel type: dos
Disk identifier: 0x974bb19a
Device Boot Start End Sectors Size Id Type
/dev/xvda1 * 2048 526335 524288 256M 83 Linux
/dev/xvda2 526336 209715166 209188831 99.8G 83 Linux
Disk /dev/xvdc: 100 GiB, 107374182400 bytes, 209715200 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
This shows that the disk /dev/xvda1
has already been partitioned and contains 2 partitions
one a boot partition /dev/xvda1
of 256MB and another linux parition /dev/xvda2
of 99.8G
The disk /dev/xvdc
does not contain any partitions
To create a partition we need to enter command mode we do this by typing the command
root@server2:/home/pi# sudo fdisk /dev/xvdc
Welcome to fdisk (util-linux 2.27.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xeb288d63.
Command (m for help):
As seen we can see that since the device is not partitioned ,it does not contain any partition table
Use the option p
to print the current partition table
Command (m for help): p
Disk /dev/xvdc: 100 GiB, 107374182400 bytes, 209715200 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
Disklabel type: dos
Disk identifier: 0xeb288d63
Command (m for help):
When partitioning, you’ll need to be aware of the difference between primary, extended, and logical partitions. A disk with a traditional partition table can only have up to four partitions. Extended and logical partitions are a way to get around this limitation.
Each disk can have up to four primary partitions or three primary partitions and an extended partition. If you need four partitions or less, you can just create them as primary partitions.
However, let’s say you want six partitions on a single drive. You’d have to create three primary partitions as well as an extended partition. The extended partition effectively functions as a container that allows you to create a larger amount of logical partitions. So, if you needed six partitions, you’d create three primary partitions, an extended partition, and then three logical partitions inside the extended partition. You could also just create a single primary partition, an extended partition, and five logical partitions — you just can’t have more than four primary partitions at a time.
The n command
can be used to create a new partition.
- You can create a logical or primary partition (l for logical or p for primary). A disk can only have four primary partitions.
- Next, specify the sector of the disk you want the partition to start at. Press Enter to accept the default sector, which is the first free sector on the disk.
- Last, specify the last sector of the partition on the disk. If you want to use up all available space after the initial sector, just press Enter
Command (m for help): p
Disk /dev/xvdc: 100 GiB, 107374182400 bytes, 209715200 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
Disklabel type: dos
Disk identifier: 0xeb288d63
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p):
Using default response p.
Partition number (1-4, default 1):
First sector (2048-209715199, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-209715199, default 209715199):
Created a new partition 1 of type 'Linux' and of size 100 GiB.
we can see that by running the p
command again that linux partition of 100G space have been created
on the /dev/xvdc
device
Command (m for help): p
Disk /dev/xvdc: 100 GiB, 107374182400 bytes, 209715200 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
Disklabel type: dos
Disk identifier: 0xeb288d63
Device Boot Start End Sectors Size Id Type
/dev/xvdc1 2048 209715199 209713152 100G 83 Linux
Use w to write the changes you’ve made to disk.
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
Formatting a Partition
You must format new partitions with a file system before you can use them. You can do this with the appropriate mkfs command.
we will be formatting the partition as ext4 file system.
root@server2:/home/pi# sudo mkfs.ext4 /dev/xvdc1
mke2fs 1.42.13 (17-May-2015)
Creating filesystem with 26214144 4k blocks and 6553600 inodes
Filesystem UUID: 6aef433c-583d-4fd4-9e2b-33084414a4bd
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
We can also configure partitions using gparted
which is a graphical utility
Launch the utility
Create Parition Table
create parition Table by selecting option “Device->New Partition Table” and selecting the default options
**Create and Format the partition **
Right Click on the unallocated partition and Select New to enter the menu to create and format the new partition
Enter the parition size ,whether its a primary or extended partition and filesystem type
Upon selecting the Add options,It will create the respective parition and also format it as per selected options
Mount the parition
we can now mount the partition and start using it.
mount /dev/xvdc1 /tmp/storage1
If the computer is rebooted then we need to manually run the mount command
To configure the system to auto mount the system we need to edit the /etc/fstab
file
however icorrectly editing the files can lead to destructive changes,hence it is
better to use a graphical or commandline utility which edits the file
we will be using gnome-disks
utility
sudo apt-get install gnome-disk-utility`
sudo gnome-disks
Launch the utility
Edit and save the mount options
Once the configuration is done we can see the following entry added in the /etc/fstab
file
/dev/disk/by-uuid/6aef433c-583d-4fd4-9e2b-33084414a4bd /opt/miko/storage/disk1 auto nosuid,nodev,nofail,x-gvfs-show 0 0
Now you can restart the compute and see that all the partitions are auto mounted.
References
- https://www.howtogeek.com/106873/how-to-use-fdisk-to-manage-partitions-on-linux/