linux磁盘分区fdisk命令

Posted on 2018-01-20 21:44  ishare_zy  阅读(240)  评论(0)    收藏  举报

转自http://blog.51cto.com/linux008/548711

1、什么是分区?
    分区是将一个硬盘驱动器分成若干个逻辑驱动器,分区是把硬盘连续的区块当做一个独立的磁硬使用。分区表是一个硬盘分区的索引,分区的信息都会写进分区表。
2、为什么要有多个分区?

  • 防止数据丢失:如果系统只有一个分区,那么这个分区损坏,用户将会丢失所的有数据。

  • 增加磁盘空间使用效率:可以用不同的区块大小来格式化分区,如果有很多1K的文件,而硬盘分区区块大小为4K,那么每存储一个文件将会浪费3K空间。这时我们需要取这些文件大小的平均值进行区块大小的划分。

  • 数据激增到极限不会引起系统挂起:将用户数据和系统数据分开,可以避免用户数据填满整个硬盘,引起的系挂起。

3、分区工具fdisk用法介绍
   fdisk命令参数介绍

[root@zzzzzzz ~]# fdisk /dev/sdb 
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xa0089f06.
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): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): 

   p、打印分区表。
   n、新建一个新分区。
   d、删除一个分区。
   q、退出不保存。
   w、把分区写进分区表,保存并退出。
实例:
   1)先使用‘ fdisk -l ’查看硬盘情况

[root@zzzzzzz ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x000a7622

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64         325     2097152   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3             325        2611    18361344   83  Linux
[root@zzzzzzz ~]# 

通过编辑虚拟机给LINUX系统添加一块硬盘(设置——硬件——硬盘——添加——下一步点到底),然后重新链接终端,使用fdisk -l 就能看到新加到系统的硬盘信息。

[root@zzzzzzz ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x000a7622

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64         325     2097152   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3             325        2611    18361344   83  Linux

Disk /dev/sdb: 10.7 GB, 10737418240 bytes      //  /dev/sdb就是我们新加到系统的硬盘
255 heads, 63 sectors/track, 1305 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

给新硬盘分区输入n ,进行分区,让我们选择1-4,那我们可以选择1

然后就一直回车。

当出现:Command (m for help):后再输入W ,存盘退出fdisk模式,完成后,我们对新硬盘进行格式化,输入Mkfs –t ext3 /dev/sdb出现Proceed anyway?(y,n)时,这时输入“Y”回车。    

格式化完成后,我们要做的就是对新硬盘设定挂载点,可以新建目录,例如:mkdir /newdisk然后把新硬盘挂载到这个位置:mount /dev/sdb /newdisk 挂载好了,查看一下:df

还没有结束,我们要让系统重启后会自动挂载新硬盘,编辑:vi /etc/fstab 按Insert键插入一行:/dev/sdb/newdiskext31 2 ,输入完毕后Esc :wq保存退出,新硬盘也就添加完毕了。