Fork me on GitHub

Ubuntu挂载新的硬盘(2T以上)

目录

  • 背景
  • 第一部分 查看硬盘信息
  • 第二部分 新挂载硬盘分区
  • 第三部分 使用parted分区
  • 第四部分 格式化新建分区
  • 第五部分 挂载分区
  • 第六部分 配置开机自动挂载分区
  • 第七部分 附录
  • 参考文献及资料

背景

系统环境:

Linux version 4.13.0-37-generic (Ubuntu 5.4.0-6ubuntu1~16.04.9)。root用户登入操作。

第一部分 查看硬盘信息

机器断电时,接入硬盘。开机后用下面的命令查看硬盘状况(非root用户需sudo)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
root@deeplearning:~# fdisk -l
Disk /dev/sda: 465.8 GiB, 500107862016 bytes, 976773168 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
Disklabel type: gpt
Disk identifier: CC8004FC-D422-48FA-8ACF-54C3F48E860B

Device Start End Sectors Size Type
/dev/sda1 2048 1050623 1048576 512M EFI System
/dev/sda2 1050624 909946879 908896256 433.4G Linux filesystem
/dev/sda3 909946880 976771071 66824192 31.9G Linux swap


Disk /dev/sdb: 3.7 TiB, 4000787030016 bytes, 7814037168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes

查看到系统由两块硬盘:/dev/sda和/dev/sdb,如果还有其他硬盘会继续sdc、sdd编号。

正在使用的系统盘sda已经有三个分区(sda1、sda2、sda3),新挂载的硬盘sdb位分区。

第二部分 新挂载硬盘分区

新硬盘存储空间一共4T,我们对硬盘进行分区。划分为两个分区:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
root@deeplearning:~# fdisk /dev/sdb

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

/dev/sdb: device contains a valid 'ext4' signature; it is strongly recommended to wipe the device with wipefs(8) if this is sible collisions

Device does not contain a recognized partition table.
The size of this disk is 3.7 TiB (4000787030016 bytes). DOS partition table format can not be used on drives for volumes lar512-byte sectors. Use GUID partition table format (GPT).

Created a new DOS disklabel with disk identifier 0x6b028a17.

Command (m for help):

注意这里已经有警告:The size of this disk is 3.7 TiB (4000787030016 bytes). DOS partition table format can not be used on drives for volumes lar512-byte sectors. Use GUID partition table format (GPT)

这里情况特殊,新加入的磁盘为4T。fdisk命令对于大于2T的分区无法划分。如果继续使用fdisk工具,最多只能分出2T的分区,剩下的空间无法利用。这不坑爹嘛。提示我们使用parted命令。

第三部分 使用parted分区

parted命令可以划分单个分区大于2T的GPT格式的分区。

更改分区表类型:

1
root@deeplearning:~# parted -s /dev/sdb mklabel gpt

使用parted进行分区:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
root@deeplearning:~# parted /dev/sdb
GNU Parted 3.2
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Model: ATA WDC WD40EFRX-68N (scsi)
Disk /dev/sdb: 4001GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags

(parted) mklabel gpt
Warning: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do you want to continue?
Yes/No? yes
(parted) mkpart
Partition name? []?
File system type? [ext2]? ext4
Start? 0%
End? 100%
(parted) print
Model: ATA WDC WD40EFRX-68N (scsi)
Disk /dev/sdb: 4001GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags
1 1049kB 4001GB 4001GB ext4

(parted) quit
Information: You may need to update /etc/fstab.

最后我们验证一下,sdb1分区成功,提示我们要更新系统文件:/etc/fstab。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
root@deeplearning:~# ls /dev/sd*                                          
/dev/sda /dev/sda1 /dev/sda2 /dev/sda3 /dev/sdb /dev/sdb1
root@deeplearning:~# fdisk -l
Disk /dev/sda: 465.8 GiB, 500107862016 bytes, 976773168 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
Disklabel type: gpt
Disk identifier: CC8004FC-D422-48FA-8ACF-54C3F48E860B

Device Start End Sectors Size Type
/dev/sda1 2048 1050623 1048576 512M EFI System
/dev/sda2 1050624 909946879 908896256 433.4G Linux filesystem
/dev/sda3 909946880 976771071 66824192 31.9G Linux swap


Disk /dev/sdb: 3.7 TiB, 4000787030016 bytes, 7814037168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: 0D8B0FBC-83F6-4D77-ABDB-98875EC511E4

Device Start End Sectors Size Type
/dev/sdb1 2048 7814035455 7814033408 3.7T Linux filesystem

第四部分 格式化新建分区

将分区格式化为ext4格式的文件系统。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
root@deeplearning:~# mkfs.ext4 /dev/sdb1
mke2fs 1.42.13 (17-May-2015)
Creating filesystem with 976754176 4k blocks and 244195328 inodes
Filesystem UUID: dfcd419f-38a5-4a5c-9b93-9f236d2c2444
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

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information:
done

如果有多个分区需要依次执行格式化。

第五部分 挂载分区

新建硬盘即将挂载的目录,然后将硬盘挂载到该目录下。并验证挂载成功,检查硬盘空间。

1
2
3
4
5
6
7
8
9
10
11
12
13
root@deeplearning:/# mkdir /data
root@deeplearning:/# mount /dev/sdb1 /data
root@deeplearning:/# df -h
Filesystem Size Used Avail Use% Mounted on
udev 16G 0 16G 0% /dev
tmpfs 3.2G 9.3M 3.2G 1% /run
/dev/sda2 427G 21G 385G 5% /
tmpfs 16G 0 16G 0% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 16G 0 16G 0% /sys/fs/cgroup
/dev/sda1 511M 3.5M 508M 1% /boot/efi
tmpfs 3.2G 12K 3.2G 1% /run/user/1000
/dev/sdb1 3.6T 68M 3.4T 1% /data

上面我们把新的硬盘挂载到了/data目录,硬盘空间大小正常。

第六部分 配置开机自动挂载分区

6.1 查看分区的UUID

1
2
3
root@deeplearning:/# blkid
#(略)...
/dev/sdb1: UUID="dfcd419f-38a5-4a5c-9b93-9f236d2c2444" TYPE="ext4" PARTUUID="fe373bd5-5b19-4ed0-8713-716455a8ebb4"

6.2 配置/etc/fstab

将分区信息写到/etc/fstab文件中让它永久挂载:

将下面的配置信息加入配置文件尾部:

1
UUID=dfcd419f-38a5-4a5c-9b93-9f236d2c2444 /data ext4 defaults 0 1

第七部分 附录

7.1 /etc/fstab配置说明

1
2
3
4
5
6
7
8
9
10
11
12
13
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).

<file system> <mount point> <type> <options> <dump> <pass>
1 2 3 4 5 6
对应参数说明:
1、指代文件系统的设备名。最初,该字段只包含待挂载分区的设备名(如/dev/sda1)。现在,除设备名外,还可以包含LABEL或UUID
2、文件系统挂载点。文件系统包含挂载点下整个目录树结构里的所有数据,除非其中某个目录又挂载了另一个文件系统
3、文件系统类型。下面是多数常见文件系统类型(ext3,tmpfs,devpts,sysfs,proc,swap,vfat)
4、mount命令选项。mount选项包括noauto(启动时不挂载该文件系统)和ro(只读方式挂载文件系统)等。在该字段里添加用户或属主选项,即可允许该用户挂载文件系统。多个选项之间必须用逗号隔开。其他选项的相关信息可参看mount命令手册页(-o选项处)
5、转储文件系统?该字段只在用dump备份时才有意义。数字1表示该文件系统需要转储,0表示不需要转储
6、文件系统检查?该字段里的数字表示文件系统是否需要用fsck检查。0表示不必检查该文件系统,数字1示意该文件系统需要先行检查(用于根文件系统)。数字2则表示完成根文件系统检查后,再检查该文件系统。

7.2 Parted命令说明(本文使用交互模式完成配置)

Parted 命令分为两种模式:命令行模式和交互模式。

  • 命令行模式: parted [option] device [command] ,该模式可以直接在命令行下对磁盘进行分区操作,比较适合编程应用。
  • 交互模式:parted [option] device 类似于使用fdisk /dev/xxx
  • MBR:MBR分区表(即主引导记录)大家都很熟悉。所支持的最大卷:2T,而且对分区有限制:最多4个主分区或3个主分区加一个扩展分区
  • GPT: GPT(即GUID分区表)。是源自EFI标准的一种较新的磁盘分区表结构的标准,是未来磁盘分区的主要形式。与MBR分区方式相比,具有如下优点。突破MBR 4个主分区限制,每个磁盘最多支持128个分区。支持大于2T的分区,最大卷可达18EB。

parted是一个可以分区并进行分区调整的工具,他可以创建,破坏,移动,复制,调整ext2 linux-swap fat fat32 reiserfs类型的分区,可以创建,调整,移动Macintosh的HFS分区,检测jfs,ntfs,ufs,xfs分区。

使用方法:parted [options] [device [command [options...]...]]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
options
-h 显示帮助信息
-l 显示所有块设备上的分区
device 对哪个块设备进行操作,如果没有指定则使用第一个块设备
command [options...]

check partition 对分区做一个简单的检测

cp [source-device] source dest 复制source-device设备上的source分区到当前设备的dest分区

mklabel label-type 创建新分区表类型,label-type可以是:"bsd", "dvh", "gpt", "loop","mac", "msdos", "pc98", or "sun" 一般的pc机都是msdos格式,如果分区大于2T则需要选用gpt格式的分区表。

mkfs partition fs-type 在partition分区上创建一个fs-type文件系统,fs-type可以是:"fat16", "fat32", "ext2", "linux-swap","reiserfs" 注意不支持ext3格式的文件系统,只能先分区然后用专有命令进行格式化。

mkpart part-type [fs-type] start end 创建一个part-type类型的分区,part-type可以是:"primary", "logical", or "extended" 如果指定fs-type则在创建分区的同时进行格式化。start和end指的是分区的起始位置,单位默认是M。

eg:
mkpart primary 0 -1 0表示分区的开始 -1表示分区的结尾 意思是划分整个硬盘空间为主分区
mkpartfs part-type fs-type start end 创建一个fs-type类型的part-type分区,不推荐使用,最好是使用mkpart分区完成后使用mke2fs进行格式化。
name partition name 给分区设置一个名字,这种设置只能用在Mac, PC98, and GPT类型的分区表,设置时名字用引号括起来
select device 在机器上有多个硬盘时,选择操作那个硬盘
resize partition start end 调整分区大小
rm partition 删除一个分区
rescue start end 拯救一个位于stat和end之间的分区
unit unit 在前面分区时,默认分区时数值的单位是M,这个参数卡伊改变默认单位,"kB", "MB", "GB", "TB"
move partition start end 移动partition分区
print 显示分区表信息
quit 退出parted

参考文献

【1】 Setting up a large (2TB+) hard disk drive on Linux

本文标题:Ubuntu挂载新的硬盘(2T以上)

文章作者:rong xiang

发布时间:2018年04月01日 - 23:04

最后更新:2022年10月25日 - 23:10

原始链接:https://zjrongxiang.github.io/posts/712a6d66/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

0%