1829010 Members
2429 Online
109986 Solutions
New Discussion

LVM device special name

 
SOLVED
Go to solution
Michelle_61
Frequent Advisor

LVM device special name

Hi All,

In my system the special name for a LVM device is different in /etc/fstab than in /etc/mtab:

e.g. in /etc/fstab, it shows:
/dev/vg/space /space ......

e.g. in /etc/fstab, it shows:
/dev/mapper/vg-space /space ......

I wonder if there is an API can convert this two corresponding special names or at least if there is a fix pattern to transform between these two names?

Thanks in advance!
10 REPLIES 10
Patrick Wallek
Honored Contributor

Re: LVM device special name

This appears to more Linux related than HP-UX.

However, the transition from /dev/vgname/lvname to /dev/mapper/vgname-lvname seems relatively straightforward.

For example, here are some of mine:

cat /etc/mtab:
/dev/mapper/ediâ archive /archive ext3 rw,acl,user_xattr 0 0
/dev/mapper/ediâ gisprod /gisprod ext3 rw,acl,user_xattr 0 0
/dev/mapper/ediâ gistest /gistest ext3 rw,acl,user_xattr 0 0
/dev/mapper/ediâ oldarch /oldarch ext3 rw,acl,user_xattr 0 0
/dev/mapper/ediâ oraarch /oraarch ext3 rw,acl,user_xattr 0 0
/dev/mapper/ediâ oraback /oraback ext3 rw,acl,user_xattr 0 0

cat /etc/fstab:
/dev/edi/archive /archive ext3 acl,user_xattr 1 2
/dev/edi/gisprod /gisprod ext3 acl,user_xattr 1 2
/dev/edi/gistest /gistest ext3 acl,user_xattr 1 2
/dev/edi/oldarch /oldarch ext3 acl,user_xattr 1 2
/dev/edi/oraarch /oraarch ext3 acl,user_xattr 1 2
/dev/edi/oraback /oraback ext3 acl,user_xattr 1 2

Am I missing something in your question?
Patrick Wallek
Honored Contributor

Re: LVM device special name

Crazy flippin' cut-and-paste in Windows screwed up the dashes in mtab portion above.

It should be:

cat /etc/mtab:
/dev/mapper/edi-archive /archive ext3 rw,acl,user_xattr 0 0
/dev/mapper/edi-gisprod /gisprod ext3 rw,acl,user_xattr 0 0
/dev/mapper/edi-gistest /gistest ext3 rw,acl,user_xattr 0 0
/dev/mapper/edi-oldarch /oldarch ext3 rw,acl,user_xattr 0 0
/dev/mapper/edi-oraarch /oraarch ext3 rw,acl,user_xattr 0 0
/dev/mapper/edi-oraback /oraback ext3 rw,acl,user_xattr 0 0
Michelle_61
Frequent Advisor

Re: LVM device special name

Yes actually it is for Linux...

What I am trying to do is to write a program to convert the LVM special names. To minimize the risk I might encounter, I need to know if special names start with "/dev/mapper" are only for LVM devices but not other type of devices, and they always transform like that. I also wonder if there are any system APIs I could use to ease the programming.

Thanks!
Michelle_61
Frequent Advisor

Re: LVM device special name

I found the device name specified in /etc/fstab is actually a symbolic link to the one specified in /etc/mtab. for example:

# ls -l /dev/vg/space
lrwxrwxrwx 1 root root 20 Dec 8 09:29 /dev/vg/space -> /dev/mapper/vg-space

Now my problem would be easier if there is a function that takes the /etc/fstab device name as an in param and returns the /etc/mtab device name. The question is if this function exists?
Michelle_61
Frequent Advisor

Re: LVM device special name

Or if there is a function that compares the two paths (/dev/vg/space and /dev/mapper/vg-space) and returns TRUE since they are the same.

Your help will be appreciated!
Matti_Kurkela
Honored Contributor
Solution

Re: LVM device special name

The pattern is:
/dev// -> /dev/mapper/-

In a script, you could convert your /etc/fstab style name to /etc/mtab style (but not vice versa) with "readlink -f". For example, in your case:

readlink -f /dev/vg/space

should return:

/dev/mapper/vg-space

In a program, you can do the same with the readlink() system call.

If you wish to compare two names and see if they point to the same device, you can use the stat()/fstat() functions on both names and compare the stat structures returned. If both have the same st_rdev value, they both point to the same LV.

In a script, you could do something like this:

#!/bin/sh
FIRSTDEV=$(stat -L -c '%F.%t.%T' /dev/mapper/vg-space)
SECONDDEV=$(stat -L -c '%F.%t.%T' /dev/vg/space)
if [ "$FIRSTDEV" = "$SECONDDEV" ]; then
echo "Both names are referring to the same LV"
else
echo "The names point to different LVs, or to something completely different"
fi

MK
MK
Matti_Kurkela
Honored Contributor

Re: LVM device special name

Some background:
The /dev/mapper directory is the location where the _device-mapper subsystem_ maintains its persistent device names.

The device-mapper subsystem is a basic building block for various disk services, including (but not limited to):
- LVM (/dev/mapper/vg00-lvol1)
- disk encryption (/dev/mapper/sda2_crypt)
- multipathing (/dev/mapper/mpath1)
- software RAID

Each of these services can have its own naming pattern, and some of these services also create compatibility symlinks in other parts of the /dev hierarchy, for legacy compatibility or other reasons.

There is also /dev/mapper/control, the control device for the device-mapper subsystem itself.

So: no, not all devices in /dev/mapper always refer to LVM devices.

There is "dmsetup": a generic tool for listing and modifying all device-mapper mappings. Try "dmsetup ls --tree": if you have LVM and multipathing, or LVM and disk-encryption, this will display how they work together and what gets mapped into what.

Of course, the service-specific tools are usually more appropriate for common system administration needs.

MK
MK
Michelle_61
Frequent Advisor

Re: LVM device special name

Thanks Matti, for the profound information. That really helps a lot!

There are a few more questions:
1. Is the /dev// pattern always in /etc/fstab, and the /dev/mapper/- pattern always in /etc/mtab? In other words, the name in fstab is the sym link of the name in mtab but not the other way.
2. Which application decides those patterns in the two tab files? And which application actually edits those two tab files for LVM devices?

Thanks, for bearing with me.
Matti_Kurkela
Honored Contributor

Re: LVM device special name

1.) In /etc/fstab, you can use either form (or if you have made custom symlinks, really *anything* that points to the device you want).

The mount command always canonicalizes the device name, by reading every symlink and replacing it with the real pathname. The canonicalized form is always used in /etc/mtab.

In /etc/fstab, it's also possible to identify a disk device by LABEL= or UUID=. The mount command will use various methods to find the correct device using this information at filesystem mount time.

Use the "man fstab" command to get more information about the structure of the /etc/fstab file. If you are writing your own program, you might want to download the source code of the "mount" command and study the parts that are used to read /etc/fstab.

2.) The content of /etc/fstab is primarily for the "mount" command, but other administration tools may use the data too.

The /etc/fstab file is edited mostly by *you*, the sysadmin. You have the option of using all the helper programs you want.

Some desktop environments *may* include features that automatically add & remove entries to /etc/fstab for USB sticks and other hot-pluggable storage. The current preference seems to be to use HAL and dbus sub-systems for storing such hot-plug information instead of /etc/fstab, though.

If you use a GUI desktop that auto-manipulates /etc/fstab, it's your responsibility as a sysadmin to read the documentation so that you understand how that feature works. In a server system a GUI desktop environment can often be totally left out, so it might be a non-issue.


The /etc/mtab should normally be written by the "mount" command only. As long as the system is operating normally, there should be no need to make any other changes to the /etc/mtab file.

At boot time, the /etc/mtab file is completely cleared and then re-generated as each filesystem is mounted.

If the root filesystem is in read-only state (because of e.g. a hardware problem that affects the root filesystem), the information in /etc/mtab might be out of date. The /proc/mounts is another source for much of the same data, but it has the extra feature of being dynamically generated by the kernel, so it's never out of date.

The /etc/mtab and /proc/mounts files use the same format. The only difference is that when you've granted non-root users permission to mount some devices, /etc/mtab will store extra information to identify filesystems that have been mounted by a non-root user.

You then have the option to configure the system to allow the user-mounted filesystems to be unmounted only by the sysadmin or the user who originally mounted it. This is to prevent users interfering with each other in a multi-user system: user A might be trying to read files from a CD-ROM or memory stick, and user B might keep unmounting it.

MK
MK
Michelle_61
Frequent Advisor

Re: LVM device special name

Many thinks again, Matti.

However, from what you have mentioned, if fstab uses LABEL= or UUID= format to specify device names, then readlink cannot be used to match them in mtab. And that is a situation I cannot exclude.

My real intention is to search in mtab and try to match every FS in fstab. Originally it was written as:
for each line in fstab do
for each line in mtab do
if the mount point in fstab is exactly the same as the one in mtab then a match is found, go to the next line in fstab;
else if the device name in fstab is exactly the same as the one in mtab then a match is found, go to the next line in fstab;
else go to the next line in mtab.

The problem with the above code is, when a symlink is used in fstab for a LVM device, neither the corresponding mount points nor the corresponding device names are exactly the same. Hence the matching logic needs to be modified.

I think I still have another option to fix it. That is, to use the readlink function for a mount point in fstab, that should always return its real path which is listed in mtab, right? I try not to use lstat since it could hang on staled NFS.

Any suggestions will be appreciated!