#!/bin/sh # linux sphinx's disk carvulator # part of the gnnix conspiracy # usage dfree devicename amount-needed dfree() { req=$2 str=`fdisk -l /dev/${1} | grep -E '^Disk'` bytes=${str#*, } max=${bytes% bytes} typeset -i sum=0 fdisk -l /dev/${1} | grep /dev/ | grep -v Extended | grep -v Disk | sed 's/[*|+]//g' > /tmp/dfree while read j do if [ ! -z "$j" ]; then set $j ((sum=$4 +$sum)) fi done < /tmp/dfree echo "Results Of Disk Space Analysis " echo " Max: $max" > /tmp/carve ((sum=($sum*1024))) echo " In use: $sum" >> /tmp/carve ((dif=$max -$sum)) echo " free: $dif" >> /tmp/carve # 4*1024*1024*1024 minimum #if (( $dif > 4294967296 )); then echo "required: $req" >> /tmp/carve if (( $dif > $req )); then echo "Status: Plenty Of Room" >> /tmp/carve return 0 else echo " * NOT ENOUGH FREE DISK SPACE * NO FORMATTING PERFORMED Use cfdisk to free up $req bytes and retry " >> /tmp/carve return 1 fi } # usage: autocarve pattern devname as in autocarve 0 hda autocarve() { rm -f /tmp/crvr.map memsz=`grep MemTotal: /proc/meminfo |sed 's/ //g'` str=${memsz#MemTotal:} mem=${str%kB} ((pmem=$mem*1024)) # specify name:size_in_bytes - use 0 for ext size $pmem for swap - order is significant PARTITMAP[0]="boot:10000000 slash:100000000 swap:${pmem} ext:0 var:300000000 usr:2000000000 tmp:500000000 local:500000000 home:500000000" PARTITMAP[1]="boot:10000000 slash:100000000 swap:${pmem} ext:0 var:300000000 usr:2000000000 tmp:500000000 local:500000000 home:500000000" PARTITMAP[2]="boot:10000000 slash:100000000 swap:${pmem} ext:0 var:300000000 usr:2000000000 tmp:500000000 local:500000000 home:500000000" # determine total needed first pass for i in ${PARTITMAP[${1}]} do ((b=$b + ${i#*:})) done dfree $2 $b if (($? == 1)); then return 1 fi str=`fdisk -l /dev/${2} | grep -E '^Disk'` bytes=${str#*, } max=${bytes% bytes} str=`fdisk -l /dev/${2} | grep -E 'cylinders$'` bytes=${str#*sectors/track, } cylcnt=${bytes% cylinders} ((cylsz=${max}/${cylcnt})) echo "Resulting Disk Map name cylinders boot type" >> /tmp/carve echo "------------------------------" >> /tmp/carve sum=0 for i in ${PARTITMAP[${1}]} do ((c=${i#*:} /$cylsz)) n=${i%:*} if [ "$n" = "slash" ]; then echo "$n $c * linux" >> /tmp/carve echo ",${c},L,*" >> /tmp/crvr.map elif [ "$n" = "swap" ]; then echo "$n $c swap" >> /tmp/carve echo ",${c},S" >> /tmp/crvr.map elif [ $n = "ext" ]; then echo ",,E" >> /tmp/crvr.map else echo "$n $c linux" >> /tmp/carve echo ",${c},L" >> /tmp/crvr.map fi ((sum=${c} +$sum)) done echo " total bytes: $max" >> /tmp/carve echo "cylinder count: $cylcnt" >> /tmp/carve echo " cylinder size: $cylsz" >> /tmp/carve echo " used cyls: $sum" >> /tmp/carve ((fcyl=$cylcnt-$sum)) echo " free cyls: $fcyl" >> /tmp/carve ((tot=$max-($max-($sum*$cylsz)))) echo " used bytes: $tot" >> /tmp/carve ((tot=$max-($max-($fcyl*$cylsz)))) echo " free bytes: $tot " >> /tmp/carve #sfdisk /dev/${2} << /tmp/crvr.map #sfdisk -R /dev/${2} } # carve disk /dev/sda with pattern 1 autocarve 1 sda cat /tmp/carve