Merge pull request #24531 from poettering/loop-resize-part-modernizations

loopback block device resize partition modernizations
This commit is contained in:
Yu Watanabe
2022-09-02 00:53:38 +09:00
committed by GitHub
3 changed files with 45 additions and 18 deletions
+36 -2
View File
@@ -427,7 +427,13 @@ int path_get_whole_disk(const char *path, bool backing, dev_t *ret) {
return fd_get_whole_disk(fd, backing, ret);
}
int block_device_add_partition(int fd, const char *name, int nr, uint64_t start, uint64_t size) {
int block_device_add_partition(
int fd,
const char *name,
int nr,
uint64_t start,
uint64_t size) {
assert(fd >= 0);
assert(name);
assert(nr > 0);
@@ -452,7 +458,11 @@ int block_device_add_partition(int fd, const char *name, int nr, uint64_t start,
return RET_NERRNO(ioctl(fd, BLKPG, &ba));
}
int block_device_remove_partition(int fd, const char *name, int nr) {
int block_device_remove_partition(
int fd,
const char *name,
int nr) {
assert(fd >= 0);
assert(name);
assert(nr > 0);
@@ -475,6 +485,30 @@ int block_device_remove_partition(int fd, const char *name, int nr) {
return RET_NERRNO(ioctl(fd, BLKPG, &ba));
}
int block_device_resize_partition(
int fd,
int nr,
uint64_t start,
uint64_t size) {
assert(fd >= 0);
assert(nr > 0);
struct blkpg_partition bp = {
.pno = nr,
.start = start,
.length = size,
};
struct blkpg_ioctl_arg ba = {
.op = BLKPG_RESIZE_PARTITION,
.data = &bp,
.datalen = sizeof(bp),
};
return RET_NERRNO(ioctl(fd, BLKPG, &ba));
}
int block_device_remove_all_partitions(int fd) {
struct stat stat;
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
+1
View File
@@ -33,4 +33,5 @@ int path_get_whole_disk(const char *path, bool backing, dev_t *ret);
int block_device_add_partition(int fd, const char *name, int nr, uint64_t start, uint64_t size);
int block_device_remove_partition(int fd, const char *name, int nr);
int block_device_resize_partition(int fd, int nr, uint64_t start, uint64_t size);
int block_device_remove_all_partitions(int fd);
+8 -16
View File
@@ -824,7 +824,7 @@ static int resize_partition(int partition_fd, uint64_t offset, uint64_t size) {
assert(S_ISBLK(st.st_mode));
xsprintf(sysfs, "/sys/dev/block/%u:%u/partition", major(st.st_rdev), minor(st.st_rdev));
xsprintf(sysfs, "/sys/dev/block/" DEVNUM_FORMAT_STR "/partition", DEVNUM_FORMAT_VAL(st.st_rdev));
r = read_one_line_file(sysfs, &buffer);
if (r == -ENOENT) /* not a partition, cannot resize */
return -ENOTTY;
@@ -834,7 +834,7 @@ static int resize_partition(int partition_fd, uint64_t offset, uint64_t size) {
if (r < 0)
return r;
xsprintf(sysfs, "/sys/dev/block/%u:%u/start", major(st.st_rdev), minor(st.st_rdev));
xsprintf(sysfs, "/sys/dev/block/" DEVNUM_FORMAT_STR "/start", DEVNUM_FORMAT_VAL(st.st_rdev));
buffer = mfree(buffer);
r = read_one_line_file(sysfs, &buffer);
@@ -855,7 +855,7 @@ static int resize_partition(int partition_fd, uint64_t offset, uint64_t size) {
if (current_size == size && current_offset == offset)
return 0;
xsprintf(sysfs, "/sys/dev/block/%u:%u/../dev", major(st.st_rdev), minor(st.st_rdev));
xsprintf(sysfs, "/sys/dev/block/" DEVNUM_FORMAT_STR "/../dev", DEVNUM_FORMAT_VAL(st.st_rdev));
buffer = mfree(buffer);
r = read_one_line_file(sysfs, &buffer);
@@ -869,19 +869,11 @@ static int resize_partition(int partition_fd, uint64_t offset, uint64_t size) {
if (r < 0)
return r;
struct blkpg_partition bp = {
.pno = partno,
.start = offset == UINT64_MAX ? current_offset : offset,
.length = size == UINT64_MAX ? current_size : size,
};
struct blkpg_ioctl_arg ba = {
.op = BLKPG_RESIZE_PARTITION,
.data = &bp,
.datalen = sizeof(bp),
};
return RET_NERRNO(ioctl(whole_fd, BLKPG, &ba));
return block_device_resize_partition(
whole_fd,
partno,
offset == UINT64_MAX ? current_offset : offset,
size == UINT64_MAX ? current_size : size);
}
int loop_device_refresh_size(LoopDevice *d, uint64_t offset, uint64_t size) {