Recently, I showed you how to add a new hard drive to a VM in Proxmox VE. In this tutorial, I want to share the steps you need to take to prepare your new hard drive for storing data.
Confirm that the new hard drive is attached to the VM
Log in to your Linux VM and confirm that the hard drive is attached to it.
For this purpose, you can use the command lsblk. You can find the description from the lsblk man page below.
petru@ubuntu2204:~$ man lsblk
LSBLK(8) System Administration LSBLK(8)
NAME
lsblk - list block devices
SYNOPSIS
lsblk [options] [device...]
DESCRIPTION
lsblk lists information about all available or the specified block devices. The lsblk command reads the sysfs filesystem and udev db to gather information. If the udev db is not available or lsblk is compiled without udev support, then it tries to read LABELs, UUIDs and filesystem types from the block device. In this case root permissions are necessary.
The command prints all block devices (except RAM disks) in a tree-like format by default. Use lsblk --help to get a list of all available columns.
Run a similar command on your VM and look for the new hard drive.
petru@ubuntu2204:~$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 63.5M 1 loop /snap/core20/2015
loop1 7:1 0 4K 1 loop /snap/bare/5
loop2 7:2 0 74.1M 1 loop /snap/core22/1033
loop3 7:3 0 240.3M 1 loop /snap/firefox/3358
loop4 7:4 0 349.7M 1 loop /snap/gnome-3-38-2004/143
loop5 7:5 0 485.5M 1 loop /snap/gnome-42-2204/120
loop6 7:6 0 91.7M 1 loop /snap/gtk-common-themes/1535
loop7 7:7 0 40.9M 1 loop /snap/snapd/20290
loop8 7:8 0 452K 1 loop /snap/snapd-desktop-integration/83
loop9 7:9 0 73.9M 1 loop /snap/core22/864
loop10 7:10 0 63.9M 1 loop /snap/core20/2105
loop11 7:11 0 246M 1 loop /snap/firefox/3626
loop12 7:12 0 40.4M 1 loop /snap/snapd/20671
loop13 7:13 0 12.3M 1 loop /snap/snap-store/959
loop14 7:14 0 497M 1 loop /snap/gnome-42-2204/141
sda 8:0 0 50G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 513M 0 part /boot/efi
└─sda3 8:3 0 49.5G 0 part /var/snap/firefox/common/host-hunspell
/
sdb 8:16 0 32G 0 disk
sr0 11:0 1 4.7G 0 rom
petru@ubuntu2204:~$
My VM contains two block devices: /dev/sda and /dev/sdb. The /dev/sdb is the new hard drive that I have added in the previous post.
Create a new partition table on your hard drive
The next step is to create a partition table on your hard drive. For this, you can use the command fdisk. You can find the description from the fdisk man page below.
petru@ubuntu2204:~$ man fdisk
FDISK(8) System Administration FDISK(8)
NAME
fdisk - manipulate disk partition table
SYNOPSIS
fdisk [options] device
fdisk -l [device...]
DESCRIPTION
fdisk is a dialog-driven program for creation and manipulation of partition tables. It understands GPT, MBR, Sun, SGI and BSD partition tables.
Block devices can be divided into one or more logical disks called partitions. This division is recorded in the partition table, usually found in sector 0 of the disk. (In the BSD world one talks about `disk slices' and a `disklabel'.)
All partitioning is driven by device I/O limits (the topology) by default. fdisk is able to optimize the disk layout for a 4K-sector size and use an alignment offset on modern devices for MBR and GPT. It is always a good idea to follow fdisk's defaults as the default values (e.g., first and last partition sectors) and partition sizes specified by the +/-<size>{M,G,...} notation are always aligned according to the device properties.
Run a similar command on your VM.
petru@ubuntu2204:~$ fdisk /dev/sdb
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.
fdisk: cannot open /dev/sdb: Permission denied
petru@ubuntu2204:~$ sudo !!
sudo fdisk /dev/sdb
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 recognised partition table.
Created a new DOS disklabel with disk identifier 0xbfcb1369.
Command (m for help): m
Help:
DOS (MBR)
a toggle a bootable flag
b edit nested BSD disklabel
c toggle the dos compatibility flag
Generic
d delete a partition
F list free unpartitioned space
l list known partition types
n add a new partition
p print the partition table
t change a partition type
v verify the partition table
i print information about a partition
Misc
m print this menu
u change display/entry units
x extra functionality (experts only)
Script
I load disk layout from sfdisk script file
O dump disk layout to sfdisk script file
Save & Exit
w write table to disk and exit
q quit without saving changes
Create a new label
g create a new empty GPT partition table
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p):
Using default response p.
Partition number (1-4, default 1):
First sector (2048-67108863, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-67108863, default 67108863):
Created a new partition 1 of type 'Linux' and of size 32 GiB.
Partition #1 contains a ext4 signature.
Do you want to remove the signature? [Y]es/[N]o: y
The signature will be removed by a write command.
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Synching disks.
petru@ubuntu2204:~$
As mentioned in the man page for the fdisk command, you can create an MBR partition table or a GPT partition table. In the example above, I created an MBR partition table. To read more about the MBR, please check this wiki page.
If you want to use a GPT partition table, you can use the same fdisk command. Alternatively, you can use the gdisk command.
petru@ubuntu2204:~$ man gdisk
GDISK(8) GPT fdisk Manual GDISK(8)
NAME
gdisk - Interactive GUID partition table (GPT) manipulator
SYNOPSIS
gdisk [ -l ] device
DESCRIPTION
GPT fdisk (aka gdisk) is a text-mode menu-driven program for creation and manipulation of partition tables. It will automatically convert an old-style Master Boot Record (MBR) partition table or BSD disklabel stored without an MBR carrier partition to the newer Globally Unique Identifier (GUID) Partition Table (GPT) format, or will load a GUID partition table. When used with the -l command-line option, the program displays the current partition table and then exits.
GPT fdisk operates mainly on the GPT headers and partition tables; however, it can and will generate a fresh protective MBR, when required. (Any boot loader code in the protective MBR will not be disturbed.) If you've created an unusual protective MBR, such as a hybrid MBR created by gptsync or gdisk's own hybrid MBR creation feature, this should not be disturbed by most ordinary actions. Some advanced data recovery options require you to understand the distinctions between the main and backup data, as well as between the GPT headers and the partition tables. For information on MBR vs.GPT, as well as GPT terminology and structure, see the extended gdisk documentation at http://www.rodsbooks.com/gdisk/ or consult Wikipedia.
You can find an example below.
petru@ubuntu2204:~$ sudo gdisk /dev/sdb
GPT fdisk (gdisk) version 1.0.8
Partition table scan:
MBR: MBR only
BSD: not present
APM: not present
GPT: not present
***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format
in memory. THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by
typing 'q' if you don't want to convert your MBR partitions
to GPT format!
***************************************************************
Warning! Secondary partition table overlaps the last partition by
33 blocks!
You will need to delete this partition or resize it in another utility.
Command (? for help): ?
b back up GPT data to a file
c change a partition's name
d delete a partition
i show detailed information on a partition
l list known partition types
n add a new partition
o create a new empty GUID partition table (GPT)
p print the partition table
q quit without saving changes
r recovery and transformation options (experts only)
s sort partitions
t change a partition's type code
v verify disk
w write table to disk and exit
x extra functionality (experts only)
? print this menu
Command (? for help): d
Using 1
Command (? for help): n
Partition number (1-128, default 1):
First sector (34-67108830, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-67108830, default = 67108830) or {+-}size{KMGTP}:
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'
Command (? for help): w
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.
petru@ubuntu2204:~$
To read more about the GPT, please check this wiki page.
Confirm that the partition table was created
Next, you need to confirm that the partition table was created successfully. Once again, you can use the lsblk command.
petru@ubuntu2204:~$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 63.5M 1 loop /snap/core20/2015
loop1 7:1 0 4K 1 loop /snap/bare/5
loop2 7:2 0 74.1M 1 loop /snap/core22/1033
loop3 7:3 0 240.3M 1 loop /snap/firefox/3358
loop4 7:4 0 349.7M 1 loop /snap/gnome-3-38-2004/143
loop5 7:5 0 485.5M 1 loop /snap/gnome-42-2204/120
loop6 7:6 0 91.7M 1 loop /snap/gtk-common-themes/1535
loop7 7:7 0 40.9M 1 loop /snap/snapd/20290
loop8 7:8 0 452K 1 loop /snap/snapd-desktop-integration/83
loop9 7:9 0 73.9M 1 loop /snap/core22/864
loop10 7:10 0 63.9M 1 loop /snap/core20/2105
loop11 7:11 0 246M 1 loop /snap/firefox/3626
loop12 7:12 0 40.4M 1 loop /snap/snapd/20671
loop13 7:13 0 12.3M 1 loop /snap/snap-store/959
loop14 7:14 0 497M 1 loop /snap/gnome-42-2204/141
sda 8:0 0 50G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 513M 0 part /boot/efi
└─sda3 8:3 0 49.5G 0 part /var/snap/firefox/common/host-hunspell
/
sdb 8:16 0 32G 0 disk
└─sdb1 8:17 0 32G 0 part
sr0 11:0 1 4.7G 0 rom
petru@ubuntu2204:~$
As you can see in the above screenshot, a new partition was created, /dev/sdb1.
Create a filesystem on the new partition
Before you can use the new partition, you need to add a filesystem to it. You can use the command mkfs for this task.
petru@ubuntu2204:~$ man mkfs
MKFS(8) System Administration MKFS(8)
NAME
mkfs - build a Linux filesystem
SYNOPSIS
mkfs [options] [-t type] [fs-options] device [size]
DESCRIPTION
This mkfs frontend is deprecated in favour of filesystem specific mkfs.<type> utils.
mkfs is used to build a Linux filesystem on a device, usually a hard disk partition. The device argument is either the device name (e.g., /dev/hda1, /dev/sdb2), or a regular file that shall contain the filesystem. The size argument is the number of blocks to be used for the filesystem.
The exit status returned by mkfs is 0 on success and 1 on failure.
In actuality, mkfs is simply a front-end for the various filesystem builders (mkfs.fstype) available under Linux. The filesystem-specific builder is searched for via your PATH environment setting only.
Please see the filesystem-specific builder manual pages for further details.
OPTIONS
-t, --type type
Specify the type of filesystem to be built. If not specified, the default filesystem type
(currently ext2) is used.
I will use the mkfs.ext4 command to create a new ext4 filesystem on /dev/sdb1 partition. You can choose a different filesystem if you want. To create an XFS filesystem, you can use the command mkfs.xfs.
petru@ubuntu2204:~$ sudo mkfs.ext4 /dev/sdb1
mke2fs 1.46.5 (30-Dec-2021)
Discarding device blocks: done
Creating filesystem with 8388347 4k blocks and 2097152 inodes
Filesystem UUID: 6085ac97-6430-44eb-9dd7-ec01e5bdf75d
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
petru@ubuntu2204:~$
Mount the new filesystem
The last step is to mount the new filesystem to the root tree. You will use the mount command for this purpose.
petru@ubuntu2204:~$ man mount
MOUNT(8) System Administration MOUNT(8)
NAME
mount - mount a filesystem
SYNOPSIS
mount [-h|-V]
mount [-l] [-t fstype]
mount -a [-fFnrsvw] [-t fstype] [-O optlist]
mount [-fnrsvw] [-o options] device|mountpoint
mount [-fnrsvw] [-t fstype] [-o options] device mountpoint
mount --bind|--rbind|--move olddir newdir
mount --make-[shared|slave|private|unbindable|rshared|rslave|rprivate|runbindable] mountpoint
DESCRIPTION
All files accessible in a Unix system are arranged in one big tree, the file hierarchy, rooted at /. These files can be spread out over several devices. The mount command serves to attach the filesystem found on some device to the big file tree. Conversely, the umount(8) command will detach it again. The filesystem is used to control how data is stored on the device or provided in a virtual way by network or other services.
I will create a new directory in my home directory and I will mount the new filesystem in this directory.
petru@ubuntu2204:~$ mkdir newhard
petru@ubuntu2204:~$ sudo mount -t ext4 /dev/sdb1 newhard
petru@ubuntu2204:~$
To confirm that the new filesystem is attached to the root tree, you can use the df command.
petru@ubuntu2204:~$ df -hT
Filesystem Type Size Used Avail Use% Mounted on
tmpfs tmpfs 391M 1.5M 390M 1% /run
/dev/sda3 ext4 49G 16G 31G 35% /
tmpfs tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/sda2 vfat 512M 6.1M 506M 2% /boot/efi
tmpfs tmpfs 391M 76K 391M 1% /run/user/128
tmpfs tmpfs 391M 68K 391M 1% /run/user/1000
/dev/sdb1 ext4 32G 24K 30G 1% /home/petru/newhard
petru@ubuntu2204:~$
You have the confirmation that the filesystem /dev/sdb1 is mounted on /home/petru/newhard.
Use the new hard drive for storing new data
Let’s see if we can use this new hard drive by creating a new file and writing some lines into it.
petru@ubuntu2204:~$ echo "This is a test file stored on the new hard disk"| sudo tee newhard/newfile.txt
This is a test file stored on the new hard disk
petru@ubuntu2204:~$ cat newhard/newfile.txt
This is a test file stored on the new hard disk
petru@ubuntu2204:~$
Unmount the new filesystem
To detach the new filesystem from the root tree, you will use the umount command. When using the mount command you need to bear in mind that the filesystem is attached to the root tree until you unmount it or until the VM is rebooted.
petru@ubuntu2204:~$ man umount
UMOUNT(8) System Administration UMOUNT(8)
NAME
umount - unmount filesystems
SYNOPSIS
umount -a [-dflnrv] [-t fstype] [-O option...]
umount [-dflnrv] {directory|device}
umount -h|-V
DESCRIPTION
The umount command detaches the mentioned filesystem(s) from the file hierarchy. A filesystem is specified by giving the directory where it has been mounted. Giving the special device on which the filesystem lives may also work, but is obsolete, mainly because it will fail in case this device was mounted on more than one directory.
Note that a filesystem cannot be unmounted when it is 'busy' - for example, when there are open files on it, or when some process has its working directory there, or when a swap file on it is in use. The offending process could even be umount itself - it opens libc, and libc in its turn may open for example locale files. A lazy unmount avoids this problem, but it may introduce other issues. See --lazy description below.
Here is an example command which shows you how to unmount the new filesystem from the root tree.
petru@ubuntu2204:~$ sudo umount /dev/sdb1
petru@ubuntu2204:~$ ls -l /home/petru/newhard
total 0
petru@ubuntu2204:~$ cat newhard/newfile.txt
cat: newhard/newfile.txt: No such file or directory
petru@ubuntu2204:~$ df -hT
Filesystem Type Size Used Avail Use% Mounted on
tmpfs tmpfs 391M 1.5M 390M 1% /run
/dev/sda3 ext4 49G 16G 31G 35% /
tmpfs tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/sda2 vfat 512M 6.1M 506M 2% /boot/efi
tmpfs tmpfs 391M 76K 391M 1% /run/user/128
tmpfs tmpfs 391M 68K 391M 1% /run/user/1000
petru@ubuntu2204:~$
Mount the filesystem permanently to the root tree
To attach the filesystem permanently to the root tree, you need to use the fstab file. You can find more information about it below.
petru@ubuntu2204:~$ man fstab
FSTAB(5) File formats FSTAB(5)
NAME
fstab - static information about the filesystems
SYNOPSIS
/etc/fstab
DESCRIPTION
The file fstab contains descriptive information about the filesystems the system can mount. fstab is only read by programs, and not written; it is the duty of the system administrator to properly create and maintain this file. The order of records in fstab is important because fsck(8), mount(8), and umount(8) sequentially iterate through fstab doing their thing.
Each filesystem is described on a separate line. Fields on each line are separated by tabs or spaces. Lines starting with '#' are comments. Blank lines are ignored.
The following is a typical example of an fstab entry:
LABEL=t-home2 /home ext4 defaults,auto_da_alloc 0 2
You need to add a line similar to the one below. You will need to adjust it according to your environment. To find the UUID for your partition, you will need to use the blkid command.
petru@ubuntu2204:~$ sudo blkid /dev/sdb1
/dev/sdb1: UUID="6085ac97-6430-44eb-9dd7-ec01e5bdf75d" BLOCK_SIZE="4096" TYPE="ext4" PARTLABEL="Linux filesystem" PARTUUID="cc797dce-f26a-4532-8368-fe2d7b2fb720"
petru@ubuntu2204:~$
petru@ubuntu2204:~$ cat /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/sda3 during installation
UUID=30408cdb-e4e5-4002-aab8-b714579584d2 / ext4 errors=remount-ro 0 1
# /boot/efi was on /dev/sda2 during installation
UUID=35DA-2259 /boot/efi vfat umask=0077 0 1
/swapfile none swap sw 0 0
# Disk added by Petru
UUID=6085ac97-6430-44eb-9dd7-ec01e5bdf75d /home/petru/newhard ext4 defaults 0 0
petru@ubuntu2204:~$ sudo mount -a
petru@ubuntu2204:~$ df -hT
Filesystem Type Size Used Avail Use% Mounted on
tmpfs tmpfs 391M 1.5M 390M 1% /run
/dev/sda3 ext4 49G 16G 31G 35% /
tmpfs tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/sda2 vfat 512M 6.1M 506M 2% /boot/efi
tmpfs tmpfs 391M 76K 391M 1% /run/user/128
tmpfs tmpfs 391M 68K 391M 1% /run/user/1000
/dev/sdb1 ext4 32G 28K 30G 1% /home/petru/newhard
petru@ubuntu2204:~$
Confirm that the created file is still present on the new hard drive.
petru@ubuntu2204:~$ ls -l /home/petru/newhard/
total 20
drwx------ 2 root root 16384 Jan 14 08:19 lost+found
-rw-r--r-- 1 root root 48 Jan 14 08:36 newfile.txt
petru@ubuntu2204:~$ ls -l /home/petru/newhard/newfile.txt
-rw-r--r-- 1 root root 48 Jan 14 08:36 /home/petru/newhard/newfile.txt
petru@ubuntu2204:~$ cat /home/petru/newhard/newfile.txt
This is a test file stored on the new hard disk
petru@ubuntu2204:~$
That’s it! You have learned how to create a new partition on your hard drive, how to add a new filesystem to it and how to attach the new filesystem to the root tree. If you found this blog post helpful, please like and subscribe for more Linux tutorials. Thank you for reading it!