Kontrollera hela filsystemet enkelt.

Kontrollera hela filsystemet enkelt.

Inläggav Osprey » lör aug 26, 2023 8:18 pm

Även om Linux har ett mycket stabilare filsystem (ext4) än Windows (ntfs), så bör men kontrollera det och rätta till det emellanåt.

Jag har gjort ett litet script som fixar det (rättar endast till ext4).
Kod: Markera allt
#! /bin/bash
#
open_escape=""
close_escape=""
#
STARTDIR=$(pwd)
cd /
#
UMOUNTED=false
echo
PARTS=$(alldisks -p -f ext4 --noroot)
for PART in $PARTS; do
   echo "================================================================================"
   echo "PARTITION:  $PART"
   MOUNTED=$(mount | grep $PART | awk -F "on" '{ print $2 }')
   MOUNTPNT=$(mount | grep $PART | awk '{ print $3 }')
   if [[ -z $MOUNTED ]]; then
      echo "-Checking filesystem..."
      echo "e2fsck -f -y -C 0 -D $PART"
      #e2fsck -f -y -C 0 -D $PART
   else
      echo "-Partion $PART is mounted, can not perform e2fsck..."
      echo "-Mountpoint is: $MOUNTPNT"
   fi
   #
   mount $PART /mnt
   echo "-Checking fragmentation..."
   echo "e4defrag -c $PART"
   FRAG=$(e4defrag -c $PART | grep "Fragmentation score" | awk '{ print $3 }')
   if [[ $FRAG -lt 31 ]]; then
      echo "-Partition is not fragmented..."
      echo "-Partition score was: $FRAG  (0-30=no_problems 31-55=low >55=fragmented)"
   elif [[ $FRAG -lt 56 ]]; then
      echo $open_escape"-Partition is a little fragmented - not necessary to defragment..."$close_escape
      echo "-Partition score was: $FRAG  (0-30=no_problems 31-55=low >55=fragmented)"
   else
      echo $open_escape"-Partition is TOO fragmented..."$close_escape
      read -p "_Do you want to defragment it..?? [y/N]: " IN
      if [[ $IN == "y" || $IN == "Y" ]]; then
         e4defrag $PART
      fi
   fi
   umount $PART
   echo
done
#
cd $STARTDIR


För att det ska kunna funka, krävs då även scriptet "alldisks":
Kod: Markera allt
#! /bin/bash
#
function usage {
   cat << EOD

   alldisks [-d|-p] [-b bus] [-f filesystem] [-v]

   Get a list of all disks that meer ceratin criterias.
   
   -d|--disk  -p|--part
      Specifies if disks or partitions are to be shown. If none of them
      is specified, partitions is the default.

   -b|--bus   
      Specifies which kind of disks that are to be shown. Possible values
      are for example "ata" or "usb". For SSD disks the bus shall normally
      be specified as "none". If no bus is specified, all disks are shown.

   -f|filesystem
      Specifies the filesystem a partition must have to be shown. Possible
      values are for example "ntfs", "fat", "ext3" or "ext4". It is also
      possible to specify "ext", which means that "ext2", "ext3" and "ext4"
      is shown but! "extended" is not shown, as it is no filesystem. If not
      specified, all partitions are shown.

   -n|--noroot
      Do not list the root disk or partions on it. This may be useful when
      the system is booted from a USB-pin. This means that the USB-pin is
      excluded from the list. Only real disks or partitions om the computer
      are listed.

   -v|--verb   
      Tells the command to be verbose and print the bus and the filesystem
      for each partition and the bus for each disk. This can for example be
      used to determine how to search for the right disks.

   Example:

      alldisks
      alldisks -p -b usb
      alldisks -p -b usb -f ext4

EOD
}
#
function filesystem {
   if [[ -z $1 ]]; then
      PART=$(mount | grep "$(dirname $(readlink -f $(pwd)) | awk -F / '{ print "/"$2 }') " | awk '{ print $1 }')
   else
      PART=$1
   fi
   FS=$(blkid -o value -s TYPE $PART)
   echo $FS
}
#
function alldisks {
   #echo "====> ROOTDEV=$ROOTDEV"
   pFUNC=$1
   pBUS=$2
   pFS=$3
   pDEV=$4
   if [[ $pFUNC == "disk" ]]; then
      DISKS=$(lsblk --paths --ascii --noheadings | grep -v zram | grep ^/dev | awk '{ print $1 }' | sort)
      for DISK in $DISKS; do
         #######################################
         DISKDEV=$(echo $PART | sed 's/[1-9]$//g')
         if [[ $DISK"p" == $ROOTDEV ]]; then
            if [[ $NOROOT == true ]]; then
               continue
            fi
         fi
         BSIZE=$(blockdev --getsize64 $DISK 2> /dev/null)
         IRET=$?
         if [[ $IRET != 0 ]]; then
            continue
         fi
         #######################################
         if [[ ! -z $(echo $DISK | grep $pDEV) ]]; then
            BUS=$(udevadm info --query=all -n $DISK | grep ID_BUS | awk -F = '{ print $2 }')
            if [[ -z $BUS ]]; then
               BUS="none"
            fi
            FS="disk"
            if [[ $pBUS == "all" ]]; then
               if [[ $pFS == "all" ]]; then
                  if [[ $VERB == false ]]; then
                     echo $DISK
                  else
                     printf "%-12s%4s    %s\n" $DISK $BUS $FS
                  fi
               else
                    if [[ ! -z $(echo $FS | grep $pFS) ]]; then
                     if [[ $VERB == false ]]; then
                        echo $DISK
                     else
                        printf "%-12s%4s    %s\n" $DISK $BUS $FS
                     fi
                  fi
               fi
            else
               if [[ $BUS == $pBUS ]]; then
                  if [[ $pFS == "all" ]]; then
                     if [[ $VERB == false ]]; then
                        echo $DISK
                     else
                        printf "%-12s%4s    %s\n" $DISK $BUS $FS
                     fi
                    elif [[ ! -z $(echo $FS | grep $pFS) ]]; then
                     if [[ $VERB == false ]]; then
                        echo $DISK
                     else
                        printf "%-12s%4s    %s\n" $DISK $BUS $FS
                     fi
                  fi
               fi
            fi
         fi
      done
   elif [[ $pFUNC == "part" ]]; then
      PARTS=$(lsblk --paths --ascii --noheadings | grep -v zram | grep -v ^/dev | awk '{ print $1 }' | awk -F \- '{ print $2 }' | sort)
      for PART in $PARTS; do
         PARTDEV=$(echo $PART | sed 's/[1-9]$//g')
         if [[ $PARTDEV == $ROOTDEV ]]; then
            if [[ $NOROOT == true ]]; then
               continue
            fi
         fi
         if [[ ! -z $(echo $PART | grep $pDEV) ]]; then
            BUS=$(udevadm info --query=all -n $PART | grep ID_BUS | awk -F = '{ print $2 }')
            if [[ -z $BUS ]]; then
               BUS="none"
            fi
            FS=$(filesystem $PART)
            if [[ $pBUS == "all" ]]; then
               if [[ $pFS == "all" ]]; then
                  if [[ $VERB == false ]]; then
                     echo $PART
                  else
                        printf "%-12s%4s    %s\n" $PART $BUS $FS
                  fi
               elif [[ ! -z $(echo $FS | grep $pFS) ]]; then
                  if [[ $pFS != "ext" || $FS != "extended" ]]; then
                     if [[ $VERB == false ]]; then
                        echo $PART
                     else
                        printf "%-12s%4s    %s\n" $PART $BUS $FS
                     fi
                  fi
               fi
            elif [[ $BUS == $pBUS ]]; then
               #echo "BUS:  $BUS"
               #echo "FS:   $FS"
               #echo "P3:   $3"
               if [[ $pFS == "all" ]]; then
                  if [[ $VERB == false ]]; then
                     echo $PART
                  else
                     printf "%-12s%4s    %s\n" $PART $BUS $FS
                  fi
                 elif [[ ! -z $(echo $FS | grep $pFS) ]]; then
                  if [[ $pFS != "ext" && $FS != "extended" ]]; then
                     if [[ $VERB == false ]]; then
                        echo $PART
                     else
                        printf "%-12s%4s    %s\n" $PART $BUS $FS
                     fi
                  fi
               fi
            fi
         fi
      done
   fi
}
#
##########################################################################################################
TEMP=`getopt -ob:df:hnpv --long bus:,disk,fs:,help,noroot,part,verb -n $(basename $0) -- "$@"`
if [[ $? -ne 0 ]]; then
        usage
        exit
fi
eval set -- "$TEMP"
FS="all"
BUS="all"
DISK=false
HELP=false
PART=false
VERB=false
NOROOT=false
while true; do
        case $1 in
                -b|--bus)
                        shift
         BUS=$1
         shift
                        ;;
      -d|--disk)
         DISK=true
         shift
         ;;
      -f|--fs)
         shift
         FS=$1
         shift
         ;;
      -h|--help)
         HELP=true
         shift
         ;;
      -n|--noroot)
         NOROOT=true
         shift
         ;;
      -p|--part)
         PART=true
         shift
         ;;
      -v|--verb)
         VERB=true
         shift
         ;;
      --)
         shift
         break
         ;;
      *)
         shift
         ;;
   esac
done
if [[ -z $1 ]]; then
   DEV="/dev"
else
   DEV=$1
fi
P1=$1
P2=$2
P3=$3
##########################################################################################################
#
ROOTDEV=$(mount | grep "on / " | awk '{ print $1 }' | sed 's/[1-9]$//g')
#
if [[ $HELP == true ]]; then
   usage
   exit
elif [[ $DISK == false && $PART == false ]]; then
   PART=true
elif [[ $DISK == true && $PART == true ]]; then
   echo "-Both --disk and --part is specified, unable to do both"
   echo
   exit
fi
if [[ $DISK == true ]]; then
   alldisks disk $BUS $FS $DEV
else
   alldisks part $BUS $FS $DEV
fi

Scriptet är utformat för att köras från en USB-pinne, där alltså därför "noroot" är med för att man inte vill kolla filstrukturen på själva pinnen....
Senast redigerad av Osprey lör aug 26, 2023 8:27 pm, redigerad totalt 1 gång.
Osprey
Ninja
 
Inlägg: 150
Blev medlem: fre maj 30, 2014 3:33 pm
Ort: Falkenberg

Re: Kontrollera hela filsystemet enkelt.

Inläggav Osprey » lör aug 26, 2023 8:23 pm

Ja, ja några felstavningar och så... men orkar inte fixa nu, så stå ut med dem.... :D
Osprey
Ninja
 
Inlägg: 150
Blev medlem: fre maj 30, 2014 3:33 pm
Ort: Falkenberg


Återgå till Övriga supportfrågor

Vilka är online

Användare som besöker denna kategori: Inga registrerade användare och 1 gäst