---
# System prepended metadata

title: BTRFS

---

# BTRFS

- Fichero VagrantFile de la maquina con 4 discos adicionales
```
Vagrant.configure("2") do |config|
disco1='.vagrant/disco01.vdi'
disco2='.vagrant/disco02.vdi'
disco3='.vagrant/disco03.vdi'
disco4='.vagrant/disco04.vdi'
 config.vm.define :btrfs do |btrfs|
  btrfs.vm.box = "debian/buster64"
  btrfs.vm.hostname = "btrfs"
  btrfs.vm.provider :virtualbox do |v|
  v.customize ["createhd", "--filename", disco1, "--size", 1024]
  v.customize ["storageattach", :id, "--storagectl", "SATA Controller",
  "--port", 1, "--device", 0, "--type", "hdd",
  "--medium", disco1]
  v.customize ["createhd", "--filename", disco2, "--size", 1024]
  v.customize ["storageattach", :id, "--storagectl", "SATA Controller",
  "--port", 2, "--device", 0, "--type", "hdd",
  "--medium", disco2]
  v.customize ["createhd", "--filename", disco3, "--size", 1024]
  v.customize ["storageattach", :id, "--storagectl", "SATA Controller",
  "--port", 3, "--device", 0, "--type", "hdd",
  "--medium", disco3]
  v.customize ["createhd", "--filename", disco4, "--size", 1024]
  v.customize ["storageattach", :id, "--storagectl", "SATA Controller",
  "--port", 4, "--device", 0, "--type", "hdd",
  "--medium", disco4]
  end
end
```

- Lista de los discos
```
vagrant@btrfs:~$ lsblk 
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0 19.8G  0 disk 
├─sda1   8:1    0 18.8G  0 part /
├─sda2   8:2    0    1K  0 part 
└─sda5   8:5    0 1021M  0 part [SWAP]
sdb      8:16   0    1G  0 disk 
sdc      8:32   0    1G  0 disk 
sdd      8:48   0    1G  0 disk 
sde      8:64   0    1G  0 disk 
```

- Instalar software
```
sudo apt install btrfs-tools
```

- Dar formato btrfs a un disco
```
vagrant@btrfs:~$ sudo mkfs.btrfs /dev/sdb 
btrfs-progs v4.20.1 
See http://btrfs.wiki.kernel.org for more information.

Label:              (null)
UUID:               2ceb76ad-7f88-43d2-9346-d5add4087ba6
Node size:          16384
Sector size:        4096
Filesystem size:    1.00GiB
Block group profiles:
  Data:             single            8.00MiB
  Metadata:         DUP              51.19MiB
  System:           DUP               8.00MiB
SSD detected:       no
Incompat features:  extref, skinny-metadata
Number of devices:  1
Devices:
   ID        SIZE  PATH
    1     1.00GiB  /dev/sdb
    
vagrant@btrfs:~$ lsblk -f
NAME   FSTYPE LABEL UUID                                 FSAVAIL FSUSE% MOUNTPOINT
sda                                                                     
├─sda1 ext4         b9ffc3d1-86b2-4a2c-a8be-f2b2f4aa4cb5   16.4G     5% /
├─sda2                                                                  
└─sda5 swap         f8f6d279-1b63-4310-a668-cb468c9091d8                [SWAP]
sdb    btrfs        2ceb76ad-7f88-43d2-9346-d5add4087ba6                
sdc                                                                     
sdd                                                                     
sde  
```

- Configuracion del soporte multidispositivo que lo interpreta como un RAID
```
vagrant@btrfs:~$ sudo mkfs.btrfs /dev/sdc /dev/sdd -f
btrfs-progs v4.20.1 
See http://btrfs.wiki.kernel.org for more information.

Label:              (null)
UUID:               ae936953-d16f-4bc9-a52f-fd0ba84794bf
Node size:          16384
Sector size:        4096
Filesystem size:    2.00GiB
Block group profiles:
  Data:             RAID0           204.75MiB
  Metadata:         RAID1           102.38MiB
  System:           RAID1             8.00MiB
SSD detected:       no
Incompat features:  extref, skinny-metadata
Number of devices:  2
Devices:
   ID        SIZE  PATH
    1     1.00GiB  /dev/sdc
    2     1.00GiB  /dev/sdd
    
vagrant@btrfs:~$ lsblk -f
NAME   FSTYPE LABEL UUID                                 FSAVAIL FSUSE% MOUNTPOINT
sda                                                                     
├─sda1 ext4         b9ffc3d1-86b2-4a2c-a8be-f2b2f4aa4cb5   16.4G     5% /
├─sda2                                                                  
└─sda5 swap         f8f6d279-1b63-4310-a668-cb468c9091d8                [SWAP]
sdb    btrfs        2ceb76ad-7f88-43d2-9346-d5add4087ba6                
sdc    btrfs        ae936953-d16f-4bc9-a52f-fd0ba84794bf                
sdd    btrfs        ae936953-d16f-4bc9-a52f-fd0ba84794bf                
sde       
```

## RAID

- Creacion de un RAID 1
```
vagrant@btrfs:~$ sudo mkfs.btrfs -d raid1 -m raid1 /dev/sdc /dev/sdd /dev/sde -f
btrfs-progs v4.20.1 
See http://btrfs.wiki.kernel.org for more information.

Label:              (null)
UUID:               6c73fc22-93e0-4152-be82-c015073d3ccb
Node size:          16384
Sector size:        4096
Filesystem size:    3.00GiB
Block group profiles:
  Data:             RAID1           153.56MiB
  Metadata:         RAID1           153.56MiB
  System:           RAID1             8.00MiB
SSD detected:       no
Incompat features:  extref, skinny-metadata
Number of devices:  3
Devices:
   ID        SIZE  PATH
    1     1.00GiB  /dev/sdc
    2     1.00GiB  /dev/sdd
    3     1.00GiB  /dev/sde

```

- Tenemos el primer disco que le dimos formato y el raid 1 que hemos creado
```
vagrant@btrfs:~$ sudo btrfs filesystem show
Label: none  uuid: 2ceb76ad-7f88-43d2-9346-d5add4087ba6
	Total devices 1 FS bytes used 128.00KiB
	devid    1 size 1.00GiB used 126.38MiB path /dev/sdb

Label: none  uuid: 6c73fc22-93e0-4152-be82-c015073d3ccb
	Total devices 3 FS bytes used 128.00KiB
	devid    1 size 1.00GiB used 307.12MiB path /dev/sdc
	devid    2 size 1.00GiB used 161.56MiB path /dev/sdd
	devid    3 size 1.00GiB used 161.56MiB path /dev/sde
```

- Añadir otro disco al raid, montamos un disco del raid y luego lo añadimos
```
vagrant@btrfs:~$ sudo mount /dev/sdc /mnt/
vagrant@btrfs:~$ sudo btrfs device add /dev/sdb /mnt/ -f
vagrant@btrfs:~$ sudo btrfs filesystem show
Label: none  uuid: 6c73fc22-93e0-4152-be82-c015073d3ccb
	Total devices 4 FS bytes used 256.00KiB
	devid    1 size 1.00GiB used 307.12MiB path /dev/sdc
	devid    2 size 1.00GiB used 161.56MiB path /dev/sdd
	devid    3 size 1.00GiB used 161.56MiB path /dev/sde
	devid    4 size 1.00GiB used 0.00B path /dev/sdb
```

- Repartir la informacion de los demas discos en el disco recientemente añadido
```
vagrant@btrfs:~$ sudo btrfs balance start --full-balance /mnt/
Done, had to relocate 3 out of 3 chunks
vagrant@btrfs:~$ sudo btrfs filesystem show
Label: none  uuid: 6c73fc22-93e0-4152-be82-c015073d3ccb
	Total devices 4 FS bytes used 256.00KiB
	devid    1 size 1.00GiB used 288.00MiB path /dev/sdc
	devid    2 size 1.00GiB used 416.00MiB path /dev/sdd
	devid    3 size 1.00GiB used 288.00MiB path /dev/sde
	devid    4 size 1.00GiB used 416.00MiB path /dev/sdb
```

- Comprobar el estado del RAID
```
vagrant@btrfs:~$ sudo btrfs scrub status /mnt/
scrub status for 6c73fc22-93e0-4152-be82-c015073d3ccb
	scrub started at Thu Jan 23 11:31:10 2020 and finished after 00:00:00
	total bytes scrubbed: 512.00KiB with 0 errors
```

- Desconexion de disco 4 (/dev/sde) desde fichero vagrant
```
vagrant@btrfs:~$ sudo btrfs filesystem show
warning, device 4 is missing
Label: none  uuid: 6c73fc22-93e0-4152-be82-c015073d3ccb
	Total devices 4 FS bytes used 320.00KiB
	devid    1 size 1.00GiB used 544.00MiB path /dev/sdb
	devid    2 size 1.00GiB used 448.00MiB path /dev/sdc
	devid    3 size 1.00GiB used 544.00MiB path /dev/sdd
	*** Some devices missing
    
vagrant@btrfs:~$ lsblk -f
NAME   FSTYPE LABEL UUID                                 FSAVAIL FSUSE% MOUNTPOINT
sda                                                                     
├─sda1 ext4         b9ffc3d1-86b2-4a2c-a8be-f2b2f4aa4cb5   16.4G     5% /
├─sda2                                                                  
└─sda5 swap         f8f6d279-1b63-4310-a668-cb468c9091d8                [SWAP]
sdb    btrfs        6c73fc22-93e0-4152-be82-c015073d3ccb                /mnt 
sdc    btrfs        6c73fc22-93e0-4152-be82-c015073d3ccb                
sdd    btrfs        6c73fc22-93e0-4152-be82-c015073d3ccb                                                              
```

- Eliminar disco del raid
```
vagrant@btrfs:~$ sudo btrfs device delete /dev/sde /mnt
```

- Añadir un nuevo volumen y añadirlo al RAID
```
vagrant@btrfs:~$ lsblk -f
NAME   FSTYPE LABEL UUID                                 FSAVAIL FSUSE% MOUNTPOINT
sda                                                                     
├─sda1 ext4         b9ffc3d1-86b2-4a2c-a8be-f2b2f4aa4cb5   16.4G     5% /
├─sda2                                                                  
└─sda5 swap         f8f6d279-1b63-4310-a668-cb468c9091d8                [SWAP]
sdb    btrfs        9c3c49e8-4a01-4b46-96e8-192a60abc584  758.8M     1% /mnt
sdc    btrfs        9c3c49e8-4a01-4b46-96e8-192a60abc584                
sdd    btrfs        9c3c49e8-4a01-4b46-96e8-192a60abc584                
sde       

vagrant@btrfs:~$ sudo btrfs device add /dev/sde /mnt -f

vagrant@btrfs:~$ sudo btrfs filesystem show
Label: none  uuid: 9c3c49e8-4a01-4b46-96e8-192a60abc584
	Total devices 4 FS bytes used 256.00KiB
	devid    1 size 1.00GiB used 416.00MiB path /dev/sdb
	devid    2 size 1.00GiB used 288.00MiB path /dev/sdc
	devid    3 size 1.00GiB used 288.00MiB path /dev/sdd
	devid    4 size 1.00GiB used 416.00MiB path /dev/sde

```

# Creacion de subvolumenes

- Crear el dispositivo btrfs
```
vagrant@btrfs:~$ sudo mkfs.btrfs /dev/sdb /dev/sdc /dev/sdd /dev/sde -f -L 'Subvolumenes'
btrfs-progs v4.20.1 
See http://btrfs.wiki.kernel.org for more information.

Label:              Subvolumenes
UUID:               22d092b6-74c7-443e-8f25-c6df58fe315b
Node size:          16384
Sector size:        4096
Filesystem size:    4.00GiB
Block group profiles:
  Data:             RAID0           409.50MiB
  Metadata:         RAID1           204.75MiB
  System:           RAID1             8.00MiB
SSD detected:       no
Incompat features:  extref, skinny-metadata
Number of devices:  4
Devices:
   ID        SIZE  PATH
    1     1.00GiB  /dev/sdb
    2     1.00GiB  /dev/sdc
    3     1.00GiB  /dev/sdd
    4     1.00GiB  /dev/sde

```

- Crear el grupo de volumenes
```
vagrant@btrfs:~$ sudo btrfs subvolume create /mnt/subvolumen1/
Create subvolume '/mnt/subvolumen1'
vagrant@btrfs:~$ sudo btrfs subvolume create /mnt/subvolumen2/
Create subvolume '/mnt/subvolumen2'
vagrant@btrfs:~$ sudo btrfs subvolume create /mnt/subvolumen3/
Create subvolume '/mnt/subvolumen3'

```

- Listar subvolumenes creados
```
vagrant@btrfs:~$ sudo btrfs subvolume list /mnt
ID 256 gen 6 top level 5 path subvolumen1
ID 259 gen 7 top level 5 path subvolumen2
ID 260 gen 8 top level 5 path subvolumen3
```

- Caracteristicas de los subvolumenes
```
vagrant@btrfs:~$ sudo btrfs subvolume show /mnt/subvolumen1/
subvolumen1
	Name: 			subvolumen1
	UUID: 			21d51dc4-1991-1d4e-a63d-29cfb494548d
	Parent UUID: 		-
	Received UUID: 		-
	Creation time: 		2020-01-24 11:00:31 +0000
	Subvolume ID: 		256
	Generation: 		6
	Gen at creation: 	6
	Parent ID: 		5
	Top level ID: 		5
	Flags: 			-
	Snapshot(s):

```

- Montado de un subvolumen
```
vagrant@btrfs:~$ sudo mount -o subvolid=256 /dev/sdb /mnt/montadosubvolumenes/

vagrant@btrfs:~$ sudo df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            227M     0  227M   0% /dev
tmpfs            49M  3.4M   45M   7% /run
/dev/sda1        19G 1019M   17G   6% /
tmpfs           242M     0  242M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           242M     0  242M   0% /sys/fs/cgroup
tmpfs            49M     0   49M   0% /run/user/1000
/dev/sdb        4.0G   17M  3.6G   1% /mnt
/dev/sdb        4.0G   17M  3.6G   1% /mnt/montadosubvolumenes

```

## Snapshot de subvolumemes

- Crear un snapshot de un volumen
```
vagrant@btrfs:~$ sudo btrfs subvolume snapshot /mnt/subvolumen1/ /mnt/snapshotsubvolumen1
Create a snapshot of '/mnt/subvolumen1/' in '/mnt/snapshotsubvolumen1'

vagrant@btrfs:~$ ls -l /mnt/
total 0
drwxr-xr-x 1 root root 0 Jan 24 11:00 montadosubvolumenes
drwxr-xr-x 1 root root 0 Jan 24 11:00 snapshotsubvolumen1
drwxr-xr-x 1 root root 0 Jan 24 11:00 subvolumen1
drwxr-xr-x 1 root root 0 Jan 24 11:00 subvolumen2
drwxr-xr-x 1 root root 0 Jan 24 11:00 subvolumen3
```

## Redimensión de volumenes

- Informacion de volumenes
```
vagrant@btrfs:~$ sudo btrfs filesystem show /mnt
Label: 'Subvolumenes'  uuid: 22d092b6-74c7-443e-8f25-c6df58fe315b
	Total devices 4 FS bytes used 320.00KiB
	devid    1 size 1.00GiB used 307.12MiB path /dev/sdb
	devid    2 size 1.00GiB used 307.12MiB path /dev/sdc
	devid    3 size 1.00GiB used 110.38MiB path /dev/sdd
	devid    4 size 1.00GiB used 110.38MiB path /dev/sde
    
vagrant@btrfs:~$ lsblk -f
NAME   FSTYPE LABEL        UUID                                 FSAVAIL FSUSE% MOUNTPOINT
sda                                                                            
├─sda1 ext4                b9ffc3d1-86b2-4a2c-a8be-f2b2f4aa4cb5   16.4G     5% /
├─sda2                                                                         
└─sda5 swap                f8f6d279-1b63-4310-a668-cb468c9091d8                [SWAP]
sdb    btrfs  Subvolumenes 22d092b6-74c7-443e-8f25-c6df58fe315b    3.6G     0% /mnt
sdc    btrfs  Subvolumenes 22d092b6-74c7-443e-8f25-c6df58fe315b                
sdd    btrfs  Subvolumenes 22d092b6-74c7-443e-8f25-c6df58fe315b                
sde    btrfs  Subvolumenes 22d092b6-74c7-443e-8f25-c6df58fe315b     

```

- Reducir el tamaño de volumen /dev/sdb para que use 512MB (se especifica la cantidad que queremos quitarle o darle)
```
vagrant@btrfs:~$ sudo btrfs filesystem resize -512m /mnt/
Resize '/mnt/' of '-512m'

vagrant@btrfs:~$ sudo btrfs filesystem show /mnt
Label: 'Subvolumenes'  uuid: 22d092b6-74c7-443e-8f25-c6df58fe315b
	Total devices 4 FS bytes used 320.00KiB
	devid    1 size 512.00MiB used 307.12MiB path /dev/sdb
	devid    2 size 1.00GiB used 307.12MiB path /dev/sdc
	devid    3 size 1.00GiB used 110.38MiB path /dev/sdd
	devid    4 size 1.00GiB used 110.38MiB path /dev/sde

```

- Volver a aumentar el tamaño de volumen
```
vagrant@btrfs:~$ sudo btrfs filesystem resize +512m /mnt/
Resize '/mnt/' of '+512m'

vagrant@btrfs:~$ sudo btrfs filesystem show /mnt
Label: 'Subvolumenes'  uuid: 22d092b6-74c7-443e-8f25-c6df58fe315b
	Total devices 4 FS bytes used 320.00KiB
	devid    1 size 1.00GiB used 307.12MiB path /dev/sdb
	devid    2 size 1.00GiB used 307.12MiB path /dev/sdc
	devid    3 size 1.00GiB used 110.38MiB path /dev/sdd
	devid    4 size 1.00GiB used 110.38MiB path /dev/sde

```

## Desframentacion de volumenes

- Desfragmentar un volumen
```
vagrant@btrfs:~$ sudo btrfs filesystem defrag /mnt/
```

## Cifrado de volumenes

- Paquete necesario para cifrar cryptsetup
```
vagrant@btrfs:~$ sudo apt install cryptsetup
```

- Creamos un keyfile con el comando dd con el usuario root en su directorio (tarda bastante)
```
root@btrfs:~# dd if=/dev/urandom of=/root/KeyFile bs=1 count=4096
4096+0 records in
4096+0 records out
4096 bytes (4.1 kB, 4.0 KiB) copied, 0.0294735 s, 139 kB/s

root@btrfs:~# ls -l
total 4
-rw-r--r-- 1 root root 4096 Jan 25 14:51 KeyFile

```

- Cambiarle los permisos al fichero keyfile
```
root@btrfs:~# chmod 0400 /root/KeyFile
```

- Cifraremos el disco /dev/sdc con la clave creada anteriormente
```
root@btrfs:~# cryptsetup luksFormat --key-file /root/KeyFile /dev/sdc
WARNING: Device /dev/sdc already contains a 'btrfs' superblock signature.

WARNING!
========
This will overwrite data on /dev/sdc irrevocably.

Are you sure? (Type uppercase yes): YES
```

- Abrir el disco cifrado
```
root@btrfs:~# cryptsetup open /dev/sdc /dev/sdc
```