linux 磁盘操作 , 关联 disk, 硬盘,分区, partition,df, blockdev, fdisk, gdisk, lsblk, mkfs, e2label, ntfsfix, GPT, MBR, GUID, EFI, sector, nvme, hardware, 4K对齐

磁盘信息获取

smartctl 获取磁盘的设备信息

smartctl 可以获取磁盘的型号、序列号、固件版本等。

--all 参数查看所有 SMART 信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
sudo smartctl -i /dev/sdb

smartctl 7.3 2022-02-28 r5338 [x86_64-linux-5.15.65-1-MANJARO] (local build)
Copyright (C) 2002-22, Bruce Allen, Christian Franke, www.smartmontools.org

=== START OF INFORMATION SECTION ===
Model Family:     Western Digital Ultrastar DC HC550
Device Model:     WUH721816ALE6L4
Serial Number:    2PG28KL8
LU WWN Device Id: 5 000cca 2c1c1f20f
Add. Product Id:  202131
Firmware Version: PCGAW232
User Capacity:    16,000,900,661,248 bytes [16.0 TB]
Sector Sizes:     512 bytes logical, 4096 bytes physical
Rotation Rate:    7200 rpm
Form Factor:      3.5 inches
Device is:        In smartctl database 7.3/5319
ATA Version is:   ACS-4 published, ANSI INCITS 529-2018
SATA Version is:  SATA 3.3, 6.0 Gb/s (current: 6.0 Gb/s)
Local Time is:    Wed Dec  7 09:57:06 2022 CST
SMART support is: Available - device has SMART capability.
SMART support is: Enabled

正对U盘,上面命令可能出错,加上type参数试试:

1
sudo smartctl -i -d scsi /dev/sdb

df -h 各分区空间使用情况

lsblk 获取硬盘和分区列表

lsblk -lf 打印出文件系统格式、UUID、挂载点

sudo fdisk -l fdisk 显示分区信息

blockdev 获取磁盘的 block size

1
blockdev --getbsz partition

Example, 4K的block size

1
2
# blockdev --getbsz /dev/sda1 
4096

分区表

分区表格式查看,是MBR 还是 GPT?

  • 方法1: gdisk -l /dev/sda | grep -A4 '^Partition table scan:'

    如果是MBR,就显示:

    1
    2
    3
    4
    5
    Partition table scan:
      MBR: MBR only
      BSD: not present
      APM: not present
      GPT: not present
    

    如果是 GPT,就显示:

    1
    2
    3
    4
    5
    Partition table scan:
      MBR: protective
      BSD: not present
      APM: not present
      GPT: present
    
  • 方法2: parted /dev/sda print | grep -i '^Partition Table'

    如果是MBR,就显示: Partition Table: msdos

  • 方法3 fdisk -l /dev/sda

    如果是GPT格式,就显示如下,因为fdisk不支持GPT:

    1
    # WARNING: GPT (GUID Partition Table) detected on '/dev/sda'! The util fdisk doesn't support GPT. Use GNU Parted.
    

将 GPT 转换为 MBR

gdisk /dev/sdx

MBR to GPT
Enter w to write GPT partition on disk.

Press y to confirm your choice.

GPT to MBR
Enter r to enter in recovery and transformation options.

Enter g to convert GPT to MBR partition.

磁盘分区、格式化

mkfs 格式化

  • NTFS
    sudo mkfs.ntfs /dev/sdc1

    需要安装 ntfs-3g,格式化的时间很长,竟然要全部写零。。 执行需谨慎!!

    -f, --fast, -Q, --quick
    Perform quick (fast) format. This will skip both zeroing of the volume and bad sector checking.

  • ext4
    sudo mkfs.ext4 /dev/sdc1

  • vfat
    sudo mkfs.vfat /dev/sdc1

fdisk 格式化

  • FAT32
    • 对应名称为: C 295 FAT32(LBA)

创建EFI分区

1
2
3
4
5
6
gdisk /dev/sdxY
t
ef00

# After creating the ESP, you must format it as FAT32:
mkfs.fat -F32 /dev/sdxY

gdisk partition type

1
2
3
4
5
A small, under 1MB partition with code EF02
The EFI partition with code EF00
A windows Recovery Environment partition, code 2700
A windows System Reserved partition containing the BCD, MSR code 0C01
followed by the OS partition, perhaps code 0700

清除硬盘

1
2
3
4
5
6
7
8
9
10
# 清除硬盘内容,比较慢
dd if=/dev/zero of=/dev/sda
# 使用随机数据覆盖,据说更安全,执行起来相当慢
dd if=/dev/urandom of=/dev/hda

# wipe just the MBR
dd if=/dev/zero of=/dev/sda bs=446 count=1

# To remove the partition table & MBR
dd if=/dev/zero of=/dev/sda bs=512 count=1

磁盘备份和恢复

https://askubuntu.com/a/563946

1
partclone.ntfs -b -N -s /dev/sda3 -o /dev/sdb3

Partclone puked on the ntfs needing chkdisk run in Windows, so a quick fix got partclone happy:

1
2
ntfsfix -b /dev/sda3
ntfsfix -d /dev/sda3

dd命令的conv=fsync,oflag=sync/dsync

Full Metal Backup Using the dd Command

linux sync 命令

将内存中的缓存写入磁盘。

  • 如何查看sync进度
    有时候会有某个很慢的盘,严重拖慢 sync 速度。

    这时可以通过查看 /sys/block/<device>/stat 文件第9列数据,为0表示flush完成。

e2label 修改 ext2/ext3/ext4 文件系统的label

1
2
sudo apt-get install e2fsprogs
e2label device [ new-label ]

挂载分区 mount / umount

挂载 EFI 分区

直接mount即可,例如, sudo mount /dev/nvme0n1p1 /media/jake/efi

linux 修改 sector size

1
2
3
4
5
6
7
8
9
10
11
12
# manjaro 为例
sudo pacman -S nvme-cli

# 查看 目标硬盘的特性,关注其中 LBA Format 有几种模式
nvme id-ns -H /dev/nvmeXnY
# 上述命令,以pm983a为例,支持2中LBA
... ...
LBA Format 0: Metadata Size: 0 bytes - Data Size: 4096 bytes (in use)
LBA Format 1: Metadata Size: 0 bytes - Data Size: 512 bytes 

# 我要将 sector size 从 4K 改成 512
nvme format --lbaf=1 /dev/nvmeXnY

4k 对齐

gdisk -lfdisk -l 查看各个分区和磁盘的sector信息。

如果是 Sector size (logical/physical): 512 bytes / 4096 bytes 说明4K物理扇区盘,模拟512扇区,这种兼容性最高,性能也行,但是就是要看看是不是分区4K对齐了。

分区4K对齐: 分区的起始扇区 % 8 == 0 实际管理是对齐 2048(1M): 起始扇区 % 2048 == 0

如果4K不对齐,会看到类似: Partition 1 does not start on physical sector boundary.

查看硬盘温度

hddtemp

1
2
3
4
5
6
7
8
9
10
11
# 安装
sudo apt-get install hddtemp
# or
sudo yum install hddtemp
# or
sudo pacman -S hddtemp

# 列出所有硬盘温度
hddtemp
# or
hddtemp /dev/sda

从源代码安装

1
2
3
4
5
6
7
8
9
10
11
$ wget http://download.savannah.nongnu.org/releases/hddtemp/hddtemp-0.3-beta15.tar.bz2

$ tar -jxvf hddtemp-0.3-beta15.tar.bz2
$ cd hddtemp-0.3-beta15
$ ./configure
$ make
$ sudo make install

# Install hard disk temperature database at /usr/share/misc or /etc directory:
$ cd /usr/share/misc
wget http://download.savannah.nongnu.org/releases/hddtemp/hddtemp.db