#!/bin/bash # # DESCRIPTION: formating partition with a filesystem # if have duplicate partition, it will format the finally filesystem type. # # SCRIPT NAME: format_partition.sh # # Usage: echo "/dev/sda1 ext2" | ./format_partition.sh # # Input: stdin # 1. partition # 2. filesystem type # # Output: # success: # @ format $devname success # Return value: # 1 argument error # 2 deivce node doesn't exist # 3 filesystem no implement # 127 format partition utils not found # # AUTHOR: Qin Bo # # EMAIL: bqin@linx-info.com # # DATE: 2010-08-05 # # HISTORY: # REVISOR DATE MODIFICATION # Qin Bo 2010-08-05 create # # # source ./functions # ex: format_partition /dev/sda1 ext3 format_partition () { if [ $# -ne 2 ];then err "$FUNCNAME function argument error, it must have 2 arguments !" return 1 fi local devname="$1" local fs_type="$2" if [ ! -e "$devname" ];then # wait for device for((i=0;i<10;i++))do [ -e "$devname" ] && break sleep $i done if [ ! -e "$devname" ];then err "$devname node doesn't exist !" return 2 fi fi # partition may be used to swap, or has already mounted if grep -q "^$devname " /proc/swaps ;then swapoff "$devname" elif grep -q "^$devname " /proc/mounts;then reumount $devname 1>>"$DEV_LOG" 2>&1 fi info "formating device "$devname" as "$fs_type" filesystem" case ${fs_type} in ext2) mkfs.ext2 -F -I 128 "$devname" 1>>"$DEV_LOG" 2>&1 ;; ext3) mkfs.ext3 -F -I 128 "$devname" 1>>"$DEV_LOG" 2>&1 ;; ext4) mkfs.ext4 -F -I 128 "$devname" 1>>"$DEV_LOG" 2>&1 ;; reiserfs) yes | mkfs.reiserfs -f "$devname" 1>>"$DEV_LOG" 2>&1 ;; xfs) yes | mkfs.xfs -f "$devname" 1>>"$DEV_LOG" 2>&1 ;; jfs) yes | mkfs.jfs -q "$devname" 1>>"$DEV_LOG" 2>&1 ;; swap|linux-swap) mkswap -f "$devname" 1>>"$DEV_LOG" 2>&1 ;; vfat) mkfs.vfat "$devname" 1>>"$DEV_LOG" 2>&1 ;; *) err ""$fs_type" filesystem no implement yet !" return 3 ;; esac } main () { local line while read line;do if [ -n "$line" ];then # Notice: don't use "$line" format_partition $line erv info "format $(echo $line|awk '{print $1}') success" stdout fi done } main "$@" # vim:set ts=4 sw=4;