26-磁盘管理体系(下部)


准备环境,先创建2块0.1g的磁盘


image.png

磁盘分区知识点

查看系统中有几个磁盘

[root@oldboyedu ~]# fdisk -l|grep 'sd[a-z]:'
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Disk /dev/sdb: 106 MB, 106954752 bytes, 208896 sectors
Disk /dev/sdc: 106 MB, 106954752 bytes, 208896 sectors
[root@oldboyedu ~]# 

一、MBR

含义:Master Boot Record 主引导记录
位置:磁盘的开头(0磁头 0磁道 1扇区 512字节中的前446字节)

image.png

二、分区类型

1、主分区(primary)

1个主分区要占用16个字节的分区表
在1个磁盘中最多4个主分区
一般必须都要有

2、扩展分区(extended)

磁盘最大只能有4个主分区
扩展分区无法直接使用
1个扩展分区要占用16个字节的分区表
1个磁盘中只能创建1个扩展分区
数据必须放在扩展分区中的逻辑分区

3、逻辑分区(logical)

需要先创建扩展分区
存放数据
最多11个

三、分区的命令规则

1、硬盘名字

物理服务器:sata/sas/scsi这些磁盘接口,硬盘的名字是/ sd??开头,都放放在/dev下,如:
  第一块硬盘 /dev/sda
  第四块硬盘 /dev/sdd

云服务器(阿里云)
  /dev/vda
  /dev/vdb

2、分区名字

1>主分区或扩展分区

范围:1-4
如:/dev/sda1.../dev/sda4

2>逻辑分区

范围:从5开始(>=5)
如:/dev/sda5(第一个逻辑分区)

习题:

第3块sata磁盘的第2个主分区
/dev/sdc2
第6块sas磁盘的第2个逻辑分区
/dev/sdf6

四、分区实战※※※

1、分区命令

1>fdisk命令

参数:-l(小写字母L):查看磁盘分区信息
fdisk内部命令:
  p:print 显示磁盘分区信息
  n:new 创建分区
  d:delete删除磁盘分区
  w:write 保存并退出
  q或Ctrl+c:退出不保存
  t:改变分区类型

2>parted命令

无需保存,实时生效
功能:
  p:print 显示磁盘信息
  mktable:(make partition table) 创建磁盘分区表 MBR GPT
  mkpart:make partition 创建磁盘分区
  rm:删除磁盘分区
  q:quit 退出磁盘分区

2、实例练习:

1>fdisk创建分区实例

创建2个分区 1个10MB的主分区 创建1个使用所有剩余空间的扩展 分区 1个20MB的逻辑分区

#开始创建分区

[root@oldboyedu ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

#查看该磁盘下有多少分区

Command (m for help): p     #查看该磁盘下的分区

Disk /dev/sdb: 106 MB, 106954752 bytes, 208896 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x6e4c6459

   Device Boot      Start         End      Blocks   Id  System

#n 创建分区,第一个创建主分区

Command (m for help): n    #创建分区
Partition type:
   p   primary (0 primary, 0 extended, 4 free)  #主分区
   e   extended    #扩展分区
Select (default p): p    #创建第一个主分区
Partition number (1-4, default 1): 1 #分区编号,默认从1开始
First sector (2048-208895, default 2048):    #开始大小,默认
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-208895, default 208895): +10M    #结束,这里给10M
Partition 1 of type Linux and of size 10 MiB is set
Command (m for help): p   #查看

Disk /dev/sdb: 106 MB, 106954752 bytes, 208896 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x6e4c6459

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048       22527       10240   83  Linux

#创建1个使用所有剩余空间的扩展分区

Command (m for help): n  #创建1个使用所有剩余空间的扩展分区 
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): e    #创建扩展分区
Partition number (2-4, default 2):  默认
First sector (22528-208895, default 22528):   默认
Using default value 22528
Last sector, +sectors or +size{K,M,G} (22528-208895, default 208895): 默认
Using default value 208895
Partition 2 of type Extended and of size 91 MiB is set

Command (m for help): p

Disk /dev/sdb: 106 MB, 106954752 bytes, 208896 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x6e4c6459

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048       22527       10240   83  Linux
/dev/sdb2           22528      208895       93184    5  Extended

#创建一个20M的逻辑分区,一个磁盘只能有一个扩展分区

Command (m for help): n
Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)   #逻辑分区
Select (default p): l   
Adding logical partition 5
First sector (24576-208895, default 24576): 
Using default value 24576
Last sector, +sectors or +size{K,M,G} (24576-208895, default 208895): +20M
Partition 5 of type Linux and of size 20 MiB is set

Command (m for help): p

Disk /dev/sdb: 106 MB, 106954752 bytes, 208896 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x6e4c6459

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048       22527       10240   83  Linux
/dev/sdb2           22528      208895       93184    5  Extended
/dev/sdb5           24576       65535       20480   83  Linux

#w 保存并退出;q 退出不保存

Command (m for help): w         #w  保存并退出
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

检查是否创建成功:

[root@oldboyedu ~]# fdisk -l|grep 'sdb[0-9]'
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
/dev/sdb1            2048       22527       10240   83  Linux
/dev/sdb2           22528      208895       93184    5  Extended
/dev/sdb5           24576       65535       20480   83  Linux
[root@oldboyedu ~]# 

删除fdisk创建的分区

[root@oldboyedu ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): d    #删除分区
Partition number (1,2,5, default 5): 5   #先删除逻辑分区
Partition 5 is deleted

Command (m for help): d
Partition number (1,2, default 2): 2
Partition 2 is deleted

Command (m for help): d
Selected partition 1
Partition 1 is deleted

Command (m for help): p
Disk /dev/sdb: 106 MB, 106954752 bytes, 208896 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x6e4c6459

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): w   #保存并退出
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

#创建1个使用磁盘所有容量的分区,并挂载到/mnt上※※※

第1个里程碑-创建分区

[root@oldboyedu ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p

Disk /dev/sdb: 106 MB, 106954752 bytes, 208896 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x6e4c6459

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-208895, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-208895, default 208895): 
Using default value 208895
Partition 1 of type Linux and of size 101 MiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

第2个里程碑-格式化 创建文件系统 mkfs make filesystem

[root@oldboyedu ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=6464 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=25856, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=855, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

第3个里程碑-挂载

[root@oldboyedu ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        18G  9.9G  8.0G  56% /
devtmpfs        2.0G     0  2.0G   0% /dev
tmpfs           2.0G     0  2.0G   0% /dev/shm
tmpfs           2.0G   12M  2.0G   1% /run
tmpfs           2.0G     0  2.0G   0% /sys/fs/cgroup
/dev/sda1       197M  105M   93M  54% /boot
tmpfs           394M     0  394M   0% /run/user/0
[root@oldboyedu ~]# mount /dev/sdb1 /mnt
[root@oldboyedu ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        18G  9.9G  8.0G  56% /
devtmpfs        2.0G     0  2.0G   0% /dev
tmpfs           2.0G     0  2.0G   0% /dev/shm
tmpfs           2.0G   12M  2.0G   1% /run
tmpfs           2.0G     0  2.0G   0% /sys/fs/cgroup
/dev/sda1       197M  105M   93M  54% /boot
tmpfs           394M     0  394M   0% /run/user/0
/dev/sdb1        98M  5.3M   93M   6% /mnt
[root@oldboyedu ~]# 

第4个里程碑-永久挂载

1./etc/rc.local

[root@oldboyedu ~]# file /etc/rc.d/rc.local
/etc/rc.d/rc.local: Bourne-Again shell script, ASCII text executable
[root@oldboyedu ~]# shell脚本,可执行

2./etc/fstab

/etc/fstab名一列表示的含义
[root@oldboyedu~]# cat /etc/fstab 
UUID=1e827748-cc43-4b1d-8bbc-9a85c239782d /            xfs         defaults        0    0
UUID=29a4ef75-7502-48df-b31c-c7c10264a7c8 /boot        xfs         defaults        0    0
UUID=60c45f5e-c78d-41fe-aa92-93d10de6def3 swap         swap        defaults        0    0
第1列:UUID或设备名称                                               
第2列:挂载点(入口)
第3列:文件系统类型
第4列:挂载参数
第5列:是否进行备份
第6列:是否开机自动检查            

查询设备上所使用的文件系统类型、UUID等信息(blkid)

[root@oldboyedu ~]# blkid
/dev/sda1: UUID="21560efb-d3ed-4970-9792-e8786cec18a8" TYPE="xfs" 
/dev/sda2: UUID="5a66c37c-1a78-4781-af81-1e7001bcae62" TYPE="swap" 
/dev/sda3: UUID="9c1f4cad-aa3f-412d-996e-22e3cf7c8dc1" TYPE="xfs" 
/dev/sdb1: UUID="3977bf63-261d-4312-a301-5b6a53a8e8cc" TYPE="xfs" 
/dev/sdc1: PARTLABEL="primary" PARTUUID="a8de4ec3-90c7-4877-9f24-14ff65229bb6" 
/dev/sr0: UUID="2018-11-25-23-54-16-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos" 
[root@oldboyedu ~]# 

添加要挂载的磁盘

[root@oldboyedu ~]# tail -1 /etc/fstab
/dev/sdb1                                 /data/mnt               xfs     defaults        0 0
[root@oldboyedu ~]# 

CentOS7中默认的是xfs
CentOS6 ext系列

2>parted创建1个主分区 10M

[root@oldboyedu ~]# parted /dev/sdc
(parted) p                                                                
Error: /dev/sdc: unrecognised disk label   #无法识别的 
Model: VMware, VMware Virtual S (scsi)                                    
Disk /dev/sdc: 107MB
Sector size (logical/physical): 512B/512B
Partition Table: unknown                   #未知
Disk Flags: 

(parted) mktable gpt                #创建磁盘分区表 MBR GPT                              
(parted) p                                                                
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 107MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start  End  Size  File system  Name  Flags

(parted) mkpart primary   0   10           #创建磁盘分区 ,设置磁盘大小                    
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? i                                                          
(parted) p                                                                
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 107MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name     Flags
 1      17.4kB  10.0MB  9983kB               primary

(parted) q                       #退出磁盘分区
Information: You may need to update /etc/fstab.

fdisk与parted的共同点和区别

fdisk parted
共同点 磁盘分区 磁盘分区
区别 支持2TB以内硬盘 大于2TB磁盘
支持MBR分区表 MBR GPT(支持更大的硬盘和更多的主分

primary 主分区
extended 扩展分区
logical 逻辑分区
partition 分区
partition table 分区表

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,546评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,224评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,911评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,737评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,753评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,598评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,338评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,249评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,696评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,888评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,013评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,731评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,348评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,929评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,048评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,203评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,960评论 2 355

推荐阅读更多精彩内容

  • 1 概述 本文通过对讲解了磁盘的基本概念,分区,创建文件系统,挂载,卸载等步骤进行讲解。使得用户能够创建基本的磁盘...
    ghbsunny阅读 1,517评论 0 0
  • 目录磁盘结构磁盘分区磁盘分区管理文件系统管理挂载 一、磁盘结构 (一)设备文件 设备文件:linux系统下一切皆文...
    哈喽别样阅读 1,000评论 0 0
  • 01. 磁盘分区操作步骤 购买了多块新磁盘 --- 真实使用磁盘 1) 进行创建阵列(raid) 将几块地皮整...
    白狐脸儿阅读 760评论 0 0
  • 时间:2018-05-24 姓名:魏文应 一、硬盘 硬盘的正常使用流程: sda磁盘.png 我们看 /dev/s...
    秋的懵懂阅读 2,311评论 0 1
  • CSS中的居中可分为水平居中和垂直居中。水平居中分为行内元素居中和块状元素居中两种情况,而块状元素又分为定宽块状元...
    何wife阅读 660评论 0 0