1

I use lvcreate and mkfs to create new filesystems on my Ubuntu Server 18.04 LTS systems. One thing I find confusing is that the system does not automatically add any new filesystems that I create to the /etc/fstab file. The only ones added are the ones that were created when the system was initially created during install.

Is there any way (mkfs flag, or separate apt package) that new filesystems could be inserted automatically in the /etc/fstab file rather than requiring the administrator to manually edit the file?

S. Nixon
  • 472

2 Answers2

5

That's normal, because there is no way for tools or operating system to know where do you want to mount the partitions, logical volumes or etc that you have just created.

The ones you create while installing system are detected by your choices (When you select your root, home, etc) and are added to fstab because they are necessary for booting your system.

you have to manually edit the fstab.

BUT, there is other ways to automatically mount partitions, tools like udisks.

https://help.ubuntu.com/community/AutomaticallyMountPartitions

Ravexina
  • 57,256
0

Here's a sample Korn shell script I made to see if it was possible to automate the creation of the LV, filesystem, and mount point all in one, and then add the appropriate entry to /etc/fstab.

Not sure if it works on all systems and configs, but it seems to be working for my needs so far. It is far from a finished product (not much error checking), but just shows me that automating the lvcreate/mkfs/mount routines into one command should be possible.

#!/bin/ksh
typeset -i ERRVAL

while getopts V:L:m:s:t:h FSTR
do
  case ${FSTR} in
  V) {
     VGNAME=${OPTARG}
     };;
  L) {
     LVNAME=${OPTARG}
     };;
  m) {
     MNTPT=${OPTARG}
     };;
  s) {
     SIZE=${OPTARG}
     };;
  t) {
     FSTYPE=${OPTARG}
     };;
  h) {
     print "help screens go here"
     exit 0
     };;
  esac
done

if test "${VGNAME}" = ""
then
  print "ERROR: Volume group name must be specified. Valid volume groups:"
  vgdisplay |grep -i "vg name" |awk '{print $3}'
  exit 1
elif test "$(vgdisplay |grep -i "vg name" |grep "${VGNAME}$")" = ""
then
  print "ERROR: Unrecognized volume group name. Valid volume groups:"
  vgdisplay |grep -i "vg name" |awk '{print $3}'
  exit 1
fi

if test "${LVNAME}" = ""
then
  print "ERROR: Logical volume name must be specified."
  exit 1
elif test "$(lvdisplay|grep -i "lv name"|awk '{print $3}'|grep "^${LVNAME}$")" !
= ""
then
  print "ERROR: Logical volume already exists with that name."
  exit 1
fi

if test "${FSTYPE}" = ""
then
  print "Type of filesystem not specified, defaulting to ext4"
  FSTYPE=ext4
fi

if test "${SIZE}" = ""
then
  print "ERROR: Logical volume size must be supplied."
  exit 1
else
  TMPSIZE="$(echo "${SIZE}" |tr -d '[ a-fhijln-zA-FHIJLN-Z!@#$%~]')"
  SIZE="${TMPSIZE}"
  if test "$(echo "${SIZE}" |egrep "K|k|M|m|G|g")" = ""
  then
    print "ERROR: LV size must be listed in K, M, or G."
    exit 1
  fi
fi

if test "${MNTPT}" = ""
then
  print "ERROR: Mount point not specified."
  print ""
  exit 1
elif test -d ${MNTPT}
then
  print "Mount point already exists: ${MNTPT}"
  print "Use this directory (Y/N)? \c"
  read YORN
  if test "${YORN}" != "Y" -a "${YORN}" != "y"
  then
    exit 1
  fi
elif test ! -d ${MNTPT}
then
  print "Mount point does not exist: ${MNTPT}"
  print "Create this directory (Y/N)? \c"
  read YORN
  if test "${YORN}" = "Y" -o "${YORN}" = "y"
  then
    mkdir ${MNTPT}
  else
    exit 1
  fi
fi

DEVNAME="/dev/${VGNAME}/${LVNAME}"

# CREATE THE LOGICAL VOLUME
print "Issuing command:"
print "lvcreate -L${SIZE} -n ${DEVNAME} ${VGNAME}"
lvcreate -L${SIZE} -n ${DEVNAME} ${VGNAME}
ERRVAL=$?
if test ${ERRVAL} -ne 0
then
  print "ERROR: lvcreate command exited with non-zero status."
  exit 0
fi

# CREATE THE FILESYSTEM
print "Issuing command:"
print "mkfs -t ${FSTYPE} ${DEVNAME}"
mkfs -t ${FSTYPE} ${DEVNAME}
ERRVAL=$?
if test ${ERRVAL} -ne 0
then
  print "ERROR: mkfs command exited with non-zero status."
  exit 0
fi

# MOUNT POINT SHOULD ALREADY EXIST SO MOUNT THE NEW FILESYSTEM
print "Issuing command:"
print "mount ${DEVNAME} ${MNTPT}"
mount ${DEVNAME} ${MNTPT}
ERRVAL=$?
if test ${ERRVAL} -ne 0
then
  print "ERROR: mount command exited with non-zero status."
  exit 0
fi

# ADD TO /etc/fstab
print "Obtain UUID value from blkid"
UUID=$(blkid |grep "${VGNAME}-${LVNAME}:"|cut -f2 -d'='|cut -f2 -d'"')
if test "${UUID}" = ""
then
  print "ERROR: Unable to determine UUID to use for ${LVNAME}"
  exit 1
fi
print "Saving /etc/fstab as /etc/fstab.$$"
/bin/cp -p /etc/fstab /etc/fstab.$$
print "Adding /etc/fstab entry"
echo "UUID=${UUID} ${MNTPT} ${FSTYPE} defaults 0 0" >> /etc/fstab
ERRVAL=$?
if test ${ERRVAL} -ne 0
then
  print "ERROR: Could not save entry to /etc/fstab"
  exit 1
fi
# END OF SCRIPT #
S. Nixon
  • 472