写在前面,不建议在ubuntu 的vm的第一块盘上扩展磁盘容量,会丢数据。应新建一块磁盘
查看当前硬盘
sudo fdisk -l
就可以看到目前的硬盘了。可以看到有块空的 sda(这里忘了复制输出了,大家可以看到自己的硬盘就对了)。
新增硬盘,新建分区
下面我们将使用 fdisk 命令来创建新的分区。首先进入我们需要操作的硬盘:
$ sudo fdisk /dev/sda
sudo: unable to resolve host csc-server-0-2
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.
Device does not contain a recognized partition table.
The size of this disk is 7.3 TiB (7999376588800 bytes). DOS partition table format can not be used on drives for volumes larger than 2199023255040 bytes for 512-byte sectors. Use GUID partition table format (GPT).
Created a new DOS disklabel with disk identifier 0xcb6595dd.
然后我们可以输入 m 来查看一下帮助信息。
Command (m for help): m
Help:
DOS (MBR)
a toggle a bootable flag
b edit nested BSD disklabel
c toggle the dos compatibility flag
Generic
d delete a partition
F list free unpartitioned space
l list known partition types
n add a new partition
p print the partition table
t change a partition type
v verify the partition table
i print information about a partition
Misc
m print this menu
u change display/entry units
x extra functionality (experts only)
Script
I load disk layout from sfdisk script file
O dump disk layout to sfdisk script file
Save & Exit
w write table to disk and exit
q quit without saving changes
Create a new label
g create a new empty GPT partition table
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table
接下来,由于我这里的硬盘大于 2TB,所以不能直接新建分区,需要先增加一个 GPT 标签。小于 2TB 的硬盘不需要这个操作。输入命令 g 回车。
Command (m for help): g
Created a new GPT disklabel (GUID: B5039588-BAAA-4FA9-8537-35E03F40E7FD).
然后我们看一下当前没有被分区的空间,输入 F 回车:
Command (m for help): F
Unpartitioned space /dev/sda: 7.3 TiB, 7999375523328 bytes, 15623780319 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
Start End Sectors Size
2048 15623782366 15623780319 7.3T
可以看到目前有 7.3TB 没有被分区。输入 n 新建一个分区。这里开始和结束都使用默认的即可,这样整块硬盘都会被分进去,如果想自己定义大小,那么自己输入大小即可。
Command (m for help): n
Partition number (1-128, default 1):
First sector (2048-15623782366, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-15623782366, default 15623782366):
Created a new partition 1 of type 'Linux filesystem' and of size 7.3 TiB.
再次输入 F 查看一下,发现已经全都被分区了。
Command (m for help): F
Unpartitioned space /dev/sda: 0 B, 0 bytes, 0 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
然后我们输入 v 确认一下,没有错误。
Command (m for help): v
No errors detected.
Header version: 1.0
Using 1 out of 128 partitions.
A total of 0 free sectors is available in 0 segments (the largest is (null)).
如果没有错误,就可以输入 w 保存了。
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
至此分区结束。
格式化硬盘
新增分区后,需要格式化一下硬盘。首先我们还是查看一下当前的硬盘信息。使用 df -h 发现还是看不到刚刚新增的分区,很正常,因为还没有挂载。
$ df -h
Filesystem Size Used Avail Use% Mounted on
udev 63G 0 63G 0% /dev
tmpfs 13G 35M 13G 1% /run
/dev/sdb2 343G 326G 0 100% /
tmpfs 63G 0 63G 0% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 63G 0 63G 0% /sys/fs/cgroup
/dev/sdb1 511M 3.4M 508M 1% /boot/efi
tmpfs 13G 16K 13G 1% /run/user/1003
tmpfs 13G 0 13G 0% /run/user/1001
tmpfs 13G 0 13G 0% /run/user/1002
tmpfs 13G 0 13G 0% /run/user/1000
再次查看所有硬盘,会看到已经有一个 7.3TB 的分区。
$ sudo fdisk -l
sudo: unable to resolve host csc-server-0-2
Disk /dev/sda: 7.3 TiB, 7999376588800 bytes, 15623782400 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: B5039588-BAAA-4FA9-8537-35E03F40E7FD
Device Start End Sectors Size Type
/dev/sda1 2048 15623782366 15623780319 7.3T Linux filesystem
Disk /dev/sdb: 477 GiB, 512110190592 bytes, 1000215216 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: 8D6EC320-DBA6-4239-9C96-EEBBD4E51C84
Device Start End Sectors Size Type
/dev/sdb1 2048 1050623 1048576 512M EFI System
/dev/sdb2 1050624 732002303 730951680 348.6G Linux filesystem
/dev/sdb3 732002304 1000214527 268212224 127.9G Linux swap
然后我们开始格式化。
$ sudo mkfs.ext4 /dev/sda1
sudo: unable to resolve host csc-server-0-2
mke2fs 1.42.13 (17-May-2015)
/dev/sda1 contains a ext3 file system
last mounted on Tue Dec 19 11:09:41 2017
Proceed anyway? (y,n) y
Creating filesystem with 1952972539 4k blocks and 244121600 inodes
Filesystem UUID: 785af015-7752-4f93-b314-ecb112757e7d
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, 1934917632
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
格式化的时间比较长,耐心等待,直到它全部 done 即可。
挂载分区
格式化完成后,就可以挂载分区了。
$ sudo mount /dev/sda1 /home
sudo: unable to resolve host csc-server-0-2
[sudo] password for we:
这里需要输入一下 sudo 密码。然后我们再查看一下当前的硬盘:
$ df -h
Filesystem Size Used Avail Use% Mounted on
udev 63G 0 63G 0% /dev
tmpfs 13G 35M 13G 1% /run
/dev/sdb2 343G 326G 0 100% /
tmpfs 63G 0 63G 0% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 63G 0 63G 0% /sys/fs/cgroup
/dev/sdb1 511M 3.4M 508M 1% /boot/efi
tmpfs 13G 16K 13G 1% /run/user/1003
tmpfs 13G 0 13G 0% /run/user/1001
tmpfs 13G 0 13G 0% /run/user/1000
/dev/sda1 7.3T 51M 6.9T 1% /home
可以看到,这块 7.3TB 的硬盘已经成功挂载到 /home 目录了。
最后我们需要添加一下开机自动挂载。
编辑 /etc/fstab 这个文件,然后加入:
UUID=785af015-7752-4f93-b314-ecb112757e7d /home ext4 defaults 0 2
其中,UUID 是之前格式化硬盘的时候显示的,大家可以往上翻看一看。/home 就是挂载点。 ext4 是文件格式, defaults 不需要修改。最后 0 2,建议也不修改。最后的 2 是针对所有非 boot 硬盘的,设置成 0 也可以,这样就是开机时候不检查这块硬盘。