#!/usr/bin/env bash ### _ _ _ _ ### __ _ ___ | |_| |__ | | ___| |_ _ _ ### / _` |/ _ \| __| '_ \| |/ _ \ __| | | | ###| (_| | (_) | |_| |_) | | __/ |_| |_| | ### \__, |\___/ \__|_.__/|_|\___|\__|\__,_| ### |___/ ### https://www.youtube.com/user/gotbletu ### https://twitter.com/gotbletu ### https://github.com/gotbletu ### gotbletu@gmail.com ### ### Author : gotbletu ### Name : mountjutsu ### Version : 0.2 ### Date : 20190626 ### Description : menu interface to mount, unmount, eject, clone, exactcopy, format, LUKS encryption ### Depends On : bash sudo grep gawk coreutils udisks2 util-linux ### gptfdisk dosfstools ntfs-3g hfsprogs exfatprogs e2fsprogs ### cryptsetup clonezilla partclone partimage vlc cdw f3 smartmontools ### dvdbackup libdvdread libdvdcss cdrdao cdrtools(or cdrkit) ### Video Demo : https://www.youtube.com/watch?v=jipILuNW5Ks ### 2021-01-16 : change to exfatprogs, exFAT 32KB or 64KB cluster # Next improvements: 1) delete MBR on format https://www.cyberciti.biz/faq/linux-clearing-out-master-boot-record-dd-command/ # clear out leftover label and fstype # $ sudo dd if=/dev/zero of=/dev/sdX bs=446 count=1 # 2) blinking text warning #-------- Bash Color Code {{{ #------------------------------------------------------ # DESC: color code for bash compatible shell # LINK: https://wiki.archlinux.org/index.php?title=Bash/Prompt_customization&oldid=419076#List_of_colors_for_prompt_and_Bash # Reset Color_Off='\e[0m' # Text Reset # Regular Colors Black='\e[0;30m' # Black Red='\e[0;31m' # Red Green='\e[0;32m' # Green Yellow='\e[0;33m' # Yellow Blue='\e[0;34m' # Blue Purple='\e[0;35m' # Purple Cyan='\e[0;36m' # Cyan White='\e[0;37m' # White # }}} #-------- Mount Device (udisksctl) {{{ #------------------------------------------------------ mount-udisksctl() { if [ $# -lt 1 ]; then echo -e "mount device like most GUI file manager" echo -e "\nUsage: $0 " echo -e "Example: $0 sdx" echo -e " $0 sdx1" echo -e "Multiple:$0 sdx sdy sdz" echo -e " $0 sdx*" return 1 fi myArray=( "$@" ) for arg in "${myArray[@]}"; do udisksctl mount -b /dev/"$arg" done } unmount-udisksctl() { if [ $# -lt 1 ]; then echo -e "mount device like most GUI file manager" echo -e "\nUsage: $0 " echo -e "Example: $0 sdx" echo -e " $0 sdx1" echo -e "Multiple:$0 sdx sdy sdz" echo -e " $0 sdx*" return 1 fi myArray=( "$@" ) for arg in "${myArray[@]}"; do udisksctl unmount --force -b /dev/"$arg" done } # }}} #-------- Mount LUKS {{{ #------------------------------------------------------ mount-udisksctl-luks() { if [ $# -lt 1 ] then echo -e "mount LUKS encrypted device like most GUI file manager" echo -e "\nUsage: $0 " echo -e "Example: $0 sdx" echo -e " $0 sdx1" echo -e "Multiple:$0 sdx sdy sdz" echo -e " $0 sdx*" return 1 fi myArray=( "$@" ) for arg in "${myArray[@]}"; do udisksctl unlock -b /dev/"$arg" sleep 0.1 dm_mountpoint=$(lsblk -o "KNAME,NAME" | grep -A 1 "$arg" | tail -1 | awk '{print $1}') udisksctl mount -b /dev/"$dm_mountpoint" done } unmount-udisksctl-luks() { if [ $# -lt 1 ] then echo -e "unmount LUKS encrypted device like most GUI file manager" echo -e "\nUsage: $0 " echo -e "Example: $0 sdx" echo -e " $0 sdx1" echo -e "Multiple:$0 sdx sdy sdz" echo -e " $0 sdx*" return 1 fi myArray=( "$@" ) for arg in "${myArray[@]}"; do dm_mountpoint=$(lsblk -o "KNAME,NAME" | grep -A 1 "$arg" | tail -1 | awk '{print $1}') udisksctl unmount --force -b /dev/"$dm_mountpoint" udisksctl lock -b /dev/"$arg" done } mount-luks() { if [ $# -lt 1 ]; then echo -e "mount LUKS disk encrypted device" echo -e "\nUsage: $0 " echo -e "Example: $0 sdX" echo -e " $0 sdX1" echo -e "Multiple:$0 sdx sdy sdz" echo -e " $0 sdx*" return 1 fi myArray=( "$@" ) for arg in "${myArray[@]}"; do mappername="$arg" sudo cryptsetup luksOpen /dev/"$arg" "$mappername" mkdir -p /tmp/"$arg" sudo mount /dev/mapper/"$mappername" /tmp/"$arg" done } unmount-luks() { if [ $# -lt 1 ]; then echo -e "unmount LUKS disk encrypted device" echo -e "\nUsage: $0 " echo -e "Example: $0 sdX" echo -e " $0 sdX1" echo -e "Multiple:$0 sdx sdy sdz" echo -e " $0 sdx*" return 1 fi myArray=( "$@" ) for arg in "${myArray[@]}"; do mappername="$arg" sudo umount /tmp/"$arg" sudo cryptsetup luksClose "$mappername" rmdir "/tmp/$arg" done } # }}} #-------- Eject Device {{{ #------------------------------------------------------ eject-udisksctl() { if [ $# -lt 1 ] then echo -e "mount using gvfs" echo -e "\nUsage:\n$0 " echo -e "\nExample:\n$0 disc_image.iso" echo -e "$0 disc_image.iso disc_image2.bin disc_image3.mdf" echo -e "$0 *.iso" return 1 fi myArray=( "$@" ) for arg in "${myArray[@]}"; do udisksctl power-off -b /dev/"$arg" done } # }}} #-------- Format USB v4 (MBR 2TB Max) [last updated February 03, 2018] {{{ #------------------------------------------------------ # DEMO: https://www.youtube.com/watch?v=7txO1cdNJsQ # DESC: format USB and create a single partition format2usb-ext() { if [ $# -lt 3 ]; then echo -e "format and create a partition that fills up the whole device" echo -e "\nUsage: $0 " echo -e "Example: $0 ext2 MY_USB sdx" echo -e " $0 ext3 MY_USB sdx" echo -e " $0 ext4 MY_USB sdx" return 1 fi FSTYPE="$1" DEVICE_LABEL="$2" DEVICE_NAME="$3" echo -e "${Yellow}>>>Checking if device is mounted ${Color_Off}" MOUNT_STATUS=$(mount | grep /dev/"$DEVICE_NAME" | wc -l) if [ "$MOUNT_STATUS" -ne 0 ] then lsblk -o "NAME,SIZE,FSTYPE,TYPE,LABEL,MOUNTPOINT,UUID" | grep "$DEVICE_NAME" echo -e "${Red}>>>/dev/$DEVICE_NAME is mounted. You have to unmount the device and all of its partitions then try again ${Color_Off}" return 1 fi echo -e "${Yellow}>>>Please double check the device you are about to FORMAT ${Color_Off}" lsblk -o "NAME,SIZE,FSTYPE,TYPE,LABEL,MOUNTPOINT,UUID" | grep --color -E "$DEVICE_NAME|$" echo -ne "${Red}>>>WARNING: You are about to FORMAT a device at /dev/$DEVICE_NAME. Do you want to continue? [y/n] ${Color_Off}" read REPLY if [[ $REPLY =~ ^[Yy]$ ]] then echo -e "${Green}>>>You chose to continue ${Color_Off}" else return 1 fi echo -e "${Red}>>>Delete any existing partition then create a new single partition ${Color_Off}" echo -e "d\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\no\nn\np\n1\n\n\nw" | sudo fdisk /dev/"$DEVICE_NAME" # delete partiton x8 using d\n\n # d delete a partition # default, partition # o create a new empty DOS partition table # n add a new partition # p print the partition table # 1 partition number 1 # default, start immediately after preceding partition # default, extend partition to end of disk # w write table to disk and exit echo -e "${Red}>>>Formatting the device ${Color_Off}" echo -e "y\n" | sudo mkfs."$FSTYPE" -L "$DEVICE_LABEL" /dev/"$DEVICE_NAME"1 echo -e "${Yellow}>>>Changing permission of the filesystem ${Color_Off}" mkdir -p -v /tmp/testmount sudo mount /dev/"$DEVICE_NAME"1 /tmp/testmount sudo chmod -R 777 /tmp/testmount echo -e "${Green}>>>Change EXT filesystem 5% reserved space to 0% (increase storage space) ${Color_Off}" MOUNTED_TESTMOUNT=$(df | awk '/testmount/ {print $1}') sudo tune2fs -m 0 "$MOUNTED_TESTMOUNT" sudo tune2fs -l "$MOUNTED_TESTMOUNT" | grep --color=auto 'Reserved block count' echo -e "${Red}>>>Unmounting and cleanup ${Color_Off}" sudo umount /tmp/testmount rmdir -v /tmp/testmount } format2usb-fat32-32kbcluster() { if [ $# -lt 2 ]; then echo -e "format device to work with wii & gamecube games using FAT32 with 32KB cluster" echo -e "FAT32 label max is 11 character and is all uppercase" echo -e "(512 bytes per sector * 64 sectors per cluster)/ 1024 Bytes = 32KB clusters (32768 Bytes)" echo -e "more info: https://gist.github.com/joshenders/4376942" echo -e "\nUsage: $0