linux 用脚本建分区,脚本创建磁盘分区
首先最好添加一個空白的磁盤,以免對原數(shù)據(jù)產(chǎn)生破壞,若果不想添加磁盤也可以,用一個里面數(shù)據(jù)無關(guān)緊要的磁盤,當(dāng)然前提是必須要對該磁盤內(nèi)已有的分區(qū)進(jìn)行清空,方法后面第三步會有
1.先看腳本內(nèi)容:
[root@localhost shell]# cat auto_fdisk.sh
#!/bin/bash
#auto fdisk shell
echo '
n
p
1
#這里的一個空格別忘了,必不可少的,這是創(chuàng)建分區(qū)時填寫開始柱面時的Enter,也就是使用默認(rèn)柱面
+100M
n
p
2
#一個空格
+200M
n
p
3
#一個空格
+300M
n
e
4
#兩個空格
t
3
82
w' ? ?| ? ?fdisk /dev/sdc
這里我對/dev/sdc創(chuàng)建了3個大小分別為100M,200M,300M的主分區(qū),然后將剩余空間給了擴(kuò)展分區(qū),并且將第三個分區(qū)類型調(diào)整為82
2.然后給該腳本賦予可執(zhí)行權(quán)限,最后執(zhí)行,執(zhí)行結(jié)果是
[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.
看一下執(zhí)行結(jié)果
[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.最后如果想重新創(chuàng)建或者使開頭提到的情況,需要把一個已有分區(qū)清空,可以這樣
[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
內(nèi)核重新讀取后再看一下此時的分區(qū)情況
[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
好了,就是這樣!
閱讀(821) | 評論(0) | 轉(zhuǎn)發(fā)(0) |
總結(jié)
以上是生活随笔為你收集整理的linux 用脚本建分区,脚本创建磁盘分区的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 黄金理财在哪买?投资黄金的渠道
- 下一篇: 国债票面利率如何确定的?由这些因素决定