天天看点

sfdisk 用法

sfdisk -f $device  >& /dev/null << EOF

$part1_offset,$part1_size,83,*           //第一分区:从$part1_offset开始,$part1_size个柱面,LINUX_NATIVE (83)类型,启动分区

,$part2_size,L,-                              //第二分区:接着上一个分区,$part2_size个柱面,LINUX_NATIVE (83)类型,非启动分区

,,E                                                       //剩余空间为扩展分区        E is EXTENDED_PARTITION (5)

,,L                                                       //扩展分区全部划成逻辑分区   

EOF

>& /dev/null : 重定向command 的stdout 到/dev/null

<< EOF   EOF : list string

sfdisk : -f force

reads lines of the form : <start> <size> <id> <bootable> <c,h,s> <c,h,s>

Fields are separated by whitespace, or comma or semicolon possibly followed by whitespace; initial and trailing whitespace is ignored. Numbers can be octal, decimal or hexadecimal, decimal is default. When a field is absent or empty, a default value is used.

The <c,h,s> parts can (and probably should) be omitted - sfdisk computes them from <start> and <size> and the disk geometry as given by the kernel or specified using the -H, -S, -C flags.

Bootable is specified as [*|-], with as default not-bootable. (The value of this field is irrelevant for Linux - when Linux runs it has been booted already - but might play a role for certain boot loaders and for other operating systems. For example, when there are several primary DOS partitions, DOS assigns C: to the first among these that is bootable.)

Id is given in hex, without the 0x prefix, or is [E|S|L|X], where L (LINUX_NATIVE (83)) is the default, S is LINUX_SWAP (82), E is EXTENDED_PARTITION (5) , and X is LINUX_EXTENDED (85).

分区实例:

 60 echo "[Partitioning $1...]"

 61 

 62 DRIVE=$1

 63 dd if=/dev/zero of=$DRIVE bs=1024 count=1024 &>/dev/null

 64 

 65 SIZE=`fdisk -l $DRIVE | grep Disk | awk '{print $5}'`             //检测多少字节

 66 

 67 echo DISK SIZE - $SIZE bytes

 68 

 69 CYLINDERS=`echo $SIZE/255/63/512 | bc`                          //计算多少柱面

 70 

 71 echo CYLINDERS - $CYLINDERS

 72 {

 73 echo ,9,0x0C,*

 74 echo ,$(expr $CYLINDERS / 4),,-

 75 echo ,$(expr $CYLINDERS / 4),,-

 76 echo ,,0x0C,-

 77 } | sfdisk -D -H 255 -S 63 -C $CYLINDERS $DRIVE &> /dev/null                //分区

。。。。。。。

 86 mkfs.vfat -F 32 -n boot ${DRIVE}1 &> /dev/null                                            //格式化

 87 mkfs.ext4 -L rootfs ${DRIVE}2 &> /dev/null

 88 mkfs.ext4 -L usrdata ${DRIVE}3 &> /dev/null

 89 mkfs.vfat -F 32 -n data ${DRIVE}4 &> /dev/null