Linunx 系统挂载新硬盘的四种方式

【Linunx 系统挂载新硬盘的四种方式】著论准过秦,作赋拟子虚。这篇文章主要讲述Linunx 系统挂载新硬盘的四种方式相关的知识,希望能为你提供帮助。
目前常用的磁盘挂载方式主要有两种,一种是传统的标准分区方式,另一种是采用可以动态调整容量的LVM逻辑卷的方式挂载磁盘;
一、分区前准备 1、 查看当前系统磁盘数量登录系统后使用fdsik命令查看当前系统的磁盘数量

shell> fdisk -l

Linunx 系统挂载新硬盘的四种方式

文章图片

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

Linunx 系统挂载新硬盘的四种方式

文章图片

从返回结果看,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: 0x0cc2d83aDevice BootStartEndBlocksIdSystemCommand (m for help): n# 输入字母 n 创建一个分区 Partition type: pprimary (0 primary, 0 extended, 4 free) eextended 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 setCommand (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=https://www.songbingjia.com/dev/sdb1isize=512agcount=4, agsize=13107136 blks =sectsz=512attr=2, projid32bit=1 =crc=1finobt=0, sparse=0 data=bsize=4096blocks=52428544, imaxpct=25 =sunit=0swidth=0 blks naming=version 2bsize=4096ascii-ci=0 ftype=1 log=internal logbsize=4096blocks=25599, version=2 =sectsz=512sunit=0 blks, lazy-count=1 realtime =noneextsz=4096blocks=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 /xfsdefaults0 0 UUID=76249d73-d993-4b95-a883-cdfad9fd51a3 /bootxfsdefaults0 0 /dev/mapper/centos_vm43-swap swapswapdefaults0 0UUID=45ceaa1e-c670-453a-bdfd-a9440f466ce8 /soft xfsdefaults 0 0shell> mkdir /soft shell> mount -a shell df -l Filesystem1K-blocksUsed Available Use% Mounted on devtmpfs1918776019187760% /dev tmpfs1930644019306440% /dev/shm tmpfs19306441185219187921% /run tmpfs1930644019306440% /sys/fs/cgroup /dev/mapper/centos_vm43-root47285700 2975188443105127% / /dev/sda1103833619846083987620% /boot tmpfs38613203861320% /run/user/0 /dev/sdb120961178032992 2095787881% /softshell> lsblk NAMEMAJ:MIN RMSIZE RO TYPE MOUNTPOINT sda8:0050G0 disk ├─sda18:101G0 part /boot └─sda28:2049G0 part ├─centos_vm43-root 253:00 45.1G0 lvm/ └─centos_vm43-swap 253:103.9G0 lvm[SWAP] sdb8:160200G0 disk └─sdb18:170200G0 part /soft sdc8:3202T0 disk sr011:01 1024M0 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 NAMEMAJ:MIN RMSIZE RO TYPE MOUNTPOINT sda8:0050G0 disk ├─sda18:101G0 part /boot └─sda28:2049G0 part ├─centos_vm43-root 253:00 45.1G0 lvm/ └─centos_vm43-swap 253:103.9G0 lvm[SWAP] sdb8:160200G0 disk └─sdb18:170200G0 part /soft sdc8:3202T0 disk └─sdc18:3302T0 part sr011:01 1024M0 romshell> mkfs.xfs /dev/sdc1 meta-data=https://www.songbingjia.com/dev/sdc1isize=512agcount=4, agsize=134217600 blks =sectsz=512attr=2, projid32bit=1 =crc=1finobt=0, sparse=0 data=bsize=4096blocks=536870400, imaxpct=5 =sunit=0swidth=0 blks naming=version 2bsize=4096ascii-ci=0 ftype=1 log=internal logbsize=4096blocks=262143, version=2 =sectsz=512sunit=0 blks, lazy-count=1 realtime =noneextsz=4096blocks=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 /xfsdefaults0 0 UUID=76249d73-d993-4b95-a883-cdfad9fd51a3 /bootxfsdefaults0 0 /dev/mapper/centos_vm43-swap swapswapdefaults0 0UUID=45ceaa1e-c670-453a-bdfd-a9440f466ce8 /soft xfsdefaults 0 0 UUID=0b5afafe-d9ea-4418-b925-2970e892e3a7 /appxfsdefaults 0 0shell> mount -a shell> df -lh FilesystemSizeUsed Avail Use% Mounted on devtmpfs1.9G01.9G0% /dev tmpfs1.9G01.9G0% /dev/shm tmpfs1.9G12M1.9G1% /run tmpfs1.9G01.9G0% /sys/fs/cgroup /dev/mapper/centos_vm43-root46G2.9G43G7% / /dev/sda11014M194M821M20% /boot tmpfs378M0378M0% /run/user/0 /dev/sdc12.0T33M2.0T1% /app

三、LVM逻辑卷方式挂载通过以下命令判断系统是否使用LVM逻辑卷的方式
shell> lvs LVVGAttrLSizePool 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 AttrVSizeVFree centos_vm43120 wz--n- < 49.00g 4.00m shell> pvs PVVGFmtAttr PSizePFree /dev/sda2centos_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: pprimary (0 primary, 0 extended, 4 free) eextended 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 setCommand (m for help): w The partition table has been altered!Calling ioctl() to re-read partition table. Syncing disks. shell> lsblk NAMEMAJ:MIN RMSIZE RO TYPE MOUNTPOINT sda8:0050G0 disk ├─sda18:101G0 part /boot └─sda28:2049G0 part ├─centos_vm43-root 253:00 45.1G0 lvm/ └─centos_vm43-swap 253:103.9G0 lvm[SWAP] sdb8:160200G0 disk └─sdb18:170200G0 part sdc8:3202T0 disk sr011:01 1024M0 rom

1.2、扩容LVM逻辑卷
shell> pvs PVVGFmtAttr PSizePFree /dev/sda2centos_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 PVVGFmtAttr PSizePFree /dev/sda2centos_vm43 lvm2 a--< 49.00g4.00m /dev/sdb1lvm2 ---< 200.00g < 200.00g shell> vgs VG#PV #LV #SN AttrVSizeVFree centos_vm43220 wz--n- 248.99g 200.00g shell> vgextend centos_vm43 /dev/sdb1 Volume group "centos_vm43" successfully extended shell> vgdisplay --- Volume group --- VG Namecentos_vm43 System ID Formatlvm2 Metadata Areas2 Metadata Sequence No4 VG Accessread/write VG Statusresizable MAX LV0 Cur LV2 Open LV2 Max PV0 Cur PV2 Act PV2 VG Size248.99 GiB PE Size4.00 MiB Total PE63742 Alloc PE / Size12542 / 48.99 GiB FreePE / Size51200 / 200.00 GiB VG UUID3lyEYk-l3m9-52v7-utCw-22UM-F5dl-M4f1SQshell> lvs LVVGAttrLSizePool 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 FilesystemTypeSizeUsed Avail Use% Mounted on devtmpfsdevtmpfs1.9G01.9G0% /dev tmpfstmpfs1.9G01.9G0% /dev/shm tmpfstmpfs1.9G12M1.9G1% /run tmpfstmpfs1.9G01.9G0% /sys/fs/cgroup /dev/mapper/centos_vm43-root xfs45G2.9G42.1G7% / /dev/sda1xfs1014M194M821M20% /boot tmpfstmpfs378M0378M0% /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=https://www.songbingjia.com/dev/mapper/centos_vm43-root isize=512agcount=4, agsize=2956800 blks =sectsz=512attr=2, projid32bit=1 =crc=1finobt=0 spinodes=0 data=bsize=4096blocks=11827200, imaxpct=25 =sunit=0swidth=0 blks naming=version 2bsize=4096ascii-ci=0 ftype=1 log=internalbsize=4096blocks=5775, version=2 =sectsz=512sunit=0 blks, lazy-count=1 realtime =noneextsz=4096blocks=0, rtextents=0 data blocks changed from 11827200 to 51148800 shell> df -lhT FilesystemTypeSizeUsed Avail Use% Mounted on devtmpfsdevtmpfs1.9G01.9G0% /dev tmpfstmpfs1.9G01.9G0% /dev/shm tmpfstmpfs1.9G12M1.9G1% /run tmpfstmpfs1.9G01.9G0% /sys/fs/cgroup /dev/mapper/centos_vm43-root xfs196G2.9G193G2% / /dev/sda1xfs1014M194M821M20% /boot tmpfstmpfs378M0378M0% /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: NumberStartEndSizeFile systemNameFlags(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 PVVGFmtAttr PSizePFree /dev/sda2centos_vm43 lvm2 a--< 49.00g0 /dev/sdb1centos_vm43 lvm2 a--< 200.00g 50.00g /dev/sdc1lvm2 ---< 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 AttrVSizeVFree centos_vm43220 wz--n- 248.99g 50.00g vg_soft100 wz--n-< 2.00t < 2.00t shell> lvcreate -n lv_soft -L 1T vg_soft Logical volume "lv_soft" created. shell> lvs LVVGAttrLSizePool Origin Data%Meta%Move Log Cpy%Sync Convert rootcentos_vm43 -wi-ao---- < 195.12g swapcentos_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=https://www.songbingjia.com/dev/mapper/vg_soft-lv_soft isize=512agcount=4, agsize=67108864 blks =sectsz=512attr=2, projid32bit=1 =crc=1finobt=0, sparse=0 data=bsize=4096blocks=268435456, imaxpct=5 =sunit=0swidth=0 blks naming=version 2bsize=4096ascii-ci=0 ftype=1 log=internal logbsize=4096blocks=131072, version=2 =sectsz=512sunit=0 blks, lazy-count=1 realtime =noneextsz=4096blocks=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 /xfsdefaults0 0 UUID=76249d73-d993-4b95-a883-cdfad9fd51a3 /bootxfsdefaults0 0 /dev/mapper/centos_vm43-swap swapswapdefaults0 0UUID=03125219-53f3-4028-9218-6e075c7f9c99/softxfsdefaults0 0 shell> mkdir /soft shell> mount -a shell> df -lh FilesystemSizeUsed Avail Use% Mounted on devtmpfs1.9G01.9G0% /dev tmpfs1.9G01.9G0% /dev/shm tmpfs1.9G12M1.9G1% /run tmpfs1.9G01.9G0% /sys/fs/cgroup /dev/mapper/centos_vm43-root196G2.9G193G2% / /dev/sda11014M194M821M20% /boot tmpfs378M0378M0% /run/user/0 /dev/mapper/vg_soft-lv_soft1.0T33M1.0T1% /soft


    推荐阅读