Step-by-step attach and mount Block Volume to OCI Linux Server

This article will teach us how to attach and mount block volumes on an OCI Linux server. In the previous article, we learned how to spin up an OCI Linux server learned. By default, when we spin up an instance, it will have a boot volume where the operating system resides. When we need space, we can create a block volume and attach it to the instance. We can extend the existing boot volume when we need more space, but the ideal way is to create a block volume and use it based on needs. Block or boot volume can be extended from 50 GB to 32 TB.

Steps to create Block Volume

  • Select Block Volume by selecting Storage > Block Storage.
  • Click on Create a block volume which opens a form to fill in the details.
    • Give a name to the block volume
    • Ensure that the server’s compartment and availability domain match.
    • Volume size and performance going with default options.

Steps to attach block volume to the instance

  • Go to compute -> Instances
  • Click on the instance to which we want to attach the block volume
  • Click on the Attached block volumes and Attach the block volume
    • Select Volume
    • Choose a device path
    • Other options leave the default
  • Run ISCSI commands after logging in to the instance

Steps to mount a block volume

  • Log in to the instance and run ISCSI Commands.
  • Format the file system to ext4 or xfs
  • Mount the volume
  • Make entry of volume to fstab

Log in to the instance and run ISCSI Commands.

To run ISCSI commands, we must log into the instance. The Attached Block Volume section of the Instance information page, as shown below, contains ISCSI instructions.

Execute the following ISCSI commands on the server:

ubuntu@w3devops-test1:~$ sudo iscsiadm -m node -o new -T iqn.2015-12.com.oracleiaas:359ab4b1-b5b2-456d-b9f9-74066b2a7d73 -p 169.254.2.3:3260
sudo iscsiadm -m node -o update -T iqn.2015-12.com.oracleiaas:359ab4b1-b5b2-456d-b9f9-74066b2a7d73 -n node.startup -v automatic
sudo iscsiadm -m node -T iqn.2015-12.com.oracleiaas:359ab4b1-b5b2-456d-b9f9-74066b2a7d73 -p 169.254.2.3:3260 -l
New iSCSI node [tcp:[hw=,ip=,net_if=,iscsi_if=default] 169.254.2.3,3260,-1 iqn.2015-12.com.oracleiaas:359ab4b1-b5b2-456d-b9f9-74066b2a7d73] added
Logging in to [iface: default, target: iqn.2015-12.com.oracleiaas:359ab4b1-b5b2-456d-b9f9-74066b2a7d73, portal: 169.254.2.3,3260]
Login to [iface: default, target: iqn.2015-12.com.oracleiaas:359ab4b1-b5b2-456d-b9f9-74066b2a7d73, portal: 169.254.2.3,3260] successful.

By using the command listed below, we can check to see if the node was successfully added.

sudo iscsiadm --mode node

Output from the command above

ubuntu@w3devops-test1:~$ sudo iscsiadm --mode node
169.254.2.3:3260,4294967295 iqn.2015-12.com.oracleiaas:359ab4b1-b5b2-456d-b9f9-74066b2a7d73
169.254.2.2:3260,4294967295 iqn.2015-12.com.oracleiaas:4c6e6bbf-a4c0-4056-97cc-99d964a26e51

Using the lsblk command, we can check the available block devices as shown below.

ubuntu@w3devops-test1:~$ lsblk
NAME    MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
loop0     7:0    0  55.6M  1 loop /snap/core18/2620
loop1     7:1    0  55.6M  1 loop /snap/core18/2632
loop2     7:2    0  50.6M  1 loop /snap/oracle-cloud-agent/46
loop3     7:3    0  63.2M  1 loop /snap/core20/1695
loop5     7:5    0 136.6M  1 loop /snap/lxd/23983
loop7     7:7    0  49.6M  1 loop /snap/snapd/17883
sda       8:0    0  46.6G  0 disk
โ”œโ”€sda1    8:1    0  46.5G  0 part /
โ”œโ”€sda14   8:14   0     4M  0 part
โ””โ”€sda15   8:15   0   106M  0 part /boot/efi
sdb       8:16   0    50G  0 disk /data1
sdc       8:32   0    50G  0 disk

The sdc block device is visible as being accessible. The sdcย block device must then be formatted and mounted to a mount point.

Format the file system to ext4 or xfs

Using the commands mentioned below, we can format the block device to ext4 or xfs.

sudo mkfs.ext4 /dev/<device-name>

or

sudo mkfs.xfs /dev/<device-name>

The formatting for the ext4 format is as follows.

ubuntu@w3devops-test1:~$ sudo mkfs.ext4 /dev/sdc
mke2fs 1.46.5 (30-Dec-2021)
Creating filesystem with 13107200 4k blocks and 3276800 inodes
Filesystem UUID: c60c1d78-8bfe-4a79-8143-506f7ff9bc3e
Superblock backups stored on blocks:
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
	4096000, 7962624, 11239424

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

Note: It should be noted that formatting a block device is only necessary once. We shouldn’t format the device if we plan to attach the same block volume to another instance or the same instance again.

How to mount a block volume to the Linux server

Make a directory first, then mount the volume using the’mount’ command below.


ubuntu@w3devops-test1:~$ sudo mkdir /data2
ubuntu@w3devops-test1:~$ sudo mount /dev/sdc /data2

Using lsblk or the df -h command, we may determine whether a block device is mounted successfully or not.

ubuntu@w3devops-test1:~$ lsblk
NAME    MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
loop0     7:0    0  55.6M  1 loop /snap/core18/2620
loop1     7:1    0  55.6M  1 loop /snap/core18/2632
loop2     7:2    0  50.6M  1 loop /snap/oracle-cloud-agent/46
loop3     7:3    0  63.2M  1 loop /snap/core20/1695
loop5     7:5    0 136.6M  1 loop /snap/lxd/23983
loop7     7:7    0  49.6M  1 loop /snap/snapd/17883
sda       8:0    0  46.6G  0 disk
โ”œโ”€sda1    8:1    0  46.5G  0 part /
โ”œโ”€sda14   8:14   0     4M  0 part
โ””โ”€sda15   8:15   0   106M  0 part /boot/efi
sdb       8:16   0    50G  0 disk /data1
sdc       8:32   0    50G  0 disk /data2

ubuntu@w3devops-test1:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
tmpfs            97M  1.1M   96M   2% /run
/dev/sda1        45G  3.7G   42G   9% /
tmpfs           483M     0  483M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/sda15      105M  5.3M  100M   5% /boot/efi
/dev/sdb         49G  104K   47G   1% /data1
tmpfs            97M  4.0K   97M   1% /run/user/1001
/dev/sdc         49G   24K   47G   1% /data2

We can observe that block device sdc is successfully mounted to the directory data2.

Using the ‘umount’ command listed below, we can choose to unmount the block device.

ubuntu@w3devops-test1:~$ sudo umount /data2

The mounted block device won’t be available if we restart the instance. The block device needs to be mounted once more. Therefore, in order to make it durable, we must add the block volume device information to the /etc/fstab files.

How to make block volume persistent or add block volume to fstab.

Consistent device paths are supported by Oracle Cloud Infrastructure for block volumes attached to suitable Linux-based instances. A device path that withstands instance reboots can be chosen when attaching a block volume to an instance.

To determine whether the instance is compatible with the consistent device path below

ubuntu@w3devops-test1:~$ ll /dev/oracleoci/oraclevd*
lrwxrwxrwx 1 root root 6 Nov 27 18:48 /dev/oracleoci/oraclevda -> ../sdb
lrwxrwxrwx 1 root root 7 Nov 27 18:48 /dev/oracleoci/oraclevda1 -> ../sda1
lrwxrwxrwx 1 root root 8 Nov 27 18:48 /dev/oracleoci/oraclevda14 -> ../sda14
lrwxrwxrwx 1 root root 8 Nov 27 18:48 /dev/oracleoci/oraclevda15 -> ../sda15
lrwxrwxrwx 1 root root 6 Dec  3 11:38 /dev/oracleoci/oraclevdb -> ../sdc

Make entry to /etc/fstab as below format

<Consistant device path> <directoy> <format type> defaults,_netdev,nofail 0 2

Example:

/dev/oracleoci/oraclevdb /data2 ext4 defaults,_netdev,nofail 0 2
ubuntu@w3devops-test1:~$ cat /etc/fstab | grep -v '#'
LABEL=cloudimg-rootfs	/	 ext4	discard,errors=remount-ro	0 1
LABEL=UEFI	/boot/efi	vfat	umask=0077	0 1

/dev/oracleoci/oraclevdb /data2 ext4 defaults,_netdev,nofail 0 2

Validate /etc/fstab entries using the below command

sudo mount -a

Restart the instance to check if the mount volume is mounted properly.

NOTE: When attaching a block volume, choose a consistant device path.

FAQ’s
OCI Block volume missing after reboot
What is a consistent device path?

Conclusion

We discovered how to attach and mount block volume to the OCi Linux server. Please comment on the post if you have any problems or questions after reading the paper. We will do our best to investigate and resolve the issue.

References:

Connecting to a Volume (oracle.com)

fstab Options for Block Volumes Using Consistent Device Paths (oracle.com)