Creating a new file system for an existing Linux virtual machine

This article provides steps to create a new file system for an existing Linux virtual machine.

Create a partition on new disk:

[root@ocm12c ~]# ls -l /dev/sd*
brw-rw----. 1 root disk 8,  0 Aug 22 13:00 /dev/sda
brw-rw----. 1 root disk 8,  1 Aug 22 13:00 /dev/sda1
brw-rw----. 1 root disk 8,  2 Aug 22 13:00 /dev/sda2
brw-rw----. 1 root disk 8, 16 Aug 22 13:00 /dev/sdb

[root@ocm12c ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x10f4c93d.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): p

Disk /dev/sdb: 12.9 GB, 12884901888 bytes
255 heads, 63 sectors/track, 1566 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x10f4c93d

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1566, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1566, default 1566):
Using default value 1566

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Create an ext3 file system on the new disk partition created on previous step.

[root@ocm12c ~]# mkfs -t ext3 /dev/sdb1
mke2fs 1.43-WIP (20-Jun-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
786432 inodes, 3144715 blocks
157235 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=3221225472
96 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

Run the fdisk command to verify the existence of the new disk partition you created:

[root@ocm12c ~]# df -h
Filesystem                        Size  Used Avail Use% Mounted on
/dev/mapper/vg_racprodn1-lv_root   45G   31G   13G  71% /
tmpfs                             3.9G   80K  3.9G   1% /dev/shm
/dev/sda1                         477M   55M  397M  13% /boot

[root@ocm12c ~]# fdisk -l

Disk /dev/sda: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0003dc55

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        6528    51915776   8e  Linux LVM

Disk /dev/sdb: 12.9 GB, 12884901888 bytes
255 heads, 63 sectors/track, 1566 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x10f4c93d

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1566    12578863+  83  Linux

Disk /dev/mapper/vg_racprodn1-lv_root: 48.9 GB, 48930750464 bytes
255 heads, 63 sectors/track, 5948 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/mapper/vg_racprodn1-lv_swap: 4227 MB, 4227858432 bytes
255 heads, 63 sectors/track, 514 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Run below command to create a mount point for the new disk partition:

[root@ocm12c ~]# mkdir /u02

Using vi editor, edit fstab and add this line to add the disk partition to /etc/fstab.

/dev/sdb1               /u02                    ext3    defaults        1 3
[root@ocm12c u02]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Wed Aug  5 23:47:34 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/vg_racprodn1-lv_root /                       ext4    defaults        1 1
UUID=31e76520-7f50-4592-9ced-d4f03d6d95cc /boot                   ext4    defaults        1 2
/dev/mapper/vg_racprodn1-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/sdb1               /u02                    ext3    defaults        1 3

Restart the Linux virtual machine and validate the new file system is now available.

[root@ocm12c ~]# init 6
[root@ocm12c ~]#
login as: root
[email protected]'s password:
Last login: Sat Aug 22 13:11:09 2020 from 192.168.56.1
[root@ocm12c ~]# df -h
Filesystem                        Size  Used Avail Use% Mounted on
/dev/mapper/vg_racprodn1-lv_root   45G   31G   13G  71% /
tmpfs                             3.9G   72K  3.9G   1% /dev/shm
/dev/sda1                         477M   55M  397M  13% /boot
/dev/sdb1                          12G  159M   12G   2% /u02