repart: Add BlockDeviceReplace=
BlockDeviceReplace=/mntpnt will move the btrfs filesystem from mount point to the partition created. After moving, it will resize to take the whole partition. This is useful for OS installers that move a live system into a disk and do not require a reboot.
This commit is contained in:
+73
-4
@@ -23,6 +23,7 @@
|
||||
#include "rm-rf.h"
|
||||
#include "sparse-endian.h"
|
||||
#include "stat-util.h"
|
||||
#include "stdio-util.h"
|
||||
#include "string-util.h"
|
||||
#include "time-util.h"
|
||||
|
||||
@@ -95,14 +96,20 @@ int btrfs_subvol_get_read_only_fd(int fd) {
|
||||
return !!(flags & BTRFS_SUBVOL_RDONLY);
|
||||
}
|
||||
|
||||
int btrfs_get_block_device_at(int dir_fd, const char *path, dev_t *ret) {
|
||||
int btrfs_get_block_device_at_full(int dir_fd, const char *path, uint64_t *ret_devid, char **ret_path, dev_t *ret) {
|
||||
struct btrfs_ioctl_fs_info_args fsi = {};
|
||||
_cleanup_close_ int fd = -EBADF;
|
||||
uint64_t id;
|
||||
int r;
|
||||
|
||||
/*
|
||||
* Returns:
|
||||
* ret_devid - the device id in the filesystem for the returned block device
|
||||
* ret_path - the path to the returned block device
|
||||
* ret - the returned block device
|
||||
*/
|
||||
|
||||
assert(dir_fd >= 0 || IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT));
|
||||
assert(ret);
|
||||
|
||||
fd = xopenat(dir_fd, path, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
|
||||
if (fd < 0)
|
||||
@@ -119,7 +126,12 @@ int btrfs_get_block_device_at(int dir_fd, const char *path, dev_t *ret) {
|
||||
|
||||
/* We won't do this for btrfs RAID */
|
||||
if (fsi.num_devices != 1) {
|
||||
*ret = 0;
|
||||
if (ret_devid)
|
||||
*ret_devid = 0;
|
||||
if (ret_path)
|
||||
*ret_path = NULL;
|
||||
if (ret)
|
||||
*ret = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -128,6 +140,7 @@ int btrfs_get_block_device_at(int dir_fd, const char *path, dev_t *ret) {
|
||||
.devid = id,
|
||||
};
|
||||
struct stat st;
|
||||
_cleanup_free_ char *device_path = NULL;
|
||||
|
||||
if (ioctl(fd, BTRFS_IOC_DEV_INFO, &di) < 0) {
|
||||
if (errno == ENODEV)
|
||||
@@ -155,7 +168,16 @@ int btrfs_get_block_device_at(int dir_fd, const char *path, dev_t *ret) {
|
||||
if (major(st.st_rdev) == 0)
|
||||
return -ENODEV;
|
||||
|
||||
*ret = st.st_rdev;
|
||||
device_path = strdup((char*) di.path);
|
||||
if (!device_path)
|
||||
return -ENOMEM;
|
||||
|
||||
if (ret_path)
|
||||
*ret_path = TAKE_PTR(device_path);
|
||||
if (ret)
|
||||
*ret = st.st_rdev;
|
||||
if (ret_devid)
|
||||
*ret_devid = id;
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -2122,3 +2144,50 @@ int btrfs_get_file_physical_offset_fd(int fd, uint64_t *ret) {
|
||||
|
||||
return -ENODATA;
|
||||
}
|
||||
|
||||
int btrfs_replace(int fdmntpnt, uint64_t device_id, const char *target) {
|
||||
struct btrfs_ioctl_dev_replace_args replace = {
|
||||
.cmd = BTRFS_IOCTL_DEV_REPLACE_CMD_START,
|
||||
.result = UINT64_MAX,
|
||||
.start = {
|
||||
.srcdevid = device_id,
|
||||
.cont_reading_from_srcdev_mode = BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_ALWAYS,
|
||||
},
|
||||
};
|
||||
|
||||
assert(fdmntpnt >= 0);
|
||||
assert(target);
|
||||
|
||||
if (strlen(target) >= sizeof(replace.start.tgtdev_name))
|
||||
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Path to the btrfs replace target is too long");
|
||||
strncpy((char *)replace.start.tgtdev_name, target, sizeof(replace.start.tgtdev_name));
|
||||
|
||||
if (ioctl(fdmntpnt, BTRFS_IOC_DEV_REPLACE, &replace) < 0)
|
||||
return -errno;
|
||||
|
||||
switch (replace.result) {
|
||||
case BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR:
|
||||
break;
|
||||
case BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED:
|
||||
return log_debug_errno(SYNTHETIC_ERRNO(ECANCELED), "btrfs replace was not started");
|
||||
case BTRFS_IOCTL_DEV_REPLACE_RESULT_ALREADY_STARTED:
|
||||
return log_debug_errno(SYNTHETIC_ERRNO(EALREADY), "btrfs replace was already started on this device");
|
||||
case BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS:
|
||||
return log_debug_errno(SYNTHETIC_ERRNO(EBUSY), "btrfs scrub is in progress");
|
||||
default:
|
||||
return log_debug_errno(SYNTHETIC_ERRNO(EIO), "An unknown btrfs error status occurred");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int btrfs_resize_max(int fdmntpnt, uint64_t devid) {
|
||||
struct btrfs_ioctl_vol_args args = {};
|
||||
|
||||
assert(fdmntpnt >= 0);
|
||||
|
||||
assert_cc(STRLEN(":max") + DECIMAL_STR_MAX(uint64_t) + 1 <= sizeof(args.name));
|
||||
xsprintf(args.name, "%" PRIu64 ":max", devid);
|
||||
|
||||
return RET_NERRNO(ioctl(fdmntpnt, BTRFS_IOC_RESIZE, &args));
|
||||
}
|
||||
|
||||
@@ -49,7 +49,10 @@ static inline int btrfs_is_subvol(const char *path) {
|
||||
return btrfs_is_subvol_at(AT_FDCWD, path);
|
||||
}
|
||||
|
||||
int btrfs_get_block_device_at(int dir_fd, const char *path, dev_t *ret);
|
||||
int btrfs_get_block_device_at_full(int dir_fd, const char *path, uint64_t *ret_devid, char **ret_path, dev_t *ret);
|
||||
static inline int btrfs_get_block_device_at(int dir_fd, const char *path, dev_t *ret) {
|
||||
return btrfs_get_block_device_at_full(dir_fd, path, NULL, NULL, ret);
|
||||
}
|
||||
static inline int btrfs_get_block_device(const char *path, dev_t *ret) {
|
||||
return btrfs_get_block_device_at(AT_FDCWD, path, ret);
|
||||
}
|
||||
@@ -133,3 +136,6 @@ bool btrfs_might_be_subvol(const struct stat *st) _pure_;
|
||||
int btrfs_forget_device(const char *path);
|
||||
|
||||
int btrfs_get_file_physical_offset_fd(int fd, uint64_t *ret);
|
||||
|
||||
int btrfs_replace(int fdmntpnt, uint64_t device_id, const char *target);
|
||||
int btrfs_resize_max(int fdmntpnt, uint64_t devid);
|
||||
|
||||
Reference in New Issue
Block a user