1.查看硬盘
fdisk -l
#发现硬盘为/dev/sdb 大小4T
#Disk /dev/sdb: 4096 GB, 8796093022208 bytes, 17179869184 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
2.磁盘格式化:
如果此硬盘以前有过分区,则先对磁盘格式化: 此命令会对整个磁盘格式化
mkfs -t ext4 /dev/sdb
3:使用parted进行GPT格式分区:
对新磁盘进行分区,由于fdisk仅支持2T以内磁盘分区,但该磁盘大于2T,所以使用parted进行GPT格式分区:
# parted /dev/sdb
(parted) help # 可以查看帮助信息
(parted) mklabel gpt # 执行命令mklabel gpt,把sdb改成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 primary 0 4001GB # 创建分区
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? cancel # 注意,此处提示为磁盘为对齐,会影响性能,所以在此处要进行对齐
4.查看磁盘参数:
# cat /sys/block/sdb/queue/optimal_io_size
# cat /sys/block/sdb/queue/minimum_io_size
# cat /sys/block/sdb/alignment_offset
# cat /sys/block/sdb/queue/physical_block_size
1234
如果optimal_io_size不为0,暂定是1048576,则可以用optimal_io_size的值加上alignment_offset的值,
然后除以physical_block_size的值,就是起始扇区的位置了,如上面的例子中,可以用这个公式算出来起始扇区的设定值:
(1048576 + 0) / 512 = 2048
如果optimal_io_size为0,那么我们最好直接使用起始扇区的默认值2048。
5.分区:
最后在分区的时候使用以下的命令进行分区就可以了,不会报warning:
(parted) mkpart primary 2048s 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 primary
(parted) align-check optimal 1 # 此命令会检查对齐与否
1 aligned
(parted) quit
6.查看磁盘情况:
fdisk -l
7.再次格式化磁盘:
mkfs.ext4 -F /dev/sdb
8.挂载新磁盘到/home
cd /mnt
mkdir home
mount /dev/sdb /mnt/home
cp -a /home/* home
rm -rf /home/*
umount /dev/sdb
9.设置自动挂载/home
vim /etc/fstab
#写入/etc/fstab
/dev/sdb /home ext4 defaults 1 2
# 保存退出
:wq
10.查看是否挂载/home(未挂载! )
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 100G 5.4G 95G 6% /
devtmpfs 32G 0 32G 0% /dev
tmpfs 32G 29M 32G 1% /dev/shm
tmpfs 32G 12M 32G 1% /run
tmpfs 32G 0 32G 0% /sys/fs/cgroup
/dev/sda2 1014M 221M 794M 22% /boot
/dev/sda1 200M 9.8M 191M 5% /boot/efi
/dev/mapper/centos-home 74G 52M 74G 1% /home
tmpfs 6.3G 56K 6.3G 1% /run/user/1000
11.挂载fstab中未挂载的磁盘
mount -a
12.再次查看是否挂载/home
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 100G 5.4G 95G 6% /
devtmpfs 32G 0 32G 0% /dev
tmpfs 32G 29M 32G 1% /dev/shm
tmpfs 32G 12M 32G 1% /run
tmpfs 32G 0 32G 0% /sys/fs/cgroup
/dev/sda2 1014M 221M 794M 22% /boot
/dev/sda1 200M 9.8M 191M 5% /boot/efi
/dev/sdb 3.6T 140M 3.4T 1% /home
tmpfs 6.3G 56K 6.3G 1% /run/user/1000