Linux 硬盘分区
操作步骤
磁盘分区
- 运行fdisk -l命令查看实例上的数据盘。 会列出你挂载的数据盘(我这里有两块盘,分别是/dev/sda和/dev/sdb)
[root@zstack /]# fdisk -l
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
Disk /dev/sda: 320.1 GB, 320072933376 bytes, 625142448 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
Disk label type: gpt
Disk identifier: 20AB9F00-FD2F-428C-B5F2-898C8ABAF625
# Start End Size Type Name
1 2048 411647 200M EFI System EFI System Partition
2 411648 2508799 1G Microsoft basic
3 2508800 625141759 296.9G Linux LVM
Disk /dev/sdb: 2000.4 GB, 2000398934016 bytes, 3907029168 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
Disk label type: dos
Disk identifier: 0x6e87c628
Device Boot Start End Blocks Id System
/dev/sdb1 * 2048 3907026943 1953512448 7 HPFS/NTFS/exFAT
Disk /dev/mapper/zstack-root: 301.9 GB, 301935362048 bytes, 589717504 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
Disk /dev/mapper/zstack-swap: 16.8 GB, 16844324864 bytes, 32899072 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
- 依次运行以下命令,创建一个分区。
- 运行fdisk -u /dev/sdb命令分区数据盘。
- 输入p查看数据盘的分区情况。 本示例中,数据盘分了个sdb1。
- 输入n创建一个新分区。
- 输入p选择分区类型为主分区。
创建一个单分区数据盘可以只创建主分区。如果要创建四个以上分区,您应该至少选择一次e(extended),创建至少一个扩展分区。
- 输入分区编号并按回车键。 本示例中,仅创建一个分区,输入1。
- 输入第一个可用的扇区编号,按回车键采用默认值3907026944。
- 输入最后一个扇区编号。 本示例中,仅创建一个分区,按回车键采用默认值。
- 输入p查看该数据盘的规划分区情况。
- 输入w开始分区,并在完成分区后退出。
[root@zstack /]# fdisk -u /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): p
Disk /dev/sdb: 2000.4 GB, 2000398934016 bytes, 3907029168 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
Disk label type: dos
Disk identifier: 0x6e87c628
Device Boot Start End Blocks Id System
/dev/sdb1 * 2048 3907026943 1953512448 7 HPFS/NTFS/exFAT
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
Partition number (2-4, default 2): 2
First sector (3907026944-3907029167, default 3907026944):
Using default value 3907026944
Last sector, +sectors or +size{K,M,G} (3907026944-3907029167, default 3907029167):
Using default value 3907029167
Partition 2 of type Linux and of size 1.1 MiB is set
Command (m for help): p
Disk /dev/sdb: 2000.4 GB, 2000398934016 bytes, 3907029168 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
Disk label type: dos
Disk identifier: 0x6e87c628
Device Boot Start End Blocks Id System
/dev/sdb1 * 2048 3907026943 1953512448 7 HPFS/NTFS/exFAT
/dev/sdb2 3907026944 3907029167 1112 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
- 运行fdisk -lu /dev/sdb命令查看新分区。 如果出现/dev/sdb2的相关信息,表示新分区已创建完成。
[root@zstack /]# fdisk -lu /dev/sdb
Disk /dev/sdb: 2000.4 GB, 2000398934016 bytes, 3907029168 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
Disk label type: dos
Disk identifier: 0x6e87c628
Device Boot Start End Blocks Id System
/dev/sdb1 * 2048 3907026943 1953512448 7 HPFS/NTFS/exFAT
/dev/sdb2 3907026944 3907029167 1112 83 Linux
- 运行mkfs.ext4 /dev/sdb2命令在新分区上创建一个文件系统。(格式化磁盘)
本示例中,创建一个ext4文件系统。其他文件系统格式请自行修改mkfs.ext4命令。例如,如果您需要在Linux、Windows和Mac系统之间共享文件,可以运行mkfs.vfat /dev/sdb2命令创建VFAT文件系统。
[root@zstack /]# mkfs.ext4 /dev/sdb2
mke2fs 1.42.9 (28-Dec-2013)
Filesystem too small for a journal
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
144 inodes, 1112 blocks
55 blocks (4.95%) reserved for the super user
First data block=1
Maximum filesystem blocks=1179648
1 block group
8192 blocks per group, 8192 fragments per group
144 inodes per group
Allocating group tables: done
Writing inode tables: done
Writing superblocks and filesystem accounting information: done
- 挂载磁盘
[root@zstack /]# mkdir /data
[root@zstack /]# mount /dev/sdb2 /data
- 查看是否挂载正常
[root@zstack /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/zstack-root 282G 28G 254G 10% /
devtmpfs 16G 52K 16G 1% /dev
tmpfs 16G 8.0K 16G 1% /dev/shm
tmpfs 16G 26M 16G 1% /run
tmpfs 16G 0 16G 0% /sys/fs/cgroup
/dev/sda2 1014M 163M 852M 17% /boot
/dev/sda1 200M 9.8M 191M 5% /boot/efi
tmpfs 3.2G 0 3.2G 0% /run/user/0
/dev/sdb2 1.1M 22K 990K 3% /data
- 添加开机挂载
[root@zstack /]# echo /dev/sdb2 /data ext4 defaults 0 0 >> /etc/fstab
[root@zstack /]# cat /etc/fstab |grep /dev/sdb2
/dev/sdb2 /data ext4 defaults 0 0
- 测试下fstab文件是否写入正常,如果写入有问题,磁盘不存在等情况,系统将无法开机
无任何输出证明fstab写入正常
[root@zstack /]# mount -a
查看硬盘分区情况
[root@zstack /]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 298.1G 0 disk
├─sda1 8:1 0 200M 0 part /boot/efi
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 296.9G 0 part
├─zstack-root 253:0 0 281.2G 0 lvm /
└─zstack-swap 253:1 0 15.7G 0 lvm [SWAP]
sdb 8:16 0 1.8T 0 disk
├─sdb1 8:17 0 1.8T 0 part
└─sdb2 8:18 0 1.1M 0 part
如果分区错误
删除分区
- 运行fdisk -u /dev/sdb命令分区数据盘。
- 输入d选择删除分区
- 输入要删除的分区
[root@zstack /]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): d
Partition number (1,2, default 2): 1
Partition 1 is deleted
Command (m for help): d
Selected partition 2
Partition 2 is deleted
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
验证
[root@zstack /]# fdisk -l
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
Disk /dev/sda: 320.1 GB, 320072933376 bytes, 625142448 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
Disk label type: gpt
Disk identifier: 20AB9F00-FD2F-428C-B5F2-898C8ABAF625
# Start End Size Type Name
1 2048 411647 200M EFI System EFI System Partition
2 411648 2508799 1G Microsoft basic
3 2508800 625141759 296.9G Linux LVM
Disk /dev/sdb: 2000.4 GB, 2000398934016 bytes, 3907029168 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
Disk label type: dos
Disk identifier: 0x6e87c628
Device Boot Start End Blocks Id System
Disk /dev/mapper/zstack-root: 301.9 GB, 301935362048 bytes, 589717504 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
Disk /dev/mapper/zstack-swap: 16.8 GB, 16844324864 bytes, 32899072 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