#!/bin/bash # guruparty # # This script is part of shinstall, the automatic installation engine # Do: automatic parititionising of given disk device # Run: after controls # Require: essencial packages, sfdisk # # v 1.0 # Written by JeromeHeretic 2007 ################################################################################## set -e #set -x # --------------- DEVICE=$1 BOOTABLE="" BLOCKSIZE=1024 msg(){ echo "USAGE: $0 -d device -p partitionlist -b bootable partition" echo "device - disk you want to partitionize" echo "bootable partition - number of partition with bootable flag (count from 1). This parameter is not mandatory" echo "partitionlist - is list of partitions separate by spaces in format partsize-parttype" echo " partsize - can be in bytes, k M or G (1073741824 is the same as 1048576k or 1024M or 1G)" echo " special parameter is TOEND, which means that partition is created to end of the drive" echo " TOEND parameter can be used only at last partition of course (in other case guruparty fails)" echo " parttype - is Id of filesystem (try sfdisk -T for complete list of partition Ids)" echo "-f - I am an anarchist" echo " I wanna destroy (all data on the disk)" echo echo "EXAMPLE: ./guruparty -d /dev/sda -p 1G:82 10G:fd TOEND:83 -b 3" echo "This command create on /dev/sda 1GB swap partition, 10GB raid autodetect partition and rest of disk" echo "as linux partition. Linux partition will be marked with boot flag" } countsize(){ echo $1 |sed s/':.*'//g |sed -e 's/k/*1024/g' -e 's/M/*1024*1024/g' -e 's/G/*1024*1024*1024/g' |bc } gettype(){ echo $1 |sed s/'.*:'//g } parsecmdline(){ PCOUNT="`echo $@ |awk -v FS="-" '{print NF}'`" for SHIFT in `seq 1 $PCOUNT`; do PARAM=`echo $@ |awk -v FS="-" '{print $'$SHIFT'}'` echo $PARAM |grep -q ^d && DEVICE="`echo $PARAM |sed s/^d\ //g`" echo $PARAM |grep -q ^p && PARTITIONLIST="`echo $PARAM |sed s/^p\ //g`" echo $PARAM |grep -q ^b && BOOTABLE="`echo $PARAM |sed s/^b\ //g`" echo $PARAM |grep -q ^f && FORCE="1" done for PART in $PARTITIONLIST; do if echo $PART |tr [A-Z] [a-z] |grep -q toend; then SIZES="$SIZES TOEND" else SIZES="$SIZES `countsize $PART`" fi TYPES="$TYPES `gettype $PART`" done TYPES=(NULL $TYPES) } get_geometry(){ BLOCKCOUNT=`sfdisk -s $1` || exit 1 BYTES=$((BLOCKCOUNT * $BLOCKSIZE)) GEOMETRY=(`sfdisk -g $1`) || exit 1 CYL=${GEOMETRY[1]} HEAD=${GEOMETRY[3]} SEC=${GEOMETRY[5]} CYLSIZE=`echo "scale=4; $BLOCKCOUNT / $CYL" | bc` || exit 1 #echo "BLOCKCOUNT=$BLOCKCOUNT BYTES=$BYTES CYL=$CYL HEAD=$HEAD SEC=$SEC CYLSIZE=$CYLSIZE" # sfdisk -g $1 } gen_partitions(){ START="0" for PARTSIZE in $SIZES; do let ++PARTNO [ "$PARTNO" = "$BOOTABLE" ] && FLAG="bootable" || FLAG="" TYPE=${TYPES[$PARTNO]} if echo $PARTSIZE |tr [A-Z] [a-z] |grep -q toend; then SETCYLINDER="" else SETCYLINDER="`echo \"$PARTSIZE / ( $CYLSIZE*$BLOCKSIZE)\" |bc`" fi echo "${DEVICE}$PARTNO : start= $START, size=$SETCYLINDER, Id=$TYPE, $FLAG" [ "$SETCYLINDER" != "" ] && START="`echo \"scale=4; $START + $SETCYLINDER + 1\" |bc`" done } # Let's Rock... # ----------------------------------------- case "$1" in ''|-h|--help) msg ;; *) parsecmdline "$@" # This returns 1 if no partitions found # in this case we can partitionize without afraid of some data lost sfdisk -d $DEVICE |awk '{print $7}' |sed s/Id=//g |grep -vq ^$ || FORCE=1 if [ "$FORCE" = "1" ]; then get_geometry $DEVICE gen_partitions |sfdisk $DEVICE >/dev/null 2>&1 && echo done else echo "This device looks partitionized." echo "If you sure know what you are doing, try -f parameter" exit 1 fi ;; esac