Operation,  Other

Linux 生产运维之 disk 管理

Linux 生产运维之 disk 管理

1. 新加磁盘分区

# ----------------- 分区 ------------------
# 首先查看一下磁盘及分区情况
root@cn-south1-web1:~# fdisk -lu
Disk /dev/sda: 465.78 GiB, 500107862016 bytes, 976773168 sectors
Disk model: Samsung SSD 870 
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: C1C2BB63-F03A-46A3-902F-CE543B2AA89E

# 可以看到物理盘 sda 还没有任何分区
root@cn-south1-web1:~# fdisk /dev/sda

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

# 输入 m 查看帮助,输入 n 创建新分区
Command (m for help): n
Partition number (1-128, default 1): 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.

# 注:如果没确认好则输入 q 不保存退出,确认好了则输入 w 保存退出
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Synching disks.

# 重新再查看一下磁盘及分区情况
root@cn-south1-web1:~# fdisk -lu
Disk /dev/sda: 465.78 GiB, 500107862016 bytes, 976773168 sectors
Disk model: Samsung SSD 870 
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: C1C2BB63-F03A-46A3-902F-CE543B2AA89E

Device     Start       End   Sectors   Size Type
/dev/sda1   2048 976773134 976771087 465.8G Linux filesystem

# ----------------- 挂载 ------------------

# 错误示例,尝试挂载磁盘设备会报以下错误
root@cn-south1-web1:~# mount /dev/sda /mnt/disk2/
mount: /mnt/disk2: wrong fs type, bad option, bad superblock on /dev/sda, missing codepage or helper program, or other error.

# 错误示例,尝试挂载未分区的设备会报以下错误
root@cn-south1-web1:~# mount -t ext4 /dev/sda1 /mnt/disk2/; echo $?
mount: /mnt/disk2: wrong fs type, bad option, bad superblock on /dev/sda1, missing codepage or helper program, or other error.
32

# 格式化刚刚新创建的分区 (device),注:挂载的不是磁盘而是磁盘的分区且必须已格式化
root@cn-south1-web1:~# mkfs.ext4 /dev/sda1

# 将这个新分区 (device) 挂载到目录 /mnt/disk2
root@cn-south1-web1:~# mount /dev/sda1 /mnt/disk2/

# 再查看一下文件系统信息,是否挂载成功
root@cn-south1-web1:~# df -hT
Filesystem     Type      Size  Used Avail Use% Mounted on
udev           devtmpfs  6.8G     0  6.8G   0% /dev
tmpfs          tmpfs     1.4G  2.0M  1.4G   1% /run
/dev/nvme0n1p8 ext4      131G  116G  8.3G  94% /
tmpfs          tmpfs     6.9G  139M  6.7G   2% /dev/shm
tmpfs          tmpfs     5.0M  4.0K  5.0M   1% /run/lock
tmpfs          tmpfs     6.9G     0  6.9G   0% /sys/fs/cgroup
/dev/nvme0n1p1 vfat      646M   78M  569M  12% /boot/efi
tmpfs          tmpfs     1.4G   32K  1.4G   1% /run/user/1000
/dev/sda1      ext4      458G   73M  435G   1% /mnt/disk2

# 永久挂载目录(解决重启失效)
# 备份原挂载配置
root@cn-south1-web1:~# cp -r /etc/fstab /etc/fstab.bak
# 先找到新增分区的 UUID
export currentNewPartitionUUID=$(sudo blkid /dev/sda1|awk '{print $2}')
# 增加重启自动挂载配置
root@cn-south1-web1:~# echo "$currentNewPartUUID /mnt/disk2      ext4    defaults          0       1" >> /etc/fstab

2. 删除旧磁盘分区

注:删除旧磁盘分区前建议提前备份好数据,以防万一

# 首先查看一下磁盘及分区情况
root@cn-south1-web1:~# fdisk -lu
Disk /dev/sda: 465.78 GiB, 500107862016 bytes, 976773168 sectors
Disk model: Samsung SSD 870
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: C1C2BB63-F03A-46A3-902F-CE543B2AA89E

Device     Start       End   Sectors   Size Type
/dev/sda1   2048 976773134 976771087 465.8G Linux filesystem

# 可以看到物理盘 sda 已有一个分区 sda1,准备删除这个分区
root@cn-south1-web1:~# fdisk /dev/sda
Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

# 输入 m 查看帮助,输入 p 查看当前磁盘分区
Command (m for help): p
Disk /dev/sda: 465.78 GiB, 500107862016 bytes, 976773168 sectors
Disk model: Samsung SSD 870 
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: C1C2BB63-F03A-46A3-902F-CE543B2AA89E

Device     Start       End   Sectors   Size Type
/dev/sda1   2048 976773134 976771087 465.8G Linux filesystem

# 输入 d 删除分区,如果有多个分区会提示选择
Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

# 注:如果没确认好则输入 q 不保存退出,确认好了则输入 w 保存退出
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Synching disks.

3. 如何判断某个分区是否格式化?

  • 3.1 方式一

直接把一个分区进行一下挂载,通过能不能挂得上就可以知道他有没有被格式化过了,没有被格式化的分区是挂不上去的。

root@cn-south1-web1:~# fdisk /dev/sdk -l
Disk /dev/sdk: 1073.7 GB, 1073741824000 bytes
64 heads, 32 sectors/track, 1024000 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Device Boot Start End Blocks Id System
/dev/sdk1 1 512000 524287984 83 Linux
/dev/sdk2 512001 1024000 524288000 83 Linux

root@cn-south1-web1:~# mount /dev/sdk1 /data5
mount: you must specify the filesystem type
  • 3.2 方式二
# 显示所有磁盘及分区
root@cn-south1-web1:~# fdisk -l

# 显示格式化过的分区
root@cn-south1-web1:~# blkid /dev/sdb

4. FAQ

4.1 一些工具命令未安装? 如 mkfs.ext4 command not found , mkfs.ntfs command not found

# 安装 mkfs.ext2/mkfs.ext3/mkfs.ext4 工具
sudo apt install -y e2fsprogs # (focal) ubuntu/debian
sudo yum install -y e2fsprogs # (epel) centos/oracle linux

# 安装 mkfs.xfs 工具
sudo apt install -y xfsprogs # (focal) ubuntu/debian
sudo yum install -y xfsprogs # (epel) centos/oracle linux

# 安装 mkfs.msdos 工具
sudo apt install -y dosfstools
sudo yum install -y dosfstools

# 安装 mkfs.ntfs 工具
sudo apt install -y ntfs-3g # (focal) ubuntu/debian
# (epel) centos/oracle linux
sudo yum install -y ntfs-3g
sudo yum install -y ntfsprogs

留言

您的电子邮箱地址不会被公开。