2002-02-05 17:31:57 +03:00
/*
2004-03-30 23:35:44 +04:00
* Copyright ( C ) 2002 - 2004 Sistina Software , Inc . All rights reserved .
2007-08-21 00:55:30 +04:00
* Copyright ( C ) 2004 - 2007 Red Hat , Inc . All rights reserved .
2002-02-05 17:31:57 +03:00
*
2004-03-30 23:35:44 +04:00
* This file is part of LVM2 .
2002-02-05 17:31:57 +03:00
*
2004-03-30 23:35:44 +04:00
* This copyrighted material is made available to anyone wishing to use ,
* modify , copy , or redistribute it subject to the terms and conditions
2007-08-21 00:55:30 +04:00
* of the GNU Lesser General Public License v .2 .1 .
2004-03-30 23:35:44 +04:00
*
2007-08-21 00:55:30 +04:00
* You should have received a copy of the GNU Lesser General Public License
2004-03-30 23:35:44 +04:00
* along with this program ; if not , write to the Free Software Foundation ,
2016-01-21 13:49:46 +03:00
* Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 USA
2002-02-05 17:31:57 +03:00
*/
/*
* Changelog
*
* 05 / 02 / 2002 - First drop [ HM ]
*/
# include "tools.h"
2024-05-13 01:04:28 +03:00
static int _disks_found ;
static int _parts_found ;
static int _pv_disks_found ;
static int _pv_parts_found ;
static int _max_len ;
2002-02-05 17:31:57 +03:00
2018-06-15 19:03:55 +03:00
static int _get_max_dev_name_len ( struct cmd_context * cmd , struct dev_filter * filter )
2002-02-05 17:31:57 +03:00
{
2002-11-29 18:02:57 +03:00
int len = 0 ;
2002-12-20 02:25:55 +03:00
int maxlen = 0 ;
2002-02-12 00:00:35 +03:00
struct dev_iter * iter ;
struct device * dev ;
2002-02-05 17:31:57 +03:00
2024-07-08 23:32:41 +03:00
if ( ! ( iter = dev_iter_create ( filter , 1 ) ) ) {
2002-02-12 00:00:35 +03:00
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 */
2024-11-04 19:41:02 +03:00
while ( ( dev = dev_iter_get ( cmd , iter ) ) ) {
2002-11-29 18:02:57 +03:00
len = strlen ( dev_name ( dev ) ) ;
2002-12-20 02:25:55 +03:00
if ( len > maxlen )
maxlen = len ;
2002-02-12 00:00:35 +03:00
}
dev_iter_destroy ( iter ) ;
2002-02-05 17:31:57 +03:00
2002-12-20 02:25:55 +03:00
return maxlen ;
2002-11-29 18:02:57 +03:00
}
2002-02-05 17:31:57 +03:00
2002-11-29 18:02:57 +03:00
static void _count ( struct device * dev , int * disks , int * parts )
{
int c = dev_name ( dev ) [ strlen ( dev_name ( dev ) ) - 1 ] ;
if ( ! isdigit ( c ) )
( * disks ) + + ;
else
( * parts ) + + ;
}
2002-12-20 02:25:55 +03:00
static void _print ( struct cmd_context * cmd , const struct device * dev ,
uint64_t size , const char * what )
2002-11-29 18:02:57 +03:00
{
2024-05-13 01:04:28 +03:00
log_print ( " %-*s [%15s] %s " , _max_len , dev_name ( dev ) ,
2006-05-10 01:23:51 +04:00
display_size ( cmd , size ) , what ? : " " ) ;
2002-02-05 17:31:57 +03:00
}
2002-12-12 23:55:49 +03:00
static int _check_device ( struct cmd_context * cmd , struct device * dev )
2002-02-12 00:00:35 +03:00
{
2002-02-05 17:31:57 +03:00
uint64_t size ;
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 ) ) ;
2012-01-26 02:20:11 +04:00
size = 0 ;
2002-02-05 17:31:57 +03:00
}
2002-12-12 23:55:49 +03:00
_print ( cmd , dev , size , NULL ) ;
2024-05-13 01:04:28 +03:00
_count ( dev , & _disks_found , & _parts_found ) ;
2017-11-16 22:13:03 +03:00
2002-02-05 17:31:57 +03:00
return 1 ;
}
2010-07-09 19:34:40 +04:00
int lvmdiskscan ( struct cmd_context * cmd , int argc __attribute__ ( ( unused ) ) ,
char * * argv __attribute__ ( ( unused ) ) )
2002-02-12 00:00:35 +03:00
{
2002-11-29 18:02:57 +03:00
uint64_t size ;
2002-02-12 00:00:35 +03:00
struct dev_iter * iter ;
struct device * dev ;
2002-02-05 17:31:57 +03:00
2007-06-25 17:19:37 +04:00
/* initialise these here to avoid problems with the lvm shell */
2024-05-13 01:04:28 +03:00
_disks_found = 0 ;
_parts_found = 0 ;
_pv_disks_found = 0 ;
_pv_parts_found = 0 ;
2007-06-25 17:19:37 +04:00
2016-06-22 00:24:52 +03:00
if ( arg_is_set ( cmd , lvmpartition_ARG ) )
2007-06-28 21:33:44 +04:00
log_warn ( " WARNING: only considering LVM devices " ) ;
2002-11-29 18:02:57 +03:00
2018-05-04 01:12:07 +03:00
/* Call before using dev_iter which uses filters which want bcache data. */
label_scan ( cmd ) ;
2024-05-13 01:04:28 +03:00
_max_len = _get_max_dev_name_len ( cmd , cmd - > filter ) ;
2002-11-29 18:02:57 +03:00
2024-07-08 23:32:41 +03:00
if ( ! ( iter = dev_iter_create ( cmd - > filter , 0 ) ) ) {
2002-02-12 00:00:35 +03:00
log_error ( " dev_iter_create failed " ) ;
2003-10-22 02:06:07 +04:00
return ECMD_FAILED ;
2002-02-12 00:00:35 +03:00
}
2002-02-05 17:31:57 +03:00
2024-11-04 19:41:02 +03:00
while ( ( dev = dev_iter_get ( cmd , iter ) ) ) {
2018-02-15 01:43:26 +03:00
if ( lvmcache_has_dev_info ( dev ) ) {
2002-11-29 18:02:57 +03:00
if ( ! dev_get_size ( dev , & size ) ) {
log_error ( " Couldn't get size of \" %s \" " ,
dev_name ( dev ) ) ;
continue ;
}
2002-12-12 23:55:49 +03:00
_print ( cmd , dev , size , " LVM physical volume " ) ;
2024-05-13 01:04:28 +03:00
_count ( dev , & _pv_disks_found , & _pv_parts_found ) ;
2002-11-29 18:02:57 +03:00
continue ;
}
/* If user just wants PVs we are done */
2016-06-22 00:24:52 +03:00
if ( arg_is_set ( cmd , lvmpartition_ARG ) )
2002-11-29 18:02:57 +03:00
continue ;
/* What other device is it? */
2002-12-12 23:55:49 +03:00
if ( ! _check_device ( cmd , dev ) )
2002-11-29 18:02:57 +03:00
continue ;
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
2002-11-29 18:02:57 +03:00
/* Display totals */
2016-06-22 00:24:52 +03:00
if ( ! arg_is_set ( cmd , lvmpartition_ARG ) ) {
2002-11-29 18:02:57 +03:00
log_print ( " %d disk%s " ,
2024-05-13 01:04:28 +03:00
_disks_found , _disks_found = = 1 ? " " : " s " ) ;
2002-11-29 18:02:57 +03:00
log_print ( " %d partition%s " ,
2024-05-13 01:04:28 +03:00
_parts_found , _parts_found = = 1 ? " " : " s " ) ;
2002-11-29 18:02:57 +03:00
}
log_print ( " %d LVM physical volume whole disk%s " ,
2024-05-13 01:04:28 +03:00
_pv_disks_found , _pv_disks_found = = 1 ? " " : " s " ) ;
2002-11-29 18:02:57 +03:00
log_print ( " %d LVM physical volume%s " ,
2024-05-13 01:04:28 +03:00
_pv_parts_found , _pv_parts_found = = 1 ? " " : " s " ) ;
2002-02-05 17:31:57 +03:00
2003-10-22 02:06:07 +04:00
return ECMD_PROCESSED ;
2002-02-05 17:31:57 +03:00
}