block: Use LVM tools for LV block device truncation
PATCH v2 1/1 block: Use LVM tools for LV block device truncation
If a block device is an LVM logical volume we can resize it using standard LVM tools.
Add a helper to detect if a device is a DM device. In raw_co_truncate() check if the block device is DM and resize it executing lvresize.
--- block/file-posix.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+)
block/file-posix.c +61 -0
@@ -2642,6 +2642,38 @@ raw_regular_truncate(BlockDriverState *bs, int fd, int64_t offset,return raw_thread_pool_submit (handle_aiocb_truncate, &acb); } +static bool device_is_dm (struct stat *st) +{ +unsigned int maj, maj2; +char line[32 ], devname[16 ]; +bool ret = false; +FILE *f; + +if (!S_ISBLK (st->st_mode)) { +return false; + } + + f =fopen ("/proc/devices" ,"r" ); +if (!f) { +return false; + } + + maj =major (st->st_rdev); + +while (fgets (line,sizeof (line), f)) { +if (sscanf (line,"%u %15s" , &maj2, devname) !=2 ) { +continue ; + } +if (strcmp (devname,"device-mapper" ) ==0 ) { + ret = (maj == maj2); +break ; + } + } + +fclose (f); +return ret; +} + static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset,bool exact,PreallocMode prealloc,BdrvRequestFlags flags,Error **errp)
@@ -2670,6 +2702,35 @@ static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset,if (S_ISCHR (st.st_mode) ||S_ISBLK (st.st_mode)) {int64_t cur_length =raw_getlength (bs); +/* +* Try to resize an LVM device using LVM tools. +*/ +if (device_is_dm (&st) && offset >0 ) { +int spawn_flags =G_SPAWN_SEARCH_PATH |G_SPAWN_STDOUT_TO_DEV_NULL ; +int status; +bool success; +char *err; +GError *gerr =NULL ; + g_autofree char *size_str = g_strdup_printf("%ldB", offset); +const char *cmd[] = {"lvresize" ,"-f" ,"-L" , + size_str, bs->filename,NULL }; + + success =g_spawn_sync (NULL , (gchar **)cmd,NULL , spawn_flags, +NULL ,NULL ,NULL , &err, &status, &gerr); + +if (success &&WEXITSTATUS (status) ==0 ) { +return 0 ; + } + +if (!success) { +error_setg (errp,"lvresize execution error: %s" , gerr->message); + }else { +error_setg (errp,"%s" , err); + } + +return -EINVAL ; + } +if (offset != cur_length && exact) {error_setg (errp,"Cannot resize device files" );return -ENOTSUP ;
Signature · 1 line
2.40.1
Quoted 26 lines · Alexander Ivanov
If a block device is an LVM logical volume we can resize it using standard LVM tools.
Add a helper to detect if a device is a DM device. In
raw_co_truncate()check if the block device is DM and resize it executing lvresize.Signed-off-by Alexander Ivanov <[email protected]>--- block/file-posix.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+)block/file-posix.c +0
-0@@ -2642,6 +2642,38 @@ raw_regular_truncate(BlockDriverState *bs, int fd, int64_t offset,return raw_thread_pool_submit (handle_aiocb_truncate, &acb); } static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset,bool exact,PreallocMode prealloc,BdrvRequestFlags flags,Error **errp)@@ -2670,6 +2702,35 @@ static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset,if (S_ISCHR (st.st_mode) ||S_ISBLK (st.st_mode)) {int64_t cur_length =raw_getlength (bs);
block/file-posix.c static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset, · line 2714
Quoted 10 lines
block/file-posix.c +10
-0+/* +* Try to resize an LVM device using LVM tools. +*/ +if (device_is_dm (&st) && offset >0 ) { +int spawn_flags =G_SPAWN_SEARCH_PATH |G_SPAWN_STDOUT_TO_DEV_NULL ; +int status; +bool success; +char *err; +GError *gerr =NULL ; + g_autofree char *size_str = g_strdup_printf("%ldB", offset);
offset is 64-bit, but '%ld' is not guaranteed to be 64-bit. I expect this will break on 32-bit platforms. Try PRId64 instead.
block/file-posix.c static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset, · line 2723
Quoted 9 lines
block/file-posix.c +9
-0+const char *cmd[] = {"lvresize" ,"-f" ,"-L" , + size_str, bs->filename,NULL }; + + success =g_spawn_sync (NULL , (gchar **)cmd,NULL , spawn_flags, +NULL ,NULL ,NULL , &err, &status, &gerr); + +if (success &&WEXITSTATUS (status) ==0 ) { +return 0 ; + }
We should probably check 'g_spawn_check_wait_status' rather than WEXITSTATUS, as this then gives us further eror message details that....
block/file-posix.c static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset, · line 2728
Quoted 5 lines
block/file-posix.c +5
-0+ +if (!success) { +error_setg (errp,"lvresize execution error: %s" , gerr->message); + }else { +error_setg (errp,"%s" , err);
...we would also include here, such as the exit code or terminal signal.
Quoted 12 lines
block/file-posix.c +5
-0+ } + +return -EINVAL ; + } +if (offset != cur_length && exact) {error_setg (errp,"Cannot resize device files" );return -ENOTSUP ;Signature · 1 line
2.40.1
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Quoted 39 lines · Daniel P. Berrangé
Quoted 25 lines · Alexander Ivanov
If a block device is an LVM logical volume we can resize it using standard LVM tools.
Add a helper to detect if a device is a DM device. In
raw_co_truncate()check if the block device is DM and resize it executing lvresize.Signed-off-by Alexander Ivanov <[email protected]>--- block/file-posix.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+)block/file-posix.c +0
-0@@ -2642,6 +2642,38 @@ raw_regular_truncate(BlockDriverState *bs, int fd, int64_t offset,return raw_thread_pool_submit (handle_aiocb_truncate, &acb); } static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset,bool exact,PreallocMode prealloc,BdrvRequestFlags flags,Error **errp)@@ -2670,6 +2702,35 @@ static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset,if (S_ISCHR (st.st_mode) ||S_ISBLK (st.st_mode)) {int64_t cur_length =raw_getlength (bs);block/file-posix.c static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset, · line 2714
Quoted 10 lines
block/file-posix.c +10
-0+/* +* Try to resize an LVM device using LVM tools. +*/ +if (device_is_dm (&st) && offset >0 ) { +int spawn_flags =G_SPAWN_SEARCH_PATH |G_SPAWN_STDOUT_TO_DEV_NULL ; +int status; +bool success; +char *err; +GError *gerr =NULL ; + g_autofree char *size_str = g_strdup_printf("%ldB", offset);offset is 64-bit, but '%ld' is not guaranteed to be 64-bit. I expect this will break on 32-bit platforms. Try PRId64 instead.
Quoted 12 lines
block/file-posix.c static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset, · line 2723
Quoted 9 lines
block/file-posix.c +9
-0+const char *cmd[] = {"lvresize" ,"-f" ,"-L" , + size_str, bs->filename,NULL }; + + success =g_spawn_sync (NULL , (gchar **)cmd,NULL , spawn_flags, +NULL ,NULL ,NULL , &err, &status, &gerr); + +if (success &&WEXITSTATUS (status) ==0 ) { +return 0 ; + }We should probably check '
g_spawn_check_wait_status' rather than WEXITSTATUS, as this then gives us further eror message details that....
Thank you.
I think it would be better to use 'g_spawn_check_exit_status' because there is no 'g_spawn_check_wait_status' in glib before 2.70 and even in 2.78 it leads to 'g_spawn_check_wait_status is deprecated: Not available before 2.70' error.
Quoted 23 lines
block/file-posix.c static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset, · line 2728
Quoted 6 lines
block/file-posix.c +5
-0+ +if (!success) { +error_setg (errp,"lvresize execution error: %s" , gerr->message); + }else { +error_setg (errp,"%s" , err);...we would also include here, such as the exit code or terminal signal.
Quoted 12 lines
block/file-posix.c +5
-0+ } + +return -EINVAL ; + } +if (offset != cur_length && exact) {error_setg (errp,"Cannot resize device files" );return -ENOTSUP ;Signature · 1 line
2.40.1With regards,
Daniel
Signature · 2 lines
Best regards, Alexander Ivanov
Quoted 53 lines · Alexander Ivanov
Quoted 38 lines · Daniel P. Berrangé
Quoted 24 lines · Alexander Ivanov
If a block device is an LVM logical volume we can resize it using standard LVM tools.
Add a helper to detect if a device is a DM device. In
raw_co_truncate()check if the block device is DM and resize it executing lvresize.Signed-off-by Alexander Ivanov <[email protected]>--- block/file-posix.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+)block/file-posix.c +0
-0@@ -2642,6 +2642,38 @@ raw_regular_truncate(BlockDriverState *bs, int fd, int64_t offset,return raw_thread_pool_submit (handle_aiocb_truncate, &acb); } static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset,bool exact,PreallocMode prealloc,BdrvRequestFlags flags,Error **errp)@@ -2670,6 +2702,35 @@ static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset,if (S_ISCHR (st.st_mode) ||S_ISBLK (st.st_mode)) {int64_t cur_length =raw_getlength (bs);block/file-posix.c static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset, · line 2713
Quoted 10 lines
block/file-posix.c +10
-0+/* +* Try to resize an LVM device using LVM tools. +*/ +if (device_is_dm (&st) && offset >0 ) { +int spawn_flags =G_SPAWN_SEARCH_PATH |G_SPAWN_STDOUT_TO_DEV_NULL ; +int status; +bool success; +char *err; +GError *gerr =NULL ; + g_autofree char *size_str = g_strdup_printf("%ldB", offset);offset is 64-bit, but '%ld' is not guaranteed to be 64-bit. I expect this will break on 32-bit platforms. Try PRId64 instead.
Quoted 12 lines
block/file-posix.c static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset, · line 2723
Quoted 9 lines
block/file-posix.c +9
-0+const char *cmd[] = {"lvresize" ,"-f" ,"-L" , + size_str, bs->filename,NULL }; + + success =g_spawn_sync (NULL , (gchar **)cmd,NULL , spawn_flags, +NULL ,NULL ,NULL , &err, &status, &gerr); + +if (success &&WEXITSTATUS (status) ==0 ) { +return 0 ; + }We should probably check '
g_spawn_check_wait_status' rather than WEXITSTATUS, as this then gives us further eror message details that....
Quoted 5 lines
Thank you.
I think it would be better to use 'g_spawn_check_exit_status' because there is no
'g_spawn_check_wait_status' in glib before 2.70 and even in 2.78 it leads to 'g_spawn_check_wait_statusis deprecated: Not available before 2.70' error.
Ah yes, well spotted.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|