欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Linux-磁盘存储

程序员文章站 2024-03-08 10:54:28
...

磁盘存储

一.磁盘

3.5英寸磁盘:
Linux-磁盘存储

磁盘作用:存储数据,物理设备。

1.1 磁盘外部结构

正面板:产地,容量,日期,转数。品牌,电压,透气孔,条形码,六个螺丝,类型。
反面板:绿色控制电路板(芯片、电容、电阻、缓存作用)

Linux-磁盘存储

buffer:缓冲(写入缓冲区)
cache:缓存(读取缓冲区)

侧面:电源接口,跳线,数据接口(IDE,SATA,SAS,SCSI)

1.2 磁盘内部结构

Linux-磁盘存储

1:磁盘片:2-14张,每个面都可以放数据
2:主轴组件:轴承、马达电机
3:磁头组件:磁头、机械手臂、传动轴、前置控制电路、数据转换器、进步电机(控制磁头径向移动)
4:核心组件:磁头及盘片

1.3 磁盘存取数据原理:

磁盘读数据时:将磁盘上的磁粒子极性转换成电脉冲信号,然后通过数据转换器转换成电脑可以识别的数据。

磁盘写数据的原理:磁盘读写数据时,按照柱面来读写数据,可能先读一个盘面的某一个磁道的数据,读完之后,再向下读取相同磁盘不同盘面的数据。知道所有盘面相同磁道的数据被读取完毕,切换到下一个柱面,这个切换的过程叫寻道,寻道要靠步进电机控制,让磁头做移动了,这是机械运动。所以很慢。

1.4 磁盘的有关名词

disk 磁盘(网上卖的一块一块硬盘)
head 磁头
sectors 扇区
Track 磁道
Cylinder 柱面
Units 单区块(一个柱面的大小)
bolck 磁盘块
inode 索引节点

磁盘:盘面上以盘片圆心为中心向四周画不同半径的圆圈,这样的圆圈的轨迹就是磁道。
扇区:由盘片圆心向四周画直线,把不同磁道分隔成的一个个圆弧就是扇区(512bytes);
柱面:相同半径不同盘面的磁道由上向下组成的圆柱形轨迹就是柱面。

1.5 磁盘相关信息:

[aaa@qq.com ~]# fdisk -l

Linux-磁盘存储

1.6 磁盘分区

系统分区:主分区最多不超过4个。
分区:为什么要分区?(好比家里的房子,客厅,厨房,卧室。。。)
案例01
/dev/sdb1 安装软件。 /install_software
/dev/sdb2 存储软件。 /storage_software
/dev/sdb3 存储脚本。 /storage_scripts
/dev/sdb5 存储备份。 /storage_backup
/dev/sdb6 存储杂乱东西的。/storage_mess
Linux-磁盘存储

分区的分类:主分区和逻辑分区(先分区再格式化)

[aaa@qq.com ~]# fdisk -l /dev/sdb

Disk /dev/sdb: 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: 0x00000000
#进入分区界面
[aaa@qq.com ~]# 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 0xb9ba7f69.
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  #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): nn
Command action
   e   extended		#扩展分区(在扩展分区的基础上创建逻辑分区(1-64))
   p   primary partition (1-4)	#主分区(最多4个,包括扩展分区)
p	#选择创建主分区
Partition number (1-4): 1  #选择分区号
First cylinder (1-2610, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +2G		#分区的大小

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (263-2610, default 263): 
Using default value 263
Last cylinder, +cylinders or +size{K,M,G} (263-2610, default 2610): +2G

Command (m for help): p	#查看分区信息

Disk /dev/sdb: 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: 0xb9ba7f69

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         262     2104483+  83  Linux
/dev/sdb2             263         524     2104515   83  Linux

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (525-2610, default 525): 
Using default value 525
Last cylinder, +cylinders or +size{K,M,G} (525-2610, default 2610): +2G


Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
e	#选择创建扩展分区
Selected partition 4
First cylinder (787-2610, default 787):    
Using default value 787
Last cylinder, +cylinders or +size{K,M,G} (787-2610, default 2610): +12G

Command (m for help): p

Disk /dev/sdb: 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: 0xb9ba7f69

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         262     2104483+  83  Linux
/dev/sdb2             263         524     2104515   83  Linux
/dev/sdb3             525         786     2104515   83  Linux
/dev/sdb4             787        2354    12594960    5  Extended

#在扩展分区的基础上创建逻辑分区
Command (m for help): n
First cylinder (787-2354, default 787): 
Using default value 787
Last cylinder, +cylinders or +size{K,M,G} (787-2354, default 2354): +2G

Command (m for help): p  

Disk /dev/sdb: 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: 0xb9ba7f69

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         262     2104483+  83  Linux
/dev/sdb2             263         524     2104515   83  Linux
/dev/sdb3             525         786     2104515   83  Linux
/dev/sdb4             787        2354    12594960    5  Extended
/dev/sdb5             787        1048     2104483+  83  Linux
# 再建一个逻辑分区
Command (m for help): n   
First cylinder (1049-2354, default 1049): 
Using default value 1049
Last cylinder, +cylinders or +size{K,M,G} (1049-2354, default 2354): +10G

Command (m for help): p

Disk /dev/sdb: 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: 0xb9ba7f69

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         262     2104483+  83  Linux
/dev/sdb2             263         524     2104515   83  Linux
/dev/sdb3             525         786     2104515   83  Linux
/dev/sdb4             787        2354    12594960    5  Extended
/dev/sdb5             787        1048     2104483+  83  Linux
/dev/sdb6            1049        2354    10490413+  83  Linux
#保存并退出
Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
#使分区立即生效
[aaa@qq.com ~]# partprobe /dev/sdb
[aaa@qq.com ~]# 

这一步只是把分区做了(相当于房间号做完了,接下来要做装修)

格式化分区:

[aaa@qq.com ~]# mkfs.ext4 /dev/sdb1
[aaa@qq.com ~]# mkfs.ext3 /dev/sdb2
[aaa@qq.com ~]# mkfs.ext3 /dev/sdb3
[aaa@qq.com ~]# mkfs.ext3 /dev/sdb5
[aaa@qq.com ~]# mkfs.ext3 /dev/sdb6

Linux-磁盘存储

接下来该挂载了(相当于入住)
首先在/(根)目录下创建好对应的分区名

[aaa@qq.com ~]# cd /
[aaa@qq.com /]# mkdir /install_software
[aaa@qq.com /]# mkdir /storage_software
[aaa@qq.com /]# mkdir /storage_scripts
[aaa@qq.com /]# mkdir /storage_backup
[aaa@qq.com /]# mkdir /storage_mess

Linux-磁盘存储

挂载的方式(临时性)(永久性):

[aaa@qq.com /]# vi /etc/fstab
在原有文件的基础上追加这些;
/dev/sdb1 /install_software ext4 defaults 0 0
/dev/sdb2 /storage_software ext3 defaults 0 0
/dev/sdb3 /storage_scripts ext3 defaults 0 0
/dev/sdb5 /storage_backup ext3 defaults 0 0
/dev/sdb6 /storage_mess ext3 defaults 0 0

Linux-磁盘存储

再检查:

[aaa@qq.com /]# mount
/dev/sda2 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
/dev/sda5 on /private-data type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
vmware-vmblock on /var/run/vmblock-fuse type fuse.vmware-vmblock (rw,nosuid,nodev,default_permissions,allow_other)
gvfs-fuse-daemon on /root/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev)
/dev/sr0 on /media/CentOS_6.5_Final type iso9660 (ro,nosuid,nodev,uhelper=udisks,uid=0,gid=0,iocharset=utf8,mode=0400,dmode=0500)
[aaa@qq.com /]# mount -a  #让他生效
[aaa@qq.com /]# mount
/dev/sda2 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
/dev/sda5 on /private-data type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
vmware-vmblock on /var/run/vmblock-fuse type fuse.vmware-vmblock (rw,nosuid,nodev,default_permissions,allow_other)
gvfs-fuse-daemon on /root/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev)
/dev/sr0 on /media/CentOS_6.5_Final type iso9660 (ro,nosuid,nodev,uhelper=udisks,uid=0,gid=0,iocharset=utf8,mode=0400,dmode=0500)
/dev/sdb1 on /install_software type ext4 (rw)
/dev/sdb2 on /storage_software type ext3 (rw)
/dev/sdb3 on /storage_scripts type ext3 (rw)
/dev/sdb5 on /storage_backup type ext3 (rw)
/dev/sdb6 on /storage_mess type ext3 (rw)

检查每个磁盘的容量:

[aaa@qq.com /]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        20G  4.2G   15G  23% /
tmpfs           932M  224K  932M   1% /dev/shm
/dev/sda1       194M   34M  151M  19% /boot
/dev/sda5       174G  188M  165G   1% /private-data
/dev/sr0        4.2G  4.2G     0 100% /media/CentOS_6.5_Final
/dev/sdb1       2.0G   68M  1.9G   4% /install_software
/dev/sdb2       2.0G   68M  1.9G   4% /storage_software
/dev/sdb3       2.0G   68M  1.9G   4% /storage_scripts
/dev/sdb5       2.0G   68M  1.9G   4% /storage_backup
/dev/sdb6       9.9G  151M  9.3G   2% /storage_mess

查看一个目录的空间已使用的大小

[aaa@qq.com /]# du -sh /install_software
20K	/install_software

disk 缺点:

1.不支持2T以上的硬盘分区

2.不支持在线的扩容

一个磁盘想分6个分区
1p+1e,2p+1e,3p+1e:即3个主分区,一个扩展分区(3个逻辑分区)

1.7 磁盘分区方案

  1. 常规分区方案
    /boot 100M 主分区
    swap 内存的1-2倍 主分区
    /剩余的硬盘大小 主分区

  2. 数据库或存储:大量重要的数据空间
    /boot 100M 主分区
    / 50G-100G 主分区
    Swap 内存1-2倍 主分区
    其他的空间保留。作为扩展分区,以后根据自己的要求灵活划分

  3. 门户网站:
    /boot 100M 主分区
    swap 内存1-2倍 主分区
    其他的空间保留。 作为扩展分区,以后根据自己要求灵活划分

fdisk工具的缺点:超过2T的硬盘就不能使用了。part可以支持超过2T的硬盘大小分区。

问题:分区划分好了之后,每个分区大小可不可以再调整(在线的调整LVM就可以做到){相当于蜗居:中间租房(打隔断)}
注意的是磁盘分区必须是LVM格式的才可以支持在线的扩容。
在生产环境下面:LVM性能差,没必要!
生产环境下面硬件RAID(现实操作非常简单,但是原理很多也很复杂),组合多块磁盘。而不是LVM以及软RAID

1.8 改成LVM格式

[aaa@qq.com ~]# 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: 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: 0xb9ba7f69

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         262     2104483+  83  Linux
/dev/sdb2             263         524     2104515   83  Linux
/dev/sdb3             525         786     2104515   83  Linux
/dev/sdb4             787        2354    12594960    5  Extended
/dev/sdb5             787        1048     2104483+  83  Linux
/dev/sdb6            1049        2354    10490413+  83  Linux

Command (m for help): t		#更改分区格式
Partition number (1-6): 1
Hex code (type L to list codes): L

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           39  Plan 9          82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      3c  PartitionMagic  83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       40  Venix 80286     84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      41  PPC PReP Boot   85  Linux extended  c7  Syrinx         
 5  Extended        42  SFS             86  NTFS volume set da  Non-FS data    
 6  FAT16           4d  QNX4.x          87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS       4e  QNX4.x 2nd part 88  Linux plaintext de  Dell Utility   
 8  AIX             4f  QNX4.x 3rd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    50  OnTrack DM      93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 51  OnTrack DM6 Aux 94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       52  CP/M            9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 53  OnTrack DM6 Aux a0  IBM Thinkpad hi eb  BeOS fs        
 e  W95 FAT16 (LBA) 54  OnTrackDM6      a5  FreeBSD         ee  GPT            
 f  W95 Ext'd (LBA) 55  EZ-Drive        a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            56  Golden Bow      a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    5c  Priam Edisk     a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 61  SpeedStor       a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 63  GNU HURD or Sys ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    64  Novell Netware  af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 65  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST SmartSleep  70  DiskSecure Mult b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 75  PC/IX           bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 80  Old Minix       be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1
Hex code (type L to list codes): 8e    #改成LVM格式
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/sdb: 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: 0xb9ba7f69

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         262     2104483+  8e  Linux LVM
/dev/sdb2             263         524     2104515   83  Linux
/dev/sdb3             525         786     2104515   83  Linux
/dev/sdb4             787        2354    12594960    5  Extended
/dev/sdb5             787        1048     2104483+  83  Linux
/dev/sdb6            1049        2354    10490413+  83  Linux

Command (m for help): q

[aaa@qq.com ~]# partprobe /dev/sdb1		#使分区立即生效

1.9 parted 分区:

首先将分区类型改成GPT(GPT类型才支持超过2T以上的硬盘)

[aaa@qq.com ~]# parted /dev/sdc	#对磁盘/dev/sdc进行分区
GNU Parted 2.1
使用 /dev/sdc
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel gpt  #1.首先创建gpt分区表,如果以存在分区表也可以通过这儿的命令进行修改                                                  
(parted) print   #查看分区表信息
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdc: 21.5GB		#磁盘大小
Sector size (logical/physical): 512B/512B
Partition Table: gpt		#分区表是gpt格式

Number  Start  End  Size  File system  Name  标志

(parted) mkpart primary ext4	#2.输入mkpart新建分区
起始点? 0                                                  结束点? 2000                                             #通过起始点和结束点设置分区大小;                
警告: The resulting partition is not properly aligned for best performance.
忽略/Ignore/放弃/Cancel? i                                                
(parted) print      #查看分区信息                                                      
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdc: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End     Size    File system  Name     标志
 1      17.4kB  2000MB  2000MB               primary
#成功添加第一个分区
(parted) mkpart primary ext4      #3.创建第二个分区                                        
起始点? 2000                                                             
结束点? 4000                                                             
警告: The resulting partition is not properly aligned for best performance.
忽略/Ignore/放弃/Cancel? i                                                
(parted) p                                                                
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdc: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End     Size    File system  Name     标志
 1      17.4kB  2000MB  2000MB               primary
 2      2000MB  4000MB  2000MB               primary

(parted) mkpart primary ext4	#3.创建一个新的分区
起始点? 4000                                                             
结束点? 6000                                                             
(parted) mkpart logical 6000 20000
(parted) p                                                                
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdc: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End     Size    File system  Name     标志
 1      17.4kB  2000MB  2000MB               primary
 2      2000MB  4000MB  2000MB               primary
 3      4000MB  6000MB  2000MB               primary
 4      6000MB  20.0GB  14.0GB               logical

(parted) q       #保存退出                                                         
信息: You may need to update /etc/fstab.                                  
[aaa@qq.com dev]# mkfs.ext4 /dev/sdc1   #格式化分区
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
122160 inodes, 488277 blocks
24413 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=503316480
15 block groups
32768 blocks per group, 32768 fragments per group
8144 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912

正在写入inode表: 完成                            
Creating journal (8192 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 21 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[aaa@qq.com dev]# mkfs.ext4 /dev/sdc2
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
122160 inodes, 488281 blocks
24414 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=503316480
15 block groups
32768 blocks per group, 32768 fragments per group
8144 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912

正在写入inode表: 完成                            
Creating journal (8192 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 35 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[aaa@qq.com dev]# mkfs.ext4 /dev/sdc3
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
122160 inodes, 488192 blocks
24409 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=503316480
15 block groups
32768 blocks per group, 32768 fragments per group
8144 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912

正在写入inode表: 完成                            
Creating journal (8192 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 21 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[aaa@qq.com dev]# mkfs.ext4 /dev/sdc4
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
855120 inodes, 3417980 blocks
170899 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=3502243840
105 block groups
32768 blocks per group, 32768 fragments per group
8144 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208

正在写入inode表: 完成                            
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 36 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[aaa@qq.com dev]# 
[aaa@qq.com dev]# 
#开始进行挂载,和fdisk挂载方式相同
[aaa@qq.com dev]# vi /etc/fstab #在编辑器内把内容加入

#开始挂载
#创建好分区挂载的目录,要与/etc/fstab内容相同
[aaa@qq.com /]# mkdir sdc
[aaa@qq.com /]# cd sdc
[aaa@qq.com sdc]# mkdir sdc1
[aaa@qq.com sdc]# mkdir sdc2
[aaa@qq.com sdc]# mkdir sdc3
[aaa@qq.com sdc]# mkdir sdc4
[aaa@qq.com sdc]# exit
[aaa@qq.com ~]# vi /etc/fastable
[aaa@qq.com ~]# ls
anaconda-ks.cfg  install.log         公共的  视频  文档  音乐
client.py        install.log.syslog  模板    图片  下载  桌面
[aaa@qq.com ~]# vi /etc/fstab
[aaa@qq.com ~]# mount -a
[aaa@qq.com ~]# cd /sdc
[aaa@qq.com sdc]# ls
sdc1  sdc2  sdc3  sdc4			#已经可以使用
[aaa@qq.com sdc]# cd sdc1
[aaa@qq.com sdc1]# mkdir sdc1
[aaa@qq.com sdc1]# touch a.txt
[aaa@qq.com sdc1]# ls
a.txt  lost+found  sdc1

1.10 LVM

为什么要LVM,(分区划分好了,大小也就固定了)存储容量可以在线的扩展和缩小。
LVM(逻辑卷管理)。

三个名词:
PV(physical volume):物理卷(就是物理的磁盘分区)
VG(volume group):卷组(就是PV的组合)可以将VG理解为一个仓库
LV(逻辑分区):从仓库里面划出可以逻辑分区。
Linux-磁盘存储
Linux-磁盘存储

第一步:fdisk做如下4个分区
Linux-磁盘存储

第二步:把格式配置为支持LVM的8e格式

[aaa@qq.com /]# fdisk /dev/sdd

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): t
Partition number (1-5): 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): t
Partition number (1-5): 2
Hex code (type L to list codes): 8e
Changed system type of partition 2 to 8e (Linux LVM)

Command (m for help): t
Partition number (1-5): 3
Hex code (type L to list codes): 8e
Changed system type of partition 3 to 8e (Linux LVM)

Command (m for help): t
Partition number (1-5): 4	#扩展分区不能修改
Hex code (type L to list codes): 8e
You cannot change a partition into an extended one or vice versa
Delete it first.

Command (m for help): t
Partition number (1-5): 5
Hex code (type L to list codes): 8e
Changed system type of partition 5 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/sdd: 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: 0xb139f1e0

   Device Boot      Start         End      Blocks   Id  System
/dev/sdd1               1         654     5253223+  8e  Linux LVM
/dev/sdd2             655        1308     5253255   8e  Linux LVM
/dev/sdd3            1309        1962     5253255   8e  Linux LVM
/dev/sdd4            1963        2610     5205060    5  Extended
/dev/sdd5            1963        2610     5205028+  8e  Linux LVM

Command (m for help): 

第三步:将这些分区转化成物理卷

[aaa@qq.com /]# pvcreate /dev/sdd1 /dev/sdd2 /dev/sdd3 /dev/sdd5
  dev_is_mpath: failed to get device for 8:49
  Can't open /dev/sdd1 exclusively.  Mounted filesystem?  #挂载好了的不能直接转化
  dev_is_mpath: failed to get device for 8:50
  Can't open /dev/sdd2 exclusively.  Mounted filesystem?
  dev_is_mpath: failed to get device for 8:51
  Can't open /dev/sdd3 exclusively.  Mounted filesystem?
  dev_is_mpath: failed to get device for 8:53
  Can't open /dev/sdd5 exclusively.  Mounted filesystem?

先取消挂载;

[aaa@qq.com /]# umount /dev/sdd1	#取消挂载
[aaa@qq.com /]# umount /dev/sdd2
[aaa@qq.com /]# umount /dev/sdd3
[aaa@qq.com /]# umount /dev/sdd5
[aaa@qq.com /]# pvcreate /dev/sdd1 /dev/sdd2 /dev/sdd3 /dev/sdd5
  Physical volume "/dev/sdd1" successfully created
  Physical volume "/dev/sdd2" successfully created
  Physical volume "/dev/sdd3" successfully created
  Physical volume "/dev/sdd5" successfully created
#再次添加,成功!!!!

查看LVM

[aaa@qq.com /]# pvdisplay 
  "/dev/sdd1" is a new physical volume of "5.01 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdd1
  VG Name               
  PV Size               5.01 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               4Jp4ln-V2Fv-l64t-thX3-eElm-2yf0-RBHCSX
   
  "/dev/sdd2" is a new physical volume of "5.01 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdd2
  VG Name               
  PV Size               5.01 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               2FuILS-cgKw-GijK-10pj-dOUx-3lad-gAziC8
   
  "/dev/sdd3" is a new physical volume of "5.01 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdd3
  VG Name               
  PV Size               5.01 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               q4YTeq-4xFQ-n17Y-8aGd-rbil-YZFf-Vr3uUR
   
  "/dev/sdd5" is a new physical volume of "4.96 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdd5
  VG Name               
  PV Size               4.96 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               dO6ZqA-uQeM-Ujzs-ldlX-r9D4-tMxN-cQNhlk

第四步:创建卷组并将物理卷加入到卷组中

[aaa@qq.com /]# vgcreate disk_pools /dev/sdd1 /dev/sdd2 /dev/sdd3 /dev/sdd5
  Volume group "disk_pools" successfully created
[aaa@qq.com /]# vgdisplay 
  --- Volume group ---
  VG Name               disk_pools
  System ID             
  Format                lvm2
  Metadata Areas        4
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                4
  Act PV                4
  VG Size               19.98 GiB
  PE Size               4.00 MiB
  Total PE              5116
  Alloc PE / Size       0 / 0   
  Free  PE / Size       5116 / 19.98 GiB
  VG UUID               KDOse7-HisC-jp8V-oWMO-dIbm-Wbsq-bra9qM

第五步:利用卷组创建逻辑分区

[aaa@qq.com /]# lvcreate -L 5G -n software disk_pools
  Logical volume "software" created
[aaa@qq.com /]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/disk_pools/software
  LV Name                software
  VG Name                disk_pools
  LV UUID                WRrVAG-lB8J-M1r0-MxIj-swWo-SBcC-0Gue6Z
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2020-08-19 18:32:02 +0800
  LV Status              available
  # open                 0
  LV Size                5.00 GiB
  Current LE             1280
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
[aaa@qq.com /]# lvcreate -L 5G -n web disk_pools
  Logical volume "web" created
[aaa@qq.com /]# lvcreate -L 5G -n script disk_pools
  Logical volume "script" created
[aaa@qq.com /]# lvcreate -L 4G -n database disk_pools
  Logical volume "database" created

第六步:查看我们生成的LV逻辑分区和格式

第七步:
mkfs.ext4 /dev/mapper/disk_pools-software
mkfs.ext4 /dev/mapper/disk_pools-database
mkfs.ext4 /dev/mapper/disk_pools-web
mkfs.ext4 /dev/mapper/disk_pools-script

第八步:创建挂载目录并挂载
[aaa@qq.com ~]# mkdir /software
[aaa@qq.com ~]# mkdir /database
[aaa@qq.com ~]# mkdir /web
[aaa@qq.com ~]# mkdir /scripts

[aaa@qq.com ~]# cat /etc/fstab
/dev/mapper/disk_pools-software /software ext4 defaults 0 0
/dev/mapper/disk_pools-database /database ext4 defaults 0 0
/dev/mapper/disk_pools-web /web ext4 defaults 0 0
/dev/mapper/disk_pools-script /scripts ext4 defaults 0 0

[aaa@qq.com ~]# mount -a
[aaa@qq.com ~]# mount
/dev/sda2 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
/dev/sda5 on /data type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
gvfs-fuse-daemon on /root/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev)
/dev/mapper/disk_pools-software on /software type ext4 (rw)
/dev/mapper/disk_pools-database on /database type ext4 (rw)
/dev/mapper/disk_pools-web on /web type ext4 (rw)
/dev/mapper/disk_pools-script on /scripts type ext4 (rw)
[aaa@qq.com ~]# cd /software
[aaa@qq.com software]# touch 1.txht
[aaa@qq.com software]# ls
1.txht lost+found

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Elef5LQe-1599704139355)(C:\Users\ASUS\AppData\Roaming\Typora\typora-user-images\1597835269803.png)]

当LV不够用了。/database这个目录不够用了。

Linux-磁盘存储

因为/database是一个目录,没办法直接对这个目录增加容量,这里必须对目录所对应的逻辑分区增加容量:
[aaa@qq.com ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/disk_pools-software 5.0G 138M 4.6G 3% /software
/dev/mapper/disk_pools-database 5.0G 138M 4.6G 3% /database
/dev/mapper/disk_pools-web 5.0G 138M 4.6G 3% /web
/dev/mapper/disk_pools-script 4.0G 136M 3.7G 4% /scripts
[aaa@qq.com ~]# lvextend -L +100M /dev/mapper/disk_pools-database #增加容量
Extending logical volume database to 5.10 GiB
Logical volume database successfully resized
[aaa@qq.com ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/disk_pools-software 5.0G 138M 4.6G 3% /software
/dev/mapper/disk_pools-database 5.0G 138M 4.6G 3% /database
/dev/mapper/disk_pools-web 5.0G 138M 4.6G 3% /web
/dev/mapper/disk_pools-script 4.0G 136M 3.7G 4% /scripts
[aaa@qq.com ~]# resize2fs /dev/mapper/disk_pools-database #同步文件使系统生效
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/disk_pools-database is mounted on /database; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/mapper/disk_pools-database to 1336320 (4k) blocks.
The filesystem on /dev/mapper/disk_pools-database is now 1336320 blocks long.

[aaa@qq.com ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/disk_pools-software 5.0G 138M 4.6G 3% /software
/dev/mapper/disk_pools-database 5.1G 138M 4.7G 3% /database
/dev/mapper/disk_pools-web 5.0G 138M 4.6G 3% /web
/dev/mapper/disk_pools-script 4.0G 136M 3.7G 4% /scripts

参考:
当卷组不够用的情况下,如何扩大卷组
重新插入一个硬盘,在上面建分区。步骤省略。并将创建好的分区加入到已经存在的卷组vgdata中。通过pvs命令查看是否成功。

Linux-磁盘存储

案例:添加一块100G的硬盘,做两个分区。分别加到LVM里面来,从而增加容量。
添加硬盘,增加(PV,VG)从而增加LV的容量(LV的容量你就可以任意去做规划)

1.做如下的分区:

[aaa@qq.com ~]# fdisk -l /dev/sdb

Disk /dev/sdb: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 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: 0x9639ffed

Device Boot Start End Blocks Id System
/dev/sdb1 1 6528 52436128+ 8e Linux LVM
/dev/sdb2 6529 13054 52420095 8e Linux LVM

2.加入卷组
[aaa@qq.com ~]# partprobe /dev/sdb
[aaa@qq.com ~]# vgextend disk_pools /dev/sdb1 /dev/sdb2 #加入卷组命令。
No physical volume label read from /dev/sdb1
Physical volume /dev/sdb1 not found
No physical volume label read from /dev/sdb2
Physical volume /dev/sdb2 not found
Physical volume “/dev/sdb1” successfully created #成功
Physical volume “/dev/sdb2” successfully created #成功
Volume group “disk_pools” successfully extended #扩展
[aaa@qq.com ~]# vgdisplay
— Volume group —
VG Name disk_pools
System ID
Format lvm2
Metadata Areas 6
Metadata Sequence No 7
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 4
Open LV 4
Max PV 0
Cur PV 6
Act PV 6
VG Size 119.98 GiB
PE Size 4.00 MiB
Total PE 30714
Alloc PE / Size 4889 / 19.10 GiB
Free PE / Size 25825 / 100.88 GiB
VG UUID DNxVLf-Hgtq-ucrZ-vyCf-OxeF-EgkD-eyPIYS

3.要先将之后添加的磁盘做格式化处理
mkfs.ext4 /dev/sdc1
mkfs.ext4 /dev/sdc2
4.向原来磁盘空间不够的分区扩大容量
[aaa@qq.com ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/disk_pools-software 5.0G 138M 4.6G 3% /software
/dev/mapper/disk_pools-database 5.0G 138M 4.6G 3% /database
/dev/mapper/disk_pools-web 5.0G 138M 4.6G 3% /web
/dev/mapper/disk_pools-script 4.0G 136M 3.7G 4% /scripts
[aaa@qq.com ~]# lvextend -L +50G /dev/mapper/disk_pools-database #增加容量
Extending logical volume database to 55.10 GiB
Logical volume database successfully resized
[aaa@qq.com ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/disk_pools-software 5.0G 138M 4.6G 3% /software
/dev/mapper/disk_pools-database 5.1G 138M 4.6G 3% /database
/dev/mapper/disk_pools-web 5.0G 138M 4.6G 3% /web
/dev/mapper/disk_pools-script 4.0G 136M 3.7G 4% /scripts
[aaa@qq.com ~]# resize2fs /dev/mapper/disk_pools-database #同步文件使系统生效
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/disk_pools-database is mounted on /database; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/mapper/disk_pools-database to 1336320 (4k) blocks.
The filesystem on /dev/mapper/disk_pools-database is now 1336320 blocks long.

[aaa@qq.com ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/disk_pools-software 5.0G 138M 4.6G 3% /software
/dev/mapper/disk_pools-database 55.1G 138M 4.7G 3% /database
/dev/mapper/disk_pools-web 5.0G 138M 4.6G 3% /web
/dev/mapper/disk_pools-script 4.0G 136M 3.7G 4% /scripts

1.11、quota磁盘配额管理

1、磁盘配额的概念

quota磁盘配额功能只在指定的文件系统(分区)内有效,未设置配额的文件系统不受限制。
quota针对指定的用户账号、组账号进行限制,其他用户或组不受影响。
磁盘配额可以进行两方面的限制:磁盘容量、文件数量。
磁盘容量:限制用户能够使用的磁盘数据块(block)大小,也就是限制磁盘空间大小,默认单位为KB。
文件数量:限制用户能够拥有的文件个数。
磁盘配额的限制方法分为软限制和硬限制两种。
软限制:指设定一个软性的配额数值(如500MB磁盘空间、200个文件),在固定的宽期限(默认为7天)内允许暂时超过这个限制,但系统会给出警告信息。
硬限制:指设定一个硬性的配额数值,而且绝对禁止用户超过该限值。硬限制的配额值应大于相应的软限制值,否则软限制值将失效。

2、设置磁盘配额

下面以硬盘分区“/dev/sdb1”为例,先将其挂载到“/data”目录下,然后在文件系统中实现磁盘配额:

1、启用quota磁盘配额

首先要通过修改配置文件“/etc/fstab”的方式启用quota磁盘配额。
[aaa@qq.com var]# vim /etc/fstab
/dev/sdb1 /data ext4 defaults,usrquota,grpquota 0 0
将该文件系统重新挂载
[aaa@qq.com ~]# mount -o remount /data
执行 mount 命令查看已经挂载的文件系统,检查是否已经启用了 usrquota 和 grpquota 功能
[aaa@qq.com ~]# mount | grep sdb1
/dev/sdb1 on /data type ext4 (rw,usrquota,grpquota)

2、生成配额文件

quotacheck命令可以对文件系统进行磁盘配额检测,发现哪些文件系统启用了磁盘配额功能,并在这些文件系统中生成配额文件aquota.user和aquota.group
[aaa@qq.com ~]# quotacheck -cvug /data
相关选项的作用:
-c,创建配额文件。
-v,显示详细信息。
-u,检查用户配额信息,创建 aquota.user 文件。
-g,检查组配额信息,创建 aquota.group 文件。
[aaa@qq.com ~]# setenforce 0 将 SELinux 设为许可模式
[aaa@qq.com ~]# quotacheck -cvug /data ’再次执行 quotacheck 命令
[aaa@qq.com ~]# ls /data ’查看生成的配额文件
aquota.group aquota.user lost+found

3、编辑用户和组账号的配额设置

编辑用户的配额设置:
使用edquota命令结合“-u”,"-g"选项可用于编辑用户或组的配额设置。
创建financial组,创建Jerry,将financial指定为Jerry的基本组。
[aaa@qq.com ~]# edquota -u jerry ’设置用户 jerry 的磁盘配额

Linux-磁盘存储

Filesystem:表示本行配置对应的文件系统(分区),即配额的作用范围。
blocks:表示当前已使用的磁盘容量,默认单位为KB。该值由edquota程序自动计算生成。
soft:第 3 列中的 soft 对应为磁盘容量的软限制数值,默认单位为 KB;
第 6 列中的 soft 对应为文件数量的软限制数值位为个。
hard:第 4 列中的 hard 对应为磁盘容量的硬限制数值,默认单位为 KB; ,
第 7 列中的 hard 对应为文件数量的硬限制数值,默认单位为个;
inodes:表示当前已拥有的文件数量。该值由edquota程序自动计算生成。

编辑组的配额设置
[aaa@qq.com ~]# edquota -g financial ’设置 financial 组的磁盘配额

Linux-磁盘存储

配额设置仅对基本组生效。如用户Jerry所属的基本组是“financial”,所属的附加组是“technology”那么只有针对“financial”组设置的配额才对Jerry有效,而针对“technology”组设置的配额则对Jerry没有限制。

4、**磁盘配额

[aaa@qq.com ~]# quotaon -ugv /data ’**“/data”文件系统的用户、组配额
/dev/sdb1 [/data]: group quotas turned on
/dev/sdb1 [/data]: user quotas turned on
相关选项的作用:
-u,**用户磁盘配额。
-g,**组磁盘配额。
-v,显示详细信息。

5、验证并查看磁盘配额

验证磁盘配额功能:
下面使用受配额限制的用户账号(Jerry)登录Linux系统,并向应用了配额的文件系统进行复杂文件等写操作,测试所设置磁盘配额项是否有效。为了方便测试,将用户jery的基本组设为financial,对用户和组的磁盘配额功能一并进行测试。
用dd命令生成指定大小的测试文件
从设备文件/dev/zero中复制数据到/home/jerry/test文件,读取210个大小为1MB的数据块。
[aaa@qq.com ~]# dd if=/dev/zero of=/mnt/data bs=1KB count=120
记录了 210+0 的读入
记录了 210+0 的写出
220200960 字节(220 MB)已复制,13.3504 秒,16.5 MB/秒
[aaa@qq.com ~]# ll -h /home/jerry
总用量 211M
-rw-r–r--. 1 root root 210M 11 月 6 10:49 test

开放 data/的写入权限
[aaa@qq.com ~]#chmod 777 /data

切换到 jerry 用户的身份进行测试,磁盘配额功能验证成功

Linux-磁盘存储

查看用户或分区的配额使用情况
使用 quota 命令结合“-u” 、“-g”选项分别查看指定用户和组的配额使用情况

使用 quota 命令结合“-u” 、“-g”选项分别查看指定用户和组的配额使用情况

Linux-磁盘存储

执行 repquota /data 查看/data 文件系统的配额使用情况报告

Linux-磁盘存储

6、关闭磁盘配额

利用quotaoff -vug 命令关闭磁盘配额

Linux-磁盘存储

7、磁盘配额小结

通过 quota 磁盘配额可以方便地对单个用户或用户组的可用磁盘空间进行限制,使磁盘管理工作具有 更大的灵活性。
在生产环境中具体应用时还应注意,quota 是以每一个使用者,每一个文件系统为基础的,它不能跨 文件系统对用户做出了如指掌,如果使用者可能在超过一个以上的系统中建立文件,那么必须在每一个文 件系统上分别设定 quota 配额。
另外,由于 root 用户在 Linux 环境中具有至高无上的权限,所以无法对 root 用户设置配额。

相关标签: 学习笔记 linux