使用移动硬盘传输数据详细教程


作者Lou Xiao创建时间2024-03-20 12:00:00更新时间2024-03-22 17:30:00

0. 简介

虽然使用网络传输数据是最方便快捷的,但是当传输的数据量达到TB级时,使用硬盘传输数据反而速度更快。本文总结了使用硬盘输出数据的要点、难点,方便新手快速入门。

⚠️⚠️⚠️ 硬盘有价,数据无价。
⚠️⚠️⚠️ 在充分理解文档之后,请谨慎地进行操作。

1. 文件系统

1.1 什么是文件系统

文件系统(file system)是一种用于管理计算机中文件和文件夹的组织、存储和检索的操作系统组件。在操作系统中,文件系统用于处理文件的创建、修改、删除、移动等操作,并负责文件与文件夹之间的存储和管理。

一个典型的文件系统由三部分组成:文件目录结构、文件信息和文件存储。文件目录结构用于存储文件的元数据,包括文件名、路径、创建日期、权限等。文件信息包含文件的实际内容,比如文本文件包含文本内容,图像文件包含图像数据等。文件存储则用于存储文件的的实际数据和元数据。

在Unix和类Unix系统中,文件系统通常采用分层目录结构,比如最上层目录是用户目录(user directory),接着是组目录(group directory),最后是最底层的文件目录(file directory)。用户可以在这个文件目录结构中创建文件和文件夹。

在Windows系统中,文件系统则采用一种称为“文件夹视图”的文件组织方式,其中文件和文件夹以树状结构排列,用户可以按照不同的逻辑方式来组织文件和文件夹,比如按照日期、名称大小等。

文件系统是操作系统中最基本的组件之一,它为用户提供了方便的管理文件和文件夹的方式,是用户使用计算机时不可或缺的一部分。不同的文件系统有着不同的特点和使用场景,操作系统在选择文件系统时需要根据系统的用途、硬件、性能等因素进行考虑。

1.2 移动硬盘的文件系统选择

在Linux/MacOS/Windows之间,使用移动硬盘传输数据,应该采用什么文件系统(file system)?
Linux支持ext4、xfs、exfat、ntfs;MacOS支持APFS、exfat;Windows支持ntfs、exfat。

  • Linux与Linux传输数据时,推荐采用 ext4
  • Linux与MacOS/Windows传输数据时,推荐exfat

本教程的末尾会给出ext4、xfs、exfat、ntfs等文件系统的性能。性能最好的时ext4、exfat,性能最差的时ntfs。

2. Linux的硬盘管理

2.1 查看系统上可用的硬盘

1.双击鼠标左键复制此行;2.单击复制所有代码。
                                
                                    
1 lsblk
2 # 输出如下:
3 NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
4 sda 8:0 0 3.6T 0 disk
5 └─sda1 8:1 0 3.6T 0 part
6 └─md0 9:0 0 14.6T 0 raid5
7 └─Home 253:0 0 14.6T 0 crypt /home
8 sdb 8:16 0 3.6T 0 disk
9 └─sdb1 8:17 0 3.6T 0 part
10 └─md0 9:0 0 14.6T 0 raid5
11 └─Home 253:0 0 14.6T 0 crypt /home
12 sdc 8:32 0 447.1G 0 disk
13 ├─sdc1 8:33 0 476M 0 part /boot
14 ├─sdc2 8:34 0 59.6G 0 part
15 └─sdc3 8:35 0 387.1G 0 part /
16 sdd 8:48 0 3.6T 0 disk
17 └─sdd1 8:49 0 3.6T 0 part
18 └─md0 9:0 0 14.6T 0 raid5
19 └─Home 253:0 0 14.6T 0 crypt /home
20 sde 8:64 0 3.6T 0 disk
21 └─sde1 8:65 0 3.6T 0 part
22 └─md0 9:0 0 14.6T 0 raid5
23 └─Home 253:0 0 14.6T 0 crypt /home
24 sdf 8:80 0 3.6T 0 disk
25 └─sdf1 8:81 0 3.6T 0 part
26 └─md0 9:0 0 14.6T 0 raid5
27 └─Home 253:0 0 14.6T 0 crypt /home
28 sdg 8:96 0 931.5G 0 disk
29 └─sdg1 8:97 0 931.5G 0 part

可以看到硬盘 /dev/sdg:容量是931.5GB(其实就是1TB的机械硬盘),存在一个分区(partition)/dev/sdg1

2.2 硬盘的分区管理

2.2.1 名词介绍

❶ 硬盘分区
硬盘分区是指将硬盘空间分成了多个逻辑单元,每个单元在物理存储上对应一个磁盘分区。分区的目的是为了更好地管理和组织系统中的文件和数据。

  • 分区表(Partition Table):用于记录硬盘空间分配和管理的数据结构。在大多数操作系统中,硬盘分区表是一个名为“分区引导扇区”或“磁盘分区引导扇区”的特殊扇区。该扇区包含有关硬盘分区数量、类型、大小、文件系统类型和其他元数据的详细信息。
  • 分区(Partition)是硬盘上的一个独立的分片,它被分配给一个特定的应用程序或操作系统,用于存储操作系统文件、应用程序文件、用户数据等。在硬盘分区表中,每个硬盘上有多个分区。

❷ 分区技术
硬盘的分区存在两种分区管理技术:MBR(Master Boot Record)和GPT(GUID Partition Table)。

  • MBR是由微软开发的一种硬盘分区管理技术,它是一种基于扇区0的硬盘分区表示法。每个硬盘只有一个MBR,MBR包含有关硬盘分区和系统分区的信息,包括分区类型、分区数量、分区大小、物理分区和逻辑分区等。MBR是操作系统启动时读取的分区引导扇区的一部分,它包含硬盘分区表的元数据,而操作系统通过读取硬盘分区引导扇区中的MBR来识别和管理硬盘上的分区。
  • GPT是一种现代的硬盘分区管理技术,包括现代操作系统如Linux、macOS、FreeBSD等。GPT支持GUID格式,具有更高的安全性、灵活性和恢复性。GPT能够管理多种不同类型的硬盘分区,如主分区和逻辑分区、非对称分区、动态扩展分区等,能够更加方便地管理和维护硬盘分区。在恢复方面,GPT分区格式化后,只需要重新创建GPT分区表,数据可以得到保留。

一句话概括MBR和GPT的差异:MBR最多划分4个主分区,最大2TiB的硬盘;GPT最多划分128个分区,最大8ZiB的硬盘,大于2TiB的硬盘必须选择GPT;

2.2.2 查看分区

/dev/sdg为例,查看其分区情况

1.双击鼠标左键复制此行;2.单击复制所有代码。
                                
                                    
1 // 使用fdisk命令(主要支持MBR管理技术)
2 # fdisk -l /dev/sdg
3 // 输出如下
4 Disk /dev/sdg: 931.51 GiB, 1000204886016 bytes, 1953525168 sectors
5 Disk model: nal USB 3.0
6 Units: sectors of 1 * 512 = 512 bytes
7 Sector size (logical/physical): 512 bytes / 4096 bytes
8 I/O size (minimum/optimal): 4096 bytes / 4096 bytes
9 Disklabel type: dos
10 Disk identifier: 0x0002eb5e
11
12 Device Boot Start End Sectors Size Id Type
13 /dev/sdg1 * 2048 1953523711 1953521664 931.5G 7 HPFS/NTFS/exFAT
14
15 // 使用gdisk命令(主要支持GPT管理技术)
16 # gdisk -l /dev/sdg
17 // 输出如下
18 GPT fdisk (gdisk) version 1.0.9
19
20 Partition table scan:
21 MBR: MBR only
22 BSD: not present
23 APM: not present
24 GPT: not present
25
26
27 ***************************************************************
28 Found invalid GPT and valid MBR; converting MBR to GPT format
29 in memory.
30 ***************************************************************
31
32 Disk /dev/sdg: 1953525168 sectors, 931.5 GiB
33 Model: nal USB 3.0
34 Sector size (logical/physical): 512/4096 bytes
35 Disk identifier (GUID): 9ECDB15B-5AFB-41F6-BC1A-11C8B6AEB8B0
36 Partition table holds up to 128 entries
37 Main partition table begins at sector 2 and ends at sector 33
38 First usable sector is 34, last usable sector is 1953525134
39 Partitions will be aligned on 2048-sector boundaries
40 Total free space is 3437 sectors (1.7 MiB)
41
42 Number Start (sector) End (sector) Size Code Name
43 1 2048 1953523711 931.5 GiB 0700 Microsoft basic data

2.2.3 MBR与GPT相互转化

❶ MBR => GPT

1.双击鼠标左键复制此行;2.单击复制所有代码。
                                
                                    
1 // 执行命令
2 # gdisk /dev/sdg
3
4 // 程序输出如下
5 GPT fdisk (gdisk) version 1.0.9
6
7 Partition table scan:
8 MBR: MBR only
9 BSD: not present
10 APM: not present
11 GPT: not present
12
13
14 ***************************************************************
15 Found invalid GPT and valid MBR; converting MBR to GPT format
16 in memory. THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by
17 typing 'q' if you don't want to convert your MBR partitions
18 to GPT format!
19 ***************************************************************
20
21
22 Command (? for help): w
23
24 Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
25 PARTITIONS!!
26
27 Do you want to proceed? (Y/N): Y
28 OK; writing new GUID partition table (GPT) to /dev/sdg.
29 The operation has completed successfully.
30
31 // 验证
32 # gdisk -l /dev/sdg
33
34 // 程序输出如下
35 GPT fdisk (gdisk) version 1.0.9
36
37 Partition table scan:
38 MBR: protective
39 BSD: not present
40 APM: not present
41 GPT: present
42
43 Found valid GPT with protective MBR; using GPT.
44 Disk /dev/sdg: 1953525168 sectors, 931.5 GiB
45 Model: nal USB 3.0
46 Sector size (logical/physical): 512/4096 bytes
47 Disk identifier (GUID): 326E4C93-1405-4CB8-B32E-A0F1863BFF0E
48 Partition table holds up to 128 entries
49 Main partition table begins at sector 2 and ends at sector 33
50 First usable sector is 34, last usable sector is 1953525134
51 Partitions will be aligned on 2048-sector boundaries
52 Total free space is 3437 sectors (1.7 MiB)
53
54 Number Start (sector) End (sector) Size Code Name
55 1 2048 1953523711 931.5 GiB 0700 Microsoft basic data

❷ GPT => MBR

1.双击鼠标左键复制此行;2.单击复制所有代码。
                                
                                    
1 // 执行命令
2 # gdisk /dev/sdg
3 // 程序输出如下
4 GPT fdisk (gdisk) version 1.0.9
5
6 Partition table scan:
7 MBR: protective
8 BSD: not present
9 APM: not present
10 GPT: present
11
12 Found valid GPT with protective MBR; using GPT.
13
14 Command (? for help): r
15
16 Recovery/transformation command (? for help): g
17
18 MBR command (? for help): p
19
20 ** NOTE: Partition numbers do NOT indicate final primary/logical status,
21 ** unlike in most MBR partitioning tools!
22
23 ** Extended partitions are not displayed, but will be generated as required.
24
25 Disk size is 1953525168 sectors (931.5 GiB)
26 MBR disk identifier: 0x00000000
27 MBR partitions:
28
29 Can Be Can Be
30 Number Boot Start Sector End Sector Status Logical Primary Code
31 1 2048 1953523711 primary Y Y 0x07
32
33 MBR command (? for help): w
34
35 Converted 1 partitions. Finalize and exit? (Y/N): Y
36 GPT data structures destroyed! You may now partition the disk using fdisk or
37 other utilities.
38 // 验证
39 # gdisk -l /dev/sdg
40
41 // 程序输出如下
42 GPT fdisk (gdisk) version 1.0.9
43
44 Partition table scan:
45 MBR: MBR only
46 BSD: not present
47 APM: not present
48 GPT: not present
49
50
51 ***************************************************************
52 Found invalid GPT and valid MBR; converting MBR to GPT format
53 in memory.
54 ***************************************************************
55
56 Disk /dev/sdg: 1953525168 sectors, 931.5 GiB
57 Model: nal USB 3.0
58 Sector size (logical/physical): 512/4096 bytes
59 Disk identifier (GUID): EF185F90-25D8-49DD-849D-6A2D3786CFD7
60 Partition table holds up to 128 entries
61 Main partition table begins at sector 2 and ends at sector 33
62 First usable sector is 34, last usable sector is 1953525134
63 Partitions will be aligned on 2048-sector boundaries
64 Total free space is 3437 sectors (1.7 MiB)
65
66 Number Start (sector) End (sector) Size Code Name
67 1 2048 1953523711 931.5 GiB 0700 Microsoft basic data

2.2.3 删除/添加分区

注意:若硬盘是MBR则用fdisk操作,若硬盘是GPT则用gdisk操作。
现在以/dev/sdggdisk为例,演示删除分区和添加分区

❶ 添加分区

1.双击鼠标左键复制此行;2.单击复制所有代码。
                                
                                    
1 # gdisk /dev/sdg
2
3 // 程序输出如下
4 GPT fdisk (gdisk) version 1.0.9
5
6 Partition table scan:
7 MBR: protective
8 BSD: not present
9 APM: not present
10 GPT: present
11
12 Found valid GPT with protective MBR; using GPT.
13
14 Command (? for help): p #p表示打印分区
15 Disk /dev/sdg: 1953525168 sectors, 931.5 GiB
16 Model: nal USB 3.0
17 Sector size (logical/physical): 512/4096 bytes
18 Disk identifier (GUID): 05B2E065-6F34-44C3-9352-633BEDB28769
19 Partition table holds up to 128 entries
20 Main partition table begins at sector 2 and ends at sector 33
21 First usable sector is 34, last usable sector is 1953525134
22 Partitions will be aligned on 2048-sector boundaries
23 Total free space is 1953525101 sectors (931.5 GiB)
24
25 Number Start (sector) End (sector) Size Code Name
26
27 Command (? for help): n #n表示新建分区
28 Partition number (1-128, default 1): #直接回车,此处默认是1,第1个分区
29 First sector (34-1953525134, default = 2048) or {+-}size{KMGTP}: #直接回车,此处默认是2048
30 Last sector (2048-1953525134, default = 1953523711) or {+-}size{KMGTP}: +100G #+100G表示分区大小是100G
31 Current type is 8300 (Linux filesystem)
32 Hex code or GUID (L to show codes, Enter = 8300): #8300表示用于ext4/xfs
33 Changed type of partition to 'Linux filesystem'
34
35 Command (? for help): n #n表示新建分区
36 Partition number (2-128, default 2): #直接回车,此处默认是2,第2个分区
37 First sector (34-1953525134, default = 209717248) or {+-}size{KMGTP}: #直接回车,此处默认是209717248
38 Last sector (209717248-1953525134, default = 1953523711) or {+-}size{KMGTP}: +100G #+100G表示分区大小是100G
39 Current type is 8300 (Linux filesystem)
40 Hex code or GUID (L to show codes, Enter = 8300): 0700 #0700表示用于NTFS/exFAT
41 Changed type of partition to 'Microsoft basic data'
42
43 Command (? for help): p #p表示打印分区
44 Disk /dev/sdg: 1953525168 sectors, 931.5 GiB
45 Model: nal USB 3.0
46 Sector size (logical/physical): 512/4096 bytes
47 Disk identifier (GUID): 05B2E065-6F34-44C3-9352-633BEDB28769
48 Partition table holds up to 128 entries
49 Main partition table begins at sector 2 and ends at sector 33
50 First usable sector is 34, last usable sector is 1953525134
51 Partitions will be aligned on 2048-sector boundaries
52 Total free space is 1534094701 sectors (731.5 GiB)
53
54 Number Start (sector) End (sector) Size Code Name
55 1 2048 209717247 100.0 GiB 8300 Linux filesystem
56 2 209717248 419432447 100.0 GiB 0700 Microsoft basic data
57
58 Command (? for help): w #w表示保存分区表到硬盘
59
60 Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
61 PARTITIONS!!
62
63 Do you want to proceed? (Y/N): Y # Y确认写入,N表示放弃
64 OK; writing new GUID partition table (GPT) to /dev/sdg.
65 The operation has completed successfully.

❷ 删除分区

1.双击鼠标左键复制此行;2.单击复制所有代码。
                                
                                    
1 # gdisk /dev/sdg
2
3 // 程序输出如下
4 GPT fdisk (gdisk) version 1.0.9
5
6 Partition table scan:
7 MBR: protective
8 BSD: not present
9 APM: not present
10 GPT: present
11
12 Found valid GPT with protective MBR; using GPT.
13
14 Command (? for help): p #p表示打印分区
15 Disk /dev/sdg: 1953525168 sectors, 931.5 GiB
16 Model: nal USB 3.0
17 Sector size (logical/physical): 512/4096 bytes
18 Disk identifier (GUID): 05B2E065-6F34-44C3-9352-633BEDB28769
19 Partition table holds up to 128 entries
20 Main partition table begins at sector 2 and ends at sector 33
21 First usable sector is 34, last usable sector is 1953525134
22 Partitions will be aligned on 2048-sector boundaries
23 Total free space is 1534094701 sectors (731.5 GiB)
24
25 Number Start (sector) End (sector) Size Code Name
26 1 2048 209717247 100.0 GiB 8300 Linux filesystem
27 2 209717248 419432447 100.0 GiB 0700 Microsoft basic data
28
29 Command (? for help): d #d表示删除分区
30 Partition number (1-2): 2 #2表示第2个分区,分区编号=2
31
32 Command (? for help): p #p表示打印分区
33 Disk /dev/sdg: 1953525168 sectors, 931.5 GiB
34 Model: nal USB 3.0
35 Sector size (logical/physical): 512/4096 bytes
36 Disk identifier (GUID): 05B2E065-6F34-44C3-9352-633BEDB28769
37 Partition table holds up to 128 entries
38 Main partition table begins at sector 2 and ends at sector 33
39 First usable sector is 34, last usable sector is 1953525134
40 Partitions will be aligned on 2048-sector boundaries
41 Total free space is 1743809901 sectors (831.5 GiB)
42
43 Number Start (sector) End (sector) Size Code Name
44 1 2048 209717247 100.0 GiB 8300 Linux filesystem
45
46 Command (? for help): w #w表示保存分区表到硬盘
47
48 Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
49 PARTITIONS!!
50
51 Do you want to proceed? (Y/N): Y # Y确认写入,N表示放弃
52 OK; writing new GUID partition table (GPT) to /dev/sdg.
53 The operation has completed successfully.

3. Linux文件系统操作

3.1 安装软件

确保Linux系统上已经安装ext4/xfs/exfat/ntfs相关软件包。

1.双击鼠标左键复制此行;2.单击复制所有代码。
                                
                                    
1 # 对于CentOS系统
2 yum install e2fsprogs xfsprogs exfat-utils fuse-exfat ntfs-3g ntfsprogs
3 # 对于Debian系统
4 apt install e2fsprogs xfsprogs exfatprogs exfat-fuse ntfs-3g

3.2 创建文件系统

在划分好的分区上创建文件系统ext4/xfs/exfat/ntfs。
⚠️⚠️⚠️危险操作,操作前仔细核对,使用lsblk核对设备路径(dev path)。

1.双击鼠标左键复制此行;2.单击复制所有代码。
                                
                                    
1 # 创建ext4
2 mkfs.ext4 -L MyData -m 0 -E lazy_itable_init=0,lazy_journal_init=0 /dev/sdg1
3 # 创建xfs
4 mkfs.xfs -L MyData /dev/sdg1
5 mkfs.xfs -f -L MyData /dev/sdg1 # -f 强制格式化,即使/dev/sdg1已经存在文件系统
6 # 创建exFAT
7 mkfs.exfat -L MyData /dev/sdg1
8 # 创建NTFS
9 mkfs.ntfs -f -L MyData /dev/sdg1 # -f 表示快速格式化,而非强制

3.3 挂载分区

1.双击鼠标左键复制此行;2.单击复制所有代码。
                                
                                    
1 # 智能挂载方式
2 mount /dev/sdg1 /mnt/disk0
3 mount -o ro /dev/sdg1 /mnt/disk0 # 只读挂载
4
5 # 明确挂载,指定文件系统
6 # 挂载 ext4
7 mount.ext4 /dev/sdg1 /mnt/disk0
8 # 挂载 xfs
9 mount.xfs /dev/sdg1 /mnt/disk0
10 # 挂载 exFAT 用mount.exfat-fuse 或 mount.exfat
11 mount.exfat-fuse -o ro,allow_other /dev/sdg1 /mnt/disk0 # 只读挂载,允许其他任何用户
12 mount.exfat-fuse -o rw,allow_other /dev/sdg1 /mnt/disk0 # 读写挂载,允许其他任何用户
13 mount.exfat -o ro,allow_other /dev/sdg1 /mnt/disk0 # 只读挂载,允许其他任何用户
14 mount.exfat -o rw,allow_other /dev/sdg1 /mnt/disk0 # 读写挂载,允许其他任何用户
15 # 挂载 ntfs
16 mount.ntfs -o ro,allow_other /dev/sdg1 /mnt/disk0 # 只读挂载,允许其他任何用户
17 mount.ntfs -o rw,allow_other /dev/sdg1 /mnt/disk0 # 读写挂载,允许其他任何用户

备注:在Linux系统上向exFAT/ntfs分区拷贝文件,推荐使用如下命令:

1.双击鼠标左键复制此行;2.单击复制所有代码。
                                
                                    
1 rsync -avmL --fsync --inplace --no-times --no-owner --no-group --no-perms /src/path /mnt/disk0

选项解释:

  • --fsync 参数表示在文件写入时同步fsync,确保数据真的写入硬盘;
  • --inplace 不创建临时文件,直接更新目标文件;
  • --no-times 参数表示不按照修改时间创建目标文件;
  • --no-owner 参数表示不根据文件所有者创建目标文件;
  • --no-group 参数表示不根据文件组创建目标文件;
  • --no-perms 参数表示不根据文件权限创建目标文件。

4. 文件系统性能测试

在Debian 12系统上,我测试了四种文件系统(ext4/xfs/exfat/ntfs)的读写性能。 测试的硬盘是一块1TB的机械硬盘,通过usb3.0硬盘盒与计算机连接。

机械硬盘的主要参数:

1.双击鼠标左键复制此行;2.单击复制所有代码。
                                
                                    
1 Model Family: Western Digital Blue
2 Device Model: WDC WD10EZEX-22MFCA0
3 LU WWN Device Id: 5 0014ee 264999cd6
4 Firmware Version: 01.01A01
5 User Capacity: 1,000,204,886,016 bytes [1.00 TB]
6 Sector Sizes: 512 bytes logical, 4096 bytes physical
7 Rotation Rate: 7200 rpm
8 Form Factor: 3.5 inches
9 Device is: In smartctl database 7.3/5319
10 ATA Version is: ACS-3 T13/2161-D revision 3b
11 SATA Version is: SATA 3.1, 6.0 Gb/s (current: 6.0 Gb/s)

测试结果如下:

测试汇总表

写入速度图

读取速度图

结论:

  1. ext4/exfat 读写性能最优,xfs中等,ntfs最差;
  2. 文件大小对硬盘读写性能有很大影响。文件越小,读写速度越慢。为保证较好的读写速度,建议传输文件的大小应大于16MB。
  3. 当需要传输大量小于16MB的文件,特别是小于1MB的文件时,我们建议可以按照文件夹的形式使用Zip或Tar进行打包,然后传输打包好的文件。

如何对文件加打包?

1.双击鼠标左键复制此行;2.单击复制所有代码。
                                
                                    
1 # 使用zip命令
2 zip -9r /mnt/disk0/20240101.zip 20240101/
3 zip -9r /mnt/disk0/20240102.zip 20240102/
4 # 使用tar命令
5 tar -zcvf /mnt/disk0/20240101.tar.gz 20240101/
6 tar -zcvf /mnt/disk0/20240102.tar.gz 20240102/
文章目录