Some of the latest Linux distros like Mandriva 2008.1 use UUID
(Universally Unique Identifier) strings to identify partitions in fstab
and in grub's menu.lst rather than the old /dev/* device ids. I can see
that this has it's advantages, but the UUIDs are long strings of random
numbers which are not so easy to maintain for people that tweak these
files by hand. So I've set volume-labels on all partitions, and now I
need to change fstab and menu.lst to use labels instead of UUIDs.
I've already noted useful commands:
$ e2label <device> [new-label] # display/set label on ext2/3
$ mlabel -i /dev/hdaX -s :: # display dos label (from mtools)
$ mlabel -i /dev/hdaX ::<new-label> # set dos label
$ ntfslabel /dev/hdaX <new-label> # set ntfs label (from ntfsprogs)
$ mkswap -L <new-label> /dev/hdaX # set swap label - CHANGES UUID
$ findfs LABEL=<label> # find device by label (script)
$ findfs UUID=<uuid> # find device by UUID (script)
$ vol_id -l /dev/hdaX # show label (script)
$ vol_id -u /dev/hdaX # show uuid (script)
$ vol_id /dev/hdaX # shows UUID, label, and more
$ vol_id /dev/hdaX ID_FS_UUID=<uuid> # set UUID - only if fs is closed
$ tune2fs /dev/hdaX -U <uuid> # set UUID on ext2/3
$ blkid -s UUID /dev/hda* # list all UUIDs (non-ntfs)
$ blkid -s LABEL /dev/hda* # list all labels (non-ntfs)
$ blkid /dev/hda* # All labels & UUIDs (non-ntfs)
$ dumpe2fs /dev/hdaX | grep UUID # extract UUID from ext2/3 header
$ dumpe2fs /dev/hdaX | grep volume.name # extract label from ext2/3 head
--
Dave Farrance
Here is a quick kludge I made, use at your own risk.
I wanted different mount points for winders and different arguments.
Also changes other distribution installations to user,noauto and
strips comment lines.
Adjust to suit yourself. For some light shell scripting reading, there is
http://tldp.org/LDP/abs/html/index.html
------8<------8<------8<--cut below this line----8<------8<------8<------8<---
#!/bin/bash
#**********************************************************
#*
#* fix_fstab - remove comments, change uuid to label and add my changes
#*
#**********************************************************
_debug=0
_wd=/etc
_ifn=$_wd/fstab
if [ ! -r /etc/fstab_orig ] ; then
cp /etc/fstab /etc/fstab_orig
fi
if [ ! -r /etc/blkid_orig ] ; then
blkid /dev/sd* > /etc/blkid_orig
fi
if [ $_debug -eq 1 ] ; then
_wd=$PWD
_ifn=$_wd/fstab
if [ ! -e $_ifn ] ; then
cp /etc/fstab_orig .
cp /etc/fstab_orig ./fstab
fi
fi
wd0=""
wd2=""
_dev=""
_lbl=""
function get_vol_id
{
_mp=$1
_cnt=$(mount | grep -c $_mp)
if [ $_cnt -ne 0 ] ; then
set -- $(mount | grep $_mp)
_dev=$1
if [ "$_mp" = "swap" ] ; then
set -- $(blkid /dev/sd* | grep swap)
_dev=$1
set -- $(IFS=':'; echo $_dev )
_dev=$1
fi
_lbl=$(vol_id --label $_dev 2> /dev/null)
if [ $? -eq 0 ] ; then
wd0="LABEL=$_lbl"
if [ -z "$_lbl" ] ; then
wd0="$_dev"
fi
else
wd0="$_dev"
fi
else
wd0="$wd1"
fi
} # end get_vol_id
#************************************
#* create my mount points and
#* an original backup of fstab
#************************************
for d in /win_c /win_d /win_e /dvd /nfs /local /iso; do
if [ ! -d $d ] ; then
mkdir $d
fi
done
if [ ! -e ${_ifn}_orig ] ; then # make original copy
cp $_ifn ${_ifn}_orig
fi
#********************************************
#* create empty output file and make changes
#********************************************
_ofn="/tmp/fstab"
/bin/cp /dev/null $_ofn
while read line
do
set -- $line # parse each column into $1, $2,...
wd1=$1 # save first word of line (IE. LABEL=fc7)
wd2=$2 # save second word of line
wd3=$3
wd4=$4
wd5=$5
wd6=$6
fch=${wd1:0:1} # save first char of first word
get_vol_id $wd2
case $fch in
"#") # remove comment line
;;
"L") # add user,noauto, except for the following:
case $wd2 in
"/")
echo "$line" >> $_ofn
;;
"/mnt/hd")
echo "LABEL=local /local $wd3 $wd4 $wd5 $wd6" >> $_ofn
;;
"/local")
echo "$line" >> $_ofn
;;
"/accounts")
echo "$line" >> $_ofn
;;
"/vmguest")
echo "$line" >> $_ofn
;;
"/spool")
echo "$line" >> $_ofn
;;
*)
_tmp=${wd4:0:4}
if [ "$_tmp" = "user" ] ; then
echo "$wd0 $wd2 $wd3 $wd4 $wd5 $wd6" >> $_ofn
else
echo "$wd0 $wd2 $wd3 user,noauto,$wd4 $wd5 $wd6" >> $_ofn
fi
;;
esac
;;
"U") # change UUID to /dev for swap and doze entries
case $wd2 in
"/mnt/win_c")
echo "$wd0 /win_c $wd3 user,noauto,defaults,locale=en_US.UTF-8 $wd5 $wd6" >> $_ofn
;;
"/mnt/win_d")
echo "$wd0 /win_d vfat user,noauto,iocharset=iso8859-1,codepage=850 $wd5 $wd6" >> $_ofn
;;
"/mnt/win_e")
echo "$wd0 /win_e vfat user,noauto,iocharset=iso8859-1,codepage=850 $wd5 $wd6" >> $_ofn
;;
"/win_c")
echo "$wd0 /win_c $wd3 $wd4 $wd5 $wd6" >> $_ofn
;;
"/win_d")
echo "$wd0 /win_d vfat user,noauto,iocharset=iso8859-1,codepage=850 $wd5 $wd6" >> $_ofn
;;
"/win_e")
echo "$wd0 /win_e vfat user,noauto,iocharset=iso8859-1,codepage=850 $wd5 $wd6" >> $_ofn
;;
"swap")
# echo "$_dev swap swap $wd4 $wd5 $_wd6" >> $_ofn
echo "$line" >> $_ofn
;;
"/mnt/hd")
;;
*)
echo "$line" >> $_ofn
;;
esac
;;
*)
if [ "$wd3" != "xiso9660" ] ; then
echo "$line" >> $_ofn
fi
;;
esac
done < $_ifn
echo " "
cat $_ofn
echo "
if looks good
/bin/cp $_ofn ${_wd}/
"
#****************** end fix_fstab **************************
>On Mon, 09 Jun 2008 10:20:33 GMT, Dave Farrance wrote:
>> Has anybody already written a script to convert UUID to LABEL identifiers
>> in a file? To save me duplicating the effort?
>
>Here is a quick kludge I made, use at your own risk.
>I wanted different mount points for winders and different arguments.
>Also changes other distribution installations to user,noauto and
>strips comment lines.
Many thanks. I stripped out the case structures etc that did the extra
stuff and tested it. It works great. I like the way that it ignores the
first field in fstab and works out the label from the mount-point.
I've also made a UUID to LABEL conversion script so that I can also
convert the grub config. It's useful to compare the output of the two
scripts when modifying fstab.
I'm still using Mdv2007 for main use, and I've installed Mdv2008.1 in a
separate partition to debug. To my surprise, some process in Mandriva
2007 spotted that I'd put labels on the partitions, and it modified
/etc/fstab to put in the labels without asking. The dos and ntfs
partitions wouldn't mount so I had to revert those to device names.
It seems that the label/uuid-based mounting (and the commands findfs and
blkid) don't work too well for non-linux partitions in Mdv2007 but work
fine in Mdv2008.
--
Dave Farrance
>Has anybody already written a script to convert UUID to LABEL identifiers
>in a file? To save me duplicating the effort?
Done. The file to be converted is the command line parameter. The new
file is created with a .new suffix added.
I've found that commands like findfs and blkid must be recent versions if
they are to handle non-linux partitions like ntfs OK.
#!/bin/bash
[[ $(whoami) != root ]] && echo "Must be root" && exit
[[ ! -r $1 ]] && echo "Can't read $1" && exit
cp /dev/null $1.new
while read line; do
if [[ "$line" == *UUID=* ]]; then
uuid1=${line#*UUID=}
uuid=${uuid1%%[[:blank:]]*}
dev=$(findfs UUID="$uuid")
label=$(vol_id -l "$dev")
[[ -n "$label" ]] && line=${line//UUID=$uuid/LABEL=$label}
fi
echo $line >>$1.new
done < $1
--
Dave Farrance