Linux扩容-新增磁盘分区挂载-fdisk
Linux扩容-新增磁盘分区挂载-fdisk
扩容后的云硬盘,挂载给实例后,需要对扩容的部分划分分区并初始化。本节以“CentOS 7.0 64位”操作系统为例,介绍使⽤fdisk⼯具为扩容后的磁盘创建新的分区。
⼀背景信息
扩容成功后,需要为扩容部分的容量创建新的分区,或者重新创建分区替换原有分区。
为扩容部分的容量创建新的分区
该场景下,不需要卸载原有的分区,⽽是在原有分区的基础上新增分区。该操作不会中断业务,对业务影响较⼩。推荐系统盘或者需要保证业务不中断的磁盘扩容场景使⽤。如果当前磁盘使⽤的是MBR格式,则要求扩容后的磁盘容量不超过2TB,并且磁盘的分区数量还未达到上限。
重新创建分区替换原有分区
如果当前磁盘使⽤的是MBR分区,且磁盘分区数量已达到上限,⽆法再增加新的分区。此时需要先卸载已
有分区,重新创建新的分区替换原有分区。该操作不会删除原有分区的数据,但操作过程中需预先中断业务,因此会对⽤户正在运⾏的业务产⽣影响。
MBR分区转换为GPT分区
如果当前磁盘使⽤的是MBR分区,且扩容后磁盘容量已超过2TB,则此时需要将MBR分区转换为GPT分区,该操作会清除磁盘上的数据,请您预先对原数据进⾏备份。
⼆挂载操作步骤
以下步骤,举例说明如何将磁盘扩容部分的空间划分为⼀个新的分区,并挂载到“/opt”下。
该磁盘采⽤MBR分区,磁盘容量⼩于2TB,且分区数量未达到上限。分区⽅式为MBR时,可以使⽤fdisk⼯具或parted⼯具划分分区,本节以fdisk⼯具为例。
1. 执⾏以下命令,查看磁盘信息。
fdisk -l
回显类似如下信息,“/dev/xvda”表⽰系统盘。
[root@ecs-bab9 test]# fdisk -l
Disk /dev/xvda: 64.4 GB, 64424509440 bytes, 125829120 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: 0x000cc4ad
Device Boot      Start        End      Blocks  Id  System
/dev/xvda1  *        2048    2050047    1024000  83  Linux
/dev/xvda2        2050048    22530047    10240000  83  Linux
/dev/xvda3        22530048    24578047    1024000  83  Linux
/
dev/xvda4        24578048    83886079    29654016    5  Extended
/dev/xvda5        24580096    26628095    1024000  82  Linux swap / Solaris
2. 执⾏以下命令,进⼊fdisk模式,以“/dev/xvda”为例。
fdisk /dev/xvda
回显类似如下信息:
[root@ecs-bab9 test]# fdisk /dev/xvda
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):
3. 输⼊“n”,按“Enter”,开始新建分区。
本例中由于系统盘原来已经有5个分区,所以系统⾃动添加第6分区。
回显类似如下信息:
Command (m for help): n
All primary partitions are in use
Adding logical partition 6
First sector (26630144-83886079, default 26630144):
4. 输⼊新分区的起始磁柱编号,如设置默认值,按“Enter”。
起始磁柱编号必须⼤于原有分区的结束磁柱编号。
回显类似如下信息:
First sector (26630144-83886079, default 26630144):
Using default value 26630144
Last sector, +sectors or +size{K,M,G} (26630144-83886079, default 83886079): 5. 输⼊新分区的截⽌磁柱编号,按“Enter”。
本步骤中使⽤默认截⽌磁柱编号为例。
回显类似如下信息:
Last sector, +sectors or +size{K,M,G} (26630144-83886079, default 83886079): Using default value 83886079
Partition 6 of type Linux and of size 27.3 GiB is set
Command (m for help):
6. 输⼊“p”,按“Enter”,查看新建分区。
回显类似如下信息:
Disk /dev/xvda: 64.4 GB, 64424509440 bytes, 125829120 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: 0x000cc4ad
Device Boot      Start        End      Blocks  Id  System
/dev/xvda1  *        2048    2050047    1024000  83  Linux
/dev/xvda2        2050048    22530047    10240000  83  Linux
/dev/xvda3        22530048    24578047    1024000  83  Linux
/dev/xvda4        24578048    83886079    29654016    5  Extended
/dev/xvda5        24580096    26628095    1024000  82  Linux swap / Solaris
/dev/xvda6        26630144    83886079    28627968  83  Linux
Command (m for help):
7. 输⼊“w”,按“Enter”。将分区结果写⼊分区表中,分区创建完毕。
回显类似如下信息:
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.
8. 执⾏partprobe命令,将新的分区表变更同步⾄操作系统。
partprobe
9. 执⾏以下命令,设置新建分区⽂件系统格式(df -T查看原有⽂件系统)。
以“ext4” ⽂件格式为例:
mkfs -t ext4 /dev/xvda6
回显类似如下信息:
[root@ecs-bab9 test]# mkfs -t ext4 /dev/xvda6
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1790544 inodes, 7156992 blocks
357849 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2155872256
219 block groups
32768 blocks per group, 32768 fragments per group
8176 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
格式化需要等待⼀段时间,请观察系统运⾏状态,若回显中进程提⽰为done,则表⽰格式化完成。
10. 执⾏以下命令,将新建分区挂载到需要增加空间的⽬录下,以“/opt”为例。
mount /dev/xvda6 /opt
回显类似如下信息:
[root@ecs-bab9 test]# mount /dev/xvda6 /opt
[root@ecs-bab9 test]#
说明:
新增加的分区挂载到不为空的⽬录时,该⽬录下原本的⼦⽬录和⽂件会被隐藏,所以,新增的分区最好
挂载到空⽬录或者新建⽬录。如确实要挂载到不为空的⽬录,可将该⽬录下的⼦⽬录和⽂件临时移动到其他⽬录下,新分区挂载成功后,再将⼦⽬录和⽂件移动回来。
11. 执⾏以下命令,查看挂载结果。
df -TH
回显类似如下信息:
[root@ecs-bab9 test]# df -TH
Filesystem    Type      Size  Used Avail Use% Mounted on
/dev/xvda2    xfs        11G  7.4G  3.2G  71% /
devtmpfs      devtmpfs  4.1G    0  4.1G  0% /dev
tmpfs          tmpfs    4.1G  82k  4.1G  1% /dev/shm
tmpfs          tmpfs    4.1G  9.2M  4.1G  1% /run
tmpfs          tmpfs    4.1G    0  4.1G  0% /sys/fs/cgroup
/dev/xvda3    xfs      1.1G  39M  1.1G  4% /home
/dev/xvda1    xfs      1.1G  131M  915M  13% /boot
/dev/xvda6    ext4      29G  47M  28G  1% /opt
三设置开机⾃动挂载磁盘
如果您需要在实例启动时⾃动挂载磁盘,请在上述操作之后参考本节设置开机⾃动挂载磁盘。设置时不能采⽤在 /etc/fstab直接指定
/dev/xvdb1的⽅法,因为实例中设备的顺序编码在关闭或者开启实例过程中可能发⽣改变。推荐使⽤UUID来配置⾃动挂载数据盘。
说明:
磁盘的UUID(Universally Unique Identifier)是Linux系统为存储设备提供的唯⼀的标识字符串。
1. 执⾏如下命令,查询磁盘分区的UUID。
blkid 磁盘分区
以查询磁盘分区“/dev/xvdb1”的UUID为例:
blkid /dev/xvdb1
回显类似如下信息,表⽰“/dev/xvdb1”的UUID。
[root@ecs-b656 test]# blkid /dev/xvdb1
/dev/xvdb1: UUID="1851e23f-1c57-40ab-86bb-5fc5fc606ffa" TYPE="ext4" 2. 执⾏以下命令,使⽤VI编辑器打开“fstab”⽂件。
vi /etc/fstab
3. 按“i”,进⼊编辑模式。
4. 将光标移⾄⽂件末尾,按“Enter”,添加如下内容(UUID)。
UUID=1851e23f-1c57-40ab-86bb-5fc5fc606ffa /mnt/sdc      ext4 defaults    0  2 5. 按“Esc”后,输⼊“:wq”,按“Enter”。
保存设置并退出编辑器。
>linux磁盘管理