home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lvmtlk12.zip / samples / querylvm.cpp < prev    next >
C/C++ Source or Header  |  2002-06-02  |  14KB  |  267 lines

  1. /* -------------------------------------------------------------------------- *
  2.  * QUERYLVM.CPP                                                               *
  3.  *                                                                            *
  4.  * Simple program to query volumes, drives & partitions using the LVM Engine. *
  5.  * By Alex Taylor, alex@eddie.cis.uoguelph.ca                                 *
  6.  * Some of this code is derived from lvmtest.cpp by John Martin Alfredsson.   *
  7.  *                                                                            *
  8.  * Syntax:                                                                    *
  9.  *      querylvm [options]                                                    *
  10.  *      where available options are                                           *
  11.  *      <none> : print volume information                                     *
  12.  *          -v : print volume information (verbose)                           *
  13.  *          -d : print disk and partition information                         *
  14.  *                                                                            *
  15.  * Returns:                                                                   *
  16.  *      0 on normal exit                                                      *
  17.  *      LVM Engine error-code (positive integer) on error                     *
  18.  *                                                                            *
  19.  *                                                                            *
  20.  * LICENSE:                                                                   *
  21.  *                                                                            *
  22.  *   This program is free software;  you can redistribute it and/or modify    *
  23.  *   it under the terms of the GNU General Public License as published by     *
  24.  *   the Free Software Foundation; either version 2 of the License, or        *
  25.  *   (at your option) any later version.                                      *
  26.  *                                                                            *
  27.  *   This program is distributed in the hope that it will be useful,          *
  28.  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of          *
  29.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See                *
  30.  *   the GNU General Public License for more details.                         *
  31.  *                                                                            *
  32.  *   You should have received a copy of the GNU General Public License        *
  33.  *   along with this program;  if not, write to the Free Software             *
  34.  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA  *
  35.  *                                                                            *
  36.  * -------------------------------------------------------------------------- */
  37.  
  38. #include <os2.h>
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include "lvm_intr.h"
  43.  
  44.  
  45. void ListVolumes( int verbosity );
  46. void ListDisks( void );
  47. int PartitionsByDrive( ADDRESS hdrv );
  48. void PrintHelp( void );
  49.  
  50. /* ------------------------------------------------------------------------- */
  51. int main( int argc, char *argv[] )
  52. {
  53.     CARDINAL32 Error_Code = 1;
  54.  
  55.                     /* Open the LVM engine */
  56.     Open_LVM_Engine( TRUE, &Error_Code );
  57.     if ( Error_Code != LVM_ENGINE_NO_ERROR ) {
  58.         printf("ERROR\nOpen_LVM_Engine returned %d\n", Error_Code );
  59.         return ( Error_Code );
  60.     }
  61.  
  62.     if ( argc > 1 ) {
  63.         if ( strncmp( argv[1], "-v", 2 ) == 0 ) ListVolumes( 1 );
  64.         else if ( strncmp( argv[1], "-d", 2 ) == 0 ) ListDisks();
  65.         else if ( strncmp( argv[1], "-?", 2 ) == 0 ) PrintHelp();
  66.         else { printf("Unrecognized option: %s\n", argv[1] ); PrintHelp(); }
  67.     } else ListVolumes( 0 );
  68.  
  69.     Close_LVM_Engine();
  70.     return ( 0 );
  71. }
  72.  
  73.  
  74.  
  75. /* ------------------------------------------------------------------------- *
  76.  * ListVolumes                                                               *
  77.  *    Prints a list of all defined volumes.                                  *
  78.  *                                                                           *
  79.  * Parameters:                                                               *
  80.  *    int verbosity - controls the detail level of the output.               *
  81.  *                     0 = omit 'Device Type' and 'Number of Partitions'.    *
  82.  *                    >0 = show all details.                                 *
  83.  *                                                                           *
  84.  * Returns: N/A                                                              *
  85.  * ------------------------------------------------------------------------- */
  86. void ListVolumes( int verbosity ) {
  87.     CARDINAL32 Error_Code = 1;
  88.     Volume_Control_Array vca;
  89.     Volume_Information_Record volinfo;
  90.     char dletter;
  91.     char devtype[ 8 ],
  92.          status[ 12 ],
  93.          voltype[ 14 ];
  94.     int index;
  95.     long vsize;
  96.  
  97.                     /* Get the array of volume handles */
  98.     vca = Get_Volume_Control_Data( &Error_Code );
  99.     if ( Error_Code != LVM_ENGINE_NO_ERROR )
  100.         printf("ERROR\nGet_Volume_Control_Data returned %d\n", Error_Code );
  101.     else {
  102.         if ( verbosity > 0 ) {
  103.             printf("====================================================================================\n");
  104.             printf("LOGICAL VOLUME SUMMARY\n");
  105.             printf("                Volume  FileSys  Size/MB  Parts   Device  Volume Type    Flags\n");
  106.             printf("------------------------------------------------------------------------------------\n");
  107.         } else {
  108.             printf("====================================================================\n");
  109.             printf("LOGICAL VOLUME SUMMARY\n");
  110.             printf("                Volume  FileSys  Size/MB  Volume Type    Flags\n");
  111.             printf("--------------------------------------------------------------------\n");
  112.         }
  113.                         /* Now get the volume information for each volume */
  114.         for ( index = 0; index < vca.Count; index++ ) {
  115.             volinfo = Get_Volume_Information( vca.Volume_Control_Data[ index ].Volume_Handle,
  116.                                               &Error_Code                                     );
  117.             if (Error_Code != 0)
  118.                 printf("ERROR\nGet_Volume_Information returned %d\n", Error_Code );
  119.             else {
  120.                 switch( vca.Volume_Control_Data[ index ].Device_Type ) {
  121.                     case 0:  strcpy( devtype, "HDD"); break;
  122.                     case 1:  strcpy( devtype, "PRM"); break;
  123.                     case 2:  strcpy( devtype, "CD-ROM"); break;
  124.                     case 3:  strcpy( devtype, "Network"); break;
  125.                     default: strcpy( devtype, "Unknown"); break;
  126.                 }
  127.                 switch ( volinfo.Status ) {
  128.                     case 1:  strcpy( status, "Bootable"); break;
  129.                     case 2:  strcpy( status, "Startable"); break;
  130.                     case 3:  strcpy( status, "Installable"); break;
  131.                     default: strcpy( status, "None"); break;
  132.                 }
  133.                 if ( vca.Volume_Control_Data[ index ].Compatibility_Volume == TRUE )
  134.                    strcpy( voltype, "Compatibility");
  135.                 else strcpy( voltype, "LVM");
  136.                 vsize = volinfo.Volume_Size / 2048;
  137.                 if ( volinfo.Current_Drive_Letter == 0 ) {
  138.                     dletter = '-';
  139.                 } else dletter = volinfo.Current_Drive_Letter;
  140.                 printf("%c %20s  %7s  %7d  ", dletter,
  141.                                               volinfo.Volume_Name,
  142.                                               volinfo.File_System_Name,
  143.                                               vsize                        );
  144.                 if ( verbosity > 0 )
  145.                     printf("%5d %8s  ", volinfo.Partition_Count, devtype );
  146.                 printf("%13s  %s\n", voltype, status );
  147.             }
  148.         }
  149.         if ( verbosity > 0 )
  150.             printf("====================================================================================\n");
  151.         else printf("====================================================================\n");
  152.         Free_Engine_Memory( vca.Volume_Control_Data );
  153.     }
  154. }
  155.  
  156.  
  157. /* ------------------------------------------------------------------------- *
  158.  * ListDisks                                                                 *
  159.  *    Prints a list of physical disks and their partitions.                  *
  160.  *                                                                           *
  161.  * Parameters: N/A                                                           *
  162.  *                                                                           *
  163.  * Returns: N/A                                                              *
  164.  * ------------------------------------------------------------------------- */
  165. void ListDisks( void ) {
  166.     CARDINAL32 Error_Code = 1;
  167.     Drive_Control_Array dca;
  168.     Drive_Control_Record dcr;
  169.     Drive_Information_Record drvinfo;
  170.     int index;
  171.     long dsize;
  172.  
  173.     dca = Get_Drive_Control_Data( &Error_Code );
  174.     if ( Error_Code != LVM_ENGINE_NO_ERROR )
  175.         printf("ERROR\nGet_Drive_Control_Data returned %d\n", Error_Code );
  176.     else {
  177.         printf("===============================================================================\n");
  178.         printf("PHYSICAL DISK SUMMARY\n");
  179.         printf(" #  Disk/Partition Name  Size/MB  P.Type   OS                Volume  Status\n");
  180.         for ( index = 0; index < dca.Count; index++ ) {
  181.             printf("-------------------------------------------------------------------------------\n");
  182.             dcr = dca.Drive_Control_Data[ index ];
  183.             drvinfo = Get_Drive_Status( dcr.Drive_Handle, &Error_Code );
  184.             if (Error_Code != 0)
  185.                 printf("ERROR\nGet_Drive_Status returned %d\n", Error_Code );
  186.             else {
  187.                 dsize = ( dcr.Drive_Size ) / 2048;
  188.                 printf("%2d %20s  %7d\n", dcr.Drive_Number,
  189.                                           drvinfo.Drive_Name,
  190.                                           dsize               );
  191.                 PartitionsByDrive( dcr.Drive_Handle );
  192.             }
  193.         }
  194.         printf("===============================================================================\n");
  195.         Free_Engine_Memory( dca.Drive_Control_Data );
  196.     }
  197. }
  198.  
  199.  
  200.  
  201. /* ------------------------------------------------------------------------- *
  202.  * PartitionsByDrive                                                         *
  203.  *    Lists all partitions that exist on the given disk.                     *
  204.  *                                                                           *
  205.  * Parameters:                                                               *
  206.  *    ADDRESS hdrv - handle of the disk to be listed.                        *
  207.  *                                                                           *
  208.  * Returns: int                                                              *
  209.  *    Number of partitions found.                                            *
  210.  * ------------------------------------------------------------------------- */
  211. int PartitionsByDrive( ADDRESS hdrv ) {
  212.     CARDINAL32 Error_Code = 1;
  213.     Partition_Information_Array pia;
  214.     Partition_Information_Record partinfo;
  215.     int idx;
  216.     long psize;
  217.     char ptype[ 8 ],
  218.          pstatus[ 11 ];
  219.  
  220.     pia = Get_Partitions( hdrv, &Error_Code );
  221.     if ( Error_Code != LVM_ENGINE_NO_ERROR ) {
  222.         printf("ERROR\nGet_Partitions returned %d\n", Error_Code );
  223.         return ( -1 );
  224.     }
  225.     else {
  226.         for ( idx = 0; idx < pia.Count; idx++ ) {
  227.             partinfo = pia.Partition_Array[ idx ];
  228.             if ( partinfo.Primary_Partition ) strcpy( ptype, "Primary");
  229.             else strcpy( ptype, "Logical");
  230.             switch ( partinfo.Partition_Status ) {
  231.                 case 0:  strcpy( pstatus, "Free Space"); break;
  232.                 case 1:  strcpy( pstatus, "In Use"); break;
  233.                 case 2:  strcpy( pstatus, "Available"); break;
  234.                 default: strcpy( pstatus, "Unknown"); break;
  235.             }
  236.             psize = partinfo.Usable_Partition_Size / 2048; /* convert sectors to MB */
  237.             printf("   %20s  %7d %8s  %2x  %20s  %s\n", partinfo.Partition_Name,
  238.                                                         psize,
  239.                                                         ptype,
  240.                                                         partinfo.OS_Flag,
  241.                                                         partinfo.Volume_Name,
  242.                                                         pstatus                  );
  243.         }
  244.         Free_Engine_Memory( pia.Partition_Array );
  245.         return ( pia.Count );
  246.     }
  247.  
  248. }
  249.  
  250.  
  251. /* ------------------------------------------------------------------------- *
  252.  * PrintHelp                                                                 *
  253.  *    Prints the command-line syntax help.                                   *
  254.  *                                                                           *
  255.  * Parameters: N/A                                                           *
  256.  *                                                                           *
  257.  * Returns: N/A                                                              *
  258.  * ------------------------------------------------------------------------- */
  259. void PrintHelp( void ) {
  260.     printf("Syntax:\n\tquerylvm [options]\n");
  261.     printf("\nwhere available options are\n");
  262.     printf("  <none> : print volume information\n");
  263.     printf("      -v : print volume information (verbose)\n");
  264.     printf("      -d : print disk and partition information\n\n");
  265. }
  266.  
  267.