自动挂载硬盘

自动挂载硬盘

设置开机自动挂载硬盘

初始化待挂载的硬盘

查看硬盘信息

shell
1
fdisk -l

例如:

shell
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
$ fdisk -l
Disk /dev/sda: 2 TiB, 2199023255552 bytes, 4294967296 sectors
Disk model: QEMU HARDDISK   
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/sdb: 99.95 GiB, 107323850752 bytes, 209616896 sectors
Disk model: QEMU HARDDISK   
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: dos
Disk identifier: 0x860b4782

Device     Boot Start       End   Sectors  Size Id Type
/dev/sdb1  *     2048 209616862 209614815  100G 83 Linux

/dev/sda 是需要挂载的硬盘

格式化硬盘

shell
1
2
3
4
5
mkfs -t ext4 /dev/sda

# mkfs -t <type> <disk>
# type: ext2、ext3、ext4、FAT、NTFS 等
# disk: 硬盘

挂载硬盘

创建挂载目录

shell
1
2
3
mkdir /mnt/data

# /mnt/data  自行定义

使用磁盘UUID方式

查看磁盘分区的UUID

shell
1
blkid

例如:

shell
1
2
3
4
$ blkid
/dev/sdb1: UUID="47649f2e-953d-4642-bd46-0bc4a96e381b" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="860b4782-01"
/dev/sr0: BLOCK_SIZE="2048" UUID="2024-08-15-17-03-12-00" LABEL="cidata" TYPE="iso9660"
/dev/sda: UUID="aa0ff0e4-dd4b-46de-a9be-8b2dead41768" BLOCK_SIZE="4096" TYPE="ext4"

aa0ff0e4-dd4b-46de-a9be-8b2dead41768 就是磁盘 /dev/sda 的 UUID

设置 /etc/fstab

shell
1
2
3
4
5
6
# 备份
cp /etc/fstab /etc/fstab.bak

# UUID=<UUID>  <目录>  <type> defaults 0 0 

echo "UUID=aa0ff0e4-dd4b-46de-a9be-8b2dead41768 /mnt/data ext4 defaults 0 0" >> /etc/fstab

检查验证

检查挂载是否成功

shell
1
mount -a

查看文件系统信息

shell
1
df -h
shell
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            3.9G     0  3.9G   0% /dev
tmpfs           797M  856K  797M   1% /run
/dev/sdb1        99G  2.3G   93G   3% /
tmpfs           3.9G     0  3.9G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
overlay          99G  2.3G   93G   3% /var/lib/docker/overlay2/823c308f8dd93b9a4a28ed153ecf6e5ae615174d2065a27daa0ac15989116230/merged
overlay          99G  2.3G   93G   3% /var/lib/docker/overlay2/a8dd8592252b3f1adf3b588dcd2966dc7578d39aeaf44c1195b3fc401bf65454/merged
overlay          99G  2.3G   93G   3% /var/lib/docker/overlay2/0710b5ecdb6322cb0bacd736f41c6c30463ef6d8f7a355f55500c9a1c558339a/merged
overlay          99G  2.3G   93G   3% /var/lib/docker/overlay2/cb0621dcdaa7aa499e0cfad63104dfc2789ab14182c7ed928691b2c36cc0639e/merged
tmpfs           797M     0  797M   0% /run/user/0
/dev/sda        2.0T   28K  1.9T   1% /mnt/data