一、功能说明

1 根据自定义的配置文件,对指定硬盘做分区,一键安装,不需要交互
2 支持 Swap,Primary,Extend,Logical 分区格式
3 可以自动计算大小
4 支持LVM分区,并自动创建
5 自动分区顺序是:Swap,Primary,Extend,Logical 
 
遗留的问题 : 
1 目前暂时没有考虑对大于2T磁盘的分区(那个需要使用GPT分区格式)
2 另外在分区时候 可能实际分区大小跟 配置文件指定的有微小出入
 
 
二、分区配置文件格式说明:
举例:
nova_volumes sdb msdos 200 primary 1 0
 
nova_volumes sdb msdos 400 primary 2 0
 
- sdb msdos 300 extended 3 0
nova_volumes sdb msdos 100 logical 4 3
nova_volumes sdb msdos 100 logical 5 3
 
第一列 : LVM 卷名 ,如不需要用 “-”替代
第二列  :   所用的物理磁盘名 , sda/sdb/sdc 等
第三列 : 磁盘分区表格式,支持 (msdos,小于2T用; gpt 大于2T用) 
第四列  :  分区大小,以M作为单位
第五列  :  分区类型(主分区/扩展分区/逻辑分区  并且一个物理磁盘下只允许有一个扩展分区)
第六列  :  配置ID,整型数字,不能为空,更不能重复
第七列  :  配置父ID号,整型数字。ID为0时,表示没有父ID号;如果不为0,则也不允许重复.
 
第5,第6列的配置,做个关系型数据开发的都应明白,是用做标示配置行间的父子关系
不过在此脚本中,不支持多层父子关系
 
上例的配置中的解释:
第一行: 配置 /dev/sdb第一个 分区,类型是 主分区 200M,所属的LVM卷名是 : nova_volumes
第二行: 配置 /dev/sdb 第二个 分区,类型是 主分区 400M,所属的LVM卷名是 : nova_volumes
 
第三行: 配置 /dev/sdb 第三个 分区,类型是 扩展分区 300M
第四行 :   配置 /dev/sdb 第四个 分区,类型是 逻辑分区 100M,它属于 第三行的扩展分区300M的一部分,并且它所属的LVM卷名是 : nova_volumes
第五行 :   配置 /dev/sdb 第四个 分区,类型是 逻辑分区 100M,它属于 第三行的扩展分区300M的一部分,并且它所属的LVM卷名是 : nova_volumes
 
 
三、实现代码
 
 
 
     
  1. #!/bin/bash 
  2. cfglist="disk_custsize_partition.list"
  3. cfglist_vaild="disk_custsize_partition_vaild.list"
  4. cfglist_vaild_for_disk_tmp="disk_custsize_partition_vaild_for_disk_tmp.list"
  5. cfglist_vaild_for_disk_logical_tmp="disk_custsize_partition_vaild_for_disk_logical_tmp.list"
  6.   
  7. fdisk_cmd_part="parted"
  8. fdisk_cmd_fdk="fdisk"
  9.   
  10. partition_array=("primary" "extended" "logical"); 
  11. partition_main_max=4
  12.   
  13. dev_array=(); 
  14. length=${#dev_array[@]}; 
  15.   
  16. for i in `ls /dev/sd* |grep -vE "[0-9]"` 
  17. do 
  18.  dev_basename=`basename $i`; 
  19.  dev_dirname=`dirname $i`; 
  20.  #echo $dev_basename" "$dev_dirname; 
  21.  dev_array[$length]=$dev_basename; 
  22.  _length_=$(( length++ )); 
  23.  #echo $length":" 
  24. done 
  25. #echo "OOOK,array()"; 
  26. #echo ${dev_array[@]}; 
  27.   
  28. if [[ -f $cfglist ]] 
  29. then 
  30.    > $cfglist_vaild; 
  31.    cat $cfglist|grep -v "#"|while read line 
  32.    do 
  33.       cfg_array=($line); 
  34.       sub_disk=${cfg_array[1]}; 
  35.       if [[ -e "/dev/$sub_disk" ]] 
  36.       then 
  37.          #echo "/dev/"$sub_disk; 
  38.          echo $line >> $cfglist_vaild; 
  39.       fi 
  40.    done 
  41. fi 
  42.   
  43. fdisk_test=$( which $fdisk_cmd_fdk ); 
  44. parted_test=$( which $fdisk_cmd_part ); 
  45.   
  46. if [[ -n $fdisk_test ]] && [[ -n $parted_test ]] 
  47. then 
  48.    #echo " fdisk parted OK"; 
  49.    for i in ${dev_array[@]} 
  50.    do 
  51.        echo "initial partition : "$i; 
  52.        hd_sysdir="/dev"
  53.        hd_dev="$i"
  54.        hd_allinbyte=`$fdisk_cmd_fdk -l $hd_sysdir/$hd_dev |grep -E "^Disk $hd_sysdir/$hd_dev"|awk '{print $5}'`; 
  55.        hd_allinMbyte=$(( $hd_allinbyte/1000/1000)); 
  56.         
  57.        hd_allinMbyte_Used_Str=$($fdisk_cmd_part $hd_sysdir/$hd_dev print|grep -P '^[[:space:]][1-9]+'|awk '{print $3}'|tail -n 1); 
  58.        if [[ -n $hd_allinMbyte_Used_Str ]] ; 
  59.        then  
  60.           hd_allinMbyte_Used_Array=( $( echo $hd_allinMbyte_Used_Str |sed "s/\([0-9]\+\)\(B\|MB\|GB\|TB\|PB\)/\1 \2/" ) ); 
  61.           #echo "hd_allinMbyte_Used :"${hd_allinMbyte_Used_Array[0]}" :"${hd_allinMbyte_Used_Array[1]}; 
  62.           hd_allinMbyte_Used_Array[1]=$( echo ${hd_allinMbyte_Used_Array[1]}|tr '[a-z]' '[A-Z]' ); 
  63.            
  64.           case "${hd_allinMbyte_Used_Array[1]}" in 
  65.           MB|mb) 
  66.           hd_allinMbyte_Used=${hd_allinMbyte_Used_Array[0]}; 
  67.           ;; 
  68.           GB|gb) 
  69.           hd_allinMbyte_Used=$(( ${hd_allinMbyte_Used_Array[0]}*1000 )); 
  70.           ;; 
  71.           TB|tb) 
  72.           hd_allinMbyte_Used=$(( ${hd_allinMbyte_Used_Array[0]}*1000*1000 )); 
  73.           ;; 
  74.           PB|pb) 
  75.           hd_allinMbyte_Used=$(( ${hd_allinMbyte_Used_Array[0]}*1000*1000*1000 )); 
  76.           ;; 
  77.           *) 
  78.           hd_allinMbyte_Used=$(( ${hd_allinMbyte_Used_Array[0]}/1000 )); 
  79.           ;; 
  80.           esac 
  81.            
  82.        else 
  83.           hd_allinMbyte_Used=0
  84.           #echo "hd_allinMbyte_Used_Array : 0 MB"}; 
  85.        fi 
  86.        echo "hd_allinMbyte_Used_Array:"${hd_allinMbyte_Used_Array[0]}; 
  87.        hd_allinMbyte_Used=$( printf "%.f" $hd_allinMbyte_Used ); 
  88.        hd_allinMbyte_Vaild=$(( $hd_allinMbyte - $hd_allinMbyte_Used )); 
  89.         
  90.        echo "$hd_allinbyte : $hd_allinMbyte $hd_allinMbyte_Used ($hd_allinMbyte_Vaild)"; 
  91.        #continue; 
  92.         
  93.        #disk label 
  94.        hd_lable=`$fdisk_cmd_part -s $hd_sysdir/$hd_dev print|grep -E "^Partition Table:"|awk '{print $NF}'`; 
  95.        if [[ -z $hd_lable ]]; 
  96.        then 
  97.           hd_allinGigabytes=$(( $hd_allinbyte/1000/1000/1000 )); 
  98.             if [[ $hd_allinGigabytes -gt 2048 ]]; 
  99.                   then 
  100.                      hd_lable_type="gpt"
  101.                   else 
  102.                      hd_lable_type="msdos"
  103.             fi 
  104.           mklabel_cmd="$fdisk_cmd_part -s $hd_sysdir/$hd_dev mklabel $hd_lable_type"
  105.                   echo ">>> mklabel_cmd :"$mklabel_cmd; 
  106.                   eval "$mklabel_cmd"; 
  107.        fi     
  108.        #continue; 
  109.   
  110.        for k in ${partition_array[@]}; 
  111.        do 
  112.           echo "partition type:"$k; 
  113.           if [[ ! ($k = "logical") ]]; 
  114.           then 
  115.              #primary partition process 
  116.              partition_main_used=`$fdisk_cmd_part $hd_sysdir/$hd_dev print|grep -P '^[[:space:]][1-9]+'|awk 'BEGIN{t=0};{if($5~"primary|extended"){t++}};END{print t}'`; 
  117.              if [[ $partition_main_used -ge $partition_main_max ]] 
  118.              then 
  119.                 echo "The number of Partition: primary/extend is reach the max:$partition_main_max"; 
  120.                 continue; 
  121.              fi 
  122.               
  123.              #partition start 
  124.              > $cfglist_vaild_for_disk_tmp; 
  125.              case "$k" in 
  126.              primary) 
  127.                 #echo "primary cfglist_vaild "; 
  128.                 cat $cfglist_vaild |grep -E "[[:space:]]$hd_dev[[:space:]]"|awk '{if($5~"'$k'"){print $0}}' > $cfglist_vaild_for_disk_tmp; 
  129.              ;; 
  130.              extended) 
  131.                 #echo "extended cfglist_vaild "; 
  132.                 cfg_seq_num=`cat $cfglist_vaild |grep -E "[[:space:]]$hd_dev[[:space:]]"|awk '$5 =="'$k'" {print $6}'|head -n 1`; 
  133.                     cat $cfglist_vaild |grep -E "[[:space:]]$hd_dev[[:space:]]"|awk '$5 =="'$k'" || $7 =="'$cfg_seq_num'" {print $0}' > $cfglist_vaild_for_disk_tmp; 
  134.              ;; 
  135.              esac 
  136.               
  137.                    while read line; 
  138.                    do 
  139.                        #echo $line;continue; 
  140.                        sub_dev=($line); 
  141.                        echo ">>> Subdev: " ${sub_dev[@]}; 
  142.                        hd_build_scount_req=${sub_dev[3]}; 
  143.                        hd_partition_type_req=${sub_dev[4]}; 
  144.                        hd_partition_seq=${sub_dev[5]}; 
  145.                        hd_partition_seq_parent=${sub_dev[6]}; 
  146.              
  147.                        if [[ ($hd_partition_type_req = "primary" || $hd_partition_type_req = "extended") && ($hd_allinMbyte_Vaild -ge $hd_build_scount_req) ]]; 
  148.                        then  
  149.                           echo ">>> disk count : $hd_allinMbyte  $hd_allinMbyte_Used $hd_allinMbyte_Vaild $hd_build_scount_req"; 
  150.                           if [[ $hd_allinMbyte_Used -eq 0 ]]; 
  151.                           then 
  152.                              hd_build_start=1
  153.                           else 
  154.                              hd_build_start=$hd_allinMbyte_Used; 
  155.                           fi 
  156.                      
  157.                                 hd_build_end=$(( hd_build_start + hd_build_scount_req )); 
  158.                                  
  159.                                 hd_build_partition_type=""
  160.                                 case  "$k" in 
  161.                                 primary) 
  162.                                 hd_build_partition_type="ext4"
  163.                                 ;; 
  164.                                 extended) 
  165.                                 hd_build_partition_type=""
  166.                                 ;; 
  167.                                 esac                                 
  168.                            
  169.                           #start partition create 
  170.                                   parted_cmd="$parted_test -s $hd_sysdir/$hd_dev mkpart $hd_partition_type_req $hd_build_partition_type $hd_build_start $hd_build_end"
  171.                                   echo "parted cmd: $parted_cmd"; 
  172.                                   eval "$parted_cmd"; 
  173.                                   parted_cmd_res=$?; 
  174.                                    
  175.                                   #wile loop,variables reset 
  176.                           #hd_allinMbyte_Used=$hd_build_end; 
  177.                           #echo "DDDD hd_build_end >> "$hd_build_end; 
  178.                           #hd_allinMbyte_Vaild=$(( hd_allinMbyte - hd_allinMbyte_Used )); 
  179.                            
  180.                           if [[ $k = "extended" ]]; 
  181.                           then 
  182.                           #exntended/logical partition process 
  183.                           hd_ext_all=$hd_build_scount_req; 
  184.                           hd_ext_all_start=$(( hd_build_start + 1 )); 
  185.                           hd_ext_all_end=$hd_build_end; 
  186.                           hd_ext_allinMbyte_Vaild=$hd_ext_all; 
  187.                            
  188.                           echo "exntended/logical partition process!"$hd_ext_all" : "$hd_ext_all_start" : "$hd_ext_all_end; 
  189.                           > $cfglist_vaild_for_disk_logical_tmp 
  190.                           cat $cfglist_vaild |grep -E "[[:space:]]$hd_dev[[:space:]]"|awk '{if($5=="logical" && $7=="'$hd_partition_seq'"){print $0}}' > $cfglist_vaild_for_disk_logical_tmp; 
  191.                           hd_build_logicalline_seq=0
  192.                           while read logicalline; 
  193.                           do 
  194.                              #echo $logicalline; 
  195.                              cfg_ext_array=($logicalline); 
  196.                              hd_build_ext_req=${cfg_ext_array[3]}; 
  197.                              #echo ">>> $hd_ext_allinMbyte_Vaild -ge $hd_build_ext_req"; 
  198.                               
  199.                              if [[ $hd_ext_allinMbyte_Vaild -ge $hd_build_ext_req ]]; 
  200.                              then 
  201.                                 if [[ hd_build_logicalline_seq -eq 0 ]]; 
  202.                                 then 
  203.                                    hd_build_ext_start=$hd_ext_all_start; 
  204.                                 fi 
  205.                                  
  206.                                 hd_build_ext_end=$(( hd_build_ext_start + hd_build_ext_req -1 )); 
  207.                                  
  208.                                 #start partition (extend) create 
  209.                                 parted_ext_cmd="$parted_test -s $hd_sysdir/$hd_dev mkpart logical ext4 $hd_build_ext_start $hd_build_ext_end"
  210.                                 echo "partedext cmd: $parted_ext_cmd"; 
  211.                                 eval "$parted_ext_cmd"; 
  212.                                 sleep 1; 
  213.                                 if [[ $? -eq 0 ]]; 
  214.                                 then 
  215.                                    hd_ext_allinMbyte_Vaild=$(( hd_ext_allinMbyte_Vaild - hd_build_ext_req )); 
  216.                                    hd_build_ext_start=$(( hd_build_ext_end+1 )); 
  217.                                    hd_build_ext_end=0
  218.                                     
  219.                                    #lvm create 
  220.                                    partition_create_seq=`$fdisk_cmd_fdk -l $hd_sysdir/$hd_dev|grep -E "^$hd_sysdir/$hd_dev" |cut -d " " -f 1|sed -e "s/^.*$hd_dev//"|tail -n 1`; 
  221.                                    vg_obg_id="${cfg_ext_array[0]}"
  222.                                    if [[ $vg_obg_id != '-' ]]; 
  223.                                    then 
  224.                                                    eval "vg_obg_id_length=$(echo '${#'$vg_obg_id'[@]}')"; 
  225.                                                    echo "LV-Vg Create: $vg_obg_id"; 
  226.                                                    eval "(pvdisplay -s $hd_sysdir/$hd_dev 2>&1) > /dev/null"; 
  227.                                                          if [[ $? -ne 0 ]]; 
  228.                                                          then 
  229.                                                             vg_cmd="pvcreate -f $hd_sysdir/$hd_dev"$partition_create_seq;     
  230.                                                             echo "Vgcreate cmd: $vg_cmd"; 
  231.                                                             eval "$vg_cmd"; 
  232.                                                             #put pgid in the vg arrays 
  233.                                                             eval "${vg_obg_id}[$vg_obg_id_length]='$hd_sysdir/$hd_dev$partition_create_seq'"; 
  234.                                                         echo "$vg_obg_id_length: '"$hd_sysdir"/"$hd_dev$partition_create_seq"'"; 
  235.                                                             vg_obg_id_length=$(( vg_obg_id_length+1  )); 
  236.                                              
  237.                                                          fi 
  238.                                                        fi 
  239.                                 fi 
  240.                              fi 
  241.                               
  242.                              hd_build_logicalline_seq=$(( hd_build_logicalline_seq + 1 )); 
  243.                               
  244.                           done < $cfglist_vaild_for_disk_logical_tmp; 
  245.                           > $cfglist_vaild_for_disk_logical_tmp; 
  246.                            
  247.                           fi 
  248.                                   #end if [[ $k = "extended" ]] 
  249.                                    
  250.                                   #wile loop,variables reset 
  251.                           hd_allinMbyte_Used=$hd_build_end; 
  252.                           #echo "DDDD hd_build_end >> "$hd_build_end; 
  253.                           hd_allinMbyte_Vaild=$(( hd_allinMbyte - hd_allinMbyte_Used )); 
  254.                            
  255.                                   #continue; 
  256.                                   #Lvm: pvcreate 
  257.                                   #echo "LVM: parted_cmd_res:"${sub_dev[0]}; 
  258.                                   if [[ $parted_cmd_res -eq 0 ]]; 
  259.                                   then 
  260.                                     partition_create_seq=`$fdisk_cmd_fdk -l $hd_sysdir/$hd_dev|grep -E "^$hd_sysdir/$hd_dev" |cut -d " " -f 1|sed -e "s/^.*$hd_dev//"|tail -n 1`; 
  261.                                     if [[ ${sub_dev[0]} != '-' ]]; 
  262.                                     then 
  263.                                        vg_obg_id="${sub_dev[0]}"
  264.                                        eval "vg_obg_id_length=$(echo '${#'$vg_obg_id'[@]}')"; 
  265.                                        echo "LV-Vg Create: $vg_obg_id"; 
  266.                                        eval "(pvdisplay -s $hd_sysdir/$hd_dev 2>&1) > /dev/null"; 
  267.                                             if [[ $? -ne 0 ]]; 
  268.                                             then 
  269.                                                vg_cmd="pvcreate -f $hd_sysdir/$hd_dev"$partition_create_seq;     
  270.                                                echo "Vgcreate cmd: $vg_cmd"; 
  271.                                                eval "$vg_cmd"; 
  272.                                                #put pgid in the vg arrays 
  273.                                                eval "${vg_obg_id}[$vg_obg_id_length]='$hd_sysdir/$hd_dev$partition_create_seq'"; 
  274.                                            echo "$vg_obg_id_length: '"$hd_sysdir"/"$hd_dev$partition_create_seq"'"; 
  275.                                                vg_obg_id_length=$(( vg_obg_id_length+1  )); 
  276.                                  
  277.                                             fi 
  278.                                 fi 
  279.                             fi 
  280.                           #end if [[ $? -eq 0 ]] 
  281.                            
  282.                          fi 
  283.                          #end if [[ $hd_allinMbyte_Vaild -ge $hd_build_scount_req ]] 
  284.                           
  285.                    done < $cfglist_vaild_for_disk_tmp; 
  286.            
  287.           fi 
  288.           #end if [[ ! ($k = "primary" ]] 
  289.            
  290.        done 
  291.        continue; 
  292.         
  293.         done; 
  294. else 
  295.    echo " system doesn't install fdisk or parted!"; 
  296.    exit 1; 
  297. fi 
  298.   
  299. #echo "partition customize end";exit 
  300. #lvm start 
  301. unset vg_obg_id vg_obg_id_length; 
  302. for i in `cat $cfglist_vaild |cut -d " " -f 1|grep -v "-"|sort -u`; 
  303. do 
  304. vg_obg_id=$i; 
  305. echo "__"$i"__"; 
  306. eval "vg_obg_id_length=$(echo '${#'$vg_obg_id'[@]}')"; 
  307. for ((j=0;j<$vg_obg_id_length;j++)) 
  308. do 
  309.    eval 'vg_obg_disk_name=${'$vg_obg_id'[$j]}'; 
  310.    echo $j":"$vg_obg_disk_name; 
  311.    eval "(vgdisplay -s $vg_obg_id 2>&1) > /dev/null" 
  312.    if [[ $? -eq 0 ]]; 
  313.    then 
  314.       eval "(vgextend $vg_obg_id $vg_obg_disk_name 2>&1) >/dev/null"; 
  315.    else 
  316.       eval "(vgcreate $vg_obg_id $vg_obg_disk_name 2>&1) >/dev/null"; 
  317.    fi 
  318. done; 
  319.   
  320. done; 
  321. exit 0;