2002-02-05 17:31:57 +03:00
/*
* Copyright ( C ) 2002 Sistina Software
*
* This file is released under the GPL .
*
*/
/*
* Changelog
*
* 05 / 02 / 2002 - First drop [ HM ]
*/
# include "tools.h"
2002-02-11 23:50:53 +03:00
int _get_max_dev_name_len ( struct dev_filter * filter ) ;
2002-02-12 00:00:35 +03:00
void _count ( struct device * , int * , int * ) ;
void _print ( struct device * , uint64_t , char * ) ;
int _check_device ( struct device * ) ;
2002-02-05 17:31:57 +03:00
int disks_found = 0 ;
int parts_found = 0 ;
int pv_disks_found = 0 ;
int pv_parts_found = 0 ;
int max_len ;
2002-02-11 23:50:53 +03:00
int lvmdiskscan ( struct cmd_context * cmd , int argc , char * * argv )
2002-02-05 17:31:57 +03:00
{
uint64_t size ;
2002-02-12 00:00:35 +03:00
struct dev_iter * iter ;
struct device * dev ;
2002-11-18 17:04:08 +03:00
struct label * label ;
2002-02-05 17:31:57 +03:00
2002-02-12 00:00:35 +03:00
if ( arg_count ( cmd , lvmpartition_ARG ) )
2002-02-05 17:31:57 +03:00
log_print ( " WARNING: only considering LVM devices " ) ;
2002-02-11 23:50:53 +03:00
max_len = _get_max_dev_name_len ( cmd - > filter ) ;
2002-02-05 17:31:57 +03:00
2002-02-12 00:00:35 +03:00
if ( ! ( iter = dev_iter_create ( cmd - > filter ) ) ) {
log_error ( " dev_iter_create failed " ) ;
return 0 ;
}
2002-02-05 17:31:57 +03:00
2002-02-12 00:00:35 +03:00
/* Do scan */
for ( dev = dev_iter_get ( iter ) ; dev ; dev = dev_iter_get ( iter ) ) {
2002-02-05 17:31:57 +03:00
/* Try if it is a PV first */
2002-11-18 17:04:08 +03:00
if ( ( label_read ( dev , & label ) ) ) {
2002-02-12 00:00:35 +03:00
if ( ! dev_get_size ( dev , & size ) ) {
2002-02-05 17:31:57 +03:00
log_error ( " Couldn't get size of \" %s \" " ,
dev_name ( dev ) ) ;
continue ;
}
_print ( dev , size , " LVM physical volume " ) ;
_count ( dev , & pv_disks_found , & pv_parts_found ) ;
continue ;
}
/* If user just wants PVs we are done */
2002-02-12 00:00:35 +03:00
if ( arg_count ( cmd , lvmpartition_ARG ) )
continue ;
2002-02-05 17:31:57 +03:00
/* What other device is it? */
2002-02-12 00:00:35 +03:00
if ( ! _check_device ( dev ) )
continue ;
}
dev_iter_destroy ( iter ) ;
2002-02-05 17:31:57 +03:00
/* Display totals */
2002-02-12 00:00:35 +03:00
if ( ! arg_count ( cmd , lvmpartition_ARG ) ) {
2002-02-05 17:31:57 +03:00
log_print ( " %d disk%s " ,
disks_found , disks_found = = 1 ? " " : " s " ) ;
log_print ( " %d partition%s " ,
parts_found , parts_found = = 1 ? " " : " s " ) ;
}
log_print ( " %d LVM physical volume whole disk%s " ,
pv_disks_found , pv_disks_found = = 1 ? " " : " s " ) ;
log_print ( " %d LVM physical volume%s " ,
pv_parts_found , pv_parts_found = = 1 ? " " : " s " ) ;
return 0 ;
}
2002-02-12 00:00:35 +03:00
int _check_device ( struct device * dev )
{
2002-02-05 17:31:57 +03:00
char buffer ;
uint64_t size ;
if ( ! dev_open ( dev , 0 ) ) {
return 0 ;
}
if ( dev_read ( dev , 0 , 1 , & buffer ) ! = 1 ) {
dev_close ( dev ) ;
return 0 ;
}
2002-02-12 00:00:35 +03:00
if ( ! dev_get_size ( dev , & size ) ) {
log_error ( " Couldn't get size of \" %s \" " , dev_name ( dev ) ) ;
2002-02-05 17:31:57 +03:00
}
_print ( dev , size , NULL ) ;
_count ( dev , & disks_found , & parts_found ) ;
if ( ! dev_close ( dev ) ) {
2002-02-12 00:00:35 +03:00
log_error ( " dev_close on \" %s \" failed " , dev_name ( dev ) ) ;
2002-02-05 17:31:57 +03:00
return 0 ;
}
return 1 ;
}
2002-02-12 00:00:35 +03:00
int _get_max_dev_name_len ( struct dev_filter * filter )
{
2002-02-05 17:31:57 +03:00
int len = 0 ;
int max_len = 0 ;
2002-02-12 00:00:35 +03:00
struct dev_iter * iter ;
struct device * dev ;
2002-02-05 17:31:57 +03:00
2002-02-12 00:00:35 +03:00
if ( ! ( iter = dev_iter_create ( filter ) ) ) {
log_error ( " dev_iter_create failed " ) ;
return 0 ;
}
2002-02-05 17:31:57 +03:00
2002-02-12 00:00:35 +03:00
/* Do scan */
for ( dev = dev_iter_get ( iter ) ; dev ; dev = dev_iter_get ( iter ) ) {
2002-02-05 17:31:57 +03:00
len = strlen ( dev_name ( dev ) ) ;
2002-02-12 00:00:35 +03:00
if ( len > max_len )
max_len = len ;
2002-02-05 17:31:57 +03:00
}
2002-02-12 00:00:35 +03:00
dev_iter_destroy ( iter ) ;
2002-02-05 17:31:57 +03:00
return max_len ;
}
2002-02-12 00:00:35 +03:00
void _count ( struct device * dev , int * disks , int * parts )
{
int c = dev_name ( dev ) [ strlen ( dev_name ( dev ) ) - 1 ] ;
2002-02-05 17:31:57 +03:00
2002-02-12 00:00:35 +03:00
if ( ! isdigit ( c ) )
( * disks ) + + ;
else
( * parts ) + + ;
2002-02-05 17:31:57 +03:00
}
2002-02-12 00:00:35 +03:00
void _print ( struct device * dev , uint64_t size , char * what )
{
char * dummy = display_size ( size / 2 , SIZE_SHORT ) ;
2002-02-05 17:31:57 +03:00
const char * name = dev_name ( dev ) ;
2002-11-28 18:27:59 +03:00
if ( ! what )
2002-02-05 17:31:57 +03:00
what = " " ;
log_print ( " %-*s [%15s] %s " , max_len , name , dummy , what ) ;
dbg_free ( dummy ) ;
}