In Oracle Cloud Infrastructure (OCI) you can create compute instances, which is just another name for a virtual machine. They, by default, only have one volume, being the boot volume. Consider this the “hard drive” on which your operating system is installed.
It is good practice to add a data volume to the virtual machine, where you will install your applications and/or store your data. This is how you do that.
Step 1: Create a block volume
First in OCI create a block volume. To do this go to: Storage > Block Storage > Block Volumes
Click on the Create Block Volume button and give the volume a name. Choose the size and the backup policy you desire.
Step 2: Attach the block volume to the compute instance
Once the block volume is provisioned you will have to attach it to your compute instance. Go to your compute instance and under Resources go to Attached block volumes.
Click on Attach block volume and select the volume you want to attach.
Once it is attached you can click on the three dots at the end of the row and choose the iSCSI Commands & Information option. This will give you the commands you need to execute in the guest operating system to connect the block device to the system.
Step 3: Connect the block device
Now connect to the compute instance via SSH and execute the commands that you got from the iSCSI Commands & Information menu. This will connect the block device to the system.
sudo iscsiadm -m node -o new -T iqn.2015-12.com.oracleiaas:0159a8ef-bfed-4656-9d1e-c5205f987c90 -p 169.254.2.2:3260 sudo iscsiadm -m node -o update -T iqn.2015-12.com.oracleiaas:0159a8ef-bfed-4656-9d1e-c5205f987c90 -n node.startup -v automatic sudo iscsiadm -m node -T iqn.2015-12.com.oracleiaas:0159a8ef-bfed-4656-9d1e-c5205f987c90 -p 169.254.2.2:3260 -l
Step 4: Format the block device
Now that the block device is connected we will want to create a partition, format it and mount it in the file system. First lets see if we can see the device. Execute the lsblk command:
You will see, in this example, that the device is known in the system as /dev/sdb.
[[email protected] app]$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS sda 8:0 0 46.6G 0 disk ├─sda1 8:1 0 100M 0 part /boot/efi ├─sda2 8:2 0 1000M 0 part /boot ├─sda3 8:3 0 8.9G 0 part │ └─rocky-root 253:0 0 8.9G 0 lvm / ├─sda4 8:4 0 4M 0 part └─sda5 8:5 0 1M 0 part sdb 8:16 0 50G 0 disk
Now we can create a partition and format it. We will use the XFS filesystem in this example. The following commands create a partition using 100% of the available space and format the partition.
sudo parted /dev/sdb --script mklabel gpt mkpart xfspart xfs 0% 100% sudo mkfs.xfs /dev/sdb1 sudo partprobe /dev/sdb1
If you execute lsblk again you can see that you have now created a 50G partition called /dev/sdb1.
[[email protected] app]$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS sda 8:0 0 46.6G 0 disk ├─sda1 8:1 0 100M 0 part /boot/efi ├─sda2 8:2 0 1000M 0 part /boot ├─sda3 8:3 0 8.9G 0 part │ └─rocky-root 253:0 0 8.9G 0 lvm / ├─sda4 8:4 0 4M 0 part └─sda5 8:5 0 1M 0 part sdb 8:16 0 50G 0 disk └─sdb1 8:17 0 50G 0 part
Step 5: Mounting the partition
The partition is created, now it needs to be mounted in the filesystem. First create a folder where you want to mount the partition. This can be anywhere, in this example, we’ll mount the partition in /app.
First create a the directory /app:
cd / sudo mkdir app
Then mount the partition in the /app directory. This can be done with the following command.
sudo mount /dev/sdb1 /app
Now that the block volume is mounted you can actually start writing data to it.
Step 6: Mount on boot
There is one problem. After a reboot of the compute instance the volume is not automatically mounted. To fix this we have to add an entry to the /etc/fstab file.
First we need to figure out what the unique identifier of the volume is. We can get that ID with the blkid command. The ID you are looking for is the UUID.
[[email protected] app]$ sudo blkid [sudo] password for marcel: /dev/mapper/rocky-root: UUID="982e40b2-e0b3-4040-816e-e187413e166e" TYPE="xfs" /dev/sda3: UUID="TBkc7R-q0DS-KaIM-36g9-BZxZ-RwZ2-Irv0EI" TYPE="LVM2_member" PARTUUID="deeecd56-9c8f-476f-9c9c-ae974a1eeb51" /dev/sdb1: UUID="97a3919d-51ad-4fb9-a46c-1752ef9d76a9" TYPE="xfs" PARTLABEL="xfspart" PARTUUID="fe1e5a77-ef7c-4346-8f3c-29a04f6bb6a8" /dev/sda4: PARTUUID="21b0739b-d6f6-4835-b72a-169f34c457d8" /dev/sda2: LABEL="boot" UUID="362aec04-1957-4cec-b1dd-333c57dc816a" TYPE="xfs" PARTUUID="a1dcbfcf-8d76-4ebc-bdc4-e1fa0ffa2a2f" /dev/sda5: PARTUUID="2c9f245a-c268-4abf-9bdd-afd0a29bdb3e" /dev/sda1: SEC_TYPE="msdos" UUID="31C6-0FBC" TYPE="vfat" PARTLABEL="EFI System Partition" PARTUUID="cd5c73af-efd7-4740-bca5-32c4430ded06"
We use the UUID to refer to the block device and we do not use /dev/sdb1. This is done because the UUID of the block volume will never change, but it is not guaranteed that the block volume will always be /dev/sdb. That could change if, for example, more block volumes are added.
Now that we know the UUID we can add, with your editor of choice (nano, vi, etc.), a new line to the /etc/fstab file.
UUID=97a3919d-51ad-4fb9-a46c-1752ef9d76a9 /app xfs defaults,_netdev,nofail 0 2
Now after reboot of the system the volume will be mounted to the /app directory automatically.
You can check this by running the lsblk command again. You will see that the block volume is mounted on the /app directory.
[[email protected] ~]$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS sda 8:0 0 46.6G 0 disk ├─sda1 8:1 0 100M 0 part /boot/efi ├─sda2 8:2 0 1000M 0 part /boot ├─sda3 8:3 0 8.9G 0 part │ └─rocky-root 253:0 0 8.9G 0 lvm / ├─sda4 8:4 0 4M 0 part └─sda5 8:5 0 1M 0 part sdb 8:16 0 50G 0 disk └─sdb1 8:17 0 50G 0 part /app