Operating System - HP-UX
1822320 Members
5560 Online
109642 Solutions
New Discussion юеВ

How to get LVM info from inside a C program?

 
Rebecca Callan
Occasional Advisor

How to get LVM info from inside a C program?

Hi,

I need to get info on LVM configuration from inside a C/C++ program - just basic stuff, a list of volume groups and what disks are in each group, and a list of logical volumes in each group and the size of each if these. I can't use system commands from inside the program as it can cause blocking issues. This info may be available from an ioctl() but I am not sure which command to pass it. I can't find any documentation about this at all.

Does anyone know how I can get this info? Any info or ideas on this would be much appreciated.

Thanks,
Rebecca
12 REPLIES 12
Andy Monks
Honored Contributor

Re: How to get LVM info from inside a C program?

The safest way is to use the pstat() system call. It has lvm info available to it.

After that it's called reading the headers from the disk and working out what's going on.

The advantage pstat() has is that you don't need to be root to run it and your program will be release independant.
Rebecca Callan
Occasional Advisor

Re: How to get LVM info from inside a C program?

pstat doesn't provide the name of the logical volume (only the device number- Is there a way to determine the volume name from the device number),it doesn't specify which volume group the logical volume is in and it doesn't give size info.
Also for disks same thing - it doesn't give the name of the disk only the device number and it doesn't specify which volume group (if any) it is in, or any size info.

This is what I still need to find out. As for reading the disk labels is there any standard way of doing this - an ioctl() or other call to retrieve this info.

Thanks alot for your help,
Rebecca
James A. Donovan
Honored Contributor

Re: How to get LVM info from inside a C program?

I'm not 100% sure, but I don't think HP provides any user callable functions to get that information. Commands like vgdisplay and lvdisplay come statically compiled, located under /sbin. They are provided this way, so that they can be used when the system is in single-user mode.

If you absolutely must use function calls, you may need to install Veritas VxFS for HP-UX.
Remember, wherever you go, there you are...
Andy Monks
Honored Contributor

Re: How to get LVM info from inside a C program?

The system only knows about the major/minor number.

The best thing to do, would be to traverse the /dev directory using fstat(). Then you could match up the lvol name to the major/minor number.
James A. Donovan
Honored Contributor

Re: How to get LVM info from inside a C program?

hmmm...yes, I guess pstat_getlv() will get you some info on logical volumes, but it doesn't look easy to decipher.
Remember, wherever you go, there you are...
Andy Monks
Honored Contributor

Re: How to get LVM info from inside a C program?

It doesn't really tell you that much about the lvol using pstat, and looking at discs isn't any better.

Therefore I would suggest just running the 'lvm' program attached (for hp-ux 11). Written by a colleague with the source code for lvm!.
Andy Monks
Honored Contributor

Re: How to get LVM info from inside a C program?

hmmm, it didn't attach.
Rebecca Callan
Occasional Advisor

Re: How to get LVM info from inside a C program?

Thanks alot for your help so far:

I ran the executable you sent and it does help a bit - the main problem here is how to do I map the Volume Group ID which looks like this: 565706537 964590021 to either a device number or the volume group name (ie vg00).
Andy Monks
Honored Contributor

Re: How to get LVM info from inside a C program?

basically you don't.

If the vg is on an array for example, it's possible you might import the vg onto multiple systems (at different times) under different names.

None of the lvm structures know the names of anything. The lvol's are all lvol minor number only (which usually ties up with the lvol name, but not always).

The machine id (from uname) is in the output from the 'lvm' command, so at least see the last machine that had the vg.
Rebecca Callan
Occasional Advisor

Re: How to get LVM info from inside a C program?

What I really need to know is the names of the volume groups on the system and which disks belong to the group. I did a strings on the /etc/lvmtab file and this did give somoe useful info. I can use a similar algorithm to strings inside my program. But is this a reliable method of getting this information. Is /etc/lvmtab automatically updated to reflect changes in the system or is it only updated under certain conditions?

I have also done the same on the files under /etc/lvmconf which also give this information but from what I read these files are not updated automatically but only by the user - if the user doesn't do it they won't be updated - is this correct? Is ths the same for the lvmtab file?
Andy Monks
Honored Contributor

Re: How to get LVM info from inside a C program?

/etc/lvmtab 'should' always be correct, and by default will be.

lvmtab is a binary file, and therefore working out the file format wouldn't be difficult to achieve. Just dump it out in hex and compare a few machines, it's pretty simple.
Rebecca Callan
Occasional Advisor

Re: How to get LVM info from inside a C program?

Thanks for your help I have quite alot of what I need. The main info missing now is the size info. I can find out the size of a disk and a logical volume using ioctl() with the DIOC_CAPACITY cmd on the character special file ie /dev/rdsk/c1t2d0 or /dev/vg00/rlvol1. Is there a character special file for the volume group (that I could use to determine the vg size) I tried the /dev/vg00/group file but it didn't work (ioctl failed with errno = 22 (bad argument)). Is there any other way to determine the size of the volume group?

I would also like to get alloc/unalloc MB for the disks and the volume groups. This can be determined from the output of the executable you sent so I guess I can do this the same way - by reading the lvmrec, VGDA, etc from the disk character special file - but I don't know the format of the structures to read this info into. Do you know the structure definitions or can you point me to some documentation on this?