自动将文件系统添加到 /etc/fstab

自动将文件系统添加到 /etc/fstab

我用lvcreate文件系统在我的Ubuntu Server 18.04 LTS系统。有一件事让我感到困惑,那就是系统不会自动将我创建的任何新文件系统添加到/etc/fstab文件。唯一添加的是系统在安装过程中最初创建时创建的文件。

有什么办法吗(文件系统标记,或单独易于包)中可以自动插入新的文件系统/etc/fstab文件,而不需要管理员手动编辑文件?

答案1

这是正常的,因为工具或操作系统无法知道您要在哪里挂载刚刚创建的分区、逻辑卷等。

您在安装系统时创建的那些会根据您的选择(当您选择roothome等时)进行检测,并添加到 fstab 中,因为它们对于启动系统是必需的。

您必须手动编辑 fstab。

但是,还有其他方法可以自动挂载分区,比如 udisks 这样的工具。

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

答案2

这是我编写的一个示例 Korn shell 脚本,用于查看是否可以自动创建 LV、文件系统和挂载点,然后将适当的条目添加到 /etc/fstab。

不确定它是否适用于所有系统和配置,但到目前为止它似乎满足了我的需求。它远未完成(没有太多错误检查),但只是向我展示了将 lvcreate/mkfs/mount 例程自动化为一个命令应该是可能的。

#!/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 #

相关内容