2001-12-11 15:16:58 +03:00
/*
2008-01-30 17:00:02 +03:00
* Copyright ( C ) 2001 - 2004 Sistina Software , Inc . All rights reserved .
2017-02-24 02:50:00 +03:00
* Copyright ( C ) 2004 - 2017 Red Hat , Inc . All rights reserved .
2001-12-11 15:16:58 +03:00
*
2004-03-30 23:35:44 +04:00
* This file is part of LVM2 .
*
* 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
2001-12-11 15:16:58 +03:00
*/
2018-06-08 15:40:53 +03:00
# include "base/memory/zalloc.h"
2018-05-14 12:30:20 +03:00
# include "lib/misc/lib.h"
2001-12-11 15:18:56 +03:00
# include "import-export.h"
2018-12-14 15:10:25 +03:00
# include "lvm-version.h"
2018-05-14 12:30:20 +03:00
# include "lib/metadata/metadata.h"
# include "lib/display/display.h"
# include "lib/misc/lvm-string.h"
# include "lib/metadata/segtype.h"
# include "lib/format_text/text_export.h"
# include "lib/commands/toolcontext.h"
device usage based on devices file
The LVM devices file lists devices that lvm can use. The default
file is /etc/lvm/devices/system.devices, and the lvmdevices(8)
command is used to add or remove device entries. If the file
does not exist, or if lvm.conf includes use_devicesfile=0, then
lvm will not use a devices file. When the devices file is in use,
the regex filter is not used, and the filter settings in lvm.conf
or on the command line are ignored.
LVM records devices in the devices file using hardware-specific
IDs, such as the WWID, and attempts to use subsystem-specific
IDs for virtual device types. These device IDs are also written
in the VG metadata. When no hardware or virtual ID is available,
lvm falls back using the unstable device name as the device ID.
When devnames are used, lvm performs extra scanning to find
devices if their devname changes, e.g. after reboot.
When proper device IDs are used, an lvm command will not look
at devices outside the devices file, but when devnames are used
as a fallback, lvm will scan devices outside the devices file
to locate PVs on renamed devices. A config setting
search_for_devnames can be used to control the scanning for
renamed devname entries.
Related to the devices file, the new command option
--devices <devnames> allows a list of devices to be specified for
the command to use, overriding the devices file. The listed
devices act as a sort of devices file in terms of limiting which
devices lvm will see and use. Devices that are not listed will
appear to be missing to the lvm command.
Multiple devices files can be kept in /etc/lvm/devices, which
allows lvm to be used with different sets of devices, e.g.
system devices do not need to be exposed to a specific application,
and the application can use lvm on its own set of devices that are
not exposed to the system. The option --devicesfile <filename> is
used to select the devices file to use with the command. Without
the option set, the default system devices file is used.
Setting --devicesfile "" causes lvm to not use a devices file.
An existing, empty devices file means lvm will see no devices.
The new command vgimportdevices adds PVs from a VG to the devices
file and updates the VG metadata to include the device IDs.
vgimportdevices -a will import all VGs into the system devices file.
LVM commands run by dmeventd not use a devices file by default,
and will look at all devices on the system. A devices file can
be created for dmeventd (/etc/lvm/devices/dmeventd.devices) If
this file exists, lvm commands run by dmeventd will use it.
Internal implementaion:
- device_ids_read - read the devices file
. add struct dev_use (du) to cmd->use_devices for each devices file entry
- dev_cache_scan - get /dev entries
. add struct device (dev) to dev_cache for each device on the system
- device_ids_match - match devices file entries to /dev entries
. match each du on cmd->use_devices to a dev in dev_cache, using device ID
. on match, set du->dev, dev->id, dev->flags MATCHED_USE_ID
- label_scan - read lvm headers and metadata from devices
. filters are applied, those that do not need data from the device
. filter-deviceid skips devs without MATCHED_USE_ID, i.e.
skips /dev entries that are not listed in the devices file
. read lvm label from dev
. filters are applied, those that use data from the device
. read lvm metadata from dev
. add info/vginfo structs for PVs/VGs (info is "lvmcache")
- device_ids_find_renamed_devs - handle devices with unstable devname ID
where devname changed
. this step only needed when devs do not have proper device IDs,
and their dev names change, e.g. after reboot sdb becomes sdc.
. detect incorrect match because PVID in the devices file entry
does not match the PVID found when the device was read above
. undo incorrect match between du and dev above
. search system devices for new location of PVID
. update devices file with new devnames for PVIDs on renamed devices
. label_scan the renamed devs
- continue with command processing
2020-06-23 21:25:41 +03:00
# include "lib/device/device_id.h"
2018-05-14 12:30:20 +03:00
# include "libdaemon/client/config-util.h"
2001-12-11 15:16:58 +03:00
2001-12-20 14:52:54 +03:00
# include <stdarg.h>
# include <time.h>
2002-11-18 17:04:08 +03:00
# include <sys/utsname.h>
2001-12-11 15:16:58 +03:00
2002-11-18 17:04:08 +03:00
struct formatter ;
2011-04-08 18:21:34 +04:00
__attribute__ ( ( format ( printf , 3 , 0 ) ) )
2002-11-18 17:04:08 +03:00
typedef int ( * out_with_comment_fn ) ( struct formatter * f , const char * comment ,
const char * fmt , va_list ap ) ;
2005-06-07 15:00:07 +04:00
typedef int ( * nl_fn ) ( struct formatter * f ) ;
2005-10-23 04:14:48 +04:00
/*
* Macro for formatted output .
* out_with_comment_fn returns - 1 if data didn ' t fit and buffer was expanded .
* Then argument list is reset and out_with_comment_fn is called again .
*/
# define _out_with_comment(f, buffer, fmt, ap) \
2008-01-30 17:00:02 +03:00
do { \
2005-10-23 04:14:48 +04:00
va_start ( ap , fmt ) ; \
2017-07-19 17:17:30 +03:00
r = ( f ) - > out_with_comment ( ( f ) , ( buffer ) , ( fmt ) , ap ) ; \
2005-10-23 04:14:48 +04:00
va_end ( ap ) ; \
} while ( r = = - 1 )
2002-11-18 17:04:08 +03:00
/*
* The first half of this file deals with
* exporting the vg , ie . writing it to a file .
*/
2001-12-11 15:16:58 +03:00
struct formatter {
2005-10-17 03:03:59 +04:00
struct dm_pool * mem ; /* pv names allocated from here */
struct dm_hash_table * pv_names ; /* dev_name -> pv_name (eg, pv1) */
2001-12-11 15:16:58 +03:00
2002-11-18 17:04:08 +03:00
union {
FILE * fp ; /* where we're writing to */
struct {
2005-06-07 15:00:07 +04:00
char * start ;
2002-11-18 17:04:08 +03:00
uint32_t size ;
uint32_t used ;
} buf ;
} data ;
out_with_comment_fn out_with_comment ;
nl_fn nl ;
2001-12-20 19:05:14 +03:00
2002-11-18 17:04:08 +03:00
int indent ; /* current level of indentation */
2001-12-20 19:05:14 +03:00
int error ;
2002-11-18 17:04:08 +03:00
int header ; /* 1 => comments at start; 0 => end */
2001-12-11 15:16:58 +03:00
} ;
2002-11-18 17:04:08 +03:00
static struct utsname _utsname ;
static void _init ( void )
{
static int _initialised = 0 ;
if ( _initialised )
return ;
if ( uname ( & _utsname ) ) {
log_error ( " uname failed: %s " , strerror ( errno ) ) ;
memset ( & _utsname , 0 , sizeof ( _utsname ) ) ;
}
_initialised = 1 ;
}
2001-12-20 19:05:14 +03:00
/*
* Formatting functions .
*/
2001-12-11 15:16:58 +03:00
# define MAX_INDENT 5
static void _inc_indent ( struct formatter * f )
{
if ( + + f - > indent > MAX_INDENT )
f - > indent = MAX_INDENT ;
}
static void _dec_indent ( struct formatter * f )
{
2001-12-20 19:05:14 +03:00
if ( ! f - > indent - - ) {
2009-12-16 22:22:11 +03:00
log_error ( INTERNAL_ERROR " problem tracking indentation " ) ;
2001-12-11 15:16:58 +03:00
f - > indent = 0 ;
}
}
/*
* Newline function for prettier layout .
*/
2005-06-07 15:00:07 +04:00
static int _nl_file ( struct formatter * f )
2001-12-11 15:16:58 +03:00
{
2002-11-18 17:04:08 +03:00
fprintf ( f - > data . fp , " \n " ) ;
2005-06-07 15:00:07 +04:00
return 1 ;
2002-11-18 17:04:08 +03:00
}
2005-10-23 04:14:48 +04:00
static int _extend_buffer ( struct formatter * f )
2002-11-18 17:04:08 +03:00
{
2005-06-07 15:00:07 +04:00
char * newbuf ;
2002-11-18 17:04:08 +03:00
2017-12-11 18:32:53 +03:00
log_debug_metadata ( " Doubling metadata output buffer to " FMTu32 ,
2013-01-08 02:30:29 +04:00
f - > data . buf . size * 2 ) ;
2018-06-08 15:40:53 +03:00
if ( ! ( newbuf = realloc ( f - > data . buf . start ,
2018-04-20 18:43:50 +03:00
f - > data . buf . size * 2 ) ) ) {
log_error ( " Buffer reallocation failed. " ) ;
return 0 ;
}
2019-07-01 23:00:34 +03:00
memset ( newbuf + f - > data . buf . size , 0 , f - > data . buf . size ) ;
2005-10-23 04:14:48 +04:00
f - > data . buf . start = newbuf ;
f - > data . buf . size * = 2 ;
return 1 ;
}
static int _nl_raw ( struct formatter * f )
{
/* If metadata doesn't fit, extend buffer */
if ( ( f - > data . buf . used + 2 > f - > data . buf . size ) & &
2008-01-30 16:19:47 +03:00
( ! _extend_buffer ( f ) ) )
return_0 ;
2005-06-07 15:00:07 +04:00
* ( f - > data . buf . start + f - > data . buf . used ) = ' \n ' ;
2002-11-18 17:04:08 +03:00
f - > data . buf . used + = 1 ;
2005-06-07 15:00:07 +04:00
* ( f - > data . buf . start + f - > data . buf . used ) = ' \0 ' ;
return 1 ;
2001-12-11 15:16:58 +03:00
}
# define COMMENT_TAB 6
2011-04-08 18:21:34 +04:00
__attribute__ ( ( format ( printf , 3 , 0 ) ) )
2002-11-18 17:04:08 +03:00
static int _out_with_comment_file ( struct formatter * f , const char * comment ,
const char * fmt , va_list ap )
2001-12-11 15:16:58 +03:00
{
int i ;
char white_space [ MAX_INDENT + 1 ] ;
2002-11-18 17:04:08 +03:00
if ( ferror ( f - > data . fp ) )
return 0 ;
2001-12-20 19:05:14 +03:00
2001-12-11 15:16:58 +03:00
for ( i = 0 ; i < f - > indent ; i + + )
white_space [ i ] = ' \t ' ;
white_space [ i ] = ' \0 ' ;
2007-04-27 23:26:57 +04:00
fputs ( white_space , f - > data . fp ) ;
2002-11-18 17:04:08 +03:00
i = vfprintf ( f - > data . fp , fmt , ap ) ;
2001-12-11 15:16:58 +03:00
if ( comment ) {
/*
* line comments up if possible .
*/
i + = 8 * f - > indent ;
i / = 8 ;
i + + ;
do
2002-11-18 17:04:08 +03:00
fputc ( ' \t ' , f - > data . fp ) ;
2001-12-11 15:16:58 +03:00
while ( + + i < COMMENT_TAB ) ;
2007-04-27 23:26:57 +04:00
fputs ( comment , f - > data . fp ) ;
2001-12-11 15:16:58 +03:00
}
2002-11-18 17:04:08 +03:00
fputc ( ' \n ' , f - > data . fp ) ;
return 1 ;
}
2011-04-08 18:21:34 +04:00
__attribute__ ( ( format ( printf , 3 , 0 ) ) )
2006-05-11 21:58:58 +04:00
static int _out_with_comment_raw ( struct formatter * f ,
2010-07-09 19:34:40 +04:00
const char * comment __attribute__ ( ( unused ) ) ,
2002-11-18 17:04:08 +03:00
const char * fmt , va_list ap )
{
int n ;
2014-09-15 17:33:56 +04:00
va_list apc ;
2002-11-18 17:04:08 +03:00
2014-09-15 17:33:56 +04:00
va_copy ( apc , ap ) ;
2005-06-07 15:00:07 +04:00
n = vsnprintf ( f - > data . buf . start + f - > data . buf . used ,
2014-09-15 17:33:56 +04:00
f - > data . buf . size - f - > data . buf . used , fmt , apc ) ;
va_end ( apc ) ;
2002-11-18 17:04:08 +03:00
2005-10-23 04:14:48 +04:00
/* If metadata doesn't fit, extend buffer */
2005-06-07 15:00:07 +04:00
if ( n < 0 | | ( n + f - > data . buf . used + 2 > f - > data . buf . size ) ) {
2008-01-30 16:19:47 +03:00
if ( ! _extend_buffer ( f ) )
return_0 ;
2005-10-23 04:14:48 +04:00
return - 1 ; /* Retry */
2005-06-07 15:00:07 +04:00
}
2002-11-18 17:04:08 +03:00
f - > data . buf . used + = n ;
2005-06-07 15:00:07 +04:00
outnl ( f ) ;
2002-11-18 17:04:08 +03:00
return 1 ;
2001-12-11 15:16:58 +03:00
}
/*
* Formats a string , converting a size specified
* in 512 - byte sectors to a more human readable
* form ( eg , megabytes ) . We may want to lift this
* for other code to use .
*/
static int _sectors_to_units ( uint64_t sectors , char * buffer , size_t s )
{
2024-05-04 01:25:33 +03:00
static const char _units [ ] [ 16 ] = {
2001-12-11 15:16:58 +03:00
" Kilobytes " ,
" Megabytes " ,
" Gigabytes " ,
2002-11-18 17:04:08 +03:00
" Terabytes " ,
2006-11-10 21:24:11 +03:00
" Petabytes " ,
" Exabytes " ,
2002-04-24 22:20:51 +04:00
} ;
2001-12-11 15:16:58 +03:00
2024-05-14 16:36:34 +03:00
unsigned i ;
2001-12-11 15:16:58 +03:00
double d = ( double ) sectors ;
/* to convert to K */
d / = 2.0 ;
2024-05-04 01:25:33 +03:00
for ( i = 0 ; ( d > 1024.0 ) & & i < DM_ARRAY_SIZE ( _units ) ; + + i )
2001-12-11 15:16:58 +03:00
d / = 1024.0 ;
2006-08-21 16:54:53 +04:00
return dm_snprintf ( buffer , s , " # %g %s " , d , _units [ i ] ) > 0 ;
2001-12-11 15:16:58 +03:00
}
2009-11-03 14:00:46 +03:00
/* increment indention level */
void out_inc_indent ( struct formatter * f )
{
_inc_indent ( f ) ;
}
/* decrement indention level */
void out_dec_indent ( struct formatter * f )
{
_dec_indent ( f ) ;
}
/* insert new line */
int out_newline ( struct formatter * f )
{
return f - > nl ( f ) ;
}
2001-12-11 15:16:58 +03:00
/*
* Appends a comment giving a size in more easily
* readable form ( eg , 4 M instead of 8096 ) .
*/
2004-05-05 01:25:57 +04:00
int out_size ( struct formatter * f , uint64_t size , const char * fmt , . . . )
2001-12-11 15:16:58 +03:00
{
char buffer [ 64 ] ;
va_list ap ;
2002-11-18 17:04:08 +03:00
int r ;
2001-12-11 15:16:58 +03:00
2002-11-18 17:04:08 +03:00
if ( ! _sectors_to_units ( size , buffer , sizeof ( buffer ) ) )
return 0 ;
2001-12-11 15:16:58 +03:00
2005-10-23 04:14:48 +04:00
_out_with_comment ( f , buffer , fmt , ap ) ;
2002-11-18 17:04:08 +03:00
return r ;
2001-12-11 15:16:58 +03:00
}
/*
* Appends a comment indicating that the line is
* only a hint .
*/
2004-05-05 01:25:57 +04:00
int out_hint ( struct formatter * f , const char * fmt , . . . )
2001-12-11 15:16:58 +03:00
{
va_list ap ;
2002-11-18 17:04:08 +03:00
int r ;
2001-12-11 15:16:58 +03:00
2005-10-23 04:14:48 +04:00
_out_with_comment ( f , " # Hint only " , fmt , ap ) ;
2002-11-18 17:04:08 +03:00
return r ;
2001-12-11 15:16:58 +03:00
}
2007-11-09 19:51:54 +03:00
/*
2010-01-07 17:45:28 +03:00
* The normal output function with comment
2007-11-09 19:51:54 +03:00
*/
2010-01-07 17:45:28 +03:00
int out_text_with_comment ( struct formatter * f , const char * comment , const char * fmt , . . . )
2007-11-09 19:51:54 +03:00
{
va_list ap ;
int r ;
_out_with_comment ( f , comment , fmt , ap ) ;
return r ;
}
2001-12-11 15:16:58 +03:00
/*
* The normal output function .
*/
2004-05-05 01:25:57 +04:00
int out_text ( struct formatter * f , const char * fmt , . . . )
2001-12-11 15:16:58 +03:00
{
va_list ap ;
2002-11-18 17:04:08 +03:00
int r ;
2001-12-11 15:16:58 +03:00
2005-10-23 04:14:48 +04:00
_out_with_comment ( f , NULL , fmt , ap ) ;
2002-11-18 17:04:08 +03:00
return r ;
2001-12-11 15:16:58 +03:00
}
2009-10-16 21:41:49 +04:00
static int _out_line ( const char * line , void * _f ) {
struct formatter * f = ( struct formatter * ) _f ;
return out_text ( f , " %s " , line ) ;
}
2011-08-30 18:55:15 +04:00
int out_config_node ( struct formatter * f , const struct dm_config_node * cn )
2009-10-16 21:41:49 +04:00
{
2011-08-30 18:55:15 +04:00
return dm_config_write_node ( cn , _out_line , f ) ;
2009-10-16 21:41:49 +04:00
}
2015-02-23 20:54:47 +03:00
static int _print_header ( struct cmd_context * cmd , struct formatter * f ,
2006-05-11 21:58:58 +04:00
const char * desc )
2001-12-11 15:16:58 +03:00
{
2008-03-12 19:03:22 +03:00
char * buf ;
2001-12-20 14:52:54 +03:00
time_t t ;
t = time ( NULL ) ;
2007-11-04 22:16:34 +03:00
outf ( f , " # Generated by LVM2 version %s: %s " , LVM_VERSION , ctime ( & t ) ) ;
2004-05-05 01:25:57 +04:00
outf ( f , CONTENTS_FIELD " = \" " CONTENTS_VALUE " \" " ) ;
outf ( f , FORMAT_VERSION_FIELD " = %d " , FORMAT_VERSION_VALUE ) ;
2005-06-07 15:00:07 +04:00
outnl ( f ) ;
2002-02-08 14:13:47 +03:00
2015-11-05 20:32:32 +03:00
buf = alloca ( dm_escaped_len ( desc ) ) ;
2011-08-30 18:55:15 +04:00
outf ( f , " description = \" %s \" " , dm_escape_double_quotes ( buf , desc ) ) ;
2005-06-07 15:00:07 +04:00
outnl ( f ) ;
2014-07-21 18:54:20 +04:00
outf ( f , " creation_host = \" %s \" \t # %s %s %s %s %s " , _utsname . nodename ,
_utsname . sysname , _utsname . nodename , _utsname . release ,
_utsname . version , _utsname . machine ) ;
2015-02-23 20:54:47 +03:00
if ( cmd - > system_id & & * cmd - > system_id )
2015-02-23 22:19:48 +03:00
outf ( f , " creation_host_system_id = \" %s \" " , cmd - > system_id ) ;
2017-02-15 19:01:49 +03:00
outf ( f , " creation_time = " FMTu64 " \t # %s " , ( uint64_t ) t , ctime ( & t ) ) ;
2002-02-08 14:13:47 +03:00
2001-12-11 15:16:58 +03:00
return 1 ;
}
2024-05-07 14:58:21 +03:00
static int _print_flag_config ( struct formatter * f , uint64_t status , enum pv_vg_lv_e type )
2008-07-10 15:30:57 +04:00
{
char buffer [ 4096 ] ;
2017-05-26 16:47:17 +03:00
if ( ! print_flags ( buffer , sizeof ( buffer ) , type , STATUS_FLAG , status ) )
2008-07-10 15:30:57 +04:00
return_0 ;
outf ( f , " status = %s " , buffer ) ;
2017-05-26 16:47:17 +03:00
if ( ! print_flags ( buffer , sizeof ( buffer ) , type , COMPATIBLE_FLAG , status ) )
2008-07-10 15:30:57 +04:00
return_0 ;
outf ( f , " flags = %s " , buffer ) ;
return 1 ;
}
2015-05-06 14:58:17 +03:00
static char * _alloc_printed_str_list ( struct dm_list * list )
{
struct dm_str_list * sl ;
int first = 1 ;
size_t size = 0 ;
char * buffer , * buf ;
dm_list_iterate_items ( sl , list )
/* '"' + item + '"' + ',' + ' ' */
size + = strlen ( sl - > str ) + 4 ;
/* '[' + ']' + '\0' */
size + = 3 ;
2018-06-08 15:40:53 +03:00
if ( ! ( buffer = buf = malloc ( size ) ) ) {
2015-05-06 14:58:17 +03:00
log_error ( " Could not allocate memory for string list buffer. " ) ;
return NULL ;
}
if ( ! emit_to_buffer ( & buf , & size , " [ " ) )
goto_bad ;
dm_list_iterate_items ( sl , list ) {
if ( ! first ) {
if ( ! emit_to_buffer ( & buf , & size , " , " ) )
goto_bad ;
} else
first = 0 ;
if ( ! emit_to_buffer ( & buf , & size , " \" %s \" " , sl - > str ) )
goto_bad ;
}
if ( ! emit_to_buffer ( & buf , & size , " ] " ) )
goto_bad ;
return buffer ;
bad :
2018-06-08 15:40:53 +03:00
free ( buffer ) ;
2015-05-06 14:58:17 +03:00
return_NULL ;
}
2010-11-29 15:19:58 +03:00
2015-05-06 15:06:44 +03:00
static int _out_list ( struct formatter * f , struct dm_list * list ,
const char * list_name )
2010-11-29 15:19:58 +03:00
{
2015-05-06 15:06:44 +03:00
char * buffer ;
2010-11-29 15:19:58 +03:00
2015-05-06 15:06:44 +03:00
if ( ! dm_list_empty ( list ) ) {
if ( ! ( buffer = _alloc_printed_str_list ( list ) ) )
2010-11-29 15:19:58 +03:00
return_0 ;
2015-05-06 15:06:44 +03:00
if ( ! out_text ( f , " %s = %s " , list_name , buffer ) ) {
2018-06-08 15:40:53 +03:00
free ( buffer ) ;
2010-11-29 15:19:58 +03:00
return_0 ;
}
2018-06-08 15:40:53 +03:00
free ( buffer ) ;
2010-11-29 15:19:58 +03:00
}
return 1 ;
}
2001-12-11 15:16:58 +03:00
static int _print_vg ( struct formatter * f , struct volume_group * vg )
{
2004-03-08 20:19:15 +03:00
char buffer [ 4096 ] ;
2015-03-04 03:30:26 +03:00
const struct format_type * fmt = NULL ;
2015-03-05 19:23:16 +03:00
uint64_t status = vg - > status ;
2001-12-11 15:16:58 +03:00
2008-01-30 16:19:47 +03:00
if ( ! id_write_format ( & vg - > id , buffer , sizeof ( buffer ) ) )
return_0 ;
2001-12-11 15:16:58 +03:00
2004-05-05 01:25:57 +04:00
outf ( f , " id = \" %s \" " , buffer ) ;
2001-12-11 15:16:58 +03:00
2004-05-05 01:25:57 +04:00
outf ( f , " seqno = %u " , vg - > seqno ) ;
2004-03-08 20:19:15 +03:00
2015-03-04 03:30:26 +03:00
if ( vg - > original_fmt )
fmt = vg - > original_fmt ;
else if ( vg - > fid )
fmt = vg - > fid - > fmt ;
if ( fmt )
outfc ( f , " # informational " , " format = \" %s \" " , fmt - > name ) ;
2012-02-23 17:11:07 +04:00
system_id: make new VGs read-only for old lvm versions
Previous versions of lvm will not obey the restrictions
imposed by the new system_id, and would allow such a VG
to be written. So, a VG with a new system_id is further
changed to force previous lvm versions to treat it as
read-only. This is done by removing the WRITE flag from
the metadata status line of these VGs, and putting a new
WRITE_LOCKED flag in the flags line of the metadata.
Versions of lvm that recognize WRITE_LOCKED, also obey the
new system_id. For these lvm versions, WRITE_LOCKED is
identical to WRITE, and the rules associated with matching
system_id's are imposed.
A new VG lock_type field is also added that causes the same
WRITE/WRITE_LOCKED transformation when set. A previous
version of lvm will also see a VG with lock_type as read-only.
Versions of lvm that recognize WRITE_LOCKED, must also obey
the lock_type setting. Until the lock_type feature is added,
lvm will fail to read any VG with lock_type set and report an
error about an unsupported lock_type. Once the lock_type
feature is added, lvm will allow VGs with lock_type to be
used according to the rules imposed by the lock_type.
When both system_id and lock_type settings are removed, a VG
is written with the old WRITE status flag, and without the
new WRITE_LOCKED flag. This allows old versions of lvm to
use the VG as before.
2015-03-04 20:30:53 +03:00
/*
2015-03-09 21:53:22 +03:00
* Removing WRITE and adding LVM_WRITE_LOCKED makes it read - only
* to old versions of lvm that only look for LVM_WRITE .
system_id: make new VGs read-only for old lvm versions
Previous versions of lvm will not obey the restrictions
imposed by the new system_id, and would allow such a VG
to be written. So, a VG with a new system_id is further
changed to force previous lvm versions to treat it as
read-only. This is done by removing the WRITE flag from
the metadata status line of these VGs, and putting a new
WRITE_LOCKED flag in the flags line of the metadata.
Versions of lvm that recognize WRITE_LOCKED, also obey the
new system_id. For these lvm versions, WRITE_LOCKED is
identical to WRITE, and the rules associated with matching
system_id's are imposed.
A new VG lock_type field is also added that causes the same
WRITE/WRITE_LOCKED transformation when set. A previous
version of lvm will also see a VG with lock_type as read-only.
Versions of lvm that recognize WRITE_LOCKED, must also obey
the lock_type setting. Until the lock_type feature is added,
lvm will fail to read any VG with lock_type set and report an
error about an unsupported lock_type. Once the lock_type
feature is added, lvm will allow VGs with lock_type to be
used according to the rules imposed by the lock_type.
When both system_id and lock_type settings are removed, a VG
is written with the old WRITE status flag, and without the
new WRITE_LOCKED flag. This allows old versions of lvm to
use the VG as before.
2015-03-04 20:30:53 +03:00
*/
2015-03-05 19:23:16 +03:00
if ( ( status & LVM_WRITE ) & & vg_flag_write_locked ( vg ) ) {
status & = ~ LVM_WRITE ;
status | = LVM_WRITE_LOCKED ;
system_id: make new VGs read-only for old lvm versions
Previous versions of lvm will not obey the restrictions
imposed by the new system_id, and would allow such a VG
to be written. So, a VG with a new system_id is further
changed to force previous lvm versions to treat it as
read-only. This is done by removing the WRITE flag from
the metadata status line of these VGs, and putting a new
WRITE_LOCKED flag in the flags line of the metadata.
Versions of lvm that recognize WRITE_LOCKED, also obey the
new system_id. For these lvm versions, WRITE_LOCKED is
identical to WRITE, and the rules associated with matching
system_id's are imposed.
A new VG lock_type field is also added that causes the same
WRITE/WRITE_LOCKED transformation when set. A previous
version of lvm will also see a VG with lock_type as read-only.
Versions of lvm that recognize WRITE_LOCKED, must also obey
the lock_type setting. Until the lock_type feature is added,
lvm will fail to read any VG with lock_type set and report an
error about an unsupported lock_type. Once the lock_type
feature is added, lvm will allow VGs with lock_type to be
used according to the rules imposed by the lock_type.
When both system_id and lock_type settings are removed, a VG
is written with the old WRITE status flag, and without the
new WRITE_LOCKED flag. This allows old versions of lvm to
use the VG as before.
2015-03-04 20:30:53 +03:00
}
2015-03-05 19:23:16 +03:00
if ( ! _print_flag_config ( f , status , VG_FLAGS ) )
2008-01-30 16:19:47 +03:00
return_0 ;
2004-03-08 20:19:15 +03:00
2015-05-06 15:06:44 +03:00
if ( ! _out_list ( f , & vg - > tags , " tags " ) )
2010-11-29 15:19:58 +03:00
return_0 ;
2015-03-04 04:00:51 +03:00
2002-01-29 20:23:33 +03:00
if ( vg - > system_id & & * vg - > system_id )
2004-05-05 01:25:57 +04:00
outf ( f , " system_id = \" %s \" " , vg - > system_id ) ;
2004-03-08 20:19:15 +03:00
2015-03-05 23:00:44 +03:00
if ( vg - > lock_type ) {
system_id: make new VGs read-only for old lvm versions
Previous versions of lvm will not obey the restrictions
imposed by the new system_id, and would allow such a VG
to be written. So, a VG with a new system_id is further
changed to force previous lvm versions to treat it as
read-only. This is done by removing the WRITE flag from
the metadata status line of these VGs, and putting a new
WRITE_LOCKED flag in the flags line of the metadata.
Versions of lvm that recognize WRITE_LOCKED, also obey the
new system_id. For these lvm versions, WRITE_LOCKED is
identical to WRITE, and the rules associated with matching
system_id's are imposed.
A new VG lock_type field is also added that causes the same
WRITE/WRITE_LOCKED transformation when set. A previous
version of lvm will also see a VG with lock_type as read-only.
Versions of lvm that recognize WRITE_LOCKED, must also obey
the lock_type setting. Until the lock_type feature is added,
lvm will fail to read any VG with lock_type set and report an
error about an unsupported lock_type. Once the lock_type
feature is added, lvm will allow VGs with lock_type to be
used according to the rules imposed by the lock_type.
When both system_id and lock_type settings are removed, a VG
is written with the old WRITE status flag, and without the
new WRITE_LOCKED flag. This allows old versions of lvm to
use the VG as before.
2015-03-04 20:30:53 +03:00
outf ( f , " lock_type = \" %s \" " , vg - > lock_type ) ;
2015-03-05 23:00:44 +03:00
if ( vg - > lock_args )
outf ( f , " lock_args = \" %s \" " , vg - > lock_args ) ;
}
system_id: make new VGs read-only for old lvm versions
Previous versions of lvm will not obey the restrictions
imposed by the new system_id, and would allow such a VG
to be written. So, a VG with a new system_id is further
changed to force previous lvm versions to treat it as
read-only. This is done by removing the WRITE flag from
the metadata status line of these VGs, and putting a new
WRITE_LOCKED flag in the flags line of the metadata.
Versions of lvm that recognize WRITE_LOCKED, also obey the
new system_id. For these lvm versions, WRITE_LOCKED is
identical to WRITE, and the rules associated with matching
system_id's are imposed.
A new VG lock_type field is also added that causes the same
WRITE/WRITE_LOCKED transformation when set. A previous
version of lvm will also see a VG with lock_type as read-only.
Versions of lvm that recognize WRITE_LOCKED, must also obey
the lock_type setting. Until the lock_type feature is added,
lvm will fail to read any VG with lock_type set and report an
error about an unsupported lock_type. Once the lock_type
feature is added, lvm will allow VGs with lock_type to be
used according to the rules imposed by the lock_type.
When both system_id and lock_type settings are removed, a VG
is written with the old WRITE status flag, and without the
new WRITE_LOCKED flag. This allows old versions of lvm to
use the VG as before.
2015-03-04 20:30:53 +03:00
2010-01-07 17:40:46 +03:00
outsize ( f , ( uint64_t ) vg - > extent_size , " extent_size = %u " ,
vg - > extent_size ) ;
2004-05-05 01:25:57 +04:00
outf ( f , " max_lv = %u " , vg - > max_lv ) ;
outf ( f , " max_pv = %u " , vg - > max_pv ) ;
2001-12-11 15:16:58 +03:00
2004-05-19 02:12:53 +04:00
/* Default policy is NORMAL; INHERIT is meaningless */
if ( vg - > alloc ! = ALLOC_NORMAL & & vg - > alloc ! = ALLOC_INHERIT ) {
2005-06-07 15:00:07 +04:00
outnl ( f ) ;
2004-05-19 02:12:53 +04:00
outf ( f , " allocation_policy = \" %s \" " ,
get_alloc_string ( vg - > alloc ) ) ;
}
2013-06-25 14:32:09 +04:00
if ( vg - > profile )
outf ( f , " profile = \" %s \" " , vg - > profile - > name ) ;
2010-06-29 00:37:10 +04:00
outf ( f , " metadata_copies = %u " , vg - > mda_copies ) ;
2004-05-19 02:12:53 +04:00
2001-12-11 15:16:58 +03:00
return 1 ;
}
/*
* Get the pv % d name from the formatters hash
* table .
*/
2009-05-19 13:48:32 +04:00
static const char * _get_pv_name_from_uuid ( struct formatter * f , char * uuid )
{
2017-05-23 01:30:34 +03:00
const char * pv_name = dm_hash_lookup ( f - > pv_names , uuid ) ;
if ( ! pv_name )
log_error ( INTERNAL_ERROR " PV name for uuid %s missing from text metadata export hash table. " ,
uuid ) ;
return pv_name ;
2009-05-19 13:48:32 +04:00
}
2006-04-19 19:33:07 +04:00
static const char * _get_pv_name ( struct formatter * f , struct physical_volume * pv )
2001-12-11 15:16:58 +03:00
{
2010-07-09 19:34:40 +04:00
char uuid [ 64 ] __attribute__ ( ( aligned ( 8 ) ) ) ;
2009-05-19 13:48:32 +04:00
if ( ! pv | | ! id_write_format ( & pv - > id , uuid , sizeof ( uuid ) ) )
return_NULL ;
return _get_pv_name_from_uuid ( f , uuid ) ;
2001-12-11 15:16:58 +03:00
}
static int _print_pvs ( struct formatter * f , struct volume_group * vg )
{
2005-06-01 20:51:55 +04:00
struct pv_list * pvl ;
2001-12-11 15:16:58 +03:00
struct physical_volume * pv ;
2014-03-26 17:00:51 +04:00
char buffer [ PATH_MAX * 2 ] ;
2001-12-11 15:16:58 +03:00
const char * name ;
device usage based on devices file
The LVM devices file lists devices that lvm can use. The default
file is /etc/lvm/devices/system.devices, and the lvmdevices(8)
command is used to add or remove device entries. If the file
does not exist, or if lvm.conf includes use_devicesfile=0, then
lvm will not use a devices file. When the devices file is in use,
the regex filter is not used, and the filter settings in lvm.conf
or on the command line are ignored.
LVM records devices in the devices file using hardware-specific
IDs, such as the WWID, and attempts to use subsystem-specific
IDs for virtual device types. These device IDs are also written
in the VG metadata. When no hardware or virtual ID is available,
lvm falls back using the unstable device name as the device ID.
When devnames are used, lvm performs extra scanning to find
devices if their devname changes, e.g. after reboot.
When proper device IDs are used, an lvm command will not look
at devices outside the devices file, but when devnames are used
as a fallback, lvm will scan devices outside the devices file
to locate PVs on renamed devices. A config setting
search_for_devnames can be used to control the scanning for
renamed devname entries.
Related to the devices file, the new command option
--devices <devnames> allows a list of devices to be specified for
the command to use, overriding the devices file. The listed
devices act as a sort of devices file in terms of limiting which
devices lvm will see and use. Devices that are not listed will
appear to be missing to the lvm command.
Multiple devices files can be kept in /etc/lvm/devices, which
allows lvm to be used with different sets of devices, e.g.
system devices do not need to be exposed to a specific application,
and the application can use lvm on its own set of devices that are
not exposed to the system. The option --devicesfile <filename> is
used to select the devices file to use with the command. Without
the option set, the default system devices file is used.
Setting --devicesfile "" causes lvm to not use a devices file.
An existing, empty devices file means lvm will see no devices.
The new command vgimportdevices adds PVs from a VG to the devices
file and updates the VG metadata to include the device IDs.
vgimportdevices -a will import all VGs into the system devices file.
LVM commands run by dmeventd not use a devices file by default,
and will look at all devices on the system. A devices file can
be created for dmeventd (/etc/lvm/devices/dmeventd.devices) If
this file exists, lvm commands run by dmeventd will use it.
Internal implementaion:
- device_ids_read - read the devices file
. add struct dev_use (du) to cmd->use_devices for each devices file entry
- dev_cache_scan - get /dev entries
. add struct device (dev) to dev_cache for each device on the system
- device_ids_match - match devices file entries to /dev entries
. match each du on cmd->use_devices to a dev in dev_cache, using device ID
. on match, set du->dev, dev->id, dev->flags MATCHED_USE_ID
- label_scan - read lvm headers and metadata from devices
. filters are applied, those that do not need data from the device
. filter-deviceid skips devs without MATCHED_USE_ID, i.e.
skips /dev entries that are not listed in the devices file
. read lvm label from dev
. filters are applied, those that use data from the device
. read lvm metadata from dev
. add info/vginfo structs for PVs/VGs (info is "lvmcache")
- device_ids_find_renamed_devs - handle devices with unstable devname ID
where devname changed
. this step only needed when devs do not have proper device IDs,
and their dev names change, e.g. after reboot sdb becomes sdc.
. detect incorrect match because PVID in the devices file entry
does not match the PVID found when the device was read above
. undo incorrect match between du and dev above
. search system devices for new location of PVID
. update devices file with new devnames for PVIDs on renamed devices
. label_scan the renamed devs
- continue with command processing
2020-06-23 21:25:41 +03:00
const char * idtype , * idname ;
2001-12-11 15:16:58 +03:00
2004-05-05 01:25:57 +04:00
outf ( f , " physical_volumes { " ) ;
2001-12-11 15:16:58 +03:00
_inc_indent ( f ) ;
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( pvl , & vg - > pvs ) {
2005-06-01 20:51:55 +04:00
pv = pvl - > pv ;
2001-12-20 19:05:14 +03:00
2009-05-19 13:48:32 +04:00
if ( ! id_write_format ( & pv - > id , buffer , sizeof ( buffer ) ) )
return_0 ;
if ( ! ( name = _get_pv_name_from_uuid ( f , buffer ) ) )
2008-01-30 16:19:47 +03:00
return_0 ;
2001-12-11 15:16:58 +03:00
2005-06-07 15:00:07 +04:00
outnl ( f ) ;
2004-05-05 01:25:57 +04:00
outf ( f , " %s { " , name ) ;
2001-12-11 15:16:58 +03:00
_inc_indent ( f ) ;
2004-05-05 01:25:57 +04:00
outf ( f , " id = \" %s \" " , buffer ) ;
2008-03-12 19:03:22 +03:00
2014-03-26 17:00:51 +04:00
if ( strlen ( pv_dev_name ( pv ) ) > = PATH_MAX ) {
log_error ( " pv device name size is out of bounds. " ) ;
2008-03-12 19:03:22 +03:00
return 0 ;
}
2010-01-07 17:40:46 +03:00
outhint ( f , " device = \" %s \" " ,
2014-03-26 17:00:51 +04:00
dm_escape_double_quotes ( buffer , pv_dev_name ( pv ) ) ) ;
2005-06-07 15:00:07 +04:00
outnl ( f ) ;
2001-12-11 15:16:58 +03:00
device usage based on devices file
The LVM devices file lists devices that lvm can use. The default
file is /etc/lvm/devices/system.devices, and the lvmdevices(8)
command is used to add or remove device entries. If the file
does not exist, or if lvm.conf includes use_devicesfile=0, then
lvm will not use a devices file. When the devices file is in use,
the regex filter is not used, and the filter settings in lvm.conf
or on the command line are ignored.
LVM records devices in the devices file using hardware-specific
IDs, such as the WWID, and attempts to use subsystem-specific
IDs for virtual device types. These device IDs are also written
in the VG metadata. When no hardware or virtual ID is available,
lvm falls back using the unstable device name as the device ID.
When devnames are used, lvm performs extra scanning to find
devices if their devname changes, e.g. after reboot.
When proper device IDs are used, an lvm command will not look
at devices outside the devices file, but when devnames are used
as a fallback, lvm will scan devices outside the devices file
to locate PVs on renamed devices. A config setting
search_for_devnames can be used to control the scanning for
renamed devname entries.
Related to the devices file, the new command option
--devices <devnames> allows a list of devices to be specified for
the command to use, overriding the devices file. The listed
devices act as a sort of devices file in terms of limiting which
devices lvm will see and use. Devices that are not listed will
appear to be missing to the lvm command.
Multiple devices files can be kept in /etc/lvm/devices, which
allows lvm to be used with different sets of devices, e.g.
system devices do not need to be exposed to a specific application,
and the application can use lvm on its own set of devices that are
not exposed to the system. The option --devicesfile <filename> is
used to select the devices file to use with the command. Without
the option set, the default system devices file is used.
Setting --devicesfile "" causes lvm to not use a devices file.
An existing, empty devices file means lvm will see no devices.
The new command vgimportdevices adds PVs from a VG to the devices
file and updates the VG metadata to include the device IDs.
vgimportdevices -a will import all VGs into the system devices file.
LVM commands run by dmeventd not use a devices file by default,
and will look at all devices on the system. A devices file can
be created for dmeventd (/etc/lvm/devices/dmeventd.devices) If
this file exists, lvm commands run by dmeventd will use it.
Internal implementaion:
- device_ids_read - read the devices file
. add struct dev_use (du) to cmd->use_devices for each devices file entry
- dev_cache_scan - get /dev entries
. add struct device (dev) to dev_cache for each device on the system
- device_ids_match - match devices file entries to /dev entries
. match each du on cmd->use_devices to a dev in dev_cache, using device ID
. on match, set du->dev, dev->id, dev->flags MATCHED_USE_ID
- label_scan - read lvm headers and metadata from devices
. filters are applied, those that do not need data from the device
. filter-deviceid skips devs without MATCHED_USE_ID, i.e.
skips /dev entries that are not listed in the devices file
. read lvm label from dev
. filters are applied, those that use data from the device
. read lvm metadata from dev
. add info/vginfo structs for PVs/VGs (info is "lvmcache")
- device_ids_find_renamed_devs - handle devices with unstable devname ID
where devname changed
. this step only needed when devs do not have proper device IDs,
and their dev names change, e.g. after reboot sdb becomes sdc.
. detect incorrect match because PVID in the devices file entry
does not match the PVID found when the device was read above
. undo incorrect match between du and dev above
. search system devices for new location of PVID
. update devices file with new devnames for PVIDs on renamed devices
. label_scan the renamed devs
- continue with command processing
2020-06-23 21:25:41 +03:00
idtype = dev_idtype_for_metadata ( vg - > cmd , pv - > dev ) ;
idname = dev_idname_for_metadata ( vg - > cmd , pv - > dev ) ;
if ( idtype & & idname ) {
outf ( f , " device_id_type = \" %s \" " , idtype ) ;
outf ( f , " device_id = \" %s \" " , idname ) ;
}
2008-07-10 15:30:57 +04:00
if ( ! _print_flag_config ( f , pv - > status , PV_FLAGS ) )
2008-01-30 16:19:47 +03:00
return_0 ;
2004-03-08 20:19:15 +03:00
2015-05-06 15:06:44 +03:00
if ( ! _out_list ( f , & pv - > tags , " tags " ) )
2010-11-29 15:19:58 +03:00
return_0 ;
2004-03-08 20:19:15 +03:00
2017-12-11 18:32:53 +03:00
outsize ( f , pv - > size , " dev_size = " FMTu64 , pv - > size ) ;
2006-10-08 03:17:17 +04:00
2017-12-11 18:32:53 +03:00
outf ( f , " pe_start = " FMTu64 , pv - > pe_start ) ;
2010-01-07 17:40:46 +03:00
outsize ( f , vg - > extent_size * ( uint64_t ) pv - > pe_count ,
" pe_count = %u " , pv - > pe_count ) ;
2001-12-11 15:16:58 +03:00
2013-05-28 14:37:22 +04:00
if ( pv - > ba_start & & pv - > ba_size ) {
2017-12-11 18:32:53 +03:00
outf ( f , " ba_start = " FMTu64 , pv - > ba_start ) ;
outsize ( f , pv - > ba_size , " ba_size = " FMTu64 , pv - > ba_size ) ;
2013-02-15 14:02:53 +04:00
}
2001-12-11 15:16:58 +03:00
_dec_indent ( f ) ;
2004-05-05 01:25:57 +04:00
outf ( f , " } " ) ;
2001-12-11 15:16:58 +03:00
}
_dec_indent ( f ) ;
2004-05-05 01:25:57 +04:00
outf ( f , " } " ) ;
2001-12-11 15:16:58 +03:00
return 1 ;
}
static int _print_segment ( struct formatter * f , struct volume_group * vg ,
2002-11-18 17:04:08 +03:00
int count , struct lv_segment * seg )
2001-12-11 15:16:58 +03:00
{
2017-05-29 15:20:38 +03:00
char buffer [ 2048 ] ;
if ( ! print_segtype_lvflags ( buffer , sizeof ( buffer ) , seg - > lv - > status ) )
return_0 ;
2004-05-05 01:25:57 +04:00
outf ( f , " segment%u { " , count ) ;
2001-12-11 15:16:58 +03:00
_inc_indent ( f ) ;
2004-05-05 01:25:57 +04:00
outf ( f , " start_extent = %u " , seg - > le ) ;
2010-01-07 17:40:46 +03:00
outsize ( f , ( uint64_t ) seg - > len * vg - > extent_size ,
" extent_count = %u " , seg - > len ) ;
2005-06-07 15:00:07 +04:00
outnl ( f ) ;
2017-02-24 02:50:00 +03:00
if ( seg - > reshape_len )
outsize ( f , ( uint64_t ) seg - > reshape_len * vg - > extent_size ,
" reshape_count = %u " , seg - > reshape_len ) ;
2017-05-29 15:20:38 +03:00
outf ( f , " type = \" %s%s \" " , seg - > segtype - > name , buffer ) ;
2001-12-11 15:16:58 +03:00
2015-05-06 15:06:44 +03:00
if ( ! _out_list ( f , & seg - > tags , " tags " ) )
2010-11-29 15:19:58 +03:00
return_0 ;
2004-03-08 20:19:15 +03:00
2004-05-11 20:01:58 +04:00
if ( seg - > segtype - > ops - > text_export & &
2008-01-30 16:19:47 +03:00
! seg - > segtype - > ops - > text_export ( seg , f ) )
return_0 ;
2003-04-25 02:23:24 +04:00
2004-05-05 01:25:57 +04:00
_dec_indent ( f ) ;
outf ( f , " } " ) ;
2001-12-11 15:16:58 +03:00
2004-05-05 01:25:57 +04:00
return 1 ;
}
2003-05-06 16:02:36 +04:00
2004-05-05 01:25:57 +04:00
int out_areas ( struct formatter * f , const struct lv_segment * seg ,
const char * type )
{
const char * name ;
unsigned int s ;
2017-05-23 01:30:34 +03:00
struct physical_volume * pv ;
2002-11-18 17:04:08 +03:00
2005-06-07 15:00:07 +04:00
outnl ( f ) ;
2003-04-25 02:23:24 +04:00
2004-05-05 01:25:57 +04:00
outf ( f , " %ss = [ " , type ) ;
_inc_indent ( f ) ;
2002-11-18 17:04:08 +03:00
2004-05-05 01:25:57 +04:00
for ( s = 0 ; s < seg - > area_count ; s + + ) {
2005-06-01 20:51:55 +04:00
switch ( seg_type ( seg , s ) ) {
2004-05-05 01:25:57 +04:00
case AREA_PV :
2017-05-23 01:30:34 +03:00
if ( ! ( pv = seg_pv ( seg , s ) ) ) {
2017-12-11 18:32:53 +03:00
log_error ( INTERNAL_ERROR " Missing PV for area " FMTu32 " of %s segment of LV %s. " ,
2017-05-23 01:30:34 +03:00
s , type , display_lvname ( seg - > lv ) ) ;
return 0 ;
}
if ( ! ( name = _get_pv_name ( f , pv ) ) )
2008-01-30 16:19:47 +03:00
return_0 ;
2001-12-11 15:16:58 +03:00
2004-05-05 01:25:57 +04:00
outf ( f , " \" %s \" , %u%s " , name ,
2005-06-01 20:51:55 +04:00
seg_pe ( seg , s ) ,
2004-05-05 01:25:57 +04:00
( s = = seg - > area_count - 1 ) ? " " : " , " ) ;
break ;
case AREA_LV :
2016-05-23 18:46:38 +03:00
/* FIXME This helper code should be target-independent! Check for metadata LV property. */
2016-12-13 16:50:48 +03:00
if ( ! seg_is_raid ( seg ) ) {
2011-08-03 02:07:20 +04:00
outf ( f , " \" %s \" , %u%s " ,
seg_lv ( seg , s ) - > name ,
seg_le ( seg , s ) ,
( s = = seg - > area_count - 1 ) ? " " : " , " ) ;
continue ;
}
/* RAID devices are laid-out in metadata/data pairs */
2016-05-23 18:46:38 +03:00
/* FIXME Validation should be elsewhere, not here! */
2014-09-16 00:33:53 +04:00
if ( ! lv_is_raid_image ( seg_lv ( seg , s ) ) | |
2016-05-23 18:46:38 +03:00
( seg - > meta_areas & & seg_metalv ( seg , s ) & & ! lv_is_raid_metadata ( seg_metalv ( seg , s ) ) ) ) {
2011-08-03 02:07:20 +04:00
log_error ( " RAID segment has non-RAID areas " ) ;
return 0 ;
}
2016-05-23 18:46:38 +03:00
if ( seg - > meta_areas & & seg_metalv ( seg , s ) )
outf ( f , " \" %s \" , \" %s \" %s " ,
( seg - > meta_areas & & seg_metalv ( seg , s ) ) ? seg_metalv ( seg , s ) - > name : " " ,
seg_lv ( seg , s ) - > name , ( s = = seg - > area_count - 1 ) ? " " : " , " ) ;
else
outf ( f , " \" %s \" %s " , seg_lv ( seg , s ) - > name , ( s = = seg - > area_count - 1 ) ? " " : " , " ) ;
2011-08-03 02:07:20 +04:00
2005-06-14 21:54:48 +04:00
break ;
case AREA_UNASSIGNED :
2017-12-11 18:32:53 +03:00
log_error ( INTERNAL_ERROR " Invalid type for area " FMTu32 " of %s segment of LV %s. " ,
2017-05-23 01:30:34 +03:00
s , type , display_lvname ( seg - > lv ) ) ;
2005-06-14 21:54:48 +04:00
return 0 ;
2004-05-05 01:25:57 +04:00
}
2001-12-11 15:16:58 +03:00
}
_dec_indent ( f ) ;
2004-05-05 01:25:57 +04:00
outf ( f , " ] " ) ;
2001-12-11 15:16:58 +03:00
return 1 ;
}
2016-03-01 17:20:49 +03:00
static int _print_timestamp ( struct formatter * f ,
const char * name , time_t ts ,
char * buf , size_t buf_size )
{
struct tm * local_tm ;
if ( ts ) {
strncpy ( buf , " # " , buf_size ) ;
if ( ! ( local_tm = localtime ( & ts ) ) | |
! strftime ( buf + 2 , buf_size - 2 ,
" %Y-%m-%d %T %z " , local_tm ) )
buf [ 0 ] = 0 ;
2017-12-11 18:32:53 +03:00
outfc ( f , buf , " %s = " FMTu64 , name , ( uint64_t ) ts ) ;
2016-03-01 17:20:49 +03:00
}
return 1 ;
}
2005-06-01 20:51:55 +04:00
static int _print_lv ( struct formatter * f , struct logical_volume * lv )
2001-12-20 14:52:54 +03:00
{
2005-06-01 20:51:55 +04:00
struct lv_segment * seg ;
char buffer [ 4096 ] ;
int seg_count ;
2015-03-05 19:23:16 +03:00
uint64_t status = lv - > status ;
2001-12-20 14:52:54 +03:00
2005-06-07 15:00:07 +04:00
outnl ( f ) ;
2005-06-01 20:51:55 +04:00
outf ( f , " %s { " , lv - > name ) ;
_inc_indent ( f ) ;
2001-12-20 14:52:54 +03:00
2005-06-01 20:51:55 +04:00
/* FIXME: Write full lvid */
2008-01-30 16:19:47 +03:00
if ( ! id_write_format ( & lv - > lvid . id [ 1 ] , buffer , sizeof ( buffer ) ) )
return_0 ;
2005-06-01 20:51:55 +04:00
outf ( f , " id = \" %s \" " , buffer ) ;
system_id: make new VGs read-only for old lvm versions
Previous versions of lvm will not obey the restrictions
imposed by the new system_id, and would allow such a VG
to be written. So, a VG with a new system_id is further
changed to force previous lvm versions to treat it as
read-only. This is done by removing the WRITE flag from
the metadata status line of these VGs, and putting a new
WRITE_LOCKED flag in the flags line of the metadata.
Versions of lvm that recognize WRITE_LOCKED, also obey the
new system_id. For these lvm versions, WRITE_LOCKED is
identical to WRITE, and the rules associated with matching
system_id's are imposed.
A new VG lock_type field is also added that causes the same
WRITE/WRITE_LOCKED transformation when set. A previous
version of lvm will also see a VG with lock_type as read-only.
Versions of lvm that recognize WRITE_LOCKED, must also obey
the lock_type setting. Until the lock_type feature is added,
lvm will fail to read any VG with lock_type set and report an
error about an unsupported lock_type. Once the lock_type
feature is added, lvm will allow VGs with lock_type to be
used according to the rules imposed by the lock_type.
When both system_id and lock_type settings are removed, a VG
is written with the old WRITE status flag, and without the
new WRITE_LOCKED flag. This allows old versions of lvm to
use the VG as before.
2015-03-04 20:30:53 +03:00
/*
2015-03-09 21:53:22 +03:00
* Removing WRITE and adding LVM_WRITE_LOCKED makes it read - only
* to old versions of lvm that only look for LVM_WRITE .
system_id: make new VGs read-only for old lvm versions
Previous versions of lvm will not obey the restrictions
imposed by the new system_id, and would allow such a VG
to be written. So, a VG with a new system_id is further
changed to force previous lvm versions to treat it as
read-only. This is done by removing the WRITE flag from
the metadata status line of these VGs, and putting a new
WRITE_LOCKED flag in the flags line of the metadata.
Versions of lvm that recognize WRITE_LOCKED, also obey the
new system_id. For these lvm versions, WRITE_LOCKED is
identical to WRITE, and the rules associated with matching
system_id's are imposed.
A new VG lock_type field is also added that causes the same
WRITE/WRITE_LOCKED transformation when set. A previous
version of lvm will also see a VG with lock_type as read-only.
Versions of lvm that recognize WRITE_LOCKED, must also obey
the lock_type setting. Until the lock_type feature is added,
lvm will fail to read any VG with lock_type set and report an
error about an unsupported lock_type. Once the lock_type
feature is added, lvm will allow VGs with lock_type to be
used according to the rules imposed by the lock_type.
When both system_id and lock_type settings are removed, a VG
is written with the old WRITE status flag, and without the
new WRITE_LOCKED flag. This allows old versions of lvm to
use the VG as before.
2015-03-04 20:30:53 +03:00
*/
2015-03-05 19:23:16 +03:00
if ( ( status & LVM_WRITE ) & & vg_flag_write_locked ( lv - > vg ) ) {
status & = ~ LVM_WRITE ;
status | = LVM_WRITE_LOCKED ;
system_id: make new VGs read-only for old lvm versions
Previous versions of lvm will not obey the restrictions
imposed by the new system_id, and would allow such a VG
to be written. So, a VG with a new system_id is further
changed to force previous lvm versions to treat it as
read-only. This is done by removing the WRITE flag from
the metadata status line of these VGs, and putting a new
WRITE_LOCKED flag in the flags line of the metadata.
Versions of lvm that recognize WRITE_LOCKED, also obey the
new system_id. For these lvm versions, WRITE_LOCKED is
identical to WRITE, and the rules associated with matching
system_id's are imposed.
A new VG lock_type field is also added that causes the same
WRITE/WRITE_LOCKED transformation when set. A previous
version of lvm will also see a VG with lock_type as read-only.
Versions of lvm that recognize WRITE_LOCKED, must also obey
the lock_type setting. Until the lock_type feature is added,
lvm will fail to read any VG with lock_type set and report an
error about an unsupported lock_type. Once the lock_type
feature is added, lvm will allow VGs with lock_type to be
used according to the rules imposed by the lock_type.
When both system_id and lock_type settings are removed, a VG
is written with the old WRITE status flag, and without the
new WRITE_LOCKED flag. This allows old versions of lvm to
use the VG as before.
2015-03-04 20:30:53 +03:00
}
2015-03-05 19:23:16 +03:00
if ( ! _print_flag_config ( f , status , LV_FLAGS ) )
2008-01-30 16:19:47 +03:00
return_0 ;
2005-06-01 20:51:55 +04:00
2015-05-06 15:06:44 +03:00
if ( ! _out_list ( f , & lv - > tags , " tags " ) )
2010-11-29 15:19:58 +03:00
return_0 ;
2005-06-01 20:51:55 +04:00
2012-01-19 19:31:45 +04:00
if ( lv - > timestamp ) {
2016-03-01 17:21:05 +03:00
if ( ! _print_timestamp ( f , " creation_time " , lv - > timestamp ,
buffer , sizeof ( buffer ) ) )
return_0 ;
2012-01-19 19:31:45 +04:00
outf ( f , " creation_host = \" %s \" " , lv - > hostname ) ;
}
2015-03-05 23:00:44 +03:00
if ( lv - > lock_args )
outf ( f , " lock_args = \" %s \" " , lv - > lock_args ) ;
2005-06-01 20:51:55 +04:00
if ( lv - > alloc ! = ALLOC_INHERIT )
outf ( f , " allocation_policy = \" %s \" " ,
get_alloc_string ( lv - > alloc ) ) ;
2013-06-25 14:32:09 +04:00
if ( lv - > profile )
outf ( f , " profile = \" %s \" " , lv - > profile - > name ) ;
2007-11-09 19:51:54 +03:00
switch ( lv - > read_ahead ) {
case DM_READ_AHEAD_NONE :
2010-01-07 17:45:28 +03:00
outfc ( f , " # None " , " read_ahead = -1 " ) ;
2007-11-09 19:51:54 +03:00
break ;
case DM_READ_AHEAD_AUTO :
/* No output - use default */
break ;
default :
2005-06-01 20:51:55 +04:00
outf ( f , " read_ahead = %u " , lv - > read_ahead ) ;
2007-11-09 19:51:54 +03:00
}
2005-06-01 20:51:55 +04:00
if ( lv - > major > = 0 )
outf ( f , " major = %d " , lv - > major ) ;
if ( lv - > minor > = 0 )
outf ( f , " minor = %d " , lv - > minor ) ;
2008-11-04 01:14:30 +03:00
outf ( f , " segment_count = %u " , dm_list_size ( & lv - > segments ) ) ;
2005-06-07 15:00:07 +04:00
outnl ( f ) ;
2005-06-01 20:51:55 +04:00
seg_count = 1 ;
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( seg , & lv - > segments ) {
2008-01-30 16:19:47 +03:00
if ( ! _print_segment ( f , lv - > vg , seg_count + + , seg ) )
return_0 ;
2005-06-01 20:51:55 +04:00
}
_dec_indent ( f ) ;
outf ( f , " } " ) ;
return 1 ;
2001-12-20 14:52:54 +03:00
}
2001-12-11 15:16:58 +03:00
static int _print_lvs ( struct formatter * f , struct volume_group * vg )
{
2005-06-01 20:51:55 +04:00
struct lv_list * lvl ;
2001-12-11 15:16:58 +03:00
2002-02-13 16:29:16 +03:00
/*
* Don ' t bother with an lv section if there are no lvs .
*/
2008-11-04 01:14:30 +03:00
if ( dm_list_empty ( & vg - > lvs ) )
2002-02-13 16:29:16 +03:00
return 1 ;
2004-05-05 01:25:57 +04:00
outf ( f , " logical_volumes { " ) ;
2001-12-11 15:16:58 +03:00
_inc_indent ( f ) ;
2005-06-01 20:51:55 +04:00
/*
* Write visible LVs first
*/
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( lvl , & vg - > lvs ) {
2009-05-14 01:27:43 +04:00
if ( ! ( lv_is_visible ( lvl - > lv ) ) )
2005-06-01 20:51:55 +04:00
continue ;
2008-01-30 16:19:47 +03:00
if ( ! _print_lv ( f , lvl - > lv ) )
return_0 ;
2005-06-01 20:51:55 +04:00
}
2002-02-21 18:26:44 +03:00
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( lvl , & vg - > lvs ) {
2009-05-14 01:27:43 +04:00
if ( ( lv_is_visible ( lvl - > lv ) ) )
2005-06-01 20:51:55 +04:00
continue ;
2008-01-30 16:19:47 +03:00
if ( ! _print_lv ( f , lvl - > lv ) )
return_0 ;
2001-12-11 15:16:58 +03:00
}
2002-02-13 16:29:16 +03:00
_dec_indent ( f ) ;
2004-05-05 01:25:57 +04:00
outf ( f , " } " ) ;
2002-02-13 16:29:16 +03:00
return 1 ;
}
2016-03-01 17:20:49 +03:00
static int _alloc_printed_indirect_descendants ( struct dm_list * indirect_glvs , char * * buffer )
{
struct glv_list * user_glvl ;
size_t buf_size = 0 ;
int first = 1 ;
char * buf ;
* buffer = NULL ;
dm_list_iterate_items ( user_glvl , indirect_glvs ) {
if ( user_glvl - > glv - > is_historical )
continue ;
/* '"' + name + '"' + ',' + ' ' */
buf_size + = strlen ( user_glvl - > glv - > live - > name ) + 4 ;
}
if ( ! buf_size )
return 1 ;
/* '[' + ']' + '\0' */
buf_size + = 3 ;
2018-06-08 15:40:53 +03:00
if ( ! ( * buffer = malloc ( buf_size ) ) ) {
2016-03-01 17:20:49 +03:00
log_error ( " Could not allocate memory for ancestor list buffer. " ) ;
return 0 ;
}
buf = * buffer ;
if ( ! emit_to_buffer ( & buf , & buf_size , " [ " ) )
goto_bad ;
dm_list_iterate_items ( user_glvl , indirect_glvs ) {
if ( user_glvl - > glv - > is_historical )
continue ;
if ( ! first ) {
if ( ! emit_to_buffer ( & buf , & buf_size , " , " ) )
goto_bad ;
} else
first = 0 ;
if ( ! emit_to_buffer ( & buf , & buf_size , " \" %s \" " , user_glvl - > glv - > live - > name ) )
goto_bad ;
}
if ( ! emit_to_buffer ( & buf , & buf_size , " ] " ) )
goto_bad ;
return 1 ;
bad :
if ( * buffer ) {
2018-06-08 15:40:53 +03:00
free ( * buffer ) ;
2016-03-01 17:20:49 +03:00
* buffer = NULL ;
}
return 0 ;
}
2021-03-09 14:30:25 +03:00
static int _print_historical_lv_with_descendants ( struct formatter * f , struct historical_logical_volume * hlv ,
char * descendants_buffer )
2016-03-01 17:20:49 +03:00
{
char buffer [ 40 ] ;
if ( ! id_write_format ( & hlv - > lvid . id [ 1 ] , buffer , sizeof ( buffer ) ) )
2021-03-09 14:30:25 +03:00
return_0 ;
2016-03-01 17:20:49 +03:00
2021-03-09 14:30:25 +03:00
outnl ( f ) ;
outf ( f , " %s { " , hlv - > name ) ;
2016-03-01 17:20:49 +03:00
_inc_indent ( f ) ;
2021-03-09 14:30:25 +03:00
outf ( f , " id = \" %s \" " , buffer ) ;
2016-03-01 17:20:49 +03:00
if ( ! _print_timestamp ( f , " creation_time " , hlv - > timestamp , buffer , sizeof ( buffer ) ) )
2021-03-09 14:30:25 +03:00
return_0 ;
2016-03-01 17:20:49 +03:00
if ( ! _print_timestamp ( f , " removal_time " , hlv - > timestamp_removed , buffer , sizeof ( buffer ) ) )
2021-03-09 14:30:25 +03:00
return_0 ;
2016-03-01 17:20:49 +03:00
if ( hlv - > indirect_origin ) {
if ( hlv - > indirect_origin - > is_historical )
2021-03-09 14:30:25 +03:00
outf ( f , " origin = \" %s%s \" " , HISTORICAL_LV_PREFIX , hlv - > indirect_origin - > historical - > name ) ;
2016-03-01 17:20:49 +03:00
else
2021-03-09 14:30:25 +03:00
outf ( f , " origin = \" %s \" " , hlv - > indirect_origin - > live - > name ) ;
2016-03-01 17:20:49 +03:00
}
if ( descendants_buffer )
2021-03-09 14:30:25 +03:00
outf ( f , " descendants = %s " , descendants_buffer ) ;
2016-03-01 17:20:49 +03:00
_dec_indent ( f ) ;
2021-03-09 14:30:25 +03:00
outf ( f , " } " ) ;
return 1 ;
}
static int _print_historical_lv ( struct formatter * f , struct historical_logical_volume * hlv )
{
char * descendants_buffer = NULL ;
int r = 0 ;
if ( _alloc_printed_indirect_descendants ( & hlv - > indirect_glvs , & descendants_buffer ) )
r = _print_historical_lv_with_descendants ( f , hlv , descendants_buffer ) ;
2016-03-01 17:20:49 +03:00
2018-06-08 15:40:53 +03:00
free ( descendants_buffer ) ;
2017-07-16 11:22:20 +03:00
2016-03-01 17:20:49 +03:00
return r ;
}
static int _print_historical_lvs ( struct formatter * f , struct volume_group * vg )
{
struct glv_list * glvl ;
if ( dm_list_empty ( & vg - > historical_lvs ) )
return 1 ;
outf ( f , " historical_logical_volumes { " ) ;
_inc_indent ( f ) ;
dm_list_iterate_items ( glvl , & vg - > historical_lvs ) {
if ( ! _print_historical_lv ( f , glvl - > glv - > historical ) )
return_0 ;
}
_dec_indent ( f ) ;
outf ( f , " } " ) ;
return 1 ;
}
2001-12-11 15:16:58 +03:00
/*
* In the text format we refer to pv ' s as ' pv1 ' ,
* ' pv2 ' etc . This function builds a hash table
* to enable a quick lookup from device - > name .
*/
2002-04-24 22:20:51 +04:00
static int _build_pv_names ( struct formatter * f , struct volume_group * vg )
2001-12-11 15:16:58 +03:00
{
int count = 0 ;
2005-06-01 20:51:55 +04:00
struct pv_list * pvl ;
2001-12-11 15:16:58 +03:00
struct physical_volume * pv ;
2021-03-04 20:17:03 +03:00
char buffer [ 32 ] , * name ;
char uuid [ 64 ] ;
2001-12-11 15:16:58 +03:00
2006-05-17 00:53:13 +04:00
if ( ! ( f - > mem = dm_pool_create ( " text pv_names " , 512 ) ) )
return_0 ;
2001-12-11 15:16:58 +03:00
2021-03-07 17:33:50 +03:00
if ( ! ( f - > pv_names = dm_hash_create ( 115 ) ) )
2006-05-17 00:53:13 +04:00
return_0 ;
2001-12-11 15:16:58 +03:00
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( pvl , & vg - > pvs ) {
2005-06-01 20:51:55 +04:00
pv = pvl - > pv ;
2001-12-11 15:16:58 +03:00
2003-04-25 02:23:24 +04:00
/* FIXME But skip if there's already an LV called pv%d ! */
2006-08-21 16:54:53 +04:00
if ( dm_snprintf ( buffer , sizeof ( buffer ) , " pv%d " , count + + ) < 0 )
2006-05-17 00:53:13 +04:00
return_0 ;
2001-12-11 15:16:58 +03:00
2006-05-17 00:53:13 +04:00
if ( ! ( name = dm_pool_strdup ( f - > mem , buffer ) ) )
return_0 ;
2001-12-11 15:16:58 +03:00
2021-03-04 20:17:03 +03:00
if ( ! id_write_format ( & pv - > id , uuid , sizeof ( uuid ) ) )
2009-05-19 13:48:32 +04:00
return_0 ;
if ( ! dm_hash_insert ( f - > pv_names , uuid , name ) )
2006-05-17 00:53:13 +04:00
return_0 ;
2001-12-11 15:16:58 +03:00
}
return 1 ;
}
2002-11-18 17:04:08 +03:00
static int _text_vg_export ( struct formatter * f ,
struct volume_group * vg , const char * desc )
2001-12-11 15:16:58 +03:00
{
int r = 0 ;
2008-01-30 16:19:47 +03:00
if ( ! _build_pv_names ( f , vg ) )
goto_out ;
2001-12-11 15:16:58 +03:00
2015-02-23 20:54:47 +03:00
if ( f - > header & & ! _print_header ( vg - > cmd , f , desc ) )
2006-05-17 00:53:13 +04:00
goto_out ;
2002-11-18 17:04:08 +03:00
2004-05-05 01:25:57 +04:00
if ( ! out_text ( f , " %s { " , vg - > name ) )
2006-05-17 00:53:13 +04:00
goto_out ;
2001-12-11 15:16:58 +03:00
2001-12-20 14:52:54 +03:00
_inc_indent ( f ) ;
2001-12-11 15:16:58 +03:00
if ( ! _print_vg ( f , vg ) )
2006-05-17 00:53:13 +04:00
goto_out ;
2001-12-11 15:16:58 +03:00
2005-06-07 15:00:07 +04:00
outnl ( f ) ;
2001-12-11 15:16:58 +03:00
if ( ! _print_pvs ( f , vg ) )
2006-05-17 00:53:13 +04:00
goto_out ;
2001-12-11 15:16:58 +03:00
2005-06-07 15:00:07 +04:00
outnl ( f ) ;
2001-12-11 15:16:58 +03:00
if ( ! _print_lvs ( f , vg ) )
2006-05-17 00:53:13 +04:00
goto_out ;
2001-12-11 15:16:58 +03:00
2016-03-01 17:20:49 +03:00
outnl ( f ) ;
if ( ! _print_historical_lvs ( f , vg ) )
goto_out ;
2002-11-18 17:04:08 +03:00
_dec_indent ( f ) ;
2004-05-05 01:25:57 +04:00
if ( ! out_text ( f , " } " ) )
2006-05-17 00:53:13 +04:00
goto_out ;
2002-02-13 16:29:16 +03:00
2015-02-23 20:54:47 +03:00
if ( ! f - > header & & ! _print_header ( vg - > cmd , f , desc ) )
2006-05-17 00:53:13 +04:00
goto_out ;
2001-12-11 15:16:58 +03:00
2002-11-18 17:04:08 +03:00
r = 1 ;
2001-12-11 15:16:58 +03:00
2002-04-24 22:20:51 +04:00
out :
2012-01-26 02:35:36 +04:00
if ( f - > mem ) {
2005-10-17 03:03:59 +04:00
dm_pool_destroy ( f - > mem ) ;
2012-01-26 02:35:36 +04:00
f - > mem = NULL ;
}
2001-12-11 15:16:58 +03:00
2012-01-26 02:35:36 +04:00
if ( f - > pv_names ) {
2005-10-17 03:03:59 +04:00
dm_hash_destroy ( f - > pv_names ) ;
2012-01-26 02:35:36 +04:00
f - > pv_names = NULL ;
}
2001-12-11 15:16:58 +03:00
2002-11-18 17:04:08 +03:00
return r ;
}
int text_vg_export_file ( struct volume_group * vg , const char * desc , FILE * fp )
{
int r ;
2021-03-04 20:09:58 +03:00
struct formatter f = {
. indent = 0 ,
. header = 1 ,
. out_with_comment = & _out_with_comment_file ,
. nl = & _nl_file ,
. data . fp = fp ,
} ;
2002-11-18 17:04:08 +03:00
_init ( ) ;
2021-03-04 20:09:58 +03:00
if ( ( r = _text_vg_export ( & f , vg , desc ) ) )
r = ! ferror ( f . data . fp ) ;
2002-11-18 17:04:08 +03:00
2001-12-11 15:16:58 +03:00
return r ;
}
2002-11-18 17:04:08 +03:00
/* Returns amount of buffer used incl. terminating NUL */
2019-07-01 23:00:34 +03:00
size_t text_vg_export_raw ( struct volume_group * vg , const char * desc , char * * buf , uint32_t * buf_size )
2002-11-18 17:04:08 +03:00
{
2021-03-04 20:09:58 +03:00
size_t r ;
struct formatter f = {
. indent = 0 ,
. header = 0 ,
. out_with_comment = & _out_with_comment_raw ,
. nl = & _nl_raw ,
2021-09-27 16:55:53 +03:00
. data . buf . size = vg - > buffer_size_hint + 16384 , /* Initial metadata limit */
2021-03-04 20:09:58 +03:00
} ;
2002-11-18 17:04:08 +03:00
_init ( ) ;
2021-03-04 20:09:58 +03:00
if ( ! ( f . data . buf . start = zalloc ( f . data . buf . size ) ) ) {
2005-06-07 15:00:07 +04:00
log_error ( " text_export buffer allocation failed " ) ;
2021-03-04 20:09:58 +03:00
return 0 ;
2005-06-07 15:00:07 +04:00
}
2021-03-04 20:09:58 +03:00
if ( ! _text_vg_export ( & f , vg , desc ) ) {
free ( f . data . buf . start ) ;
return 0 ;
2002-11-18 17:04:08 +03:00
}
2021-03-04 20:09:58 +03:00
r = f . data . buf . used + 1 ;
* buf = f . data . buf . start ;
2002-11-18 17:04:08 +03:00
2019-07-01 23:00:34 +03:00
if ( buf_size )
2021-03-04 20:09:58 +03:00
* buf_size = f . data . buf . size ;
2019-07-01 23:00:34 +03:00
2002-11-18 17:04:08 +03:00
return r ;
}
2019-06-19 00:10:06 +03:00
static size_t _export_vg_to_buffer ( struct volume_group * vg , char * * buf )
2008-04-02 02:40:13 +04:00
{
2019-07-01 23:00:34 +03:00
return text_vg_export_raw ( vg , " " , buf , NULL ) ;
2008-04-02 02:40:13 +04:00
}
2013-03-17 19:25:12 +04:00
struct dm_config_tree * export_vg_to_config_tree ( struct volume_group * vg )
{
char * buf = NULL ;
struct dm_config_tree * vg_cft ;
2019-06-19 00:10:06 +03:00
if ( ! _export_vg_to_buffer ( vg , & buf ) ) {
2013-03-17 19:25:12 +04:00
log_error ( " Could not format metadata for VG %s. " , vg - > name ) ;
2018-12-23 01:44:42 +03:00
return NULL ;
2013-03-17 19:25:12 +04:00
}
2016-09-21 15:59:14 +03:00
if ( ! ( vg_cft = config_tree_from_string_without_dup_node_check ( buf ) ) ) {
2013-03-17 19:25:12 +04:00
log_error ( " Error parsing metadata for VG %s. " , vg - > name ) ;
2018-06-08 15:40:53 +03:00
free ( buf ) ;
2018-12-23 01:44:42 +03:00
return NULL ;
2013-03-17 19:25:12 +04:00
}
2018-06-08 15:40:53 +03:00
free ( buf ) ;
2013-03-17 19:25:12 +04:00
return vg_cft ;
}
2004-05-05 01:25:57 +04:00
# undef outf
2005-06-07 15:00:07 +04:00
# undef outnl