首先最好添加一个空白的磁盘,以免对原数据产生破坏,若果不想添加磁盘也可以,用一个里面数据无关紧要的磁盘,当然前提是必须要对该磁盘内已有的分区进行清空,方法后面第三步会有
1.先看脚本内容:
[root@localhost shell]# cat auto_fdisk.sh
#!/bin/bash
#auto fdisk shell
echo '
n
p
1
#这里的一个空格别忘了,必不可少的,这是创建分区时填写开始柱面时的Enter,也就是使用默认柱面
+100M
n
p
2
#一个空格
+200M
n
p
3
#一个空格
+300M
n
e
4
#两个空格
t
3
82
w' | fdisk /dev/sdc
这里我对/dev/sdc创建了3个大小分别为100M,200M,300M的主分区,然后将剩余空间给了扩展分区,并且将第三个分区类型调整为82
2.然后给该脚本赋予可执行权限,最后执行,执行结果是
[root@localhost shell]# chmod +x auto_fdisk.sh
[root@localhost shell]# ./auto_fdisk.sh
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x0cf693d0.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): Command (m for help): Command action
e extended
p primary partition (1-4)
Partition number (1-4): First cylinder (1-2349, default 1): Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2349, default 2349):
Command (m for help): Command action
e extended
p primary partition (1-4)
Partition number (1-4): First cylinder (15-2349, default 15): Using default value 15
Last cylinder, +cylinders or +size{K,M,G} (15-2349, default 2349):
Command (m for help): Command action
e extended
p primary partition (1-4)
Partition number (1-4): First cylinder (41-2349, default 41): Using default value 41
Last cylinder, +cylinders or +size{K,M,G} (41-2349, default 2349):
Command (m for help): Command action
e extended
p primary partition (1-4)
Selected partition 4
First cylinder (80-2349, default 80): Value out of range.
First cylinder (80-2349, default 80): Using default value 80
Last cylinder, +cylinders or +size{K,M,G} (80-2349, default 2349): Using default value 2349
Command (m for help): Partition number (1-5): Hex code (type L to list codes): Changed system type of partition 3 to 82 (Linux swap / Solaris)
Command (m for help): The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
看一下执行结果
[root@localhost shell]# fdisk -l /dev/sdc
Disk /dev/sdc: 19.3 GB, 19327352832 bytes
255 heads, 63 sectors/track, 2349 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0cf693d0
Device Boot Start End Blocks Id System
/dev/sdc1 1 14 112423+ 83 Linux
/dev/sdc2 15 40 208845 83 Linux
/dev/sdc3 41 79 313267+ 82 Linux swap / Solaris
/dev/sdc4 80 2349 18233775 5 Extended
3.最后如果想重新创建或者使开头提到的情况,需要把一个已有分区清空,可以这样
[root@localhost shell]# dd if=/dev/zero of=/dev/sdc bs=512 count=1
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.0178267 s, 28.7 kB/s
内核重新读取后再看一下此时的分区情况
[root@localhost shell]# partprobe
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sdb (Device or resource busy). As a result, it may not reflect all of your changes until after reboot.
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
Error: Invalid partition table - recursive partition on /dev/sr0.
[root@localhost shell]# fdisk -l /dev/sdc
Disk /dev/sdc: 19.3 GB, 19327352832 bytes
255 heads, 63 sectors/track, 2349 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
好了,就是这样!
阅读(1210) | 评论(0) | 转发(0) |