btrfs-progs: add --subvol option to mkfs.btrfs
PATCH v4 1/1 btrfs-progs: add --subvol option to mkfs.btrfs
This patch adds a --subvol option, which tells mkfs.btrfs to create the specified directories as subvolumes.
Given a populated directory img, the command
$ mkfs.btrfs --rootdir img --subvol img/usr --subvol img/home --subvol img/home/username /dev/loop0
will create subvolumes usr and home within the FS root, and subvolume username within the home subvolume. It will fail if any of the directories do not yet exist.
---
Changelog:
Patch 2:
* Rebased against upstream changes
* Rewrote so that directory sizes are correct within transactions
* Changed --subvol so that it is relative to cwd rather than rootdir, so that in future we might allow out-of-tree subvols
Patch 3:
* Changed btrfs_mkfs_fill_dir so it doesn't start a transaction itself
* Moved subvol creation and linking into traverse_directory
* Removed depth calculation code, no longer needed
Patch 4:
* Rebased against upstream changes
mkfs/main.c | 155 ++++++++++++++++++-- mkfs/rootdir.c | 132 +++++++++++++---- mkfs/rootdir.h | 9 +- tests/mkfs-tests/036-rootdir-subvol/test.sh | 33 +++++ 4 files changed, 287 insertions(+), 42 deletions(-) create mode 100755 tests/mkfs-tests/036-rootdir-subvol/test.sh
mkfs/main.c +143 -12
@@ -440,6 +440,7 @@ static const char * const mkfs_usage[] = {"Creation:" ,OPTLINE ("-b|--byte-count SIZE" ,"set size of each device to SIZE (filesystem size is sum of all device sizes)" ),OPTLINE ("-r|--rootdir DIR" ,"copy files from DIR to the image root directory" ), +OPTLINE ("-u|--subvol SUBDIR" ,"create SUBDIR as subvolume rather than normal directory" ),OPTLINE ("--shrink" ,"(with --rootdir) shrink the filled filesystem to minimal size" ),OPTLINE ("-K|--nodiscard" ,"do not perform whole device TRIM" ), OPTLINE("-f|--force", "force overwrite of existing filesystem"),
@@ -1055,6 +1056,7 @@ int BOX_MAIN(mkfs)(int argc, char **argv)char *label =NULL ;int nr_global_roots =sysconf (_SC_NPROCESSORS_ONLN);char *source_dir =NULL ; +LIST_HEAD (subvols);cpu_detect_flags ();hash_init_accel ();
@@ -1085,6 +1087,7 @@ int BOX_MAIN(mkfs)(int argc, char **argv) { "data", required_argument, NULL, 'd' }, { "version", no_argument, NULL, 'V' }, { "rootdir", required_argument, NULL, 'r' }, + { "subvol", required_argument, NULL, 'u' }, { "nodiscard", no_argument, NULL, 'K' }, { "features", required_argument, NULL, 'O' }, { "runtime-features", required_argument, NULL, 'R' },
@@ -1102,7 +1105,7 @@ int BOX_MAIN(mkfs)(int argc, char **argv) { NULL, 0, NULL, 0} }; - c =getopt_long (argc, argv,"A:b:fl:n:s:m:d:L:R:O:r:U:VvMKq" , + c =getopt_long (argc, argv,"A:b:fl:n:s:m:d:L:R:O:r:U:VvMKqu:" , long_options,NULL );if (c <0 )break ;
@@ -1208,6 +1211,22 @@ int BOX_MAIN(mkfs)(int argc, char **argv)free (source_dir); source_dir =strdup (optarg);break ; +case 'u' : { +struct rootdir_subvol *s; + + s =malloc (sizeof (struct rootdir_subvol )); +if (!s) { +error ("out of memory" ); + ret =1 ; + goto error; + } + + s->dir =strdup (optarg); + s->full_path =NULL ; + +list_add_tail (&s->list, &subvols); +break ; + }case 'U' :strncpy_null (fs_uuid, optarg,BTRFS_UUID_UNPARSED_SIZE );break ;
@@ -1272,6 +1291,77 @@ int BOX_MAIN(mkfs)(int argc, char **argv) ret =1 ; goto error; } +if (!list_empty (&subvols) && source_dir ==NULL ) { +error ("the option --subvol must be used with --rootdir" ); + ret =1 ; + goto error; + } + +if (source_dir) { +char *canonical =realpath (source_dir,NULL ); + +if (!canonical) { +error ("could not get canonical path to %s" , source_dir); + ret =1 ; + goto error; + } + +free (source_dir); + source_dir = canonical; + } + +if (!list_empty (&subvols)) { +size_t source_dir_len =strlen (source_dir); +struct rootdir_subvol *s; + +list_for_each_entry (s, &subvols, list) { +char *path; +struct rootdir_subvol *s2; + +if (!path_exists (s->dir)) { +error ("subvol %s does not exist" , + s->dir); + ret =1 ; + goto error; + } + +if (!path_is_dir (s->dir)) { +error ("subvol %s is not a directory" , s->dir); + ret =1 ; + goto error; + } + + path =realpath (s->dir,NULL ); + +if (!path) { +error ("could not get canonical path to %s" , + s->dir); + ret =1 ; + goto error; + } + + s->full_path = path; + +if (strlen (path) < source_dir_len +1 || +memcmp (path, source_dir, source_dir_len) || + path[source_dir_len] !='/' ) { +error ("subvol %s is not a child of %s" , + s->dir, source_dir); + ret =1 ; + goto error; + } + + for (s2 = list_first_entry(&subvols, struct rootdir_subvol, list); + s2 != s; s2 =list_next_entry (s2, list)) { +if (!strcmp (s2->full_path, path)) { +error ("subvol %s specified more than once" , + s->dir); + ret =1 ; + goto error; + } + } + } + }if (*fs_uuid) {uuid_t dummy_uuid;
@@ -1821,24 +1911,42 @@ raid_groups:error_msg (ERROR_MSG_START_TRANS ,"%m" ); goto out; } - ret =btrfs_rebuild_uuid_tree (fs_info); -if (ret <0 ) - goto out; - - ret =cleanup_temp_chunks (fs_info, &allocation, data_profile, - metadata_profile, metadata_profile); -if (ret <0 ) { -error ("failed to cleanup temporary chunks: %d" , ret); - goto out; - }if (source_dir) {pr_verbose (LOG_DEFAULT ,"Rootdir from: %s\n" , source_dir); - ret =btrfs_mkfs_fill_dir (source_dir, root); + + trans =btrfs_start_transaction (root,1 ); +if (IS_ERR (trans)) { + errno = -PTR_ERR (trans); +error_msg (ERROR_MSG_START_TRANS ,"%m" ); + goto out; + } + + ret =btrfs_mkfs_fill_dir (trans, source_dir, root, + &subvols);if (ret) {error ("error while filling filesystem: %d" , ret); +btrfs_abort_transaction (trans, ret); goto out; } + + ret =btrfs_commit_transaction (trans, root); +if (ret) { + errno = -ret; +error_msg (ERROR_MSG_COMMIT_TRANS ,"%m" ); + goto out; + } + +if (!list_empty (&subvols)) { +struct rootdir_subvol *s; + +list_for_each_entry (s, &subvols, list) { +pr_verbose (LOG_DEFAULT , +" Subvol from: %s\n" , + s->full_path); + } + } +if (shrink_rootdir) {pr_verbose (LOG_DEFAULT ," Shrink: yes\n" ); ret = btrfs_mkfs_shrink_fs(fs_info, &shrink_size,
@@ -1853,6 +1961,17 @@ raid_groups: } } + ret =btrfs_rebuild_uuid_tree (fs_info); +if (ret <0 ) + goto out; + + ret =cleanup_temp_chunks (fs_info, &allocation, data_profile, + metadata_profile, metadata_profile); +if (ret <0 ) { +error ("failed to cleanup temporary chunks: %d" , ret); + goto out; + } +if (features.runtime_flags &BTRFS_FEATURE_RUNTIME_QUOTA || features.incompat_flags &BTRFS_FEATURE_INCOMPAT_SIMPLE_QUOTA ) { ret =setup_quota_root (fs_info);
@@ -1946,6 +2065,18 @@ error:free (label);free (source_dir); +while (!list_empty (&subvols)) { +struct rootdir_subvol *head =list_entry (subvols.next, + struct rootdir_subvol, + list); + +free (head->dir); +free (head->full_path); + +list_del (&head->list); +free (head); + } +return !!ret; success:
mkfs/rootdir.c +103 -29
228 lines not on this page · raw message
mkfs/rootdir.h +8 -1
@@ -28,7 +28,14 @@struct btrfs_fs_info ;struct btrfs_root ; -int btrfs_mkfs_fill_dir (const char *source_dir,struct btrfs_root *root); +struct rootdir_subvol { +struct list_head list; +char *dir; +char *full_path; +}; + +int btrfs_mkfs_fill_dir (struct btrfs_trans_handle *trans,const char *source_dir, +struct btrfs_root *root,struct list_head *subvols);u64 btrfs_mkfs_size_dir (const char *dir_name,u32 sectorsize,u64 min_dev_size,u64 meta_profile,u64 data_profile); int btrfs_mkfs_shrink_fs(struct btrfs_fs_info *fs_info, u64 *new_size_ret,
tests/mkfs-tests/036-rootdir-subvol/test.sh new file +33 -0
@@ -0,0 +1,33 @@ +#!/bin/bash +# smoke test for mkfs.btrfs --subvol option + +source "$TEST_TOP/common" || exit + +check_prereq mkfs.btrfs +check_prereq btrfs + +setup_root_helper +prepare_test_dev + +tmp=$(_mktemp_dir mkfs-rootdir) + +touch $tmp/foo +mkdir $tmp/dir +mkdir $tmp/dir/subvol +touch $tmp/dir/subvol/bar + +run_check_mkfs_test_dev --rootdir "$tmp" --subvol "$tmp/dir/subvol" +run_check $SUDO_HELPER "$TOP/btrfs" check "$TEST_DEV" + +run_check_mount_test_dev +run_check_stdout $SUDO_HELPER "$TOP/btrfs" subvolume list "$TEST_MNT" | \ + cut -d\ -f9 > "$tmp/output" +run_check_umount_test_dev + +result=$(cat "$tmp/output") + +if [ "$result" != "dir/subvol" ]; then + _fail "dir/subvol not in subvolume list" +fi + +rm -rf -- "$tmp"
Signature · 1 line
2.44.2
Quoted 5 lines · Mark Harmstone
This patch adds a --subvol option, which tells mkfs.btrfs to create the specified directories as subvolumes.
Given a populated directory img, the command
Quoted 7 lines
$ mkfs.btrfs --rootdir img --subvol img/usr --subvol img/home --subvol img/home/username /dev/loop0will create subvolumes usr and home within the FS root, and subvolume username within the home subvolume. It will fail if any of the directories do not yet exist.
Signed-off-by Mark Harmstone <[email protected]>
This version looks much better.
Just some small code style related comments.
[...]
Quoted 14 lines
mkfs/main.c +10
-0@@ -1272,6 +1291,77 @@ int BOX_MAIN(mkfs)(int argc, char **argv) ret =1 ; goto error; } +if (!list_empty (&subvols) && source_dir ==NULL ) { +error ("the option --subvol must be used with --rootdir" ); + ret =1 ; + goto error; + } + +if (source_dir) { +char *canonical =realpath (source_dir,NULL ); + +if (!canonical) {
mkfs/main.c int BOX_MAIN(mkfs)(int argc, char **argv) · line 1313
Quoted 10 lines
mkfs/main.c +10
-0+error ("could not get canonical path to %s" , source_dir); + ret =1 ; + goto error; + } + +free (source_dir); + source_dir = canonical; + } + +if (!list_empty (&subvols)) {
We can skip the list_empty() check, as the later list_for_each_entry() can handle empty list pretty well.
This will save us one indent.
[...]
Quoted 32 lines
mkfs/main.c +11
-11@@ -1821,24 +1911,42 @@ raid_groups:error_msg (ERROR_MSG_START_TRANS ,"%m" ); goto out; } - ret =btrfs_rebuild_uuid_tree (fs_info); -if (ret <0 ) - goto out; - - ret =cleanup_temp_chunks (fs_info, &allocation, data_profile, - metadata_profile, metadata_profile); -if (ret <0 ) { -error ("failed to cleanup temporary chunks: %d" , ret); - goto out; - }if (source_dir) {pr_verbose (LOG_DEFAULT ,"Rootdir from: %s\n" , source_dir); - ret =btrfs_mkfs_fill_dir (source_dir, root); + + trans =btrfs_start_transaction (root,1 ); +if (IS_ERR (trans)) { + errno = -PTR_ERR (trans); +error_msg (ERROR_MSG_START_TRANS ,"%m" ); + goto out; + } + + ret =btrfs_mkfs_fill_dir (trans, source_dir, root, + &subvols);if (ret) {error ("error while filling filesystem: %d" , ret); +btrfs_abort_transaction (trans, ret); goto out;
mkfs/main.c raid_groups: · line 1940
Quoted 10 lines
mkfs/main.c +9
-0} + + ret =btrfs_commit_transaction (trans, root); +if (ret) { + errno = -ret; +error_msg (ERROR_MSG_COMMIT_TRANS ,"%m" ); + goto out; + } + +if (!list_empty (&subvols)) {
Same here.
[...]
mkfs/rootdir.c static int ftw_add_inode(const char *full_path, const struct stat *st, · line 593
Quoted 5 lines
mkfs/rootdir.c +1
-0@@ -511,6 +590,20 @@ static int ftw_add_inode(const char *full_path, const struct stat *st,while (current_path.level > ftwbuf->level)rootdir_path_pop (¤t_path); +if (!list_empty (g_subvols) &&S_ISDIR (st->st_mode)) {
The same here.
mkfs/rootdir.c static int ftw_add_inode(const char *full_path, const struct stat *st, · line 599
Quoted 6 lines
mkfs/rootdir.c +6
-0+struct rootdir_subvol *s; + +list_for_each_entry (s, g_subvols, list) { +if (!strcmp (full_path, s->full_path)) { +return ftw_add_subvol (full_path, st, typeflag, + ftwbuf, s);
Another optimization can be, to remove @s from the g_subvols list. So that the next search will spend a little less time to do the search.
Thanks,
Qu
Thanks Qu.
Quoted 19 lines · Qu Wenruo
Quoted 19 lines
mkfs/main.c +15
-0@@ -1272,6 +1291,77 @@ int BOX_MAIN(mkfs)(int argc, char **argv) ret = 1; goto error; } + if (!list_empty(&subvols) && source_dir == NULL) { + error("the option --subvol must be used with --rootdir"); + ret = 1; + goto error; + } + + if (source_dir) { + char *canonical = realpath(source_dir, NULL); + + if (!canonical) { + error("could not get canonical path to %s", source_dir); + ret = 1; + goto error; + } +
Quoted 8 lines
mkfs/main.c int BOX_MAIN(mkfs)(int argc, char **argv) · line 1313
Quoted 5 lines
mkfs/main.c +5
-0+ free(source_dir); + source_dir = canonical; + } + + if (!list_empty(&subvols)) {We can skip the
list_empty()check, as the laterlist_for_each_entry()can handle empty list pretty well.
I did this to avoid a strlen call if there's no subvols, but that's not very important.
Quoted 9 lines
mkfs/rootdir.c static int ftw_add_inode(const char *full_path, const struct stat *st, · line 599
Quoted 6 lines
mkfs/rootdir.c +6
-0+ struct rootdir_subvol *s; + + list_for_each_entry(s, g_subvols, list) { + if (!strcmp(full_path, s->full_path)) { + return ftw_add_subvol(full_path, st, typeflag, + ftwbuf, s);Another optimization can be, to remove @s from the
g_subvolslist. So that the next search will spend a little less time to do the search.
Unfortunately you can't do that, as it means that the pointer will be missed in the mkfs_main cleanup.
Mark
Quoted 41 lines · Mark Harmstone
Thanks Qu.
Quoted 19 lines · Qu Wenruo
Quoted 19 lines
mkfs/main.c +15
-0@@ -1272,6 +1291,77 @@ int BOX_MAIN(mkfs)(int argc, char **argv) ret = 1; goto error; } + if (!list_empty(&subvols) && source_dir == NULL) { + error("the option --subvol must be used with --rootdir"); + ret = 1; + goto error; + } + + if (source_dir) { + char *canonical = realpath(source_dir, NULL); + + if (!canonical) { + error("could not get canonical path to %s", source_dir); + ret = 1; + goto error; + } +Quoted 8 lines
mkfs/main.c int BOX_MAIN(mkfs)(int argc, char **argv) · line 1313
Quoted 5 lines
mkfs/main.c +5
-0+ free(source_dir); + source_dir = canonical; + } + + if (!list_empty(&subvols)) {We can skip the
list_empty()check, as the laterlist_for_each_entry()can handle empty list pretty well.I did this to avoid a strlen call if there's no subvols, but that's not very important.
Quoted 7 lines
Quoted 6 lines
mkfs/rootdir.c +6
-0+ struct rootdir_subvol *s; + + list_for_each_entry(s, g_subvols, list) { + if (!strcmp(full_path, s->full_path)) { + return ftw_add_subvol(full_path, st, typeflag, + ftwbuf, s);
Quoted 5 lines
Quoted 2 lines
Another optimization can be, to remove @s from the
g_subvolslist. So that the next search will spend a little less time to do the search.Unfortunately you can't do that, as it means that the pointer will be missed in the
mkfs_maincleanup.
Why? You removed it from the list and free the memory.
And at the final cleanup, there will be no entry left.
Thanks,
Qu
Quoted 3 lines
Mark
Quoted 4 lines · Qu Wenruo
Why? You removed it from the list and free the memory.
And at the final cleanup, there will be no entry left.
Okay - if you can commit patch v5 to the repo, I'll send through a patch for this.
Thanks
Mark