# - bof - #

#!/bin/bash



<<'comment'
# -  - #
# - File name - #
Btrfs-FileSystemConfiguration.sh

cd ~/; rm -rf ~/Btrfs_Arch.sh; vim ~/Btrfs_Arch.sh;
copy & paste this content
chmod +x ~/Btrfs_Arch.sh; ~/./Btrfs_Arch.sh;

cd ~/.ssh;
ssh-keygen -t rsa;

# -  - #
# - Fixing ssh error - #
ssh-keygen -R "you server hostname or ip"
ssh-keygen -R 10.0.0.1
ssh-keygen -R 192.168.1.1;
comment




# :1
# -  - #
# - Btrfs - #


# :1.a
# -  - #
# - Documentation - #
https://christitus.com/btrfs-guide/
https://www.youtube.com/watch?v=J2QP4onqJKI


# :1b
# -  - #
# - Install btrfs - #
sudo pacman -S btrfs-progs --noconfirm --needed;

which btrfs;
/usr/bin/brtfs



# :2
# -  - #
# - List partitions - #
sudo fdisk -l;
lsblk -f;
sudo blkid;
sudo blkid /dev/sdb;

# :2.a
# -  - #
# - Create Raids btrfs partition - #
Jump to    # :4



# :3
# -  - #
# - Create single btrfs partition - #

# :3.a
# -  - #
# - Create single btrfs partition - #
# https://commandmasters.com/commands/mkfs.btrfs-linux/
# https://www.thegeekdiary.com/centos-rhel-how-to-create-and-mount-btrfs-file-system-explained-with-examples/
sudo mkfs.btrfs -f -L DataBtrfs -m single /dev/sdb;


# :3.b
# -  - #
# - Mount btrfs partition - #
sudo mkdir /DataBtrfs;
sudo mount /dev/sdb /DataBtrfs;
sudo btrfs subvolume create /DataBtrfs/@DataBtrfs;
sudo umount /DataBtrfs;


# :3.c
# -  - #
# - Mount Btrfs filesystem permanently on fstab - #

sudo vim /etc/fstab;

# -  - #
# - Btrfs filesystem - #
UUID=524a63f7-c67a-4f4b-bd6f-9a09eb0986fa  /DataBtrfs   btrfs   ssd,noatime,nodiratime,compress=lzo,space_cache=v2,discard=async,autodefrag,subvol=/@DataBtrfs   0   0
#UUID=524a63f7-c67a-4f4b-bd6f-9a09eb0986fa  /DataBtrfs   btrfs   rw,noatime,nodiratime,compress=lzo,space_cache=v2,discard=async,autodefrag,subvol=/@DataBtrfs    0   0
#UUID=524a63f7-c67a-4f4b-bd6f-9a09eb0986fa  /DataBtrfs   btrfs   rw,noatime,compress=lzo,discard=async,space_cache=v2,subvol=/@DataBtrfs                          0   0

sudo mount -av;
sudo systemctl daemon-reload;


# :3.d
# -  - #
# - Show Btrfs device info - #
sudo blkid /dev/sdb;
sudo lsblk --fs /dev/sdb;

NAME FSTYPE FSVER LABEL     UUID                                 FSAVAIL FSUSE% MOUNTPOINTS
sdb  btrfs        DataBtrfs 524a63f7-c67a-4f4b-bd6f-9a09eb0986fa   14.6T     0% /DataBtrfs


# :3.e
# -  - #
# - Show DataBtrfs device info - #
sudo btrfs filesystem df /DataBtrfs;

Data, single: total=8.00MiB, used=0.00B
System, RAID1: total=8.00MiB, used=16.00KiB
Metadata, RAID1: total=1.00GiB, used=128.00KiB
GlobalReserve, single: total=5.50MiB, used=0.00B


# :3.f
# -  - #
# - Create btrfs DataBtrfs subvolumes - #
sudo btrfs subvolume create /DataBtrfs/Documents;
sudo btrfs subvolume create /DataBtrfs/Documents/Downloads;
sudo btrfs subvolume create /DataBtrfs/Documents/Music;
sudo btrfs subvolume create /DataBtrfs/Documents/Pictures;
sudo btrfs subvolume create /DataBtrfs/Documents/Videos;

sudo btrfs subvolume create /DataBtrfs/Images;
sudo btrfs subvolume create /DataBtrfs/Images/Backup;
sudo btrfs subvolume create /DataBtrfs/Images/Programs;
sudo btrfs subvolume create /DataBtrfs/Images/VManager;

tree /DataBtrfs;

/DataBtrfs
└── Images
    ├── Backup
    ├── Programs
    └── VManager

# -  - #
# -  - #
# - Change /DataBtrfs ownership - #
sudo chown -R is_derayo:is_derayo /DataBtrfs;
chmod -R 775 /DataBtrfs;


# :3.g
# -  - #
# - Listing subvolumes - #
sudo btrfs subvolume list /DataBtrfs;


# :3.h
# -  - #
# - Check available space on Btrfs subvolumes - #
sudo btrfs filesystem df /DataBtrfs;





# :4
# -  - #
# - Create Raids btrfs partition - #


# :4.a
# -  - #
# - Documentation - #
https://www.cyberciti.biz/faq/linux-btrfs-fstab-entry-to-mount-filesystem-at-startup/


# :4.b
# -  - #
# - Create btrfs Raid1 partition - #
# https://commandmasters.com/commands/mkfs.btrfs-linux/
# https://www.thegeekdiary.com/centos-rhel-how-to-create-and-mount-btrfs-file-system-explained-with-examples/
sudo mkfs.btrfs -f -L DataBtrfs -d single -m raid1 /dev/sdb /dev/sdc;
sudo mkfs.btrfs -f -L DataBtrfs -d --metadata single --data single -m raid1 /dev/sdb /dev/sdc;
sudo mkfs.btrfs -f -L DataBtrfs -d --metadata raid1 --data raid1 /dev/sdb /dev/sdc;


# -  - #
# - List Btrfs devices - #
sudo btrfs device scan;


# :4.c
# -  - #
# - Mount DataBtrfs partition - #
sudo mkdir /DataBtrfs;
sudo mount /dev/sdb /DataBtrfs;
sudo btrfs subvolume create /DataBtrfs/@DataBtrfs;
sudo umount /DataBtrfs;


# :4.d
# -  - #
# - Show Btrfs device info - #
sudo blkid /dev/sdb;
sudo lsblk --fs /dev/sdb;

NAME FSTYPE FSVER LABEL     UUID                                 FSAVAIL FSUSE% MOUNTPOINTS
sdb  btrfs        DataBtrfs 524a63f7-c67a-4f4b-bd6f-9a09eb0986fa   14.6T     0% /DataBtrfs


# :4.e
# -  - #
# - Mount Btrfs filesystem permanently on fstab - #

sudo vim /etc/fstab;

# -  - #
# - Btrfs filesystem - #
UUID=524a63f7-c67a-4f4b-bd6f-9a09eb0986fa  /DataBtrfs   btrfs   ssd,noatime,nodiratime,compress=lzo,space_cache=v2,discard=async,autodefrag,subvol=/@DataBtrfs   0   0
#UUID=524a63f7-c67a-4f4b-bd6f-9a09eb0986fa  /DataBtrfs   btrfs   rw,noatime,nodiratime,compress=lzo,space_cache=v2,discard=async,autodefrag,subvol=/@DataBtrfs    0   0
#UUID=524a63f7-c67a-4f4b-bd6f-9a09eb0986fa  /DataBtrfs   btrfs   rw,noatime,compress=lzo,discard=async,space_cache=v2,subvol=/@DataBtrfs                          0   0


sudo mount -av;
sudo systemctl daemon-reload;

df -H;
mount | more;
mount | grep '/Data';
mount | grep '/DataBtrfs';


# :4.f
# -  - #
# - Show DataBtrfs device info - #
sudo btrfs filesystem df /Data;
sudo btrfs filesystem df /DataBtrfs;

Data, single: total=8.00MiB, used=0.00B
System, RAID1: total=8.00MiB, used=16.00KiB
Metadata, RAID1: total=1.00GiB, used=128.00KiB
GlobalReserve, single: total=5.50MiB, used=0.00B

# -  - #
# - List Btrfs devices - #
sudo btrfs device scan;

┌─[is_derayo]──[ArchBtrfs]:/Data$
└──>> sudo btrfs device scan;
Scanning for Btrfs filesystems
registered: /dev/vda4
registered: /dev/vda3


# :4.g
# -  - #
# - Show Btrfs filesystem UUID - #
sudo btrfs filesystem show /dev/sdb;

Label: 'DataBtrfs'  uuid: 524a63f7-c67a-4f4b-bd6f-9a09eb0986fa
	Total devices 2 FS bytes used 144.00KiB
	devid    1 size 7.28TiB used 1.02GiB path /dev/sdb
	devid    2 size 7.28TiB used 1.01GiB path /dev/sdc

df -H;
mount | more;
mount | grep '/DataBtrfs';

/dev/sdb on /DataBtrfs type btrfs (rw,relatime,space_cache=v2,subvolid=5,subvol=/)


# :4.h
# -  - #
# - Create btrfs DataBtrfs subvolumes - #
sudo btrfs subvolume create /DataBtrfs/Documents;
sudo btrfs subvolume create /DataBtrfs/Documents/Downloads;
sudo btrfs subvolume create /DataBtrfs/Documents/Music;
sudo btrfs subvolume create /DataBtrfs/Documents/Pictures;
sudo btrfs subvolume create /DataBtrfs/Documents/Videos;

sudo btrfs subvolume create /DataBtrfs/Images;
sudo btrfs subvolume create /DataBtrfs/Images/Backup;
sudo btrfs subvolume create /DataBtrfs/Images/Programs;
sudo btrfs subvolume create /DataBtrfs/Images/VManager;

tree /DataBtrfs;

/DataBtrfs
└── Images
    ├── Backup
    ├── Programs
    └── VManager

# -  - #
# - Create btrfs Data subvolumes - #
sudo btrfs subvolume create /Data/Documents;
sudo btrfs subvolume create /Data/Documents/Downloads;
sudo btrfs subvolume create /Data/Documents/Music;
sudo btrfs subvolume create /Data/Documents/Pictures;
sudo btrfs subvolume create /Data/Documents/Videos;

sudo btrfs subvolume create /Data/Images;
sudo btrfs subvolume create /Data/Images/Backup;
sudo btrfs subvolume create /Data/Images/Programs;
sudo btrfs subvolume create /Data/Images/VManager;


# :4.i
# -  - #
# - Change /DataBtrfs ownership - #
sudo chown -R is_derayo:is_derayo /DataBtrfs;
chmod -R 775 /DataBtrfs;


# :4.j
# -  - #
# - Show btrfs subvolumes info - #
sudo btrfs fi du /;
sudo btrfs fi du /DataBtrfs;
sudo btrfs fi df /;
sudo btrfs fi df /DataBtrfs;
sudo btrfs fi df -H /DataBtrfs;
sudo btrfs fi show;

sudo btrfs filesystem df /Data;

┌─[is_derayo]──[ArchBtrfs]:/Data$
└──>> sudo btrfs filesystem df /Data;
Data, single: total=8.00MiB, used=0.00B
System, DUP: total=8.00MiB, used=16.00KiB
Metadata, DUP: total=1.00GiB, used=288.00KiB
GlobalReserve, single: total=5.50MiB, used=0.00B

sudo btrfs subvolume list /;
sudo btrfs subvolume list /Data;
sudo btrfs subvolume list /DataBtrfs;


sudo btrfs fi du /DataBtrfs;

     Total   Exclusive  Set shared  Filename
     0.00B       0.00B           -  /DataBtrfs/Images/Backup
   2.61GiB     2.61GiB           -  /DataBtrfs/Images/Programs/Linux_Rhino.Img/RhinoLinux_2025-1(64bit).iso
   2.61GiB     2.61GiB           -  /DataBtrfs/Images/Programs/Linux_Rhino.Img
   2.61GiB     2.61GiB           -  /DataBtrfs/Images/Programs
     0.00B       0.00B           -  /DataBtrfs/Images/VManager
   2.61GiB     2.61GiB           -  /DataBtrfs/Images
   2.61GiB     2.61GiB       0.00B  /DataBtrfs


sudo btrfs filesystem usage /DataBtrfs;

Overall:
    Device size:		 14.55TiB
    Device allocated:		  5.02GiB
    Device unallocated:		 14.55TiB
    Device missing:		    0.00B
    Device slack:		    0.00B
    Used:			  2.61GiB
    Free (estimated):		 14.55TiB	(min: 7.27TiB)
    Free (statfs, df):		 14.55TiB
    Data ratio:			     1.00
    Metadata ratio:		     2.00
    Global reserve:		  5.50MiB	(used: 0.00B)
    Multiple profiles:		       no

Data,single: Size:3.01GiB, Used:2.61GiB (86.75%)
   /dev/sdb	  1.01GiB
   /dev/sdc	  2.00GiB

Metadata,RAID1: Size:1.00GiB, Used:2.83MiB (0.28%)
   /dev/sdb	  1.00GiB
   /dev/sdc	  1.00GiB

System,RAID1: Size:8.00MiB, Used:16.00KiB (0.20%)
   /dev/sdb	  8.00MiB
   /dev/sdc	  8.00MiB

Unallocated:
   /dev/sdb	  7.28TiB
   /dev/sdc	  7.27TiB


sudo btrfs subvolume list /DataBtrfs;

ID 256 gen 11 top level 5 path Images
ID 257 gen 11 top level 256 path Images/Backup
ID 258 gen 13 top level 256 path Images/Programs
ID 259 gen 11 top level 256 path Images/VManager


sudo btrfs fi df /DataBtrfs;

Data, single: total=3.01GiB, used=2.61GiB
System, RAID1: total=8.00MiB, used=16.00KiB
Metadata, RAID1: total=1.00GiB, used=2.84MiB
GlobalReserve, single: total=5.50MiB, used=0.00B

sudo btrfs fi df -H /DataBtrfs;

Data, single: total=3.23GB, used=2.80GB
System, RAID1: total=8.39MB, used=16.38kB
Metadata, RAID1: total=1.07GB, used=2.98MB
GlobalReserve, single: total=5.77MB, used=0.00B


sudo btrfs fi show;

Label: 'DataBtrfs'  uuid: 524a63f7-c67a-4f4b-bd6f-9a09eb0986fa
	Total devices 2 FS bytes used 2.61GiB
	devid    1 size 7.28TiB used 2.02GiB path /dev/sdb
	devid    2 size 7.28TiB used 3.01GiB path /dev/sdc


# :4.k
# -  - #
# - Btrfs subvolumes cleaning & performance - #

sudo btrfs scrub start /DataBtrfs;
sudo btrfs balance start -musage=50 -dusage=50 /DataBtrfs;
sudo btrfs balance cancel /DataBtrfs;



# :5
# -  - #
# - Delete Btrfs subvolumes - #
sudo btrfs subvolume delete /DataBtrfs/Images/DataBtrfs-snapshot_1;
sudo btrfs subvolume delete /DataBtrfs/Images/Backup;
sudo btrfs subvolume delete /DataBtrfs/Images/Programs;
sudo btrfs subvolume delete /DataBtrfs/Images/VManager;
sudo btrfs subvolume delete /DataBtrfs/Images;





# :90
# -  - #
# - Create btrfs snapshot - #
sudo btrfs subvolume snapshot /DataBtrfs/Images /DataBtrfs/Images/DataBtrfs-snapshot_1;

Create snapshot of '/DataBtrfs/Images' in '/DataBtrfs/Images/DataBtrfs-snapshot_1'


# :91
# -  - #
# - Listing subvolumes - #
sudo btrfs subvolume list /DataBtrfs;

ID 256 gen 14 top level 5 path Images
ID 257 gen 11 top level 256 path Images/Backup
ID 258 gen 13 top level 256 path Images/Programs
ID 259 gen 11 top level 256 path Images/VManager
ID 260 gen 14 top level 256 path Images/DataBtrfs-snapshot_1


# :92
# -  - #
# - Change home subvolume mount for the backup subvolume - #
sudo vim /etc/fstab;

UUID=49348433483434-348438-439 /home    btrfs    subvol=home,compress=zstd:1 0 0
UUID=49348433483434-348438-439 /home    btrfs    subvolid=261,compress=zstd:1 0 0







# :100
# -  - #
# - Btrfs commands - #
sudo btrfs filesystem usage /DataBtrfs;
sudo btrfs subvolume list;
sudo btrfs subvolume list /DataBtrfs;




<<'comment'
# -  - #
# - File name - #
Btrfs-FileSystemConfiguration.sh

cd ~/; rm -rf ~/Btrfs_Arch.sh; vim ~/Btrfs_Arch.sh;
copy & paste this content
chmod +x ~/Btrfs_Arch.sh; ~/./Btrfs_Arch.sh;

cd ~/.ssh;
ssh-keygen -t rsa;

# -  - #
# - Fixing ssh error - #
ssh-keygen -R "you server hostname or ip"
ssh-keygen -R 10.0.0.1
ssh-keygen -R 192.168.1.1;
comment






# - eof - #
