# - bof - #

#!/bin/bash



<<'comment'
# - Configured - #

DataBtrfs_fstab-Configuration.sh

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


rm -rf ~/.bashrc;
vim ~/.bashrc;

ssh-keygen -R 192.168.1.1

# - On client - #
cd ~/.ssh;
ssh-keygen -t rsa;

cat ~/.ssh/id_rsa.pub;

# - On server - #
vim ~/.ssh/authorized_keys;

sudo apt install curl git inxi net-tools ntp screenfetch neovim -y;
sudo apt install curl git ifupdown2 inxi net-tools ntp screenfetch neovim -y;

comment

clear;



sPartition_='';
sDB_='DataBtrfs';


# -  - #
# - rootLabel_ - #
function rootLabel_(){
  local sS1_='';
  echo "";
  sS1_=$(echo $(lsblk -o name,mountpoint,label | grep " "/home" ") | awk '{print $3;}');
  if [ "$sS1_" != "Linux" ]; then
     echo "Creating Linux partition label on root...";
     sudo btrfs filesystem label / Linux;
     echo "Linux partition label on root created...";
     sleep 10;
  else
    echo "Linux partition label already exists...";
    sleep 5;
  fi;
  #echo "";
}



# -  - #
# - efiLabel_ - #
function efiLabel_(){
  local sS1_='' sS2_='' \
        sL_='EFI_BOOT';
  echo "";
  sS1_=$(echo $(lsblk -o name,mountpoint,label | grep /efi) | awk '{print $3;}');
  sS2_=$(echo $(lsblk -o name,mountpoint,label | grep /efi) | awk '{print $1;}');
  sS2_=/dev/$(echo ${sS2_:2:4});
  if [ "$sS1_" != "$sL_" ]; then
     echo "Creating efi partition label on /boot/efi...";
     sudo fatlabel $sS2_ $sL_;
     echo "Linux partition label on /boot/efi created succssesfully...";
     sleep 10;
  else
    echo "$sL_ partition label already exists...";
    sleep 5;
  fi;
}



# -  - #
# - creatingDataBtrfsPartition_ - #
function creatingDataBtrfsPartition_(){
  local sS1_='' \
        sMsg0_=$sDB_" partition already created...";
  echo "";
  sS1_=$(echo $(lsblk -f | grep " "$sDB_" ") | awk '{print $2;}');
  if [ "$sS1_" == "btrfs" ]; then echo $sMsg0_; sleep 5; return; fi;

  echo "Creating "$sDB_" partition...";
  sPartition_=$(DevSda_ "Data");

  sudo mkfs.btrfs -f -L $sDB_ -m single $sPartition_;
  sudo partprobe $sPartition_;
  echo $sDB_" partition created successfully...";
  sleep 10;  
  echo "";
  lsblk -f;
  echo "";
  sleep 10;
}



# -  - #
# - Return partition - #
function DevSda_(){
  local sD1_=$1 \
        sDevSda_='' \
        sS1_='';
  sS1_=$(echo $(lsblk -f | grep " "$sD1_" ") | awk '{print $1;}');
  sDevSda_=/dev/$(echo ${sS1_:2:4});                                  # /dev/sda   /dev/dva
  echo $sDevSda_;
}

<<'comment'
function Partition_(){
  local sD1_=$1;
  if [[ -d "/$sD1_" ]]; then
    sS1_=$(echo $(sudo btrfs subvolume list /$sDB_ | grep @$sD1_) | awk '{print $9;}'); fi;
  if [[ $sS1_ == "@DataBtrfs" ]]; then echo $sD1_; else echo "Data"; fi;
}
comment



# -  - #
# - createDataBtrfsSubvolume_ - #
function createDataBtrfsSubvolume_(){
  local sS1_=''\
        sMsg0_=$sDB_" subvolume already created...";
  echo "";

  sPartition_=$(DevSda_ $sDB_);

  sVsDB_="@"$sDB_
  sS1_=$(echo $(sudo btrfs subvolume list /$sDB_ | grep $sVsDB_) | awk '{print $9;}' &>/dev/null && echo "1");
  if [ "$sS1_" == "1" ]; then
     sS1_=$(echo $(sudo btrfs subvolume list /$sDB_ | grep $sVsDB_) | awk '{print $9;}'); fi;
  if [ "$sS1_" == "$sVsDB_" ]; then echo $sMsg0_; sleep 5; return; fi;

  echo "Creating "$sDB_" subvolume...";
  sudo mkdir -p /$sDB_;
  sudo mount $sPartition_ /$sDB_;
  sudo btrfs subvolume create /$sDB_/@$sDB_;
  sudo umount /$sDB_;
  echo $sDB_" subvolume created successfully...";
  echo "";
  sleep 20;
}



# -  - #
# - mountingDataBtrfsFstab_ - #
function mountingDataBtrfsFstab_(){
  local sS1_='' sS2_='' \
        sMsg0_=$sDB_" subvolume already mounted on fstab..." \
        tfstabF_=~/Documents/fstab.tmp \
        fstabF_=/etc/fstab;
  echo "";


<<'comment'
  sudo mkfs.ext4 /dev/sda3;
  sudo e2label /dev/sda3 Data;
comment


  sS1_=$(echo $(cat $fstabF_ | grep $sDB_) | awk '{print $2;}');
  if [ "$sS1_" == /$sDB_ ]; then echo $sMsg0_; sleep 5; return; fi;

  echo "Mounting $sDB_ subvolume into fstab...";

  mkdir -p ~/Documents;
  sudo mkdir -p /$sDB_;
  rm -rf $tfstabF_;
  cat $fstabF_ >> $tfstabF_;

  sS2_=$(echo $(lsblk -f | grep "$sDB_") | awk '{print $4;}');

  echo "";
  echo "UUID = "$sS2_;
  echo "";
  sleep 10;

  echo "UUID="$sS2_"   /"$sDB_"  btrfs   ssd,noatime,nodiratime,compress=lzo,space_cache=v2,discard=async,autodefrag,subvol=/@"$sDB_"  0  0" >> $tfstabF_;

  sudo mv $tfstabF_ $fstabF_;
  rm -rf $tfstabF_;
  echo $sDB_" subvolume mounted successfully into fstab file...";
  echo "";
  sudo mount -av;
  sudo systemctl daemon-reload;
  echo "";
  sudo btrfs filesystem df /$sDB_;
  echo "";
  sleep 10;
}



# -  - #
# - createDocumentsSubvolumes_ - #
function createDocumentsSubvolumes_(){
  local sS1_="" \
        sD_=/$sDB_ \
        sDc_=/Documents \
        sDl_=/Downloads \
        sM_=/Music \
        sP_=/Pictures \
        sV_=/Videos \
        sI_=/Images \
        sB_=/Backup \
        sPr_=/Programs \
        sVM_=/VManager;
  echo "";
  echo "Creating "$sDB_" subvolumes...";



  sS1_=$(sudo btrfs subvolume show $sD_$sDc_ &>/dev/null && echo "1");
  if [ "$sS1_" == "" ]; then sudo btrfs subvolume create $sD_$sDc_; fi;

  sS1_=$(sudo btrfs subvolume show $sD_$sDc_$sDl_ &>/dev/null && echo "1");
  if [ "$sS1_" == "" ]; then sudo btrfs subvolume create $sD_$sDc_$sDl_; fi;

  sS1_=$(sudo btrfs subvolume show $sD_$sDc_$sM_ &>/dev/null && echo "1");
  if [ "$sS1_" == "" ]; then sudo btrfs subvolume create $sD_$sDc_$sM_; fi;

  sS1_=$(sudo btrfs subvolume show $sD_$sDc_$sP_ &>/dev/null && echo "1");
  if [ "$sS1_" == "" ]; then sudo btrfs subvolume create $sD_$sDc_$sP_; fi;

  sS1_=$(sudo btrfs subvolume show $sD_$sDc_$sV_ &>/dev/null && echo "1");
  if [ "$sS1_" == "" ]; then sudo btrfs subvolume create $sD_$sDc_$sV_; fi;



  sS1_=$(sudo btrfs subvolume show $sD_$sI_ &>/dev/null && echo "1");
  if [ "$sS1_" == "" ]; then sudo btrfs subvolume create $sD_$sI_; fi;

  sS1_=$(sudo btrfs subvolume show $sD_$sI_$sB_ &>/dev/null && echo "1");
  if [ "$sS1_" == "" ]; then sudo btrfs subvolume create $sD_$sI_$sB_; fi;

  sS1_=$(sudo btrfs subvolume show $sD_$sI_$sPr_ &>/dev/null && echo "1");
  if [ "$sS1_" == "" ]; then sudo btrfs subvolume create $sD_$sI_$sPr_; fi;

  sS1_=$(sudo btrfs subvolume show $sD_$sI_$sVM_ &>/dev/null && echo "1");
  if [ "$sS1_" == "" ]; then sudo btrfs subvolume create $sD_$sI_$sVM_; fi;



  sudo chown -R $USER:$USER $sD_;
  chmod -R 775 $sD_;

  echo "";
  tree $sD_;
  echo "";
  echo "";
  echo $sDB_" subvolumes created successfully...";
  echo "";
  sudo btrfs subvolume list /$sDB_;
  #echo "";
  sleep 10;
}



# -  - #
# - displayPartitionProperties_ - #
function displayPartitionProperties_(){
  echo "";
  echo "";
  echo "lsblk --fs shown as right now...";
  echo "";
  sudo lsblk --fs $sPartition_;
  sleep 5;

  echo "";
  echo "";
  echo "sudo btrfs filesystem df /"$sDB_;
  echo "";
  sudo btrfs filesystem df /$sDB_;
  sleep 5;

  echo "";
  echo "";
  echo "sudo btrfs filesystem show /"$sDB_;
  echo "";
  sudo btrfs filesystem show /$sDB_;

  echo "";
  sleep 5;
}



<<'comment'
  # - Btrfs commands - #
  sudo pacman -S btrfs-progs --noconfirm --needed;


  sudo btrfs subvolume list /DataBtrfs;
  sudo btrfs filesystem df /DataBtrfs;
  sudo btrfs device scan;
  sudo mkdir /DataBtrfs;
  
  sudo mount /dev/sdb /dev/sdc /DataBtrfs;
  
  sudo btrfs subvolume create /DataBtrfs/@DataBtrfs;

  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;



  sudo btrfs subvolume create /Data;

  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;



  sudo umount /DataBtrfs;
  sudo umount /Data;
  sudo blkid /dev/sdb;
  sudo lsblk --fs /dev/sdb;

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

  sudo btrfs filesystem show;
  sudo btrfs filesystem show /;
  sudo btrfs filesystem show /Data;
  sudo btrfs filesystem show /DataBtrfs;
  sudo btrfs filesystem show /dev/sdb;

  sudo btrfs filesystem du /;
  sudo btrfs filesystem du /Data;
  sudo btrfs filesystem du /DataBtrfs;
  sudo btrfs filesystem du /dev/sda2;

  sudo btrfs filesystem df /;
  sudo btrfs filesystem df /Data;
  sudo btrfs filesystem df -H /Data;
  sudo btrfs filesystem df /DataBtrfs;
  sudo btrfs filesystem df -H /DataBtrfs;
  sudo btrfs filesystem df /dev/sda2;

  sudo btrfs filesystem show /dev/sda2;
  sudo btrfs fi show;

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

  sudo btrfs filesystem usage /;
  sudo btrfs filesystem usage /Data;
  sudo btrfs filesystem usage /DataBtrfs;

comment



rootLabel_;
efiLabel_;
creatingDataBtrfsPartition_;
createDataBtrfsSubvolume_;
mountingDataBtrfsFstab_;
createDocumentsSubvolumes_;
displayPartitionProperties_;


rm -rf ~/DataBtrfs.sh;



<<'comment'
# -  - #
# - File name - #
DataBtrfs_fstab-Configuration.sh

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



# - eof - #
