How to convert a directory to a subvolume
Hi,
I just installed Ubuntu 22.04 with a btrfs raid1 root filesystem. I want to convert a directory, like /opt, into a subvolume. I haven’t been having much luck. /opt is empty right now, so it’s a good candidate for conversion. Could someone please explain how to do it? I’ve been at a dozen different websites, & tried different variations of the “btrfs subvolume create” command, but nothing works when I go to mount it. I think I’m missing something simple, but not sure what it is.
Thanks!
-John
Sent from my iPad
Quoted 3 lines · John Center
Hi,
I just installed Ubuntu 22.04 with a btrfs raid1 root filesystem. I want to convert a directory, like /opt, into a subvolume. I haven’t been having much luck. /opt is empty right now, so it’s a good candidate for conversion. Could someone please explain how to do it? I’ve been at a dozen different websites, & tried different variations of the “btrfs subvolume create” command, but nothing works when I go to mount it. I think I’m missing something simple, but not sure what it is.
You can't convert a directory into a subvolume.
Since the directory in question is empty, just delete it and create
a subvol there instead:
# rmdir /opt # btrfs sub create /opt
If there's stuff in there, you need to create the subvolume with a
different name, copy the contents of the directory into it (optionally with --reflink=always) and then delete the original directory and move the subvolume into its place.
Hugo.
Signature · 4 lines
Hugo Mills | UNIX: British manufacturer of modular shelving units hugo@... carfax.org.uk | http://carfax.org.uk/ | PGP: E2AB1DE4 |
Hi Hugo,
Thanks for responding. I guess what I don't understand, @home is a subvolume, but it appears as /home when it is mounted via fstab. It has a top level ID of 5. If I create a subvolume for opt, it has a top level of 256. I've tried different variations of opt, /opt, & @opt, but they all appear as that variation under /:
john@Mariposa:~$ sudo btrfs subvolume create /@opt Create subvolume '//@opt'
john@Mariposa:~$ sudo btrfs subvolume list / ID 256 gen 5968 top level 5 path @ ID 257 gen 5968 top level 5 path @home ID 259 gen 5966 top level 256 path @opt
john@Mariposa:~$ sudo btrfs subvolume delete /@opt Delete subvolume (no-commit): '//@opt'
john@Mariposa:~$ sudo btrfs subvolume create /opt Create subvolume '//opt'
john@Mariposa:~$ sudo btrfs subvolume list -t / ID gen top level path -- --- --------- ---- 256 5993 5 @ 257 5993 5 @home 260 5993 256 opt
What I'm expecting is when I do the subvolume list, I would see something like this:
john@Mariposa:~$ sudo btrfs subvolume list -t / ID gen top level path -- --- --------- ---- 256 5993 5 @ 257 5993 5 @home 260 5993 5 @opt
I would also think the fstab would look something like this:
UUID=ce05e908-2dce-4368-b864-2f29650185e8 / btrfs defaults,space_cache=v2,subvol=@ 0 1 #
UUID=ce05e908-2dce-4368-b864-2f29650185e8 /home btrfs defaults,space_cache=v2,subvol=@home 0 2 #
UUID=ce05e908-2dce-4368-b864-2f29650185e8 /opt btrfs defaults,space_cache=v2,subvol=@opt 0 2
I also thought I would have to mount the subvolume like a directory.
So, what am I missing between what I'm seeing vs what I think I should be seeing?
Thanks for your help!
-John
Quoted 19 lines · Hugo Mills
Quoted 3 lines · John Center
Hi,
I just installed Ubuntu 22.04 with a btrfs raid1 root filesystem. I want to convert a directory, like /opt, into a subvolume. I haven’t been having much luck. /opt is empty right now, so it’s a good candidate for conversion. Could someone please explain how to do it? I’ve been at a dozen different websites, & tried different variations of the “btrfs subvolume create” command, but nothing works when I go to mount it. I think I’m missing something simple, but not sure what it is.
You can't convert a directory into a subvolume.
Since the directory in question is empty, just delete it and create
a subvol there instead:# rmdir /opt # btrfs sub create /optIf there's stuff in there, you need to create the subvolume with a
different name, copy the contents of the directory into it (optionally with --reflink=always) and then delete the original directory and move the subvolume into its place.Hugo.
Quoted 11 lines · John Center
Hi Hugo,
Thanks for responding. I guess what I don't understand, @home is a subvolume, but it appears as /home when it is mounted via fstab. It has a top level ID of 5. If I create a subvolume for opt, it has a top level of 256. I've tried different variations of opt, /opt, & @opt, but they all appear as that variation under /:
john@Mariposa:~$ sudo btrfs subvolume create /@opt Create subvolume '//@opt'
This is the mistake. Since @ is mounted at /, using /@opt actually means /@/@opt, thus it's been nested.
Quoted 5 lines
john@Mariposa:~$ sudo btrfs subvolume list / ID 256 gen 5968 top level 5 path @ ID 257 gen 5968 top level 5 path @home ID 259 gen 5966 top level 256 path @opt
@opt was created inside @, that's why it looks like this. Thus, @opt is nested within @. Thus, @ and @home follow a flat layout. And @opt follows a nested layout. To continue with the flat layout for @opt you'd need to create the @opt subvolume in the top-level of the file system alongside @ and @home rather than within one of them. To create it in the top-level you need to mount the top-level somewhere, e.g.
btrfs subvolume get-default /
Assuming this refers to ID 5, then you just
mount /dev/sdXY /mnt
btrfs subvolume create /mnt/@opt
And the fstab entry should be subvol=@opt
If get-default reports a value other than 5, then you need to explicitly mount the top level:
mount -o subvol=/ /dev/sdXY /mnt OR
mount -o subvolid=5 /dev/sdXY /mnt OR
mount -o subvolid=0 /dev/sdXY /mnt
The top-level subvolume was assigned ID 5 during initial creation of the file system. Soon after, ID 0 was set up as an alias for 5 because maybe it'd be easier to remember.
Note: The top-level nomenclature used by btrfs subvolume list is considered confusing, and I think it's going away in btrfs-progs soonish? The "top-level" of the file system is considered equivalent to subvolume ID 5, which has no name, cannot be renamed or removed, but can be snapshot, and is the subvolume created at mkfs time. Above, it sounds like there's multiple "top-level" subvolumes, e.g. 5 and
256. But in practice it's not how anyone is using the term "top level", which these days is intended to refer to just ID 5. The subvolume with no (explicit) name.
Quoted 6 lines
john@Mariposa:~$ sudo btrfs subvolume delete /@opt Delete subvolume (no-commit): '//@opt'john@Mariposa:~$ sudo btrfs subvolume create /opt Create subvolume '//opt'
Yep, / is still the @ subvolume here, because @ is bind mounted to /
Quoted 2 lines
So, what am I missing between what I'm seeing vs what I think I should be seeing?
It'd be neat if we had a way to create subvolumes in the top level directly without having to mount the top level. The FD variant of the C API found in libbtrfsutil suggests it's possible. And also we have a way to delete subvolumes by subvolid, even when they aren't locatable via the mounted file system hierarchy, using the --subvolid flag.
Signature · 1 line
Chris Murphy
Hi Chris,
Thanks for responding! Well, I made some progress:
root@Mariposa:/# mount /dev/sda3 /mnt
root@Mariposa:/# btrfs subvolume create /mnt/@opt Create subvolume '/mnt/@opt'
root@Mariposa:/# btrfs subvolume list / ID 256 gen 11732 top level 5 path @ ID 257 gen 11733 top level 5 path @home ID 268 gen 11733 top level 5 path @opt
Once I unmounted /mnt, I had to create the /opt directory again:
root@Mariposa:/# umount /mnt
root@Mariposa:/# mkdir opt
Then I mounted /opt & it didn't complain:
root@Mariposa:/# mount /opt root@Mariposa:/# btrfs subvolume list / ID 256 gen 11751 top level 5 path @ ID 257 gen 11751 top level 5 path @home ID 268 gen 11733 top level 5 path @opt
So, is this correct? It would have been good if there was a direct way to add the level that you wanted the subvolume created & mounted with the btrfs command. So if I take a snapshot of @, /opt won't be included? That'd my goal. I've been using btrfs at a very basic level, added on top of an md raid1. It worked well for a number of years, but then I needed to replace the disks, I thought I'd go all in on btrfs.
Thanks for your help!
-John
Quoted 76 lines · Chris Murphy
Quoted 10 lines · John Center
Hi Hugo,
Thanks for responding. I guess what I don't understand, @home is a subvolume, but it appears as /home when it is mounted via fstab. It has a top level ID of 5. If I create a subvolume for opt, it has a top level of 256. I've tried different variations of opt, /opt, & @opt, but they all appear as that variation under /:
john@Mariposa:~$ sudo btrfs subvolume create /@opt Create subvolume '//@opt'This is the mistake. Since @ is mounted at /, using /@opt actually means /@/@opt, thus it's been nested.
Quoted 4 lines
john@Mariposa:~$ sudo btrfs subvolume list / ID 256 gen 5968 top level 5 path @ ID 257 gen 5968 top level 5 path @home ID 259 gen 5966 top level 256 path @opt@opt was created inside @, that's why it looks like this. Thus, @opt is nested within @. Thus, @ and @home follow a flat layout. And @opt follows a nested layout. To continue with the flat layout for @opt you'd need to create the @opt subvolume in the top-level of the file system alongside @ and @home rather than within one of them. To create it in the top-level you need to mount the top-level somewhere, e.g.
btrfs subvolume get-default /
Assuming this refers to ID 5, then you just
mount
/dev/sdXY/mnt
btrfs subvolume create/mnt/@optAnd the fstab entry should be subvol=@opt
If get-default reports a value other than 5, then you need to explicitly mount the top level:
mount -o subvol=/
/dev/sdXY/mnt OR
mount -o subvolid=5/dev/sdXY/mnt OR
mount -o subvolid=0/dev/sdXY/mntThe top-level subvolume was assigned ID 5 during initial creation of the file system. Soon after, ID 0 was set up as an alias for 5 because maybe it'd be easier to remember.
Note: The top-level nomenclature used by btrfs subvolume list is considered confusing, and I think it's going away in btrfs-progs soonish? The "top-level" of the file system is considered equivalent to subvolume ID 5, which has no name, cannot be renamed or removed, but can be snapshot, and is the subvolume created at mkfs time. Above, it sounds like there's multiple "top-level" subvolumes, e.g. 5 and
256. But in practice it's not how anyone is using the term "top level", which these days is intended to refer to just ID 5. The subvolume with no (explicit) name.Quoted 5 lines
john@Mariposa:~$ sudo btrfs subvolume delete /@opt Delete subvolume (no-commit): '//@opt'john@Mariposa:~$ sudo btrfs subvolume create /opt Create subvolume '//opt'Yep, / is still the @ subvolume here, because @ is bind mounted to /
Quoted 2 lines
So, what am I missing between what I'm seeing vs what I think I should be seeing?
It'd be neat if we had a way to create subvolumes in the top level directly without having to mount the top level. The FD variant of the C API found in libbtrfsutil suggests it's possible. And also we have a way to delete subvolumes by subvolid, even when they aren't locatable via the mounted file system hierarchy, using the --subvolid flag.
Quoted 23 lines · John Center
Hi Chris,
Thanks for responding! Well, I made some progress:
root@Mariposa:/# mount /dev/sda3 /mntroot@Mariposa:/# btrfs subvolume create /mnt/@opt Create subvolume '/mnt/@opt'root@Mariposa:/# btrfs subvolume list / ID 256 gen 11732 top level 5 path @ ID 257 gen 11733 top level 5 path @home ID 268 gen 11733 top level 5 path @optOnce I unmounted /mnt, I had to create the /opt directory again:
root@Mariposa:/# umount /mntroot@Mariposa:/# mkdir optThen I mounted /opt & it didn't complain:
Quoted 7 lines
root@Mariposa:/# mount /opt root@Mariposa:/# btrfs subvolume list / ID 256 gen 11751 top level 5 path @ ID 257 gen 11751 top level 5 path @home ID 268 gen 11733 top level 5 path @optSo, is this correct?
Looks correct. You need to include something like 'mount | grep btrfs' to know for sure, that should show subvol and subvolid for each mount point.
Quoted 4 lines
It would have been good if there was a direct way
to add the level that you wanted the subvolume created & mounted with the btrfs command. So if I take a snapshot of @, /opt won't be included?
Btrfs snapshots via the btrfs-progs CLI tool are not recursive. So whether the subvolumes are arranged in a nested or flat layout doesn't matter. There is a non-atomic recursive snapshot (create and remove) option in libbtrfs both for C API and Python bindings.
Quoted 4 lines
That'd my goal. I've been using btrfs at a very basic level, added on top of an md raid1. It worked well for a number of years, but then I needed to replace the disks, I thought I'd go all in on btrfs.
Signature · 1 line
Chris Murphy
root@Mariposa:/# mount | grep btrfs
/dev/sda3 on / type btrfs (rw,relatime,space_cache=v2,autodefrag,subvolid=256,subvol=/@) /dev/sda3 on /home type btrfs (rw,relatime,space_cache=v2,autodefrag,subvolid=257,subvol=/@home) /dev/sda3 on /opt type btrfs (rw,relatime,space_cache=v2,autodefrag,subvolid=268,subvol=/@opt)
Thanks, Hugo & Chris!
-John
Quoted 51 lines · Chris Murphy
Quoted 22 lines · John Center
Hi Chris,
Thanks for responding! Well, I made some progress:
root@Mariposa:/# mount /dev/sda3 /mntroot@Mariposa:/# btrfs subvolume create /mnt/@opt Create subvolume '/mnt/@opt'root@Mariposa:/# btrfs subvolume list / ID 256 gen 11732 top level 5 path @ ID 257 gen 11733 top level 5 path @home ID 268 gen 11733 top level 5 path @optOnce I unmounted /mnt, I had to create the /opt directory again:
root@Mariposa:/# umount /mntroot@Mariposa:/# mkdir optThen I mounted /opt & it didn't complain:
Quoted 7 lines
root@Mariposa:/# mount /opt root@Mariposa:/# btrfs subvolume list / ID 256 gen 11751 top level 5 path @ ID 257 gen 11751 top level 5 path @home ID 268 gen 11733 top level 5 path @optSo, is this correct?
Looks correct. You need to include something like 'mount | grep btrfs' to know for sure, that should show subvol and subvolid for each mount point.
Quoted 4 lines
It would have been good if there was a direct way
to add the level that you wanted the subvolume created & mounted with the btrfs command. So if I take a snapshot of @, /opt won't be included?Btrfs snapshots via the btrfs-progs CLI tool are not recursive. So whether the subvolumes are arranged in a nested or flat layout doesn't matter. There is a non-atomic recursive snapshot (create and remove) option in libbtrfs both for C API and Python bindings.
Quoted 4 lines
That'd my goal. I've been using btrfs at a very basic level, added on top of an md raid1. It worked well for a number of years, but then I needed to replace the disks, I thought I'd go all in on btrfs.
Quoted 4 lines · John Center
Hi,
I just installed Ubuntu 22.04 with a btrfs raid1 root filesystem. I want to convert a directory, like /opt, into a subvolume. I haven’t been having much luck. /opt is empty right now, so it’s a good candidate for conversion. Could someone please explain how to do it? I’ve been at a dozen different websites, & tried different variations of the “btrfs subvolume create” command, but nothing works when I go to mount it. I think I’m missing something simple, but not sure what it is.
https://btrfs.wiki.kernel.org/index.php/SysadminGuide#Nested
If you're using the nested layout, you won't need to use /etc/fstab. When using the flat layout, subvolumes are mounted into position using fstab.
Signature · 1 line
Chris Murphy