攻克Linux

27-磁盘管理(下)

2019-05-26  本文已影响35人  杨丶子
image
先去虚拟机添加2块0.1G的磁盘,重启服务器后生效。
image

然后看一下添加的磁盘

fdisk -l |grep 'sd[a-z]'
[09:24 root@oldboy ~]# fdisk -l |grep 'sd[a-z]'
Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648     2508799     1048576   82  Linux swap / Solaris
/dev/sda3         2508800    41943039    19717120   83  Linux
Disk /dev/sdb: 106 MB, 106954752 bytes, 208896 sectors
Disk /dev/sdc: 106 MB, 106954752 bytes, 208896 sectors

一.MBR

Master Boot Record 主引导记录

位置
磁盘的开头(0磁头 0磁道 1扇区)
0磁头 0磁道 1扇区 521字节前446字节
0头 0道 1扇区 521bytes

image

二.分区类型

image

主分区-primary

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

扩展分区-extended

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

逻辑分区-logical

需要先创建扩展分区
存放数据

三.分区命名规则

a.硬盘名字

1.sata/sas/scsi

/dev/sd??
按字母排列
第一块硬盘 /dev/sda
第二块硬盘 /dev/sdb

2.云服务器(阿里云)

/dev/vda
/dev/vdb

b.分区名字※

主分区或扩展分区
1-4
/dev/sda1 /dev/sda4

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

c.例子

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

四. fdisk分区和parted分区

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

\ fdisk分区


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

1.创建主分区

[15:35 root@oldboy ~]# 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): n   \\新建分区
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
        主分区p   
   e   extended     
        扩展分区e   
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): +10M
磁盘分区中最后1个扇区
Partition 1 of type Linux and of size 10 MiB is set
创建了分区号是1的10MB的分区
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: 0xa9b1cdab

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

2.创建扩展分区

Command (m for help): n   \\新建分区
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: 0xe5935118

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

3.创建逻辑分区并保存退出

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: 0xe5935118

   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

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

Calling ioctl() to re-read partition table.
Syncing disks.

4.查看

[10:42 root@oldboy ~]# ll /dev/sdb*
brw-rw---- 1 root disk 8, 16 Apr 30 10:42 /dev/sdb
brw-rw---- 1 root disk 8, 17 Apr 30 10:42 /dev/sdb1
brw-rw---- 1 root disk 8, 18 Apr 30 10:42 /dev/sdb2
brw-rw---- 1 root disk 8, 21 Apr 30 10:42 /dev/sdb5

如何删除分区:(接下题先把创建的删除掉)

Command (m for help): d  \\按从后往前删除

2.创建1个使用磁盘所有容量的分区 并挂载到/mnt上(临时挂载点)

1.第一个里程碑-创建分区

[11:20 root@oldboy ~]# 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): 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): 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: 0xe5935118

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048      208895      103424   83  Linux

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

Calling ioctl() to re-read partition table.
Syncing disks.

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

mkfs.xfs /dev/sdb1

[11:21 root@oldboy ~]# 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.第3个里程碑-挂载

[11:24 root@oldboy ~]# df -h  \\查看已挂载的磁盘
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        19G  2.3G   17G  13% /
devtmpfs        980M     0  980M   0% /dev
tmpfs           991M     0  991M   0% /dev/shm
tmpfs           991M  9.6M  981M   1% /run
tmpfs           991M     0  991M   0% /sys/fs/cgroup
/dev/sda1       197M  133M   64M  68% /boot
tmpfs           199M     0  199M   0% /run/user/0
[11:24 root@oldboy ~]# mount /dev/sdb1 /mnt/     \\挂载
[11:25 root@oldboy ~]# df -h      \\再次查看
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        19G  2.3G   17G  13% /
devtmpfs        980M     0  980M   0% /dev
tmpfs           991M     0  991M   0% /dev/shm
tmpfs           991M  9.6M  981M   1% /run
tmpfs           991M     0  991M   0% /sys/fs/cgroup
/dev/sda1       197M  133M   64M  68% /boot
tmpfs           199M     0  199M   0% /run/user/0
/dev/sdb1        98M  5.3M   93M   6% /mnt

挂载分区中的报错与排错

1.
Changes will remain in memory only, until you decide to write them.
你进行的修改默认存放在内存中,直到你决定保存这些修改的时候.
:wq 
Be careful before using the write command.
保存之前小心点!
Device does not contain a recognized partition table
设备    没有    包含     磁盘分区表。
DPT= disk  partition table  磁盘分区表

2.
mount /dev/sdb1 /mnt/
mount: /dev/sdb1 is already mounted or /mnt busy
                这个分区已经安装或 繁忙

3.
mkfs.xfs /dev/sdb1
mkfs.xfs: /dev/sdb1 appears to contain an existing filesystem (ext4).
          这个磁盘分区中 已经包含了1个文件系统(已经格式化过了)
mkfs.xfs: Use the -f option to force overwrite.
          如果强制格式化覆盖使用-f 

4.
[root@cuihua ~]# mount /dev/sdb1 /mnt
mount: /dev/sdb1 is write-protected, mounting read-only
                        写保护         安装只读
mount: unknown filesystem type '(null)'
       未知的文件系统类型(没有格式化)

5.
[root@guanggege ~]# mount  /dev/sdb1  /mnt/
mount: special device /dev/sdb1 does not exist
       特殊文件(设备文件)     不存在   

6.
[root@oldboyedu59 ~]# mkfs.xfs  /dev/sdb1 
mkfs.xfs: /dev/sdb1 contains a mounted filesystem
             sdb1磁盘分区 包含  1个已经挂载的文件系统 (磁盘分区已经挂载)

7.
Invalid partition type `e'
"e"是无效的分区类型 

4.第四个里程碑-永久挂载※

1./etc/rc.local

file /etc/rc.d/rc.local
/etc/rc.d/rc.local: Bourne-Again shell script, ASCII text executable     
                    shell脚本,可执行

2./etc/fstab 开机自动挂载

cat /etc/fstab 
UUID=8a44a07b-1b45-49c1-b6aa-c5ab81c717c8   /           xfs         defaults      0     0
UUID=2e59d872-2875-46bc-9e12-e59b9751ada1   /boot       xfs         defaults      0     0
UUID=13d82528-a2cc-484e-b468-9dfc25c36f5c   swap        swap        defaults      0     0
第一列.UUID或设备名称                   
第二列.挂载点(入口)     
第三列.文件系统类型      
第四列.挂载参数  
第五列.是否备份  
第六列.是否开机自启动检查

blkid     \\查询设备上所使用的文件系统类型、LABEL、UUID等信息
/dev/sda1: UUID="2e59d872-2875-46bc-9e12-e59b9751ada1" TYPE="xfs" 
/dev/sda2: UUID="13d82528-a2cc-484e-b468-9dfc25c36f5c" TYPE="swap" 
/dev/sda3: UUID="8a44a07b-1b45-49c1-b6aa-c5ab81c717c8" TYPE="xfs" 
/dev/sdb1: UUID="b6242409-4b32-4ff3-a46e-7dab26695139" TYPE="xfs" 
/dev/sr0: UUID="2018-11-25-23-54-16-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"     

为要挂载的磁盘添加一行 
tail -1 /etc/fstab 
/dev/sdb1                 /date               xfs     defaults    0   0  

CentOS7 默认的是xfs文件类型
CentOS6 5 默认 ext类型


\ parted(修改实时生效)


p   print   显示磁盘信息

1.分区表

1.创建分区表parted /dev/sdc

[12:46 root@oldboy ~]# parted  /dev/sdc 
GNU Parted 3.1
Using /dev/sdc
Welcome to GNU Parted! Type 'help' to view a list of commands.
(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 
(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. mkpart primary指定大小
(parted) mkpart primary 0 10  
Warning: The resulting partition is not properly aligned for best
performance.
Ignore/Cancel? i                                                          
(parted)                  

3.查看创建是否成功

(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

4.退出

(parted) q
Information: You may need to update /etc/fstab.   

5.显示分区信息

ll /dev/sdc*
brw-rw---- 1 root disk 8, 32 Apr 30 12:51 /dev/sdc
brw-rw---- 1 root disk 8, 33 Apr 30 12:51 /dev/sdc1   

上一篇下一篇

猜你喜欢

热点阅读