由于公司的项目多,每拉一个android项目,都耗费非常大硬盘空间,2TB的硬盘现在已不足100GB 。本来是想让管理服务器的同事给我再分配一台机器,他果断的拒绝了我的请求,但是给我一块2TB 的硬盘。所以接下来就是自己安装的事情了。
首先关闭电脑,断开电源,然后将硬盘固定到主机上,并链接电源线和数据线。
首先我们查看系统上刚刚添加的设备
lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 1.8T 0 disk
├─sda1 8:1 0 37.3G 0 part /
├─sda2 8:2 0 1K 0 part
├─sda5 8:5 0 29.8G 0 part [SWAP]
└─sda6 8:6 0 1.8T 0 part /home
sdb 8:16 0 1.8T 0 disk
sr0 11:0 1 1024M 0 rom
其中sdb是我刚添加的硬盘设备,第一块硬盘已经分区,分区符号分别是sda1,sda2,sda5,sda6。而我们添加的硬盘没有分区。
再添加一块硬盘,那么列出的设备符号是sdc,其中sd 是英文 serial device 的缩写。硬盘需要进行分区,我们加载到系统的时候不是直接加载硬盘设备,而是加载分区,实际上数据都是存储在分区上的。
挂载分为临时和永久2种方式,临时挂载挂载usb接口类的设备,比如u盘,移动硬盘;永久挂载,挂载后,机器启动后自动加载这个设备。本文中需要永久挂载。
在挂载前,我们需要对硬盘进行分区
在linux系统中,分区命令是parted。我当前机器的分区如下:
sudo parted -l
[sudo] password for lava:
Model: ATA TOSHIBA DT01ACA2 (scsi)
Disk /dev/sda: 2000GB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos
Number Start End Size Type File system Flags
1 1049kB 40.0GB 40.0GB primary ext4 boot
2 40.0GB 2000GB 1960GB extended
5 40.0GB 72.0GB 32.0GB logical linux-swap(v1)
6 72.0GB 2000GB 1928GB logical ext4
Error: /dev/sdb: unrecognised disk label
第一个分区
sudo parted /dev/sdb
[sudo] password for lava:
GNU Parted 2.3
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)
那么这里分区后,为了保证每个分区在系统中都有唯一的名称,所以我们选择GPT方式。
(parted) mklabel gpt
(parted)
上面也可以使用mktable gpt,2种方式达到的效果相同。我们再查看结果,输入print命令:
(parted) print
Model: ATA ST2000DM008-2FR1 (scsi)
Disk /dev/sdb: 2000GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Number Start End Size File system Name Flags
创建第一个分区
(parted) mkpart primary 0 1TB
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? ignore
(parted) print
Model: ATA ST2000DM008-2FR1 (scsi)
Disk /dev/sdb: 2000GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Number Start End Size File system Name Flags
1 17.4kB 17.9kB 512B primary
看到的结果好像不太对,估计不识别TB单位,所以我再重新试试,换成Gb.不过我们可以先阅读帮助文档:
(parted) help mkpart
mkpart PART-TYPE [FS-TYPE] START END make a partition
PART-TYPE is one of: primary, logical, extended
FS-TYPE is one of: zfs, btrfs, ext4, ext3, ext2, fat32, fat16, hfsx, hfs+, hfs, jfs, swsusp, linux-swap(v1), linux-swap(v0), ntfs,
reiserfs, freebsd-ufs, hp-ufs, sun-ufs, xfs, apfs2, apfs1, asfs, amufs5, amufs4, amufs3, amufs2, amufs1, amufs0, amufs, affs7, affs6,
affs5, affs4, affs3, affs2, affs1, affs0, linux-swap, linux-swap(new), linux-swap(old)
START and END are disk locations, such as 4GB or 10%. Negative values count from the end of the disk. For example, -1s specifies
exactly the last sector.
'mkpart' makes a partition without creating a new file system on the partition. FS-TYPE may be specified to set an appropriate
partition ID.
(parted)
这里可以选择分区的类型,试试logical.
(parted) mkpart logical 1 1024GB
(parted) print
Model: ATA ST2000DM008-2FR1 (scsi)
Disk /dev/sdb: 2000GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Number Start End Size File system Name Flags
1 17.4kB 17.9kB 512B primary
2 1049kB 1024GB 1024GB logical
ok,是可以正常分区的,预期效果是我们想要的。
因为我想弄2个分区,所以上面的分区要清除,另外我是要挂载到已有系统目录,所以不能用primary分区。
重新执行的步骤结果:
(parted) mkpart logical 0 1000GB
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? ignore
(parted) print
Model: ATA ST2000DM008-2FR1 (scsi)
Disk /dev/sdb: 2000GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Number Start End Size File system Name Flags
1 17.4kB 1000GB 1000GB logical
(parted) mkpart logical 1 1000GB
Error: Unable to satisfy all constraints on the partition.
//这里有一个错误,这里说明一下命令 mkpart logical 1 1000GB 的意思
mkpart 是分区命令
logical 表示分区类型
1 起始空间地址
1000GB 末尾结束空间地址
所以这个命令错误,重复使用了空间。
(parted) print
Model: ATA ST2000DM008-2FR1 (scsi)
Disk /dev/sdb: 2000GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Number Start End Size File system Name Flags
1 17.4kB 1000GB 1000GB logical
(parted) mkpart logical 1000GB 2000GB
(parted) print
Model: ATA ST2000DM008-2FR1 (scsi)
Disk /dev/sdb: 2000GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Number Start End Size File system Name Flags
1 17.4kB 1000GB 1000GB logical
2 1000GB 2000GB 1000GB logical
按照gparted 软件
apt-get install gparted
linux的文件系统配置文件在/etc/fstab中,所以要永久挂载在系统中就要将信息写入到/etc/fstab。
# /etc/fstab: static file system information.
#
# 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).
#
#
# / was on /dev/sda1 during installation
UUID=101406a7-0f8c-49dd-91a5-995a101f304b / ext4 errors=remount-ro 0 1
# /home was on /dev/sda6 during installation
UUID=5282b5be-a17c-455e-a418-2ea59d9e5763 /home ext4 defaults 0 2
# swap was on /dev/sda5 during installation
UUID=e8e2cbd8-a7b8-41f7-aa1a-9b287b7a8160 none swap sw 0 0
# Secondary Hard Drive
/dev/sdb1 /home ntfs defaults,noatime 0 2
16 /dev/sdb2 /home ntfs defaults,noatime 0 2
UUID=092b8ada-4028-4bdb-81c2-860f2238e346 /home/lava/sdb ext4 defaults 0 2
UUID=5282b5be-a17c-455e-a418-2ea59d9e5763 /home/lava/lava ext4 defaults 0 2
~
所以将分区后的设备添加到配置文件中。
参考资料:
https://www.addictivetips.com/ubuntu-linux-tips/mount-external-hard-drives-in-linux/
https://opensource.com/article/18/6/how-partition-disk-linux
https://askubuntu.com/questions/125257/how-do-i-add-an-additional-hard-drive