Use UUID to manage Disks

Source: Internet
Author: User

How to manage your disk by UUID on Linux

Hey there,

Today's post is about something I think is pretty cool on Linux (since about Kernel 2.1.x, when/proc/Partitions was introduced, or made standard ). it has to do with disk mounting (both on the command line or through the fstab) by UUID (universal unique identifier ). UUID notation, when used as a means to access disk, is just one more way that Linux has moved ahead of the pack to (depending on your way of thinking either make disk management more accessible or make the fstab and disk identification even more confusing note: skip the next paragraph if you don't care about open Solaris 'slight support for this functionality, to date. skip to the numbered list if you just want to check out the commands and have had your fair share of my opinion

For those of you who got here by catching the Solaris and Unix tags on this post, I want to address your concerns immediately, since you may no longer be concerned with this text after the next few sentences although Solaris does "understand" UUID addressing, the level on which Solaris addresses the issue (with regards to disk management) isn' t user-friendly enough to fit in the scope of this post. basically, And this is putting it very generically, the getting and setting of object UUID's on Solaris is still only resident at the code-base layer. i'm gglad to see that sun is addressing the issue with C functions like wsreg_set_id () and wsreg_get_id (),, since the functionality provided by this layer of access hasn't been implemented in any relevant user tools, we won't be looking at Solaris 'Implementation of it for the remainder of this dialogue. OK, I'll give Solaris 10 points for having expanded greatly upon the previous version's acceptance of the standard by implementing a lot of new C routines, A "makeuuid" binary and support for UUID's of zones, but, again, since we're re going to be looking at mounting disk using the uuid (without re-writing the OS), Solaris (open and regular) is out for now (8/20/2008 just in case the future makes me incorrect, which it has a nasty habit of doing

while Linux boasts most of the same C routines and headers as Solaris (which it must, of course, since the OS supports UUID identification ), they're named slightly differently and-the biggest plus-Linux (RedHat and Ubuntu, at least) come with plenty of programs to work with disk UUID's and plenty of hooks to allow other programs to make use of the disk UUID's as well!

the most basic program (that Solaris has picked up on) is called "uuidgen. "This program will generate a uuid for you based on the output from a decent randomness-generator (like/dev/random) or resort to time-and-Mac-based randomization (generally, the only random factor used is time, unless you have the privilege to view your Ethernet adapter's MAC address ). the program can be forced to use one or the other, if you have a specific preference (with the "-R" and "-T" Flags, respectively ). this seemingly extraneous program does have one very important area of application, which we'll look at below.

where you really see the benefit with Linux is in how they 've worked it into their basic hard disk management facilities. they 've made it very simple for you to keep track of your disks by UUID using any number of methods. i'll be listing several different means to some "Ends" you may be want to achieve, as every command may not be available in your Linux distro, but at least one probably is.

One, often unmentioned (but highly valuable ), benefit of using UUID's to deal with your disks is that you don't have to worry about system naming conventions and the hassles inherent with using them. for instance, if you have a disk with a specific UUID and a block device name of/dev/sda3, If you do all your work (and system/Application customization) with that disk, as the name/dev/sda3, you might be in for a big headache if you have a system problem (or just install some new hardware and reconfigure) and Linux decides to rename/dev/sda3 as/dev/sdb3 (or "anything" else ). if you're using UUID's, you can simply use the "tune2fs" command (shown below) to assign the original UUID back to the new logical device name, so/dev/sdb3 wocould function exactly as if it were/dev/sda3, without causing any issues with your Linux OS :):

1. If you don't know the uuid of your disk, you can find it by using one of the several commands below:

Host # vol_id/dev/sda3
...
Id_fs_uuid = a1331d73-d640-4bac-97b4-cf33a375ae5b
...

Or:

Host # blkid/dev/sda3 <-- leave blank to show all disks
/Dev/sda3: Label = "/" UUID = "a1331d73-d640-4bac-97b4-cf33a375ae5b" sec_type = "ext3" type = "ext2"

Also:

Host # ls-L/dev/Disk/by-UUID | grep sda3
Lrwxrwxrwx 1 Root 10 11. OKT a1331d73-d640-4bac-97b4-cf33a375ae5b->.../sda3

2. if you prefer to generate your own UUID's (see above), you can use the uuidgen command and couple it with tune2fs to change the default UUID assigned to your disk by the system, like this:

Host # uuidgen
1d721189-7b71-4315-95a7-1c3abc90d379
Host # tune2fs-u 1d721189-7b71-4315-95a7-1c3abc90d379/dev/sda3

3. then again, if you already know the uuid, you might want to find out what disk it's associated. you can generally get this information with the "findfs" command, like so:

Host # findfs UUID = a1331d73-d640-4bac-97b4-cf33a375ae5b
/Dev/sda3

Of course, using some of the commands above and grepping out part of the uuid will also get you your answer, like:

Host # ls-L/dev/Disk/by-UUID | grep a1331d73-d640-4bac-97b4-cf33a375ae5b
Lrwxrwxrwx 1 Root 10 11. OKT a1331d73-d640-4bac-97b4-cf33a375ae5b->.../sda3

Or

Host # blkid | grep a1331d73-d640-4bac-97b4-cf33a375ae5b <-- remember that blkid with no arguments returns all of the System Disk
/Dev/sda3: Label = "/" UUID = "a1331d73-d640-4bac-97b4-cf33a375ae5b" sec_type = "ext3" type = "ext2"

4. and, lastly (for this post, at least;), you can mount your disks using the uuid, and even in1_ate that automatically UUID mounting into your/etc/fstab. to mount directly from the command line, you can do something like this:

Host # Mount-u a1331d73-d640-4bac-97b4-cf33a375ae5b/directory/you/Mount/This/Disk/on

And you coshould instruct your system to mount this partition by UUID from within the fstab, as well. It works basically the same way that the label keyword does:

Host # Cat/etc/fstab
...
UUID = a1331d73-d640-4bac-97b4-cf33a375ae5b/directory/you/Mount/This/Disk/on ext3fs defaults 1 1

And, at this point, you shocould be able to figure your way around using UUID's to manipulate your disk on Linux with no problem. Enjoy, and please "Be careful"

Cheers,

, Mike

View hard disk UUID

1 Root [/Dev/Disk/by-UUID ]# Ls-L/dev/Disk/by-UUID


View hard disk UUID

1 Blkid/Dev/sda5

Uuidgen returns a valid UUID. Combined with tune2fs, you can generate a new UUID and write it into ext2, 3,4 partitions:
For example, create or change the uuid of sda5 (root permission required)

1 Uuidgen |Xargs Tune2fs/Dev/sda5 -U

You can also write the original UUID found in fstab back to the partition:

1 Tune2fs-u c1b9d5a2-f162-11cf-9ece-0020afc76f16/Dev/sda5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.