summaryrefslogtreecommitdiff
path: root/ar/.local/bin/partlabel
blob: 643d1908597d78232175ea69bfe1bf988b9b7c21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/sh

command_exists() { command -v "$1" >/dev/null 2>&1; }
get_fs_type() { sudo blkid -o value -s TYPE "$1" 2>/dev/null; }
label_ext() { sudo e2label "$1" "$2"; }
label_fat() { sudo fatlabel "$1" "$2"; }
label_ntfs() { sudo ntfslabel --force --quiet "$1" "$2"; }
label_btrfs() { sudo btrfs filesystem label "$1" "$2"; }
label_luks() { sudo cryptsetup config "$1" --label "$2"; }
label_parted() { sudo parted "$1" name "$2" "$3"; }
print_label() { sudo lsblk -o NAME,LABEL; }

getfs() {
  print_label
  echo -n "\nEnter the partition (e.g., /dev/sda1): "
  read partition

  fs_type=$(get_fs_type "$partition")

  if [ -z "$fs_type" ]; then
    echo "Unable to determine file system type for $partition. Please ensure the partition exists and has a valid file system."
    echo "Debugging information:"
    echo "blkid output for $partition:"
    sudo blkid "$partition"
    echo "Raw blkid output:"
    sudo blkid
    exit 1
  fi

  echo "Detected file system type: $fs_type"
  echo -n "Enter the label: "
  read label
}

echo "Partition Labeling Script"
for cmd in blkid e2label fatlabel ntfslabel btrfs parted lsblk cryptsetup; do
  if ! command_exists "$cmd"; then
    echo "Error: $cmd is not installed or not in PATH."
    echo "Installing necessary package for $cmd..."
    case $cmd in
    blkid | lsblk)
      sudo pacman -S --noconfirm util-linux
      ;;
    e2label)
      sudo pacman -S --noconfirm e2fsprogs
      ;;
    fatlabel)
      sudo pacman -S --noconfirm dosfstools
      ;;
    ntfslabel)
      sudo pacman -S --noconfirm ntfs-3g
      ;;
    btrfs)
      sudo pacman -S --noconfirm btrfs-progs
      ;;
    parted)
      sudo pacman -S --noconfirm parted
      ;;
    cryptsetup)
      sudo pacman -S --noconfirm cryptsetup
      ;;
    *)
      echo "Unknown command: $cmd"
      exit 1
      ;;
    esac
  fi
done

getfs

case "$fs_type" in
ext2 | ext3 | ext4) label_ext "$partition" "$label" ;;
vfat) label_fat "$partition" "$label" ;;
ntfs) label_ntfs "$partition" "$label" ;;
btrfs) label_btrfs "$partition" "$label" ;;
crypto_LUKS)
  echo "Labeling LUKS partition using cryptsetup."
  label_luks "$partition" "$label"
  getfs
  ;;
*)
  echo "File system type $fs_type is not directly supported by this script. Attempting to label with parted."
  label_parted "$partition" "$label"
  ;;
esac

echo "Partition labeled successfully. Verifying..."
print_label
echo "Done."