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?

  • 1
    If you have a GUI on your server, you can use the Disks app to do what you want... not fully auto, but it'll edit /etc/fstab for you. – heynnema Sep 6 '18 at 16:48
  • I do not know LVM, but have multiple installs, but all my data in one larger /mnt/data partition. So with each install I run a small script to edit several configurations and add my /mnt/data into fstab. But that only works as UUID & mount point is always the same. – oldfred Sep 6 '18 at 18:09
4

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

  • It would have been nice to have an automated way though! – George Udosen Sep 6 '18 at 16:30
  • I know, right? I come from a background in working with IBM AIX and it has a tool called Logical Volume Manager which does all the steps for you, other than the final "mount". I would have thought you could use some tool and tell it the VG to install on, the LV name, the type (ext4), and the mount point. It should be able to figure out what to put in /etc/fstab with that info. I guess I can try to write a script that accepts the key details and does the steps in order for you. – S. Nixon Sep 6 '18 at 17:01
  • @S.Nixon I'm not aware of such a tool (If exists). – Ravexina Sep 6 '18 at 17:03
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 #

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.