Table of contents
To utilize the drive within the opearting system, it must be prepared at first.
These are my notes on using fdisk to partition disk in Linux.
Test drive
As I don’t want to to touch any physical drive, to play with partitioning I will use a file mounted as a block device.
Let’s create 1G file first:
truncate -s 1G fake_disk.img
My file localization is: /home/andrzej/testing/fake_disk.img.
Then, I’m going to mount the file as a block device. I will use losetup
to create a loop device:
sudo losetup --find --show /home/andrzej/testing/fake_disk.img
As a response I’m getting the name of the device, in my case /dev/loop0.
Whenever I need to display all loop devices, I can use:
losetup -a
I’m getting the loop devices list, along with the mounted files corresponding to them.
Fdisk partitioning
Dobra, to mając już gotowy dysk do zabawy, mogę zacząć partycjonowanie. Zacznę od fdisk, ponieważ jest prostszy w obsłudze, i służy do podstawowych operacji.
OK, the drive is ready, so let’s start partitioning. Let’s try some fdisk.
Single partition on whole drive
Let’s start with simple case: single partion on whole drive.
sudo fdisk /dev/loop0
On completely clean file I can see that it doesn’t contain any partition table:
Welcome to fdisk (util-linux 2.37.2).
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 0x5f9d5e8b.
Command (m for help):
With m option I can show help (it’s useful).
Using two options: g and n I create GPT partition table and one
partition on whole drive.
By using F I can display what I’ve done so far:
Command (m for help): F
Unpartitioned space /dev/loop0: 1022.98 MiB, 1072676352 bytes, 2095071 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
Start End Sectors Size
2048 2097118 2095071 1023M
Option p allows me to display current partition table:
Command (m for help): p
Disk /dev/loop0: 1 GiB, 1073741824 bytes, 2097152 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: gpt
Disk identifier: 1FD9AAE4-C2EB-E647-A737-A3C2D36DD6F8
Then v to check if everything is OK.
Command (m for help): v
No errors detected.
Header version: 1.0
Using 0 out of 128 partitions.
A total of 2095071 free sectors is available in 1 segment.
No errors here, so I’m writing partition table to disk by w:
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: Invalid argument
The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or partx(8).
Error.
But wait, fdisk just printed that table was modified. Let’s check it once again:
$ sudo fdisk -l /dev/loop0
Disk /dev/loop0: 1 GiB, 1073741824 bytes, 2097152 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: gpt
Disk identifier: B50E488E-A665-4041-B257-AFD42B8EDAE8
Device Start End Sectors Size Type
/dev/loop0p1 2048 2097118 2095071 1023M Linux filesystem
After a bit long searching, I’ve found out that kernel was not able to read the partition table, due to usage of loop device.
After creating partition, by default I expect new device to appear in /dev
directory, e.g. /dev/loop0p1. It didn’t appear, unfortunately. Let’s check
/dev:
$ ls /dev | grep loop0
loop0
Reason? Once again - loop device. To fix both problems, I need to use
--partscan option when mounting file as loop device.
I’m remounting it properly:
sudo losetup -d /dev/loop0
sudo losetup --find --show --partscan /home/andrzej/testing/fake_disk.img
I’m checking the /dev directory again:
$ ls /dev | grep loop0
loop-control
loop0
loop0p1
Mission completed, at least so far.
Multiple partitions on disk
I’m going to do two scenarios:
- two partitions, 512M each
- three partitions: 256M, 512M, and the rest of the disk
Two 512M partitions
In my working directory, I’m creating new 1G file and mounting it as loop device:
truncate -s 1G fake_disk_2.img
sudo losetup --find --show --partscan fake_disk_2.img
Mounted device is identified as /dev/loop1. Now I’m going to create partitions:
sudo fdisk /dev/loop1
Using g option, I’m creating GPT partition table, and then with n - new
partition:
Command (m for help): g
Created a new GPT disklabel (GUID: F800D377-953A-5243-8193-47D19A066CA6).
Command (m for help): n
Partition number (1-128, default 1): 1
First sector (2048-2097118, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-2097118, default 2097118): +512M
Created a new partition 1 of type 'Linux filesystem' and of size 512 MiB.
Second partition that utilizes the rest of the disk space:
Command (m for help): n
Partition number (2-128, default 2): 2
First sector (1050624-2097118, default 1050624):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (1050624-2097118, default 2097118):
Created a new partition 2 of type 'Linux filesystem' and of size 511 MiB.
I’m writing it using w
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
Checking /dev/:
$ ls /dev | grep loop1
loop1
loop1p1
loop1p2
Everything as expected. Let’s go forward.
Three partitions
I wanted to create a separate image file at first, but since I already have a disk prepared with two partitions, let’s try to alter them:
Drive is already mounted, so I’m going to fdisk:
sudo fdisk /dev/loop1
fdisk is not able to modify existing partitions, so first I need to delete them:
Command (m for help): d
Partition number (1,2, default 2): 2
Partition 2 has been deleted.
Command (m for help): d
Selected partition 1
Partition 1 has been deleted.
I’m checking the disk state:
Command (m for help): p
Disk /dev/loop1: 1 GiB, 1073741824 bytes, 2097152 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: gpt
Disk identifier: F800D377-953A-5243-8193-47D19A066CA6
Command (m for help): i
No partition is defined yet!
Alright, we have a clear GPT table here. Let’s create partitions again:
- no. 1 - 256Mb
- no. 2 - 512Mb
- no. 3 - remaining space
Command (m for help): n
Partition number (1-128, default 1): 1
First sector (2048-2097118, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-2097118, default 2097118): +256M
Created a new partition 1 of type 'Linux filesystem' and of size 256 MiB.
Command (m for help): n
Partition number (2-128, default 2):
First sector (526336-2097118, default 526336): +512M
Value out of range.
First sector (526336-2097118, default 526336):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (526336-2097118, default 2097118): +512M
Created a new partition 2 of type 'Linux filesystem' and of size 512 MiB.
Command (m for help): n
Partition number (3-128, default 3): 3
First sector (1574912-2097118, default 1574912):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (1574912-2097118, default 2097118):
Created a new partition 3 of type 'Linux filesystem' and of size 255 MiB.
Checking partitions table:
Command (m for help): p
Disk /dev/loop1: 1 GiB, 1073741824 bytes, 2097152 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: gpt
Disk identifier: F800D377-953A-5243-8193-47D19A066CA6
Device Start End Sectors Size Type
/dev/loop1p1 2048 526335 524288 256M Linux filesystem
/dev/loop1p2 526336 1574911 1048576 512M Linux filesystem
/dev/loop1p3 1574912 2097118 522207 255M Linux filesystem
Everything as expected, so I’m writing changes and checking /dev directory:
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
$ ls -la /dev | grep loop1
brw-rw---- 1 root disk 7, 1 Jul 15 12:50 loop1
brw-rw---- 1 root disk 259, 6 Jul 15 12:50 loop1p1
brw-rw---- 1 root disk 259, 7 Jul 15 12:50 loop1p2
brw-rw---- 1 root disk 259, 8 Jul 15 12:50 loop1p3
Partitions are visible as expected.
To keep everything clean, after finishing partitioning, I’m unmounting all loop devices:
sudo losetup -D
Summary
Fdisk is a very simple tool for partitioning. I haven’t covered all possible options here, but for basic operations, I don’t need anything more.
Additional plus - it’s hard to break something here.
In the next part - I will cover parted tool.