Wednesday 4 August 2010

Fedora 13 Kickstart Disk Partitioning

Work asked me to create an auto install of Fedora 13 that resizes a Windows XP install automagically... here's the results of some hair pulling and tomgeekery!!!

In your ks.cfg -
%pre
#get partition scripts / files
wget -P/tmp http://IPADDRESS/partition.sh 
sh /tmp/partition.sh >> /tmp/partition.log
 partition.sh -
#!/bin/bash
##############################################################################
#Made by Long Tom 2010
#Script resizes 160GB Windows disk to 80GB ntfs, 76GB linux & 4GB swap
#Procedure (tested manually)
##resize windoze partition
#ntfsresize -f -s 80G /dev/sda1 (works)
#Now fdisk - Delete sda1, recreate new NTFS at half size and toggle bootable
#parted can now use free space
##make a root linux partition
#parted mkpartfs primary ext2 start end
#partition remaining for swap
#parted mkpartfs primary linux-swap start end
##format the filesystems, installer can change to ext3 / 4 later on
##############################################################################

##########    Getting the numbers

#First need to get disk size(s)   
dev=`fdisk -l | grep -m 1 Disk | grep bytes | awk '{print $2}'`    #gives something like - Disk /dev/sda: 500.1 GB, 500107862016 bytes
bytes=`fdisk -l | grep -m 1 Disk | grep bytes | awk '{print $5}'`

dev=${dev%:}    #strip of : from end

#get RAM
mem=`cat /proc/meminfo | grep MemTotal | awk '{print $2}'` #gives memory in kB

#echo "$memk"

let kbytes=$bytes/1000

echo "First disk drive $dev"
echo "Total size $kbytes kB"
echo "System memory $mem kB"    #we'll use this as swap partition size

#so half size of disk -
let halfPartSize=$kbytes/2

echo "Windows partition will be $halfPartSize kB"
echo "Swap will be $mem kB"
echo "Linux will be whatever's left!"

one="1"

dev1=$dev$one    #gives /dev/sda1
let halfPartSizeK=$halfPartSize*1000    #ntfsresize needs kb

##########    Resize windows partition
echo "Now running ntfsresize -f -s $halfPartSizeK $dev1"
ntfsresize -f -s $halfPartSizeK $dev1

##########    Check to see if previously partitioned - if so skip fdisk +hope!!!

#if [`fdisk -l | grep sda2`

##########    Make fdisk.commands file with fdisk scripting

K="K"
plus="+"
halfPartSizeK=$plus$halfPartSize$K
linuxK=$plus$mem$K
#swapStartK=$plus$swapStart$K

echo "d" > /tmp/fdisk.commands    #delete
echo "n" >> /tmp/fdisk.commands    #new
echo "p" >> /tmp/fdisk.commands    #pri
echo "1" >> /tmp/fdisk.commands    #num
echo "1" >> /tmp/fdisk.commands    #start
echo $halfPartSizeK >> /tmp/fdisk.commands    #end
echo "t" >> /tmp/fdisk.commands    #type
echo "7" >> /tmp/fdisk.commands    #ntfs
echo "a" >> /tmp/fdisk.commands    #boot
echo "1" >> /tmp/fdisk.commands
echo "n" >> /tmp/fdisk.commands    #new
echo "p" >> /tmp/fdisk.commands    #pri
echo "2" >> /tmp/fdisk.commands    #num
echo "" >> /tmp/fdisk.commands    #start
echo $linuxK >> /tmp/fdisk.commands    #end
echo "n" >> /tmp/fdisk.commands    #new
echo "p" >> /tmp/fdisk.commands    #pri
echo "3" >> /tmp/fdisk.commands    #num
#echo $swapStartK >> /tmp/fdisk.commands    #start   
echo "" >> /tmp/fdisk.commands    #start   
echo "" >> /tmp/fdisk.commands    #end defaults fine here
echo "w" >> /tmp/fdisk.commands    #write & quit

##########    Now run fdisk

echo "Now running fdisk $dev"

fdisk $dev < /tmp/fdisk.commands

echo "Now running parted to format linux partitions"
parted -s $dev mkfs 3 ext2
parted -s $dev mkfs 2 linux-swap

exit 0
Get that for some dodgy scripting!!! hehehe works though :P

No comments:

Post a Comment