Script to convert UUID to LABEL in fstab? - Setup

This is a discussion on Script to convert UUID to LABEL in fstab? - Setup ; Has anybody already written a script to convert UUID to LABEL identifiers in a file? To save me duplicating the effort? Some of the latest Linux distros like Mandriva 2008.1 use UUID (Universally Unique Identifier) strings to identify partitions in ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: Script to convert UUID to LABEL in fstab?

  1. Script to convert UUID to LABEL in fstab?

    Has anybody already written a script to convert UUID to LABEL identifiers
    in a file? To save me duplicating the effort?

    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 [new-label] # display/set label on ext2/3
    $ mlabel -i /dev/hdaX -s :: # display dos label (from mtools)
    $ mlabel -i /dev/hdaX :: # set dos label
    $ ntfslabel /dev/hdaX # set ntfs label (from ntfsprogs)
    $ mkswap -L /dev/hdaX # set swap label - CHANGES UUID
    $ findfs LABEL=

  2. Re: Script to convert UUID to LABEL in fstab?

    Dave Farrance wrote:

    >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

+ Reply to Thread