Friday, August 3, 2012

KDE pendrive/external/flash format script.

This script is intended to be placed in device notifier, i.e. when you click on a FS (aka 'drive'), this script should be present with the name format.

You can do this via custom actions in the device notifier settings.

As the title suggests, this's a simple format script made using kdialog which offers formatting options in a verity of FS and also checks the contents of the disk before formatting.

The first argument supplied to the disk should be a partition name (like /dev/sdb1 or /dev/sdd1 etc...).


#!/bin/bash
DEVICE="$1"
if [[ $(echo $DEVICE | grep --ignore-case sda) != "" ]]
then
 kdialog --msgbox "I will not format an internal partition. This must be a mistake."
 exit
fi
ch=$(kdialog --menu "Choose a filesystem" "fat" "Compatible with Windows/Linux/Mac/BSD and other devices (fat32)" "ext3" "Compatible with all Linux (faster) (ext3)" "reiser" "Highest performance on small files (Linux only) (reiserfs)" "xfs" "Highest performance on large files (Linux only) (xfs))") || exit
mount_dir="$(mount | grep $DEVICE | cut -d ' ' -f3)"
if [[ $mount_dir != "" ]]
then
 cd "$mount_dir"
 contents="$(ls -a -1)"
 contents="${contents:5}"
 kdialog --yesno "Storage device is mounted in $mount_dir and has the following contents - 
$contents
Unmount and continue formatting?"
 if [[ $? == 0 ]]
 then
  cd /
  kdesudo -c "umount $DEVICE"
  if [[ $? > 0 ]]
  then
   kdialog --yesno "Storage device is in use, force umount and continue formatting?"
   if [[ $? == 0 ]]
   then
    kdesudo -c "umount -l $DEVICE"
   else
    exit
   fi
  fi
 else
  exit
 fi
fi
kdialog --yesno "This'll erase all data. Continue?"
if [[ $? != 0 ]]
then
 exit
fi
kdialog --msgbox "Formating in background... wait till \"Done!\" message occurs.
This may take a few minutes." &
case $ch in
"fat")
 kdesudo -c "mkfs.vfat $DEVICE || kdialog --error 'The device failed to format for some reason.
Re-insert the storage device and retry. If it still does not work, contact commercial support.'"
;;
"ext3")
 kdesudo -c "mkfs.ext3 $DEVICE || kdialog --error 'The device failed to format for some reason.
Re-insert the storage device and retry. If it still does not work, contact commercial support.'"
;;
"reiser")
 kdesudo -c "mkfs.reiserfs -q $DEVICE || kdialog --error 'The device failed to format for some reason.
Re-insert the storage device and retry. If it still does not work, contact commercial support.'"
;;
"xfs")
  kdesudo -c "mkfs.xfs -f $DEVICE || kdialog --error 'The device failed to format for some reason.
Re-insert the storage device and retry. If it still does not work, contact commercial support.'"
;;
esac
kdialog --msgbox "Done!"

No comments:

Post a Comment