Home > other >  How to use mkfs -t ext4 to keep data on existing volume but delete on new?
How to use mkfs -t ext4 to keep data on existing volume but delete on new?

Time:03-12

I am attaching block volume in OCI to a new or existing instance using a script (below). However, if the volume already has a file system type already assigned I will lose all my data!

Is there a way only to run the command sudo mkfs -t ext4 /dev/oracleoci/oraclevdb only if it's not already formatted?

Is there a way to run line 1 below, only if the attached volume is not already formatted?

sudo mkfs -t ext4 /dev/oracleoci/oraclevdb
sudo mkdir /data
sudo mount /dev/oracleoci/oraclevdd /data
df -h

The issue is every time a new instance is created using an existing volume, I lose all my data. However, I want to keep the behaviour for new instances were attaching a new volume.

So something like...

if condition x
   sudo mkfs -t ext4 /dev/oracleoci/oraclevdb
else
 do nothing

I'm running Oracle Linux 8.

CodePudding user response:

Just check if you can mount it.

if ! sudo mount /dev/oracleoci/oraclevdd /data; then
     if ! sudo mkfs -t ext4 /dev/oracleoci/oraclevdb; then
          echo "och nooo, formatting fialed"
     fi
     if ! sudo mount /dev/oracleoci/oraclevdd /data; then
          echo "Och nooooo, can't mount after formatting, that's odd"
     fi
fi

CodePudding user response:

You can get this info running lsblk like so:

lsblk  -o NAME,FSTYPE

and run a test using some script:

export DEVICE="/dev/oracleoci/oraclevdb"
export FSTYPE="ext4"

if ! lsblk -o NAME,FSTYPE | grep $DEVICE | grep $FSTYPE; then
    sudo mkfs -t $FSTYPE $DEVICE
fi
  • Related