Results 1 to 9 of 9

Thread: Script to change fstab entries to UUID

  1. #1
    Join Date
    Jul 2009
    Location
    California
    Beans
    261
    Distro
    Kubuntu 19.10 Eoan Ermine

    Script to change fstab entries to UUID



    FSTAB: /dev - UUID entries


    If you run an older version of Ubuntu or simply have a fstab with /dev/* entries for whatever reason, you might benefit from this script.

    You can manually change the entries of your fstab file to UUID format by running
    Code:
    ls -l /dev/disk/by-uuid
    and then manually pasting the results into your /etc/fstab file.

    This is tedious however, and you have to learn a little bit (not much, but still) about the format in your fstab file.
    This script will automatically change all entries of the form /dev/[sh]d[a-z][0-9] to their respective UUID formats.
    You can copy and paste it into your own file, or download it as an attachment.

    Code:
    #!/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!"
    To run the script, save it to a file and give it executable permissions. You must have root privelages to run it, so just use sudo.
    Code:
    chmod +x UUID_fstab.sh
    sudo ./UUID_fstab.sh
    You can also run it by typing
    Code:
    sudo /bin/bash UUID_fstab.sh
    Hopefully this is useful for you.
    Enjoy.

    (Tested on Ubuntu 10.04)
    Attached Files Attached Files
    Last edited by Buuntu; June 13th, 2010 at 11:42 PM. Reason: Fixed a few things in script
    System: Lenovo G50
    CPU: 4x intel core i3-4030U 1.90GHz
    GPU: Intel HD Graphics
    RAM: 6 GB

  2. #2
    Join Date
    Apr 2009
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Script to change fstab entries to UUID

    I really would like someone who actually does understand the code you made to have a look at it, since my knowledge in bash scripting is very limited.

    If the script's ok I save a copy of it on my fileserver, since I went through a lot of trouble to get my USB-drive in fstab.

  3. #3
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: Script to change fstab entries to UUID

    Quote Originally Posted by new_tolinux View Post
    If the script's ok I save a copy of it on my fileserver, since I went through a lot of trouble to get my USB-drive in fstab.
    The script seems OK, but it will not ad your drive to your fstab for you, it will only "convert" old-style /dev/* entries to UUID ones.
    「明後日の夕方には帰ってるからね。」


  4. #4
    Join Date
    Apr 2009
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Script to change fstab entries to UUID

    Quote Originally Posted by Bachstelze View Post
    The script seems OK, but it will not ad your drive to your fstab for you, it will only "convert" old-style /dev/* entries to UUID ones.
    Thanks.

    I guess I don't really need that, since the newer versions seem to do that by default (9.10 running) but I'll keep it anyway.
    Although it doesn't add the USB-drive, it does prevent typos if I can add the correct /dev/.... entry instead of the UUID=.... entry.

  5. #5
    Join Date
    Jul 2009
    Location
    California
    Beans
    261
    Distro
    Kubuntu 19.10 Eoan Ermine

    Re: Script to change fstab entries to UUID

    Yeah sorry if that was confusing...
    System: Lenovo G50
    CPU: 4x intel core i3-4030U 1.90GHz
    GPU: Intel HD Graphics
    RAM: 6 GB

  6. #6
    Join Date
    Apr 2009
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Script to change fstab entries to UUID

    Quote Originally Posted by Buuntu View Post
    Yeah sorry if that was confusing...
    No problem, thanks anyway for the script.
    Like I said, while typing an UUID-code it's easy to miss a character, number or have typos.

  7. #7
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: Script to change fstab entries to UUID

    Quote Originally Posted by new_tolinux View Post
    No problem, thanks anyway for the script.
    Like I said, while typing an UUID-code it's easy to miss a character, number or have typos.
    That's what copy-and-paste is for.
    「明後日の夕方には帰ってるからね。」


  8. #8
    Join Date
    Apr 2009
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Script to change fstab entries to UUID

    Quote Originally Posted by Bachstelze View Post
    That's what copy-and-paste is for.
    Since I do most of the configuring with putty that doesn't really work that well

  9. #9
    Join Date
    Aug 2008
    Beans
    567

    Re: Script to change fstab entries to UUID

    Nice tips .

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •