实战讲解磁盘分区命令fdisk

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实战讲解磁盘分区命令fdisk相关的知识,希望对你有一定的参考价值。

实战讲解磁盘分区命令fdisk

介绍MBR分区、主分区、扩展分区和逻辑分区

 

  柱面是分区的最少单位

主引导记录(Master Boot Record,缩写:MBR),又叫做主引导扇区

技术分享



分区表记录每个分区的属性,分区表每记录一个分区消耗16字节,分区表只能区分主分区和扩展分区

主分区:我们在硬盘划分出来可以直接使用的分区

扩展分区:我们在硬盘上划出来不能直接使用,要在上面再划分分区才能直接使用

逻辑分区:在硬盘上的扩展分区上再划分出来的分区

 

列出可用的磁盘设备使用情况和可用性

 

  一块硬盘最多只能划分的分区主分区+扩展分区=4  主分区只能划分4

扩展分区只能最多有一个

P+E<=4

E<=1

GBT类型的分区可以划分128个主分区

 

 

 

fdisk命令使用

查看分区信息

fdisk -l 查看本机上所有硬盘的分区信息

[[email protected] ~]# fdisk -l
 
Disk /dev/sda: 21.5 GB, 21474836480 bytes        <===显示硬盘大小
255 heads, 63sectors/track, 2610 cylinders                    <===显示柱面信息
Units = cylinders of 16065 * 512 = 8225280bytes    <===显示柱面单位和每个柱面大小
Sector size (logical/physical): 512 bytes /512 bytes
I/O size (minimum/optimal): 512 bytes / 512bytes
Disk identifier: 0x00088aac
下面是显示sda硬盘分区信息
  Device Boot      Start         End      Blocks  Id  System  /dev/sda1  *           1          26      204800  83  Linux
Partition 1 does not end on cylinderboundary.
/dev/sda2              26         287    2097152   82  Linux swap / Solaris
Partition 2 does not end on cylinderboundary.
/dev/sda3             287        2611   18668544   83  Linux
 
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280bytes
Sector size (logical/physical): 512 bytes /512 bytes
I/O size (minimum/optimal): 512 bytes / 512bytes
Disk identifier: 0x00000000

 

fdisk -l /dev/sda查看系统上某个硬盘的分区信息

[[email protected] ~]# fdisk -l /dev/sda
 
Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280bytes
Sector size (logical/physical): 512 bytes /512 bytes
I/O size (minimum/optimal): 512 bytes / 512bytes
Disk identifier: 0x00088aac
 
   Device Boot      Start         End      Blocks  Id  System
/dev/sda1  *           1          26      204800  83  Linux
Partition 1 does not end on cylinderboundary.
/dev/sda2              26         287    2097152   82  Linux swap / Solaris
Partition 2 does not end on cylinderboundary.
/dev/sda3             287        2611   18668544   83  Linux

 

 

使用fdisk对硬盘sdb分区:

步骤:分区---更新分区表---格式化分区---挂载

[[email protected] ~]# fdisk /dev/sdb
Device contains neither a valid DOSpartition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with diskidentifier 0x885f5c02.
Changes will remain in memory only, untilyou decide to write them.
After that, of course, the previous contentwon‘t be recoverable.
 
Warning: invalid flag 0x0000 of partitiontable 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        <------m显示当前帮助
Command action
  a   toggle a bootable flag
  b   edit bsd disklabel
  c   toggle the dos compatibilityflag
  d   delete a partition              <===删除一个分区
   l  list known partition types      <===列出己知分区类型
   m  print this menu                                   <===打印帮助
   n  add a new partition                    <===新建一个分区
  o   create a new empty DOSpartition table
  p   print the partition table          <===显示分区表信息
  q   quit without saving changes       <===不保存直接退出
  s   create a new empty Sundisklabel
  t   change a partition‘s system id      <===改变一个分区类型ID
  u   change display/entry units
  v   verify the partition table
  w   write table to disk and exit        <===保存并退出
  x   extra functionality (expertsonly)

 

~~新建一个分区:

fdisk /dev/sdb 然后按n,新建一个分区

[[email protected] ~]# fdisk /dev/sdb
Device contains neither a valid DOSpartition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with diskidentifier 0xc5331452.
Changes will remain in memory only, untilyou decide to write them.
After that, of course, the previous contentwon‘t be recoverable.
 
Warning: invalid flag 0x0000 of partitiontable 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): n     <===新建一个分区
Command action
   e  extended           <===新建分区类型逻辑分区
   p  primary partition (1-4)     <===新建分区类型主分区
p                                                            <===我们选择分区类型为主分区
Partition number (1-4): 1      <===分区编号
Firstcylinder (1-652, default 1): 1         <===分区的开始柱面   
Last cylinder, +cylinders or+size{K,M,G} (1-652, default 652): 100  <===分区结束柱面     也可以用K、M、G来添加,如+1G、+500M 、+1024K
Command (m for help): p       <===分完区后按P打印分区信息
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280bytes
Sector size (logical/physical): 512 bytes /512 bytes
I/O size (minimum/optimal): 512 bytes / 512bytes
Disk identifier: 0xc5331452
 
  Device Boot      Start         End      Blocks  Id  System
/dev/sdb1               1         100      803218+ 83  Linux

 

 

 

~~删除一个分区

[[email protected] ~]# fdisk /dev/sdb  
 
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): p   <===打印分区表信息
 
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280bytes
Sector size (logical/physical): 512 bytes /512 bytes
I/O size (minimum/optimal): 512 bytes / 512bytes
Disk identifier: 0x61a28730
 
  Device Boot      Start         End      Blocks  Id  System
/dev/sdb1               1         100      803218+ 83  Linux
/dev/sdb2             101         200      803250  83  Linux
 
Command (m for help): d          <===删除一个分区
Partition number(1-4): 2                             <===选择要删除的分区编号
 
Command (m for help): p         <===打印分区表信息
 
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280bytes
Sector size (logical/physical): 512 bytes /512 bytes
I/O size (minimum/optimal): 512 bytes / 512bytes
Disk identifier: 0x61a28730
 
  Device Boot      Start         End      Blocks  Id  System
/dev/sdb1               1         100      803218+ 83  Linux
 
Command (m for help): w   <===保存并退出


 

~~更新分区表

新建分区后,分区表是还没有新建的分区信息,要对分区表进行更新partprobe/dev/sda 更新分区表(对系统所在硬盘不生效,如果更新sdb,sdc系统不在的硬盘就会生效)

  partx –a/dev/sda   强制刷新分区表

 

 

~~格式化分区

更新完分区别后,要对该分区进行硬式化

mkfs/dev/sda7   sda7分区进行格式化(要重启系统)

默认mkfis格式化出来的文件系统是ext2,我们要是想把sda7格式化成ext4时,我们用mkfs.ext4来对sda7进行格式化并更改其文件系统类型 如:mkfs.ext4 /dev/sda7

mkfs.ext4 -b 1024 /dev/sdb3 可以格式化并修改分区block值为1024

 

格式化完分区后,会提示每隔23周或180天检查一下系统,

This filesystem will be automaticallychecked every 23 mounts or

180 days, whichever comes first.  Use tune2fs -c or -i to override.

我们用tune2fs -c -1 /dev/sdb3 来处理

 

~~挂载分区和删除挂载分区

把新建的分区挂载到/目录上才能进行防问

临时挂载分区:

mount–a /dev/sda3 /aa

umount/dev/sda3   umount 设备或挂载点

系统启动自动挂载:

1   mkdir /aa 先在/目录新建一个目录并命名  如:aa

     2   vim /etc/fstab  配置fstab把新建好的分区sda7挂载到/aa目录里,让其在系统启动时自动挂载到/aa目录里(要重启系统)

[[email protected] ~]# vi /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Jan  9 18:07:20 2014
#
# Accessible filesystems, by reference, aremaintained under ‘/dev/disk‘
# See man pages fstab(5), findfs(8),mount(8) and/or blkid(8) for more info
#
UUID=02835af0-7725-42ec-90a4-b4cf397823c3/                       ext4    defaults        1 1
UUID=3350ecad-ac12-4807-966b-f187a32838d6/boot                   ext4    defaults        1 2
UUID=0ee03381-3521-477c-ac99-703a1a7dbc20swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/sdb3               /mnt                    ext4    defaults        0 0

 

 

 

 

 

 

 

~~修改分区类型

 [[email protected] ~]# fdisk /dev/sdb
 
WARNING: DOS-compatible mode is deprecated. It‘sstrongly recommended to
        switch off the mode (command ‘c‘) and change display units to
        sectors (command ‘u‘).
 
Command (m for help): p
 
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x33b8ad83
 
   DeviceBoot      Start         End      Blocks  Id  System
/dev/sdb1               1         100      803218+ 83  Linux
/dev/sdb2            101         200      803250  83  Linux
/dev/sdb3            201         500     2409750  83  Linux
 
Command (m for help): t       <===修改分区类型
Partition number (1-4): 3                   <===选择要修改的分区编号
Hex code (type L to list codes): 82   <===填写修改的分区类型ID  可以按L来列出ID列表
Changed system type of partition 3 to 82 (Linux swap/ Solaris)
 
Command (m for help): p
 
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x33b8ad83
 
   DeviceBoot      Start         End      Blocks  Id  System
/dev/sdb1               1         100      803218+ 83  Linux
/dev/sdb2            101         200      803250  83  Linux
/dev/sdb3            201         500     2409750  82  Linux swap / Solaris
 
Command (m for help): w         <===保存并退出
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
Syncing disks.

 

 

刷新分区表:

在我们对分区类型进行了修改之后,一定刷新分区表才能有效

partprobe  /dev/sda  linx5以下的对系统所在硬盘分区生效,对linux5以上的硬盘中有系统所在分区的不会生效,如sda中有系统分区,就不生效,sdb中就生效

partx a /dev/sda  强制刷新分区表

 

 

fdisk只对于小于2T的硬盘进行分区,大于2T的硬盘我们只能用parted来对硬盘进行分区

 


本文出自 “Mr.Xiong`s 运维日志” 博客,请务必保留此出处http://mrxiong2017.blog.51cto.com/12559394/1932657

以上是关于实战讲解磁盘分区命令fdisk的主要内容,如果未能解决你的问题,请参考以下文章

存储管理2

centos下fdisk和parted磁盘分区实战

fdisk分区实战

linux parted分区

20170124L07-02-06老男孩linux运维实战培训-parted分区命令实战讲解

lvm讲解