linux:shell_commands:fdisk

Create partitions using CLI on linux

First and foremost:

!! WARNING !!

These commands are EXAMPLES. DELETING partitions, MODIFYING and FORMATTING filesystems destroys data and/or may prevent your machine from booting. Make backups. Use at own risk. Try on a machine you don't mind losing all data on. caveat admin.

To quickly set up a drive up as a single ext4 partition… View detected devices of class “DISK”

lshw -C disk

View existing partition table(s)

fdisk -l

Edit the partition table for my chosen device (in this case, “sdx”)

fdisk /dev/sdx

Within FDISK, press:

d ...to delete the current partition
n ...to create a new partition
p ...to specify it as a PRIMARY partition
1 ...to set it as the 1ST primary partition
w ...to write the changes.

Display the new partition table:

fdisk -l

Format the new partition's filesystem as type ext4

mkfs -t ext4 /dev/sdx1

Create a new directory where the new drive will mount into:

mkdir /storage
mount /dev/sdx1 /storage

TUNING Remove reserved blocks (i.e. set to 0%), since this drive is just for user data

tune2fs -m 0 /dev/sdx1

Since server is on UPS, Set write-back so apps don't wait for actual disk writes

tune2fs -o journal_data_writeback /dev/sdx1

Mount at boot up using /etc/fstab and also set write-back policy

vi /etc/fstab

Find (or add) the relevant line in fstab for your drive. Parameters in fstab are separated by white space, for example the drive described above might appear as:

/dev/sdx1 /storage ext4 relatime,errors=remount-ro 0 1
  • The first parameter identifies the partition (either by /dev/ or a long UUID);
  • The second parameter is the path the partition will be mounted to;
  • Third is the filesystem type;
  • The fourth parameter contains the options;
  • Fifth is the dump schedule for backups; and,
  • The sixth parameter is pass-number (used to control fsck order).

Change the options (4th parameter) to:

noatime,nodiratime,data=writeback,barrier=0,nobh,errors=remount-ro

Reboot to check that everything went well. Remember these commands are destructive! Have backups and be careful!

Speedy
su@fs:~$ lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
loop0    7:0    0 55.4M  1 loop /snap/core18/2846
loop1    7:1    0 55.4M  1 loop /snap/core18/2855
loop2    7:2    0 44.4M  1 loop /snap/snapd/23545
loop3    7:3    0 91.9M  1 loop /snap/lxd/32662
loop4    7:4    0 63.7M  1 loop /snap/core20/2434
loop5    7:5    0 44.5M  1 loop /snap/snapd/23771
loop6    7:6    0 63.8M  1 loop /snap/core20/2496
loop7    7:7    0 91.9M  1 loop /snap/lxd/29619
sda      8:0    0   32G  0 disk
├─sda1   8:1    0  512M  0 part /boot/efi
└─sda2   8:2    0 31.5G  0 part /
sdb      8:16   0  3.5T  0 disk
└─sdb1   8:17   0  3.5T  0 part /shared
sdc      8:32   0    1T  0 disk
└─sdc1   8:33   0 1024G  0 part
sr0     11:0    1 1024M  0 rom
su@fs:~$ sudo mkfs -t ext4 /dev/sdc1
 
$ fdisk /dev/sdc
n
w
 
su@fs:~$ mkfs -t ext4 /dev/sdc1
$ blkid
/dev/sda1: UUID="A3E4-5689" TYPE="vfat" PARTUUID="e7003b9a-af04-4f3c-987e-f18603949308"
/dev/sda2: UUID="56b502b1-9df0-4cb7-a510-246957938839" TYPE="ext4" PARTUUID="7dcb6a1f-0925-4f00-9c22-4643f8e6988a"
/dev/sdb1: UUID="3b02d9b1-940e-46a0-9b48-b25a5f88e329" TYPE="ext4" PARTUUID="1df82f8c-22e8-4c4b-91cc-7921fcb8705f"
/dev/loop0: TYPE="squashfs"
/dev/loop1: TYPE="squashfs"
/dev/loop2: TYPE="squashfs"
/dev/loop3: TYPE="squashfs"
/dev/loop4: TYPE="squashfs"
/dev/loop5: TYPE="squashfs"
/dev/loop6: TYPE="squashfs"
/dev/loop7: TYPE="squashfs"
/dev/sdc1: UUID="6028d925-83c5-4fd3-9e88-3ec1c60f8073" TYPE="ext4" PARTUUID="7bd58fd1-01"
 
su@fs:~$ vim /etc/fstab
/dev/disk/by-uuid/6028d925-83c5-4fd3-9e88-3ec1c60f8073 /shared/pub ext4 defaults,noacl 0 0
 
su@fs:~$ mount -a
  • linux/shell_commands/fdisk.txt
  • Last modified: 2025/04/09 12:42
  • by tplecko