将 Debian fstab 转换为通过 linux-base 使用 UUID?

将 Debian fstab 转换为通过 linux-base 使用 UUID?

我们有几个 Debian 5 系统仍在 fstab 中使用 /dev/hda。我们想将它们转换为使用 UUID。这应该通过“linux-base”包 (postinst) 自动完成,但由于某种原因,它没有启动(也许有人已经运行了它,并且它在某处保存了一些状态以不执行它)。

虽然通过手动编辑一堆文件当然可以切换到 UUID,但以某种方式编写脚本会很有用。debconf 和 debconf-set-selections 的各种咒语似乎不起作用。

那么基本上,如何调用 Debian 提供的脚本来执行所有 UUID 转换?

答案1

剧本来自Ubuntu 论坛似乎做了一个不错的代码:

#!/bin/bash
# This script will change all entries of the form /dev/sd* in /etc/fstab to their appropriate UUID names
# You must have root privelages to run this script (use sudo)
if [ `id -u` -ne 0 ]; then                                              # Checks to see if script is run as root
        echo "This script must be run as root" >&2                      # If it isn't, exit with error
        exit 1
fi

cp /etc/fstab /etc/fstab.backup
sed -n 's|^/dev/\([sh]d[a-z][0-9]\).*|\1|p' </etc/fstab >/tmp/devices   # Stores all /dev entries from fstab into a file
while read LINE; do                                                     # For each line in /tmp/devices
        UUID=`ls -l /dev/disk/by-uuid | grep "$LINE" | sed -n 's/^.* \([^ ]*\) -> .*$/\1/p'` # Sets the UUID name for that device
        sed -i "s|^/dev/${LINE}|UUID=${UUID}|" /etc/fstab               # Changes the entry in fstab to UUID form
done </tmp/devices
cat /etc/fstab                                                          # Outputs the new fstab file
printf "\n\nWrite changes to /etc/fstab? (y/n) "
read RESPONSE;
case "$RESPONSE" in
        [yY]|[yY][eE][sS])                                              # If answer is yes, keep the changes to /etc/fstab
                echo "Writing changes to /etc/fstab..."
                ;;
        [nN]|[nN][oO]|"")                                               # If answer is no, or if the user just pressed Enter
                echo "Aborting: Not saving changes..."                  # don't save the new fstab file
                cp /etc/fstab.backup /etc/fstab
                rm /etc/fstab.backup
                ;;
        *)                                                              # If answer is anything else, exit and don't save changes
                echo "Invalid Response"                                 # to fstab
                echo "Exiting"
                cp /etc/fstab.backup /etc/fstab
                rm /etc/fstab.backup
                exit 1
                ;;
esac
rm /tmp/devices
echo "DONE!"

答案2

该脚本应该位于 /var/lib/dpkg/info/linux-base.postinst 中。您可以手动启动它,或者在调用 dpkg-reconfigure 之前查找可以清除的“迁移”标志。

相关内容