Skip to contentSkip to conversation

btrfslabel - user space

PATCH In review Goffredo Baroncelli 1 participant 2 messages Jan 4, 2010

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 btrfslabel - user space

Hi all,

recently I needed to change a label of a btrfs filesystem. After a bit of googling I discovered the Morey patches (dated august 2008).

So I update the patches to the latest btrfs source. The patches creates a new command called btrfslabel which is able to change the
label of a btrfs filesystem.

The command differentiates two cases:
- the filesystem is mounted: the change is done by the kernel (see the second patch), btrfslabel uses an ioctl to start the label changing.
- the filesystem is unmounted: the change is performed in user space.

The usage is very simple: the command needs the device where is the filesystem and (optionally) the label. If the label is missing, btrfs shows the label, otherwise it changes the label.

$ ./btrfslabel /dev/sdc1
foo
$ ./btrfslabel /dev/sdc1 bar
$ ./btrfslabel /dev/sdc1
bar

Regarding the Morey patches I changed two things:
- the ioctl numbers, because the original ones are now used by other ioctl
- I removed an optimization in mkfs.c (which in any case is not relevant with btrfslabel)

If request I can modify the patch. Comments are welcome.

BR
G.Baroncelli

---

Makefile +4 -1
index 02f881e..70efe3b 100644
@@ -17,7 +17,7 @@ bindir = $(prefix)/bin
 LIBS=-luuid
 
 progs = btrfsctl mkfs.btrfs btrfs-debug-tree btrfs-show btrfs-vol btrfsck \
-	btrfs-map-logical
+	btrfs-map-logical btrfslabel
 
 # make C=1 to enable sparse
 ifdef C
@@ -48,6 +48,9 @@ btrfs-show: $(objects) btrfs-show.o
 btrfsck: $(objects) btrfsck.o
 	gcc $(CFLAGS) -o btrfsck btrfsck.o $(objects) $(LDFLAGS) $(LIBS)
 
+btrfslabel: $(objects) btrfslabel.o
+	gcc $(CFLAGS) -o btrfslabel btrfslabel.o $(objects) $(LDFLAGS) $(LIBS)
+
 mkfs.btrfs: $(objects) mkfs.o
 	gcc $(CFLAGS) -o mkfs.btrfs $(objects) mkfs.o $(LDFLAGS) $(LIBS)
 
btrfslabel.c new file +244 -0

244 lines not on this page · raw message

ioctl.h +12 -0
index 4410ac0..d3253bf 100644
@@ -20,6 +20,7 @@
 #define __IOCTL_
 #include <asm/types.h>
 #include <linux/ioctl.h>
+#include "ctree.h"
 
 #define BTRFS_IOCTL_MAGIC 0x94
 #define BTRFS_VOL_NAME_MAX 255
@@ -30,6 +31,10 @@ struct btrfs_ioctl_vol_args {
 	char name[BTRFS_PATH_NAME_MAX + 1];
 };
 
+struct btrfs_ioctl_label_args {
+	char name[BTRFS_LABEL_SIZE + 1];
+};
+
 #define BTRFS_IOC_SNAP_CREATE _IOW(BTRFS_IOCTL_MAGIC, 1, \
 				   struct btrfs_ioctl_vol_args)
 #define BTRFS_IOC_DEFRAG _IOW(BTRFS_IOCTL_MAGIC, 2, \
@@ -59,4 +64,11 @@ struct btrfs_ioctl_vol_args {
 
 #define BTRFS_IOC_SNAP_DESTROY _IOW(BTRFS_IOCTL_MAGIC, 15, \
 				   struct btrfs_ioctl_vol_args)
+
+/* Used to set and get the current volume label */
+#define BTRFS_IOC_SET_LABEL    _IOW(BTRFS_IOCTL_MAGIC, 16, \
+				   struct btrfs_ioctl_label_args)
+#define BTRFS_IOC_GET_LABEL    _IOW(BTRFS_IOCTL_MAGIC, 17, \
+				   struct btrfs_ioctl_label_args)
+
 #endif
utils.c +58 -1

74 lines not on this page · raw message

utils.h +2 -0
index 7ff542b..838b65e 100644
@@ -40,4 +40,6 @@ int check_mounted(char *devicename);
 int btrfs_device_already_in_root(struct btrfs_root *root, int fd,
 				 int super_offset);
 char *pretty_sizes(u64 size);
+int check_label(char *input);
+int get_mountpt(char *dev, char *mntpt, size_t size);
 #endif

----

Signature · 2 lines
gpg key@ keyserver.linux.it: Goffredo Baroncelli (ghigo) <kreijackATinwind.it>
Key fingerprint = 4769 7E51 5293 D36C 814E  C054 BF04 F161 3DC5 0512
#1 lore raw
Quoted 5 lines · Goffredo Baroncelli

Hi all,

recently I needed to change a label of a btrfs filesystem. After a bit of googling I discovered the Morey patches (dated august 2008).

Quoted 8 lines

So I update the patches to the latest btrfs source. The patches creates a new command called btrfslabel which is able to change the
label of a btrfs filesystem.

The command differentiates two cases:
- the filesystem is mounted: the change is done by the kernel (see the second patch), btrfslabel uses an ioctl to start the label changing.
- the filesystem is unmounted: the change is performed in user space.

I forgot to mention that even if the kernel isn't patched, it is still possible to change a label of an unmounted btrfs filesystem.

Signature · 2 lines
gpg key@ keyserver.linux.it: Goffredo Baroncelli (ghigo) <kreijackATinwind.it>
Key fingerprint = 4769 7E51 5293 D36C 814E  C054 BF04 F161 3DC5 0512