Create Partition and Filesystem - Ubuntu Server
Create a partition on new drive\
Quick directions to remember how to create a new partition and filesystem on Ubuntu server using fdisk and mkfs. Also using tune2fs to tune the volume.
List block devices on server
lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 111.8G 0 disk
└─sda1 8:1 0 111.8G 0 part /
sdb 8:16 0 1.8T 0 disk
sdc 8:32 0 931.5G 0 disk
sr0 11:0 1 1024M 0 rom
View any existing partitions
fdisk -l
Disk /dev/sda: 111.8 GiB, 120034123776 bytes, 234441648 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: 0x4c66206f
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 234440703 234438656 111.8G 83 Linux
Disk /dev/sdb: 1.8 TiB, 1997998653440 bytes, 3902341120 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk /dev/sdc: 931.5 GiB, 1000204886016 bytes, 1953525168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Select device to be partitioned
sudo fdisk /dev/sdb
Context of the command prompt is now within fdisk
. Within fdisk
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.
<span style="color:green">Welcome to fdisk (util-linux 2.31.1).</span>
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 0x57fab6d8.
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-3902341119, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-3902341119, default 3902341119):
Created a new partition 1 of type 'Linux' and of size 1.8 TiB.
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
You get kicked out back to user prompt
Display the new partition table
fdisk -l
Disk /dev/sda: 111.8 GiB, 120034123776 bytes, 234441648 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: 0x4c66206f
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 234440703 234438656 111.8G 83 Linux
Disk /dev/sdb: 1.8 TiB, 1997998653440 bytes, 3902341120 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0x57fab6d8
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 3902341119 3902339072 1.8T 83 Linux
Disk /dev/sdc: 931.5 GiB, 1000204886016 bytes, 1953525168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Format new partition filesystem as ext4
mkfs -t ext4 /dev/sdb1
- May take a while depending on size of disk
mke2fs 1.44.1 (24-Mar-2018)
Creating filesystem with 487792384 4k blocks and 121954304 inodes
Filesystem UUID: 3aa11831-5a8c-4881-bb83-ba8c7a875bad
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 (262144 blocks): done
Writing superblocks and filesystem accounting information: done
Create directory to permanently mount volume
sudo mkdir /vm
List directories
ls /
bin boot dev etc home initrd.img initrd.img.old lib lib64 lost+found media mnt opt proc root run sbin snap srv swapfile sys tmp usr var vm vmlinuz vmlinuz.old
Get UUID of partition created
sudo blkid
. This way, the mounted volume is independent of the order the underlying disk is connected in the motherboard. Hot swapping for fun.
/dev/sda1: UUID="f2ae8110-9bf6-4250-acc5-0fcb48bade42" TYPE="ext4" PARTUUID="4c66206f-01"
/dev/sdb1: UUID="3aa11831-5a8c-4881-bb83-ba8c7a875bad" TYPE="ext4" PARTUUID="57fab6d8-01"
- Some tuning courtesy askUbuntu
Remove reserved blocks (i.e. set to 0%), since this drive is just for user data:
tune2fs -m 0 /dev/sdb1
Server is on UPS, Set write-back so apps don't wait for actual disk writes:
tune2fs -o journal_data_writeback /dev/sdb1
List current values of the parameters
tune2fs -l /dev/sdb1
tune2fs 1.44.1 (24-Mar-2018)
Filesystem volume name: <none>
Last mounted on: <not available>
Filesystem UUID: 3aa11831-5a8c-4881-bb83-ba8c7a875bad
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
Filesystem features: has_journal ext_attr resize_inode dir_index filetype extent 64bit flex_bg sparse_super large_file huge_file dir_nlink extra_isize metadata_csum
Filesystem flags: signed_directory_hash
Default mount options: journal_data_writeback user_xattr acl
Filesystem state: clean
Errors behavior: Continue
Filesystem OS type: Linux
Inode count: 121954304
Block count: 487792384
Reserved block count: 0
Free blocks: 479854413
Free inodes: 121954293
First block: 0
Block size: 4096
Fragment size: 4096
Group descriptor size: 64
Reserved GDT blocks: 1024
Blocks per group: 32768
Fragments per group: 32768
Inodes per group: 8192
Inode blocks per group: 512
Flex block group size: 16
Filesystem created: Tue Apr 30 21:59:22 2019
Last mount time: n/a
Last write time: Tue Apr 30 22:07:02 2019
Mount count: 0
Maximum mount count: -1
Last checked: Tue Apr 30 21:59:22 2019
Check interval: 0 (<none>)
Lifetime writes: 1032 MB
Reserved blocks uid: 0 (user root)
Reserved blocks gid: 0 (group root)
First inode: 11
Inode size: 256
Required extra isize: 32
Desired extra isize: 32
Journal inode: 8
Default directory hash: half_md4
Directory Hash Seed: 7ed1c9f2-c1c9-4f32-9a9f-309a2e792207
Journal backup: inode blocks
Checksum type: crc32c
Checksum: 0x4cabd79d
Edit fstab to mount at boot up. Also set write-back policy
sudo vim /etc/fstab
Add the following line into fstab:
UUID=3aa11831-5a8c-4881-bb83-ba8c7a875bad /vm ext4 noatime,nodiratime,data=writeback,barrier=0,nobh,errors=remount-ro 0 1
Further Reading
How do I create and tune an ext4 partition from the command-line?