Linunx 系统挂载新硬盘的四种姿势

Posted Ldoc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linunx 系统挂载新硬盘的四种姿势相关的知识,希望对你有一定的参考价值。

目前常用的磁盘挂载方式主要有两种,一种是传统的标准分区方式,另一种是采用可以动态调整容量的LVM逻辑卷的方式挂载磁盘;

一、分区前准备

1、 查看当前系统磁盘数量

登录系统后使用fdsik命令查看当前系统的磁盘数量

shell> fdisk -l

从返回结果可以看到,该系统一共存在三块磁盘:sda,sdb,sdc;其中sda已经有两个分区sda1和sda2;在云计算环境(例如阿里云,腾讯云,zstack私有云)中,第一块磁盘默认名称为vda,新加的磁盘为sda,与本实例略有区别。

2、确认磁盘使用情况

使用lsblk命令查看当前磁盘分区的使用情况

shell> lsblk

从返回结果看,sda磁盘存在两个正在使用分区,sda1的挂载点为/boot,也就是系统引导分区,sdb2分区加入到LVM逻辑卷中,然后分成两部分,一部分挂载到了swap交换空间(累似windows的虚拟内存),一部分挂载到了 / 根分区下,而sdb和sdc两块磁盘还没有被使用。

下面以sdb(200G)演示 2TB 一下小磁盘的分区方式,以sdc演示2TB及以上大小的此案分区方式。

二、标准分区方式挂载

1、2TB 以下小容量磁盘挂载方式

1.1、创建分区

shell> fdisk /dev/sdb 开始对sdb磁盘分区
进入分区工具之后的常用命令
d           删除一个分区
g           创建一个GPT分区
l           查看系统支持的所有文件系统类型
m           查看所有的命令
n           新建一个MBR分区
p           查看当前磁盘的所有分区
q           不保存分区信息退出
t           修改分区的文件系统类型
w           保存分区信息并退出分区工具

shell> p
# 显示当前磁盘的分区情况。
Disk /dev/sdb: 214.7 GB, 214748364800 bytes, 419430400 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: 0x0cc2d83a

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n         # 输入字母 n 创建一个分区
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p               # 使用主分区(MBR分区方式可以使用的是3个主分区和1个扩展分区。)
Partition number (1-4, default 1): 
First sector (2048-419430399, default 2048):        # 默认分区的开始位置
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-419430399, default 419430399):      # 分区默认的结束位置 (分区的大小),可以使用扇区数量们也可以使用+50G,+100M这样的方式指定分区大小
Using default value 419430399
Partition 1 of type Linux and of size 200 GiB is set

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

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

1.2、格式化分区

目前Linux系统常用的分区格式有ext4和xfs

  • ext4是第四代扩展文件系统(是linux系统下的日志文件系统,是ext3文件系统的后继版本,ext4的文件系统容量达到1EB,而文件容量则达到16TB,这是一个非常大的数字了。对一般的台式机和服务器而言,这可能并不重要,但对于大型磁盘阵列的用户而言,这就非常重要了。ext3目前只支持32000个子目录,而ext4取消了这一限制,理论上支持无限数量的子目录

  • xfs是一种非常优秀的日志文件系统,它是SGI公司设计的。xfs被称为业界最先进的、最具可升级性的文件系统技术
    xfs是一个64位文件系统,最大支持8EB减1字节的单个文件系统,实际部署时取决于宿主操作系统的最大块限制。对于一个32位Linux系统,文件和文件系统的大小会被限制在16TB
shell>  mkfs.xfs  /dev/sdb1
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=13107136 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=52428544, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=25599, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

1.3、挂载分区

shell>  blkid
/dev/sdb1: UUID="45ceaa1e-c670-453a-bdfd-a9440f466ce8" TYPE="xfs" 
/dev/sda1: UUID="76249d73-d993-4b95-a883-cdfad9fd51a3" TYPE="xfs" 
/dev/sda2: UUID="SY3OgX-PpJ7-XHx7-M7tO-tgSx-ijM8-1XE8Ie" TYPE="LVM2_member" 
/dev/mapper/centos_vm43-root: UUID="e2b82aad-8b81-43cc-a7a1-b026d7931c35" TYPE="xfs" 
/dev/mapper/centos_vm43-swap: UUID="f9692663-0df0-40aa-9785-52a1e36002e1" TYPE="swap" 
shell>  vi /etc/fstab 
#
# /etc/fstab
# Created by anaconda on Sat Jun 12 11:22:30 2021
#
# Accessible filesystems, by reference, are maintained under \'/dev/disk\'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos_vm43-root /                       xfs     defaults        0 0
UUID=76249d73-d993-4b95-a883-cdfad9fd51a3 /boot                   xfs     defaults        0 0
/dev/mapper/centos_vm43-swap swap                    swap    defaults        0 0

UUID=45ceaa1e-c670-453a-bdfd-a9440f466ce8 /soft xfs     defaults 0 0

shell>  mkdir /soft
shell>  mount -a
shell df -l
Filesystem                   1K-blocks    Used Available Use% Mounted on
devtmpfs                       1918776       0   1918776   0% /dev
tmpfs                          1930644       0   1930644   0% /dev/shm
tmpfs                          1930644   11852   1918792   1% /run
tmpfs                          1930644       0   1930644   0% /sys/fs/cgroup
/dev/mapper/centos_vm43-root  47285700 2975188  44310512   7% /
/dev/sda1                      1038336  198460    839876  20% /boot
tmpfs                           386132       0    386132   0% /run/user/0
/dev/sdb1                    209611780   32992 209578788   1% /soft

shell>  lsblk
NAME                 MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                    8:0    0   50G  0 disk 
├─sda1                 8:1    0    1G  0 part /boot
└─sda2                 8:2    0   49G  0 part 
  ├─centos_vm43-root 253:0    0 45.1G  0 lvm  /
  └─centos_vm43-swap 253:1    0  3.9G  0 lvm  [SWAP]
sdb                    8:16   0  200G  0 disk 
└─sdb1                 8:17   0  200G  0 part /soft
sdc                    8:32   0    2T  0 disk 
sr0                   11:0    1 1024M  0 rom

2、2T 以上大容量磁盘挂载方式

2.1、创建分区

shell>  parted /dev/sdc
GNU Parted 3.1
Using /dev/sdc
Welcome to GNU Parted! Type \'help\' to view a list of commands.                           
(parted) mklabel gpt                                                      
(parted) mkpart                                                           
Partition name?  []? app                                                  
File system type?  [ext2]? xfs
Start? 0%                                                                 
End? 100%
shell> quit

2.2、格式化分区

shell>  lsblk
NAME                 MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                    8:0    0   50G  0 disk 
├─sda1                 8:1    0    1G  0 part /boot
└─sda2                 8:2    0   49G  0 part 
  ├─centos_vm43-root 253:0    0 45.1G  0 lvm  /
  └─centos_vm43-swap 253:1    0  3.9G  0 lvm  [SWAP]
sdb                    8:16   0  200G  0 disk 
└─sdb1                 8:17   0  200G  0 part /soft
sdc                    8:32   0    2T  0 disk 
└─sdc1                 8:33   0    2T  0 part 
sr0                   11:0    1 1024M  0 rom

shell>  mkfs.xfs /dev/sdc1
meta-data=/dev/sdc1              isize=512    agcount=4, agsize=134217600 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=536870400, imaxpct=5
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=262143, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

2.3、挂载分区

shell>  mkdir /app
shell>  blkid
/dev/sdb1: UUID="45ceaa1e-c670-453a-bdfd-a9440f466ce8" TYPE="xfs" 
/dev/sda1: UUID="76249d73-d993-4b95-a883-cdfad9fd51a3" TYPE="xfs" 
/dev/sda2: UUID="SY3OgX-PpJ7-XHx7-M7tO-tgSx-ijM8-1XE8Ie" TYPE="LVM2_member" 
/dev/mapper/centos_vm43-root: UUID="e2b82aad-8b81-43cc-a7a1-b026d7931c35" TYPE="xfs" 
/dev/mapper/centos_vm43-swap: UUID="f9692663-0df0-40aa-9785-52a1e36002e1" TYPE="swap" 
/dev/sdc1: UUID="0b5afafe-d9ea-4418-b925-2970e892e3a7" TYPE="xfs" PARTLABEL="app" PARTUUID="da48aa08-9436-4460-8139-2916055dd882" 
shell>  vi /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sat Jun 12 11:22:30 2021
#
# Accessible filesystems, by reference, are maintained under \'/dev/disk\'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos_vm43-root /                       xfs     defaults        0 0
UUID=76249d73-d993-4b95-a883-cdfad9fd51a3 /boot                   xfs     defaults        0 0
/dev/mapper/centos_vm43-swap swap                    swap    defaults        0 0

UUID=45ceaa1e-c670-453a-bdfd-a9440f466ce8 /soft xfs     defaults 0 0
UUID=0b5afafe-d9ea-4418-b925-2970e892e3a7 /app  xfs     defaults 0 0

shell>  mount -a
shell>  df -lh
Filesystem                    Size  Used Avail Use% Mounted on
devtmpfs                      1.9G     0  1.9G   0% /dev
tmpfs                         1.9G     0  1.9G   0% /dev/shm
tmpfs                         1.9G   12M  1.9G   1% /run
tmpfs                         1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos_vm43-root   46G  2.9G   43G   7% /
/dev/sda1                    1014M  194M  821M  20% /boot
tmpfs                         378M     0  378M   0% /run/user/0
/dev/sdc1                     2.0T   33M  2.0T   1% /app

三、LVM逻辑卷方式挂载

通过以下命令判断系统是否使用LVM逻辑卷的方式

shell> lvs
  LV   VG          Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root centos_vm43 -wi-ao---- <45.12g                                                    
  swap centos_vm43 -wi-ao----  <3.88g                                                    
shell> vgs
  VG          #PV #LV #SN Attr   VSize   VFree
  centos_vm43   1   2   0 wz--n- <49.00g 4.00m
shell> pvs
  PV         VG          Fmt  Attr PSize   PFree
  /dev/sda2  centos_vm43 lvm2 a--  <49.00g 4.00m

1、原系统有 LVM 逻辑卷

1.1、创建分区

shell> 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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x4f64c3e3.

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

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

Calling ioctl() to re-read partition table.
Syncing disks.
shell> lsblk
NAME                 MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                    8:0    0   50G  0 disk 
├─sda1                 8:1    0    1G  0 part /boot
└─sda2                 8:2    0   49G  0 part 
  ├─centos_vm43-root 253:0    0 45.1G  0 lvm  /
  └─centos_vm43-swap 253:1    0  3.9G  0 lvm  [SWAP]
sdb                    8:16   0  200G  0 disk 
└─sdb1                 8:17   0  200G  0 part 
sdc                    8:32   0    2T  0 disk 
sr0                   11:0    1 1024M  0 rom  

1.2、扩容LVM逻辑卷

shell> pvs
  PV         VG          Fmt  Attr PSize   PFree
  /dev/sda2  centos_vm43 lvm2 a--  <49.00g 4.00m   
shell> pvcreate /dev/sdb1
WARNING: xfs signature detected on /dev/sdb1 at offset 0. Wipe it? [y/n]: y
  Wiping xfs signature on /dev/sdb1.
  Physical volume "/dev/sdb1" successfully created.
shell> pvs
  PV         VG          Fmt  Attr PSize    PFree   
  /dev/sda2  centos_vm43 lvm2 a--   <49.00g    4.00m
  /dev/sdb1              lvm2 ---  <200.00g <200.00g
shell> vgs
  VG          #PV #LV #SN Attr   VSize   VFree  
  centos_vm43   2   2   0 wz--n- 248.99g 200.00g
shell> vgextend centos_vm43 /dev/sdb1
  Volume group "centos_vm43" successfully extended
shell> vgdisplay 
  --- Volume group ---
  VG Name               centos_vm43
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               248.99 GiB  # VG的空间大小
  PE Size               4.00 MiB    # PE可以分配的最小空间大小
  Total PE              63742
  Alloc PE / Size       12542 / 48.99 GiB
  Free  PE / Size       51200 / 200.00 GiB
  VG UUID               3lyEYk-l3m9-52v7-utCw-22UM-F5dl-M4f1SQ

shell> lvs
  LV   VG          Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root centos_vm43 -wi-ao---- <45.12g                                                    
  swap centos_vm43 -wi-ao----  <3.88g                                                    
shell> df -lhT
Filesystem                   Type      Size  Used Avail Use% Mounted on
devtmpfs                     devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                        tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                        tmpfs     1.9G   12M  1.9G   1% /run
tmpfs                        tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos_vm43-root xfs       45G  2.9G  42.1G   7% /
/dev/sda1                    xfs      1014M  194M  821M  20% /boot
tmpfs                        tmpfs     378M     0  378M   0% /run/user/0
shell> lvextend -L +150G centos_vm43-root
  Please specify a logical volume path.
  Run `lvextend --help\' for more information.
shell> lvextend -L +150G /dev/mapper/centos_vm43-root
  Size of logical volume centos_vm43/root changed from <45.12 GiB (11550 extents) to <195.12 GiB (49950 extents).
  Logical volume centos_vm43/root successfully resized.       
shell> xfs_growfs /dev/mapper/centos_vm43-root 
meta-data=/dev/mapper/centos_vm43-root isize=512    agcount=4, agsize=2956800 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=11827200, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=5775, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 11827200 to 51148800
shell> df -lhT
Filesystem                   Type      Size  Used Avail Use% Mounted on
devtmpfs                     devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                        tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                        tmpfs     1.9G   12M  1.9G   1% /run
tmpfs                        tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos_vm43-root xfs       196G  2.9G  193G   2% /
/dev/sda1                    xfs      1014M  194M  821M  20% /boot
tmpfs                        tmpfs     378M     0  378M   0% /run/user/0

2、系统无 LVM 逻辑卷

2.1、创建分区

shell> parted /dev/sdc
GNU Parted 3.1
Using /dev/sdc
Welcome to GNU Parted! Type \'help\' to view a list of commands.
(parted)                                                                  
(parted)                                                                  
(parted) mklabel gpt                                
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 2199GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start  End  Size  File system  Name  Flags

(parted) mkpart                                                           
Partition name?  []? lvm                                                  
File system type?  [ext2]? xfs                                            
Start? 0%
End? 100% 
(parted) q                                                                
Information: You may need to update /etc/fstab.

2.2、创建LVM逻辑卷

shell> pvcreate /dev/sdc1
WARNING: xfs signature detected on /dev/sdc1 at offset 0. Wipe it? [y/n]: y
  Wiping xfs signature on /dev/sdc1.
  Physical volume "/dev/sdc1" successfully created.
shell> pvs
  PV         VG          Fmt  Attr PSize    PFree 
  /dev/sda2  centos_vm43 lvm2 a--   <49.00g     0 
  /dev/sdb1  centos_vm43 lvm2 a--  <200.00g 50.00g
  /dev/sdc1              lvm2 ---    <2.00t <2.00t
shell> vgcreate 
  No command with matching syntax recognised.  Run \'vgcreate --help\' for more information.
  Correct command syntax is:
  vgcreate VG_new PV ...

shell> vgcreate vg_soft /dev/sdc1
  Volume group "vg_soft" successfully created
shell> vgs
  VG          #PV #LV #SN Attr   VSize   VFree 
  centos_vm43   2   2   0 wz--n- 248.99g 50.00g
  vg_soft       1   0   0 wz--n-  <2.00t <2.00t
shell> lvcreate -n lv_soft -L 1T vg_soft
  Logical volume "lv_soft" created.
shell> lvs
  LV      VG          Attr       LSize    Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root    centos_vm43 -wi-ao---- <195.12g                                                    
  swap    centos_vm43 -wi-ao----   <3.88g                                                    
  lv_soft vg_soft     -wi-a-----    1.00t   

2.3、挂载使用

shell> mkfs.xfs /dev/mapper/vg_soft-lv_soft -K
meta-data=/dev/mapper/vg_soft-lv_soft isize=512    agcount=4, agsize=67108864 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=268435456, imaxpct=5
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=131072, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
shell> blkid
/dev/sdb1: UUID="be0AMz-Hwt8-J3zz-4RDE-HWMB-Ltvv-nT6AlA" TYPE="LVM2_member" 
/dev/sda1: UUID="76249d73-d993-4b95-a883-cdfad9fd51a3" TYPE="xfs" 
/dev/sda2: UUID="SY3OgX-PpJ7-XHx7-M7tO-tgSx-ijM8-1XE8Ie" TYPE="LVM2_member" 
/dev/mapper/centos_vm43-root: UUID="e2b82aad-8b81-43cc-a7a1-b026d7931c35" TYPE="xfs" 
/dev/mapper/centos_vm43-swap: UUID="f9692663-0df0-40aa-9785-52a1e36002e1" TYPE="swap" 
/dev/sdc1: UUID="0B4qKh-saLI-tlF7-BQkS-VLub-od0u-hDMnCW" TYPE="LVM2_member" PARTLABEL="lvm" PARTUUID="6434ad3a-55bf-48e2-8bdb-7093aa987ae2" 
/dev/mapper/vg_soft-lv_soft: UUID="03125219-53f3-4028-9218-6e075c7f9c99" TYPE="xfs" 
shell> vi /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sat Jun 12 11:22:30 2021
#
# Accessible filesystems, by reference, are maintained under \'/dev/disk\'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos_vm43-root /                       xfs     defaults        0 0
UUID=76249d73-d993-4b95-a883-cdfad9fd51a3 /boot                   xfs     defaults        0 0
/dev/mapper/centos_vm43-swap swap                    swap    defaults        0 0

UUID=03125219-53f3-4028-9218-6e075c7f9c99       /soft   xfs     defaults        0 0
shell> mkdir /soft
shell> mount -a
shell> df -lh
Filesystem                    Size  Used Avail Use% Mounted on
devtmpfs                      1.9G     0  1.9G   0% /dev
tmpfs                         1.9G     0  1.9G   0% /dev/shm
tmpfs                         1.9G   12M  1.9G   1% /run
tmpfs                         1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos_vm43-root  196G  2.9G  193G   2% /
/dev/sda1                    1014M  194M  821M  20% /boot
tmpfs                         378M     0  378M   0% /run/user/0
/dev/mapper/vg_soft-lv_soft   1.0T   33M  1.0T   1% /soft

以上是关于Linunx 系统挂载新硬盘的四种姿势的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot系列教程web篇Servlet 注册的四种姿势

Springboot 解决跨域的四种姿势

ssh转发流量的四种姿势

mysql查看版本的四种方法

Docker 挂载数据卷的四种方式

HDFS数据迁移目录到正确姿势