disk partitioning

Partitioning a new disk is the first task you will do when configuring new or extra storage.

To get the picture of the current storage layout we can use lsblk to list the block devices (usually traditional disks or SSDs):

root:/usr/bin> lsblk
NAME                           MAJ:MIN  RM SIZE     RO TYPE MOUNTPOINT
sda                              8:0     0 931.5G    0 disk
├─ sda1                          8:1     0 476M      0 part /boot/efi
├─ sda2                          8:2     0 3.8G      0 part [SWAP]
├─ sda3                          8:3     0 41.3G     0 part
│   └─ md127                     9:127   0 41.3G     0 raid1
│         ├─ vg­root            253:0     0 7.6G      0 lvm /
│         ├─ vg­var             253:11    0 3.8G      0 lvm /var
│         └─ vg­home            253:12    0 29.8G     0 lvm /home
└─ sda4                          8:4     0 885.9G    0 part
   ├─ vgdata­documentation    253:1     0 20G       0 lvm /home/marc/documentation
   ├─ vgdata­music            253:2     0 10G       0 lvm /home/marc/music
   ├─ vgdata­vigilante        253:3     0 1G        0 lvm /home/marc/vigilante
   ├─ vgdata­photos           253:4     0 150G      0 lvm /home/marc/photos
   ├─ vgdata­companies        253:5     0 1G        0 lvm /home/marc/companies
   ├─ vgdata­software         253:6     0 80G       0 lvm /home/marc/software
   ├─ vgdata­personal         253:7     0 5G        0 lvm /home/marc/personal
   ├─ vgdata­win7             253:8     0 16G       0 lvm /win7
   ├─ vgdata­oracle           253:9     0 6G        0 lvm /oracle
   └─ vgdata­sl7ora112        253:10    0 24G       0 lvm /sl7ora112
sdb                              8:16    0 931.5G    0 disk
├─ sdb1                          8:17    0 4.3G      0 part /tmp
├─ sdb2                          8:18    0 41.3G     0 part
│   └─ md127                     9:127   0 41.3G     0 raid1
│         ├─ vg­root            253:0     0 7.6G      0 lvm /
│         ├─ vg­var             253:11    0 3.8G      0 lvm /var
│         └─ vg­home            253:12    0 29.8G     0 lvm /home
└─ sdb3                          8:19    0 885.9G    0 part
│    ├─ vgdata­documentation    253:1     0 20G       0 lvm /home/marc/documentation
│    ├─ vgdata­music            253:2     0 10G       0 lvm /home/marc/music
│    ├─ vgdata­vigilante        253:3     0 1G        0 lvm /home/marc/vigilante
│    ├─ vgdata­photos           253:4     0 150G      0 lvm /home/marc/photos
│    ├─ vgdata­companies        253:5     0 1G        0 lvm /home/marc/companies
│    ├─ vgdata­software         253:6     0 80G       0 lvm /home/marc/software
│    ├─ vgdata­personal         253:7     0 5G        0 lvm /home/marc/personal
│    ├─ vgdata­win7             253:8     0 16G       0 lvm /win7
│    ├─ vgdata­oracle           253:9     0 6G        0 lvm /oracle
│    └─ vgdata­sl7ora112        253:10    0 24G       0 lvm /sl7ora112
sr0                             11:0     1 1024M     0 rom

The layout above shows 2 block devices (sda & sdb) with 4 and 3 partitions respectively.

The 1st partition of sda is the boot partition. The 2nd one is the swap partition. The 3rd one is the RAID volume that contains “/”, “/var” and “/home” and that is mirrored in the 2nd partition of sdb. The 4th partition is an LVM volume with a bunch of logical volumes.

As regards to sdb, we have the 1st partition that is “/tmp”. The 2nd partition is the mirrored RAID mentioned above. And the 3rd partition is the same LVM volume as that in the 4th partition of sda (the LVM is composed of 2 physical volumes: /dev/sda4 and /dev/sdb3).

If we just need to get a list of SCSI devices with a brief description (vendor, mode, device, size, etc) we can use lsscsi:

root:/usr/bin> lsscsi ­-d -­s
[0:0:0:0]    disk     ATA     HGST HTS541010A9    A590    /dev/sda [8:0]  1.00TB
[2:0:0:0]    cd/dvd   hp      BD MLT UJ262        1.00    /dev/sr0 [11:0] ­
[4:0:0:0]    disk     ATA     TOSHIBA MQ01ABD1    1C      /dev/sdb [8:16] 1.00TB

The same way as lsscsi shows us only the SCSI devices, we can use lsusb and lspci to list the USB and PCI devices:

root:/usr/bin> lsusb ­-t
/: Bus 04.Port 1: Dev 1,     Class=root_hub, Driver=ehci­pci/2p,   480M
.   |__ Port 1: Dev 2, If 0,  Class=Hub, Driver=hub/8p,            480M
/: Bus 03.Port 1: Dev 1,     Class=root_hub, Driver=ehci­pci/2p,   480M
.   |__ Port 1: Dev 2, If 0,  Class=Hub, Driver=hub/6p,            480M
/: Bus 02.Port 1: Dev 1,     Class=root_hub, Driver=xhci_hcd/6p, 5000M
/: Bus 01.Port 1: Dev 1,     Class=root_hub, Driver=xhci_hcd/14p, 480M
.   |__ Port 7: Dev 3, If 0,  Class=Video, Driver=uvcvideo,        480M
.   |__ Port 7: Dev 3, If 1,  Class=Video, Driver=uvcvideo,        480M
.   |__ Port 14: Dev 4, If 0, Class=Vendor Specific Class, Driver=, 12M
root:~> lspci ­-v
00:00.0 Host bridge: Intel Corporation Xeon E3­1200 v3/4th Gen Core Processor DRAM Controller
(rev 06)
Subsystem: Hewlett­Packard Company Device 1966
Flags: bus master, fast devsel, latency 0
Capabilities: [e0] Vendor Specific Information: Len=0c <?>
Kernel modules: ie31200_edac
.
00:01.0 PCI bridge: Intel Corporation Xeon E3­1200 v3/4th Gen Core Processor PCI Express x16
Controller (rev 06) (prog­if 00 [Normal decode])
Flags: bus master, fast devsel, latency 0, IRQ 24
Bus: primary=00, secondary=01, subordinate=06, sec­latency=0
I/O behind bridge: 00005000­00005fff
Memory behind bridge: d2000000­d2ffffff
Prefetchable memory behind bridge: 00000000a0000000­00000000bfffffff
Capabilities: [88] Subsystem: Hewlett­Packard Company Device 1966
Capabilities: [80] Power Management version 3
Capabilities: [90] MSI: Enable+ Count=1/1 Maskable­ 64bit­
Capabilities: [a0] Express Root Port (Slot+), MSI 00
Capabilities: [100] Virtual Channel
Capabilities: [140] Root Complex Link
Capabilities: [d94] #19
Kernel driver in use: pcieport
Kernel modules: shpchp
.
00:02.0 VGA compatible controller: Intel Corporation 4th Gen Core Processor Integrated Graphics
Controller (rev 06) (prog­if 00 [VGA controller])
Subsystem: Hewlett­Packard Company Device 1966
Flags: bus master, fast devsel, latency 0, IRQ 29
Memory at d3000000 (64­bit, non­prefetchable) [size=4M]
Memory at c0000000 (64­bit, prefetchable) [size=256M]
I/O ports at 6000 [size=64]
Expansion ROM at <unassigned> [disabled]
Capabilities: [90] MSI: Enable+ Count=1/1 Maskable­ 64bit­
Capabilities: [d0] Power Management version 2
Capabilities: [a4] PCI Advanced Features
Kernel driver in use: i915
Kernel modules: i915
[…]

We can also use blkid to get the list of block devices listing the underlying filesystem type, label and UUID:

root:/usr/bin> blkid
/dev/block/9:127: UUID=”06whsq­Y6uc­5rtl­0NrM­NN7D­HosS­JleYFN” TYPE=”LVM2_member”
/dev/block/253:0: LABEL=”LVROOT” UUID=”85ec848a­c542­4a3a­96ac­f2810edf310b” TYPE=”xfs”
/dev/block/8:18: UUID=”649d37a8­575b­fcf3­1089­f2fc70a9c7f5″ UUID_SUB=”5867c144­1e51­e5a0­b901­2b52ea366949″ LABEL=”envy17:pv00″ TYPE=”linux_raid_member” PARTUUID=”3b634250­ed6e­4db4­bb34­642d86b5694d”
/dev/sda4: UUID=”f9R0tj­yxgA­VbYq­jxkg­7hNe­xBrb­REJmVo” TYPE=”LVM2_member” PARTLABEL=”primary” PARTUUID=”6e0f0c16­24a5­4266­aa53­d078bb371bef”
/dev/sdb1: LABEL=”TMP” UUID=”84c5bb32­f956­41a7­ba4d­983f75c155d5″ TYPE=”ext4″ PARTUUID=”9797be06­eaeb­4c25­ ac1b­9a2e05fbe4eb”
/dev/sdb3: UUID=”AiPiWz­AInj­pnui­50To­oH47­2lBw­vswa1O” TYPE=”LVM2_member” PARTLABEL=”primary” PARTUUID=”67683a15­f230­4be6­93c3­c9bd123e0265″
/dev/mapper/vg­var: LABEL=”LVVAR” UUID=”f4934d4f­255a­4db9­ae1f­495cb35576d4″ TYPE=”xfs”
/dev/mapper/vg­home: LABEL=”LVHOME” UUID=”b8033cc3­450e­4dbd­952d­c7685d1355cc” TYPE=”xfs”
[…]

To view and manipulate disk partitions we can use a variety of tools but we will focus on parted (there’s also gparted which is the GUI version) because it can do everything we will need.

We can use parted in interactive (parted CLI) and non-interactive (parted pre-pended) mode. Let’s see how to list the devices and partitions:

root:~> parted
GNU Parted 3.1
Using /dev/mapper/vg­var
Welcome to GNU Parted! Type ‘help’ to view a list of commands.
.
(parted) help
align­check TYPE N                       check partition N for TYPE(min|opt) alignment
help [COMMAND]                          print general help, or help on COMMAND
mklabel,mktable LABEL­TYPE               create a new disklabel (partition table)
mkpart PART­TYPE [FS­TYPE] START END      make a partition
name NUMBER NAME                        name partition NUMBER as NAME
print [devices|free|list,all|NUMBER]    display the partition table, available devices, free space,
all partitions, or a particular partition
quit                                    exit program
rescue START END                        rescue a lost partition near START and END
rm NUMBER                               delete partition NUMBER
select DEVICE                           choose the device to edit
disk_set FLAG STATE                     change the FLAG on selected device
disk_toggle [FLAG]                      toggle the state of FLAG on selected device
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
.
(parted) print devices
/dev/sda (1000GB)
/dev/sdb (1000GB)
/dev/md127 (44.3GB)
.
(parted) print list
Model: ATA HGST HTS541010A9 (scsi)
Disk /dev/sda: 1000GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:
.
Number   Start   End    Size    File system      Name                 Flags
1      1049kB 500MB   499MB    fat16            EFI System Partition boot, esp
2       500MB 4596MB 4096MB    linux­swap(v1)
3      4596MB 49.0GB 44.4GB                                          raid
4      49.0GB 1000GB  951GB                     primary
.
Model: ATA TOSHIBA MQ01ABD1 (scsi)
Disk /dev/sdb: 1000GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:
.
Number   Start   End    Size    File system      Name                 Flags
1      1049kB 4596MB 4595MB    ext4
2      4596MB 49.0GB 44.4GB                                          raid
3      49.0GB 1000GB  951GB                     primary
.
Error: /dev/md127: unrecognised disk label
Model: Linux Software RAID Array (md)
Disk /dev/md127: 44.3GB
Sector size (logical/physical): 512B/4096B
Partition Table: unknown
Disk Flags:

To view the physical partitions of a disk we use:

root:~> parted /dev/sda print
Model: ATA HGST HTS541010A9 (scsi)
Disk /dev/sda: 1000GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:
.
Number   Start   End    Size    File system      Name                 Flags
1      1049kB 500MB   499MB    fat16            EFI System Partition boot, esp
2       500MB 4596MB 4096MB    linux­swap(v1)
3      4596MB 49.0GB 44.4GB                                          raid
4      49.0GB 1000GB  951GB                     primary

To view all partitions of all disks we append list or all to the command above and we should get the same output as we do running “print list” from inside the parted prompt.

Now let’s create a couple of partitions on a new unformatted disk:

root:~> parted /dev/sda
GNU Parted 3.1
Using /dev/sda
Welcome to GNU Parted! Type ‘help’ to view a list of commands.
.
(parted) print
Error: /dev/sda: unrecognised disk label
Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sda: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags:
.
(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, sun, loop
.
(parted) mklabel gpt
.
(parted) print
Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sda: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
.
Number Start End Size File system Name Flags
.
(parted) help mkpart
mkpart PART­TYPE [FS­TYPE] START END      make a partition
.
PART­TYPE is one of: primary, logical, extended
.
FS­TYPE is one of: btrfs, nilfs2, ext4, ext3, ext2, fat32, fat16, hfsx, hfs+, hfs, jfs,
swsusp, linux­swap(v1), linux­swap(v0), ntfs, reiserfs, hp­ufs, sun­ufs, xfs, apfs2, apfs1,
asfs, amufs5, amufs4, amufs3, amufs2, amufs1, amufs0, amufs, affs7, affs6, affs5, affs4, affs3,
affs2, affs1, affs0, linux­swap, linux­swap(new), linux­swap(old)
.
START and END are disk locations, such as 4GB or 10%. Negative values count from the end
of the disk. For example, ­1s specifies exactly the last sector.
.
‘mkpart’ makes a partition without creating a new file system on the partition. FS­TYPE
may be specified to set an appropriate partition ID.
.
(parted) help unit
unit UNIT set the default unit to UNIT
.
UNIT is one of: s, B, kB, MB, GB, TB, compact, cyl, chs, %, kiB, MiB, GiB, TiB
.
(parted) unit s
.
(parted) print devices
/dev/sda (2097152s)
/dev/mapper/vg­home (7946240s)
/dev/mapper/vg­var (8388608s)
/dev/mapper/vgoracle­lvoracle (12574720s)
/dev/mapper/vg­root (16384000s)
/dev/vda (37748736s)
/dev/vdb (12582912s)
.
(parted) mkpart primary 1s 2097152s
Error: The location 2097152s is outside of the device /dev/sda.
.
(parted) mkpart primary 1s 2097151s
Warning: You requested a partition from 1s to 2097151s (sectors 1..2097151).
The closest location we can manage is 34s to 2097118s (sectors 34..2097118).
Is this still acceptable to you?
Yes/No? yes
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? Ignore
.
(parted) print
Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sda: 2097152s
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
.
Number   Start         End       Size    File system    Name      Flags
1         34s    2097118s   2097085s                   primary

We have now 1 partition comprising the whole disk but want to delete it and create 2 smaller partitions instead:

(parted) rm 1
(parted) print
Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sda: 2097152s
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
.
Number   Start   End    Size  File system   Name   Flags
.
(parted) unit mb
.
(parted) print devices
/dev/sda (1074MB)
/dev/mapper/vg­home (4068MB)
/dev/mapper/vg­var (4295MB)
/dev/mapper/vgoracle­lvoracle (6438MB)
/dev/mapper/vg­root (8389MB)
/dev/vda (19327MB)
/dev/vdb (6442MB)
.
(parted) mkpart primary 1m 512m
.
(parted) print
Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sda: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
.
Number  Start  End    Size  File system   Name     Flags
1    1.05MB    512MB 511MB                primary
.
(parted) mkpart primary 513m 1074m
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? Ignore
.
(parted) print
Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sda: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
.
Number  Start      End    Size  File system   Name     Flags
1      1.05MB    512MB   511MB                primary
2       513MB   1074MB   561MB                primary

There are a series of flags that are used to indicate the intended use of a partition: boot, root, lvm, raid, swap, etc. Usually these flags should not be manually modified but if there is a need to do that, we can with the toggle command:

(parted) help toggle
  toggle [NUMBER [FLAG]]          toggle the state of FLAG on partition NUMBER
.
NUMBER is the partition number used by Linux. On MS­DOS disk labels, the primary
partitions number from 1 to 4, logical partitions from 5 onwards.
.
FLAG is one of: boot, root, swap, hidden, raid, lvm, lba, hp­service, palo, prep,
msftres, bios_grub, atvrecv, diag, legacy_boot
.
(parted) toggle 1 lvm      → switch flag lvm to on in partition 1
.
(parted) print
Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sda: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
.
Number  Start      End    Size  File system   Name     Flags
1      1.05MB    512MB   511MB                primary   lvm
2       513MB   1074MB   561MB                primary
.
(parted) toggle 1 lvm      → switch flag lvm to off in partition 1
.
(parted) print
Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sda: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
.
Number  Start      End    Size  File system   Name     Flags
1      1.05MB    512MB   511MB                primary
2       513MB   1074MB   561MB                primary

If at some point we need to format many disks with the same partitioning scheme, we can do it much faster as follows:

root:~> parted /dev/sdb print
Error: /dev/sdb: unrecognised disk label
Model: QEMU QEMU HARDDISK (scsi)
Disk /dev/sdb: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags:
.
root:~> sgdisk -­R /dev/sdb /dev/sda
The operation has completed successfully.
.
root:~> sgdisk ­-G /dev/sdb
The operation has completed successfully.
.
root:~> parted /dev/sdb
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type ‘help’ to view a list of commands.
.
(parted) print
Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sda: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
.
Number  Start      End    Size  File system   Name     Flags
1      1.05MB    512MB   511MB                primary  lvm
2       513MB   1074MB   561MB                primary

The 1st sgdisk command copies the partition table of sda onto sdb. The 2nd command is optional but highly recommended and it randomises the UUIDs of both disk and its partitions.


<< Storage main          Filesystem ext4 >>