Skip to content

Instantly share code, notes, and snippets.

@abu-raihan-ddclbd
Last active October 16, 2024 12:06
Show Gist options
  • Select an option

  • Save abu-raihan-ddclbd/6c194702ad48c5217a5c28dc712d18f7 to your computer and use it in GitHub Desktop.

Select an option

Save abu-raihan-ddclbd/6c194702ad48c5217a5c28dc712d18f7 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Set variables
PHYSICAL_DEVICE="/dev/sda" # Change this to your actual device
VG_NAME="vg_nfs"
LV_NAME="lv_nfs"
MOUNT_POINT="/mnt/nfs"
LV_SIZE="100G" # Specify the size for the logical volume
# Function to check for errors
check_error() {
if [ $? -ne 0 ]; then
echo "Error occurred during the previous command. Exiting."
exit 1
fi
}
# Step 1: Install LVM (if not already installed)
echo "Installing LVM..."
sudo dnf install -y lvm2
check_error
# Step 2: Create Physical Volume
echo "Creating Physical Volume on $PHYSICAL_DEVICE..."
sudo pvcreate $PHYSICAL_DEVICE
check_error
# Step 3: Create Volume Group
echo "Creating Volume Group named $VG_NAME..."
sudo vgcreate $VG_NAME $PHYSICAL_DEVICE
check_error
# Step 4: Create Logical Volume
echo "Creating Logical Volume named $LV_NAME of size $LV_SIZE..."
sudo lvcreate -n $LV_NAME -L $LV_SIZE $VG_NAME
check_error
# Step 5: Format Logical Volume with XFS
echo "Formatting Logical Volume with XFS..."
sudo mkfs.xfs /dev/$VG_NAME/$LV_NAME
check_error
# Step 6: Create Mount Point
echo "Creating Mount Point at $MOUNT_POINT..."
sudo mkdir -p $MOUNT_POINT
check_error
# Step 7: Mount the Logical Volume
echo "Mounting the Logical Volume..."
sudo mount /dev/$VG_NAME/$LV_NAME $MOUNT_POINT
check_error
# Step 8: Add to /etc/fstab for automatic mounting
echo "Adding to /etc/fstab for automatic mounting..."
echo "/dev/$VG_NAME/$LV_NAME $MOUNT_POINT xfs defaults 0 0" | sudo tee -a /etc/fstab
check_error
# Final output
echo "LVM with XFS setup complete! Logical Volume is mounted at $MOUNT_POINT."
#!/bin/bash
# Set variables
PHYSICAL_DEVICE="/dev/sda" # Change this to your actual device
VG_NAME="vg_nfs"
LV_NAME="lv_nfs"
MOUNT_POINT="/mnt/nfs"
# Function to check for errors
check_error() {
if [ $? -ne 0 ]; then
echo "Error occurred during the previous command. Exiting."
exit 1
fi
}
# Step 1: Install LVM if not already installed
if ! command -v lvcreate &> /dev/null; then
echo "Installing LVM..."
sudo dnf install -y lvm2
check_error
else
echo "LVM is already installed."
fi
# Step 2: Create Physical Volume (PV) if it doesn't exist
if sudo pvdisplay | grep -q "$PHYSICAL_DEVICE"; then
echo "Physical Volume $PHYSICAL_DEVICE already exists."
else
echo "Creating Physical Volume on $PHYSICAL_DEVICE..."
sudo pvcreate $PHYSICAL_DEVICE
check_error
fi
# Step 3: Create Volume Group (VG) if it doesn't exist
if sudo vgdisplay | grep -q "$VG_NAME"; then
echo "Volume Group $VG_NAME already exists."
else
echo "Creating Volume Group named $VG_NAME..."
sudo vgcreate $VG_NAME $PHYSICAL_DEVICE
check_error
fi
# Step 4: Create Logical Volume (LV) if it doesn't exist
if sudo lvdisplay /dev/$VG_NAME/$LV_NAME &> /dev/null; then
echo "Logical Volume $LV_NAME already exists."
else
echo "Creating Logical Volume named $LV_NAME using all available space..."
sudo lvcreate -n $LV_NAME -l 100%FREE $VG_NAME
check_error
fi
# Step 5: Format Logical Volume with XFS if not already formatted
if sudo blkid | grep -q "/dev/$VG_NAME/$LV_NAME" && sudo blkid /dev/$VG_NAME/$LV_NAME | grep -q "xfs"; then
echo "Logical Volume $LV_NAME is already formatted with XFS."
else
echo "Formatting Logical Volume with XFS..."
sudo mkfs.xfs /dev/$VG_NAME/$LV_NAME
check_error
fi
# Step 6: Create Mount Point if it doesn't exist
if [ -d "$MOUNT_POINT" ]; then
echo "Mount Point $MOUNT_POINT already exists."
else
echo "Creating Mount Point at $MOUNT_POINT..."
sudo mkdir -p $MOUNT_POINT
check_error
fi
# Step 7: Mount the Logical Volume if not already mounted
if mountpoint -q "$MOUNT_POINT"; then
echo "Logical Volume $LV_NAME is already mounted at $MOUNT_POINT."
else
echo "Mounting the Logical Volume..."
sudo mount /dev/$VG_NAME/$LV_NAME $MOUNT_POINT
check_error
fi
# Step 8: Add to /etc/fstab if not already present
if grep -qs "/dev/$VG_NAME/$LV_NAME" /etc/fstab; then
echo "Logical Volume $LV_NAME is already in /etc/fstab."
else
echo "Adding to /etc/fstab for automatic mounting..."
echo "/dev/$VG_NAME/$LV_NAME $MOUNT_POINT xfs defaults 0 0" | sudo tee -a /etc/fstab
check_error
fi
# Final output
echo "LVM with XFS setup complete! Logical Volume is mounted at $MOUNT_POINT."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment