Linux - 磁盘/逻辑卷的分区&格式化&挂载

『磁盘』

Part1:分区

杨治:"磁盘分区格式ext4的磁盘最大支持文件系统大小为16T。"

【01】修改磁盘格式为GPT

vbnet 复制代码
# 修改磁盘格式为gpt
(parted) mklabel gpt
Warning: The existing disk label on /dev/sdb will be destroyed and all on this disk will be lost. Do you want to continue?
Yes/No? yes
(parted) print
(parted) p
Model: UN LOGICAL VOLUME (scsi)
Disk /dev/sdb: 14.0TB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt

Number	Start	End	    Size	File System	    Name	   标志

【02】2T及以下的磁盘使用fdisk分区

主要用于传统的 MBR 分区表,最大支持 2TB 的磁盘。

sql 复制代码
# 查看服务器磁盘
fdisk -l
# 使用fdisk对磁盘(/dev/sdb)进行分区
fdisk /dev/sdb
# ----------------------开始分区----------------------
# 查看菜单
Command (m for help): 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): n
Command action
	e extended
	p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-78325,default 1): (这里回车即可)
# 保存退出
Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table
Syncing disks.
# ----------------------分区完成----------------------



# 删除分区 d Number,比如分区号为1
Command (m for help): d 1

【03】2T以上的磁盘使用parted分区

用于现代的 GPT 分区表,支持远超 2TB 的磁盘(理论上可达 8 ZiB)。

sql 复制代码
# 查看服务器磁盘信息
parted -l
# 使用parted对超过2T的磁盘(/dev/sdb)分区
parted /dev/sdb
# ----------------------开始分区----------------------
GNU Parted 2.1
使用 /dev/sdb
welcome to GNU Parted! Type 'help' to view a list of commands.


# 查看帮助信息
(parted) help
  align-check TYPE N                       check partition N for TYPE(min|opt) alignment
  check NUMBER                             do a simple check on the file system
  cp [FROM-DEVICE] FROM-NUMBER TO-NUMBER   copy file system to another partition
  help [COMMAND]                           print general help, or help on COMMAND
  mklabel,mktable LABEL-TYPE               create a new disklabel (partition table)
  mkfs NUMBER FS-TYPE                      make a FS-TYPE file system on partition NUMBER
  mkpart PART-TYPE [FS-TYPE] START END     make a partition
  mkpartfs PART-TYPE FS-TYPE START END     make a partition with a file system
  move NUMBER START END                    move partition NUMBER
  name NUMBER NAME                         name partition NUMBER as NAME
  print [devices|free|list,all|NUMBER]     display the partition table, available devices, free space, all found partitions, or a particular partition
  quit                                     exit program
  rescue START END                         rescue a lost partition near START and END
  resize NUMBER START END                  resize partition NUMBER and its file system
  rm NUMBER                                delete partition NUMBER
  select DEVICE                            choose the device to edit
  set NUMBER FLAG STATE                    change the FLAG on partition NUMBER
  toggle [NUMBER [FLAG]]                   toggle the state of FLAG on partition NUMBER
  unit UNIT                                set the default unit to UNIT
  version                                  display the version number and copyright information of GNU Parted




# 打印硬盘信息
(parted) p
Model: UN LOGICAL VOLUME (scsi)
Disk /dev/sdb: 14.0TB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt

Number	Start	End	    Size	File System	    Name	   标志


# 创建分区 (第一种)
(parted) mkpart primary 0GB 14000GB
# 创建分区 (第二种)
(parted) mkpart primary 0 -1
警告: The resulting partition is not properly aligned for best performance.
忽略/Ignore/放弃/Cancel? Ignore

# 查看磁盘分区信息
(parted) print
Model: UN LOGICAL VOLUME (scsi)
Disk /dev/sdb: 14.0TB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt

Number	Start	End	  Size    File System	    Name	   标志
1	 17.4kB	14.0TB	14.0TB		         primary


# 退出parted的命令行
(parted) quit
信息: You may need to update /etc/fstab.
# ----------------------分区完成----------------------

Part2:格式化

【01】磁盘格式化

ini 复制代码
# Ext4 has a maximum filesystem size of 1EB and maximum filesize of 16TB.
# 可以支持14TB的硬盘

# 格式化
mkfs.ext4 /dev/sdb1

# -------------------格式化开始-------------------

mke2fs 1.41.12 (17-May-2010)
/dev/sdb1 alignment is offset by 244736 bytes.
This may result in very poor performance. (re) -partitioning suggested.
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=64 blocks, Stripe width=64 blocks
854523904 inodes, 3418087175 blocks
170904358 blocks (5.00%) reserverd for the super user
第一个数据块=0
Maximum filesystem blocks=4294967296
104312 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
           32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
           4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
           102400000, 214990848, 512000000, 550731776, 644972544, 1934917632,
           2560000000
正在写入inode表: 33874/104312
# (等待)
正在写入inode表: 完成
Creating journal (32768 blocks): 完成
writing superblocks and filesystem accounting information: (此处回车)

# (等待)
完成

This filesystem will be automatically checked every 22 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.

# -------------------格式化完成-------------------

Part3:挂载

bash 复制代码
# 创建挂载点
mkdir -p /mnt/disk{1..6}
# 上述命令在/mnt下创建六个目录(挂载点)
ll /mnt
# ----------------------------------------------
总用量 12
drwxr-xr-x  5 root root 4096 11月 30 14:49 disk1
drwxr-xr-x  5 root root 4096 11月 30 14:49 disk2
drwxr-xr-x  5 root root 4096 11月 30 14:49 disk3
drwxr-xr-x  5 root root 4096 11月 30 14:49 disk4
drwxr-xr-x  5 root root 4096 11月 30 14:49 disk5
drwxr-xr-x  5 root root 4096 11月 30 14:49 disk6
drwxr-xr-x. 3 root root 4096 5月   9 2021 ramdisk


# 挂载
mount /dev/sdb1 /mnt/disk1
mount /dev/sdc1 /mnt/disk2
mount /dev/sdd1 /mnt/disk3
mount /dev/sde1 /mnt/disk4
mount /dev/sdf1 /mnt/disk5
mount /dev/sdg1 /mnt/disk6


# 不小心挂载/dev/sdb1失误,可以卸载重新挂载
# 
# (通过设备名卸载)
umount /dev/sdb1
# (通过挂载点卸载)
umount /mnt/disk1


# 配置/etc/fstab
vim /etc/fstab
# --------------------------/etc/fstab--------------------------
/dev/sdb1            /mnt/disk1            ext4            defaults            0   0
/dev/sdc1            /mnt/disk2            ext4            defaults            0   0
/dev/sdd1            /mnt/disk3            ext4            defaults            0   0
/dev/sde1            /mnt/disk4            ext4            defaults            0   0
/dev/sdf1            /mnt/disk5            ext4            defaults            0   0
/dev/sdg1            /mnt/disk6            ext4            defaults            0   0


# 或者使用UUID配置,如下格式(注意: 推荐使用UUID配置/etc/fstab文件)
UUID=5a8fc680-ccab-4725-b8a2-7c561fe11fd5   /mnt/disk1   ext4   defaults       0   0

# 使用blkid查看磁盘UUID
[root@centos ~]# blkid
/dev/sda1: UUID="0275f240-39a8-45fd-87aa-46c65d7483af" TYPE="ext4"
/dev/sda2: UUID="a3940c80-f9cc-4ed5-9ee6-c4d3a7171d67" TYPE="ext4"
/dev/sda3: UUID="b986d21d-6af3-4a6e-80cc-fff567edfeb2" TYPE="ext4"
/dev/sda5: UUID="2075c135-1550-4b93-b28d-76c2c914061f" TYPE="ext4"
/dev/sda6: UUID="dfb6c9cc-d612-46a2-a7cf-4fb164b2cf22" TYPE="swap"

# 编辑/etc/fstab之后执行,命令执行后无回显则配置无误(推荐使用)
mount -a

# 以上命令执行没有报错即为配置成功,为确保配置无误,可尝试重启服务器测试配置是否正确,注意:如果该服务器已经投入生产,严禁随意使用该命令重启服务器。
reboot

『逻辑卷』

Part0:准备工作

【01】删除逻辑卷

csharp 复制代码
[root@localhost home]# lsblk
......

sdk
|-linear_dev_sdk_vg-warpdrive_Linear_linear_dev_sdk_data_tmeta    253:22    0    176M    0 lvm
| |-linear_dev_sdk_vg-warpdrive_Linear_linear_dev_sdk_data        253.25    0    5.5T    0 lvm
|-linear_dev_sdk_vg-warpdrive_Linear_linear_dev_sdk_tdata         253.23    0    5.5T    0 lvm
  |-linear_dev_sdk_vg-warpdrive_Linear_linear_dev_sdk_data        253:25    0    5.5T    0 lvm

[root@localhost home]# dmsetup remove --force linear_dev_sdk_vg-warpdrive_Linear_linear_dev_sdk_data
[root@localhost home]# 

【02】擦除文件系统确保磁盘没有残留数据

bash 复制代码
[root@localhost home]# wipefs -a /dev/sdb /dev/sdc /dev/sdd /dev/sde /dev/sdf /dev/sdg /dev/sdh /dev/sdi /dev/sdj /dev/sdk
/dev/sdb: 8 bytes were erased at offset 0x00000218 (LVM2_member): 4c 56 4d 32 20 30 30 31
/dev/sdc: 8 bytes were erased at offset 0x00000218 (LVM2_member): 4c 56 4d 32 20 30 30 31
/dev/sdd: 8 bytes were erased at offset 0x00000218 (LVM2_member): 4c 56 4d 32 20 30 30 31
/dev/sde: 8 bytes were erased at offset 0x00000218 (LVM2_member): 4c 56 4d 32 20 30 30 31
/dev/sdf: 8 bytes were erased at offset 0x00000218 (LVM2_member): 4c 56 4d 32 20 30 30 31
/dev/sdg: 8 bytes were erased at offset 0x00000218 (LVM2_member): 4c 56 4d 32 20 30 30 31
/dev/sdh: 8 bytes were erased at offset 0x00000218 (LVM2_member): 4c 56 4d 32 20 30 30 31
/dev/sdi: 8 bytes were erased at offset 0x00000218 (LVM2_member): 4c 56 4d 32 20 30 30 31
/dev/sdj: 8 bytes were erased at offset 0x00000218 (LVM2_member): 4c 56 4d 32 20 30 30 31
/dev/sdk: 8 bytes were erased at offset 0x00000218 (LVM2_member): 4c 56 4d 32 20 30 30 31
[root@localhost home]

Part1:分区

less 复制代码
[root@localhost home] 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 0x7a84815d.

The device presents a logical sector size that is smaller than
the physical sector size. Aligning to a physical sector (or optimal
I/O) size boundary is recommended, or performance may be impacted.

Command (m for help): m
Command action
    a toggle a bootable flag
    b edit bsd disklabel
    c toggle the dos compatibility flag
    d delete a partition
    g create a new empty GPT partition table
    G create an IRIX(SGI) partition table
    l list known partition types
    m print this memu
    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 funcationality (experts only)

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-1873045503, default 2048):
Using default value 2048
Last sector, +sectors os +size{K,M,G} (2048-1873045503,default 1873045503): +300G
Partition 1 of type Linux and of size 300 GiB is set

Command (m for help): p

Disk /dev/sdb: 959.0 GB, 958999298048 bytes, 1873045504 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 65536 bytes / 65536 bytes
Disk label type: dos
Disk identifier: 0x7a84815d

Device Boot    Start    End    Blocks    Id    System
/dev/sdb1    2048    62147647    313572800 83    Linux


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

Command (m for help): p

Disk /dev/sdb: 959.0 GB, 958999298048 bytes, 1873045504 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 65536 bytes / 65536 bytes
Disk label type: dos
Disk identifier: 0x7a84815d

Device Boot    Start    End    Blocks    Id    System
/dev/sdb1    2048    62147647    313572800 83    Linux
/dev/sdb1    629147648    1873045503    621948928    83    Linux

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost home]#

Part2:格式化

ini 复制代码
[root@localhost home]# mkfs.xfs -f -n ftype=1 /dev/sdb1
meta-data=/dev/sdb1        isize=512     agcount=16,agsize=4915200 blks
         =                 sectsz=4096   attr=2,projid32bit=1
         =                 crc=1         finobt=0,sparse=0
data     =                 bsize=4096    blocks=78643200, imaxpct=25
         =                 sunit=16      swidth=16 blks
naming   =version 2        bsize=4096    ascii-ci=0 ftype=1
log      =internal log     bsize=4096    blocks=38400,version=2
         =                 sectsz=4096   sunit=1 blks,lazy-count=1
realtime =none             extsz=4096    blocks=0,rtextents=0
[root@localhost home]#

Part3:挂载

css 复制代码
[root@localhost home]# mkdir -p /var/lib/docker
[root@localhost home]# mount /dev/sdb1 /var/lib/docker
[root@localhost home]# mount /dev/sdb2 /var/log

记得在/etc/fstab中配置自动挂载

相关推荐
半桔8 小时前
【网络编程】网络通信基石:从局域网到跨网段通信原理探秘
linux·运维·网络协议·php
叫我詹躲躲8 小时前
Linux 服务器磁盘满了?教你快速找到大文件,安全删掉不踩坑!
linux·前端·curl
叫我詹躲躲8 小时前
3 分钟搞定 Linux 磁盘清理:实用命令 + 自动脚本,新手也会
linux·curl
Garc8 小时前
Zookeeper删除提供者服务中的指定IP节点
linux·运维·服务器
过往入尘土9 小时前
Linux:虚拟世界的大门
linux·人工智能
Wang's Blog9 小时前
Linux小课堂: Linux 系统的多面性与 CentOS 下载指南
linux·运维·centos
FengyunSky9 小时前
高通Camx内存问题排查
android·linux·后端
matlab的学徒10 小时前
nginx+springboot+redis+mysql+elfk
linux·spring boot·redis·nginx
HappyGame0210 小时前
Linux网络编程(上)
linux·网络