磁盘管理

LVM概念简述

‌LVM(Logical Volume Manager)是Linux环境下对磁盘分区进行管理的一种机制,它通过在硬盘和分区之上建立一个逻辑层,提高了磁盘分区管理的灵活性‌‌。

LVM的主要目的是通过逻辑卷的方式对磁盘进行动态管理,使得磁盘空间的分配和管理更加灵活和高效‌。

LVM的基本概念和组成部分

  1. ‌物理卷(PV)‌:物理卷是LVM管理的最底层存储单元,可以是实际的物理硬盘或硬盘分区。每个物理卷被划分为多个物理范围(PE),PE是LVM寻址的最小单位‌。
  2. ‌卷组(VG)‌:卷组由一个或多个物理卷组成,相当于一个存储池。卷组中的空间可以被划分为多个逻辑范围(LE),用于创建逻辑卷‌。
  3. ‌逻辑卷(LV)‌:逻辑卷建立在卷组之上,用于存储数据。逻辑卷的大小可以动态调整,而不会丢失数据‌。

LVM的工作原理

LVM通过将底层的物理硬盘封装起来,以逻辑卷的方式呈现给上层应用。这种方式使得磁盘管理更加灵活,例如可以动态地增加或减少逻辑卷的空间,而不需要重新格式化或移动数据‌。LVM的最大特点是其动态管理能力,允许在不丢失数据的情况下调整逻辑卷的大小,并且可以方便地添加新的物理硬盘到系统中‌。

LVM的优点和应用场景

  1. ‌灵活的存储管理‌:LVM允许动态调整逻辑卷的大小,无需重新格式化或移动数据‌。

  2. ‌高效的存储利用‌:通过将多个物理硬盘整合为一个卷组,LVM可以提高存储资源的利用率和管理的便捷性‌。

  3. ‌易于扩展‌:当需要增加存储容量时,只需向卷组中添加新的物理硬盘,而不需要改变现有的逻辑卷‌

Raid详细解说

奇偶校验信息和相对应的数据分别存储于不同的磁盘上,其中任意N-1块磁盘上都存储完整的数据, 也就是说有相当于一块磁盘容量的空间用于存储奇偶校验信息。 因此当RAID5的一个磁盘发生损坏后,不会影响数据的完整性,从而保证了数据安全。 当损坏的磁盘被替换后,RAID还会自动利用剩下奇偶校验信息去重建此磁盘上的数据,来保持RAID5的高可靠性。 做raid 5阵列所有磁盘容量必须一样大,当容量不同时,会以最小的容量为准。 最好硬盘转速一样,否则会影响性能,而且可用空间=磁盘数n-1,其中有一块是专门用来校验的, 在存储数据的时候,校验盘里面是不会被存入数据的 RAID 0 条带化,增加速度(最少2块盘) RAID 1 镜像,增加数据安全(最少2块盘) RAID 5 奇偶校验 既增加速度,又增加安全性(最少3块盘)

磁盘相关命令速查

  • 查看磁盘和磁盘分区: sudo fdisk -l [设备名]
# -l 后不加设备, 会列出所有磁盘和分区信息
[root@localhost ~]# fdisk -l
Disk /dev/sda: 17.2 GB, 17179869184 bytes
255 heads, 63 sectors/track, 2088 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: 0x00018d63

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      102400   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              13         274     2097152   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3             274        2089    14576640   83  Linux

Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 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

# 指定设备
[root@localhost ~]# fdisk -l /dev/sda
Disk /dev/sda: 17.2 GB, 17179869184 bytes
255 heads, 63 sectors/track, 2088 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: 0x00018d63

Device Boot         Start         End      Blocks   Id  System
/dev/sda1   *           1          13      102400   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              13         274     2097152   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3             274        2089    14576640   83  Linux可以看到刚才阿铭加的一块磁盘 /dev/sdb 的信息。
  • 分区工具工作台 sudo fdisk 设备名
czhn@czhn:~$ sudo fdisk /dev/nvme0n1

Welcome to fdisk (util-linux 2.37.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

This disk is currently in use - repartitioning is probably a bad idea.
It's recommended to umount all file systems, and swapoff all swap
partitions on this disk.

# 控制台帮助, 列出了所有的操作
Command (m for help): m

Help:

  GPT
   M   enter protective/hybrid MBR

  Generic
   d   delete a partition 删除一个分区
   F   list free unpartitioned space 列出未分区的空间
   l   list known partition types 列出已知的分区类型
   n   add a new partition 添加一个新的分区
   p   print the partition table 打印分区表(也就是查看已分区的信息)
   t   change a partition type 修改分区类型
   v   verify the partition table 验证分区表
   i   print information about a partition 查看分区信息

  Misc
   m   print this menu
   x   extra functionality (experts only) 

  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table
  • 查看文件系统挂载详情: df -hT
czhn@czhn:~$ df -hT  # 加T参数会添加Type列 
Filesystem     Type   Size  Used Avail Use% Mounted on
tmpfs          tmpfs  3.2G  3.6M  3.2G   1% /run
/dev/sda4      ext4    75G   22G   50G  31% /
tmpfs          tmpfs   16G  1.2M   16G   1% /dev/shm
tmpfs          tmpfs  5.0M     0  5.0M   0% /run/lock
/dev/sda3      ext4   2.0G  246M  1.6G  14% /boot
/dev/sda1      vfat   1.1G  6.1M  1.1G   1% /boot/efi
/dev/nvme0n1p1 ext4   458G   88G  347G  21% /data
tmpfs          tmpfs  3.2G  4.0K  3.2G   1% /run/user/1000
  • 临时挂载和卸载
umask 000 # 临时设置umask为000 让用户可写
iocharset=gb2312 codepage=936 # 临时设置编码为gb2312 如果挂载后里面乱码
mount –o umask=000, iocharset=gb2312 codepage=936 /dev/sda4 /data
umount /data
  • 查看目录大小 du

如果不加参数, 他会便利所有的目录文件然后输出出来,目录小还好, 如果目录大没法看

du -sh /data  # 所有文件大小加起来 显示 /data 这个目录已使用的空间
du -sh /data/*  # 所有文件大小加起来 显示 /data 下的每个目录已使用的空间

案例实践

  1. 查看、分区、格式化、挂载一块磁盘到/data

# 1. 查看所有硬盘
czhn@czhn:~$ sudo fdisk -l|grep /dev/
Disk /dev/loop0: 63.95 MiB, 67051520 bytes, 130960 sectors
Disk /dev/loop1: 87.04 MiB, 91267072 bytes, 178256 sectors
Disk /dev/loop2: 38.83 MiB, 40714240 bytes, 79520 sectors
Disk /dev/loop3: 44.45 MiB, 46604288 bytes, 91024 sectors
Disk /dev/loop4: 89.4 MiB, 93745152 bytes, 183096 sectors
Disk /dev/loop5: 63.75 MiB, 66842624 bytes, 130552 sectors
# 目标分区
Disk /dev/nvme0n1: 465.76 GiB, 500107862016 bytes, 976773168 sectors
Disk /dev/sda: 111.79 GiB, 120034123776 bytes, 234441648 sectors
/dev/sda1      2048   2203647   2201600    1G EFI System
/dev/sda2   2203648  69312511  67108864   32G Linux swap
/dev/sda3  69312512  73506815   4194304    2G Linux filesystem
/dev/sda4  73506816 234438655 160931840 76.7G Linux filesystem
Disk /dev/sdb: 931.51 GiB, 1000204886016 bytes, 1953525168 sectors

# 2. 进入fdisk 工作台 进行分区
# 2.1 nvme协议硬盘分区
czhn@czhn:~$ sudo fdisk /dev/nvme0n1

Welcome to fdisk (util-linux 2.37.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 number (1-128, default 1):  # 默认直接回车
First sector (34-976773134, default 2048): # 默认直接回车
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-976773134, default 976773134):  # 默认直接回车

Created a new partition 1 of type 'Linux filesystem' and of size 465.8 GiB.
Partition #1 contains a vfat signature.

Do you want to remove the signature? [Y]es/[N]o: Y # 必须输入大写Y

The signature will be removed by a write command.


# 2.2 机械硬盘或固态硬盘分区 sdx 硬盘的分区
czhn@czhn:~$ sudo fdisk /dev/sdb

Welcome to fdisk (util-linux 2.37.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 (container for logical partitions)
# 这里选p主分区, 要注意主分区只能创建4个,如果要创建更多的分区 最多创建3个主分区, 然后创建扩展分区
Select (default p): 
# 这里默认就行了
Partition number (1-4, default 1): 
# # 这里是起始扇区,默认2048, 如果你输入其他值那么无非就是浪费一些空间。
First sector (2048-1953525167, default 2048): 
# 这个加号很重要没有这个加号会报错“Value out of range.”
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-1953525167, default 1953525167): +500G 

Created a new partition 1 of type 'Linux' and of size 500 GiB.

Command (m for help): p # 用p命令查看分区信息
Disk /dev/sdb: 931.51 GiB, 1000204886016 bytes, 1953525168 sectors
Disk model: WDC WD10EZEX-21W
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0xd8c6d598

Device     Boot Start        End    Sectors  Size Id Type
/dev/sdb1        2048 1048578047 1048576000  500G 83 Linux

# 这是用i命令查看分区信息
Command (m for help): i
Selected partition 1
         Device: /dev/sdb1
          Start: 2048
            End: 1048578047
        Sectors: 1048576000
      Cylinders: 65271
           Size: 500G
             Id: 83
           Type: Linux
    Start-C/H/S: 0/32/33
      End-C/H/S: 758/245/63

# 3.  w 命令写入保存
Command (m for help): w  # 这里用w命令进行写入保存
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

# 4. 重新查看分区信息
czhn@czhn:~$ sudo fdisk -l|grep /dev/
Disk /dev/loop0: 63.95 MiB, 67051520 bytes, 130960 sectors
Disk /dev/loop1: 87.04 MiB, 91267072 bytes, 178256 sectors
Disk /dev/loop2: 38.83 MiB, 40714240 bytes, 79520 sectors
Disk /dev/loop3: 44.45 MiB, 46604288 bytes, 91024 sectors
Disk /dev/loop4: 89.4 MiB, 93745152 bytes, 183096 sectors
Disk /dev/loop5: 63.75 MiB, 66842624 bytes, 130552 sectors
Disk /dev/nvme0n1: 465.76 GiB, 500107862016 bytes, 976773168 sectors
# 已分区 这里对比  第一步的时 Disk /dev/nvme0n1: 465.76 GiB, 500107862016 bytes, 976773168 sectors 
# 可以看到在 /dev/nvme0n1 后面添加了一个p1 分区
/dev/nvme0n1p1  2048 976773134 976771087 465.8G Linux filesystem
Disk /dev/sda: 111.79 GiB, 120034123776 bytes, 234441648 sectors
/dev/sda1      2048   2203647   2201600    1G EFI System
/dev/sda2   2203648  69312511  67108864   32G Linux swap
/dev/sda3  69312512  73506815   4194304    2G Linux filesystem
/dev/sda4  73506816 234438655 160931840 76.7G Linux filesystem
Disk /dev/sdb: 931.51 GiB, 1000204886016 bytes, 1953525168 sectors

# 5. 格式化分区 格式化成ext4格式的分区
czhn@czhn:~$ sudo mkfs.ext4 /dev/nvme0n1p1
mke2fs 1.46.5 (30-Dec-2021)
Discarding device blocks: done                            
Creating filesystem with 122096385 4k blocks and 30531584 inodes
Filesystem UUID: f0cfe08c-65f3-4af3-b03a-3f7fb8efd43b
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
    4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 
    102400000

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (262144 blocks): done
Writing superblocks and filesystem accounting information: done

# 6. 创建目标挂载目录 /data
czhn@czhn:~$ sudo mkdir /data

# 7. 挂载分区

# 写入到 fstab 文件
czhn@czhn:~$ sudo sh -c "echo '/dev/nvme0n1p1 /data ext4 defaults 0 2' >> /etc/fstab"
# 验证文件
czhn@czhn:~$ sudo cat /etc/fstab |grep /data
/dev/nvme0n1p1 /data ext4 defaults 0 2
# 挂载所有
czhn@czhn:~$ sudo mount -a
# 验证挂载结果
czhn@czhn:~$ df -hT
Filesystem     Type   Size  Used Avail Use% Mounted on
tmpfs          tmpfs  3.2G  1.5M  3.2G   1% /run
/dev/sda4      ext4    75G  3.3G   68G   5% /
tmpfs          tmpfs   16G     0   16G   0% /dev/shm
tmpfs          tmpfs  5.0M     0  5.0M   0% /run/lock
/dev/sda3      ext4   2.0G  127M  1.7G   7% /boot
/dev/sda1      vfat   1.1G  6.1M  1.1G   1% /boot/efi
tmpfs          tmpfs  3.2G  4.0K  3.2G   1% /run/user/1000
/dev/nvme0n1p1 ext4   458G   28K  435G   1% /data  # 挂载成功

# 重启测试挂载
  1. 制作iso镜像
把/dev/cdrom目录制作为镜像,名字为/root/rh1.iso
方法1:dd if=/dev/cdrom of=/root/rh1.iso
方法2:cat /dev/cdrom >;/root/1.iso
方法3:mkisofs -r -o myiso.iso /dev/cdrom
方法4:cp -r /home/user name.iso
  1. 把 dir1 dir2 file1 file2 一同打包成 xxx.iso
mkisodfs -o xxx.iso dir1 dir2 file1 file2