先看看我的环境:

[root@cloudera ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        28G  7.0G   20G  27% /
devtmpfs        1.9G     0  1.9G   0% /dev
tmpfs           1.9G   88K  1.9G   1% /dev/shm
tmpfs           1.9G  8.9M  1.9G   1% /run
tmpfs           1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1       509M  164M  346M  33% /boot
tmpfs           378M   20K  378M   1% /run/user/0
/dev/sdb1       2.0G  6.0M  1.8G   1% /mount_point/1
/dev/sdb2       991M  2.6M  922M   1% /mount_point/2
[root@cloudera ~]# 
[root@cloudera ~]# fdisk -l /dev/sdb

Disk /dev/sdb: 3221 MB, 3221225472 bytes, 6291456 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: 0xbb5c389b

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux
/dev/sdb2         4196352     6291455     1047552   83  Linux
[root@cloudera ~]# 

可以看到:/dev/sdb2,是直接挂载起来的,该分区没有做成逻辑卷。
需求:对/dev/sdb2扩容,但不能破坏其中的数据。

看看其中的数据:

[root@cloudera ~]# ls -ltr --time-style="+|%Y-%m-%d|%H:%M:%S|" /mount_point/2/
total 451040
-rw-r--r-- 1 root root 142039186 |2017-10-10|09:48:49| oracle-j2sdk1.7-1.7.0+update67-1.x86_64.rpm
-rw-r--r-- 1 root root 319800836 |2017-10-24|17:58:54| jdk-9.0.1_linux-x64_bin.rpm
drwx------ 2 root root     16384 |2017-10-30|10:46:28| lost+found
[root@cloudera ~]# 
[root@cloudera ~]# du -sh /mount_point/2/*
305M    /mount_point/2/jdk-9.0.1_linux-x64_bin.rpm
16K     /mount_point/2/lost+found
136M    /mount_point/2/oracle-j2sdk1.7-1.7.0+update67-1.x86_64.rpm
[root@cloudera ~]# 
[root@cloudera ~]# cksum /mount_point/2/oracle-j2sdk1.7-1.7.0+update67-1.x86_64.rpm 
707345721 142039186 /mount_point/2/oracle-j2sdk1.7-1.7.0+update67-1.x86_64.rpm
[root@cloudera ~]# 
[root@cloudera ~]# cksum /mount_point/2/jdk-9.0.1_linux-x64_bin.rpm 
2567089852 319800836 /mount_point/2/jdk-9.0.1_linux-x64_bin.rpm
[root@cloudera ~]# 

看看当前系统状态:

[root@cloudera ~]# uname -r
3.10.0-514.el7.x86_64
[root@cloudera ~]# 
[root@cloudera ~]# cat /etc/redhat-release 
CentOS Linux release 7.3.1611 (Core) 
[root@cloudera ~]# 

关闭虚拟机,对磁盘/dev/sdb扩容:

重新启动后,会发现/dev/sdb的空间已经上去了:

[root@cloudera ~]# fdisk -l /dev/sdb

Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 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: 0xbb5c389b

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux
/dev/sdb2         4196352     6291455     1047552   83  Linux
[root@cloudera ~]# 

开始扩容:

卸载分区,然后删除分区,并重建:

[root@cloudera ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        28G  7.0G   20G  27% /
devtmpfs        1.9G     0  1.9G   0% /dev
tmpfs           1.9G   84K  1.9G   1% /dev/shm
tmpfs           1.9G  8.9M  1.9G   1% /run
tmpfs           1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1       509M  164M  346M  33% /boot
tmpfs           378M   16K  378M   1% /run/user/42
tmpfs           378M     0  378M   0% /run/user/0
[root@cloudera ~]# 
[root@cloudera ~]# 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): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 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: 0xbb5c389b

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux
/dev/sdb2         4196352     6291455     1047552   83  Linux

Command (m for help): d
Partition number (1,2, default 2): 2
Partition 2 is deleted

Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 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: 0xbb5c389b

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux

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): 
First sector (4196352-10485759, default 4196352): 
Using default value 4196352
Last sector, +sectors or +size{K,M,G} (4196352-10485759, default 10485759): 
Using default value 10485759
Partition 2 of type Linux and of size 3 GiB is set

Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 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: 0xbb5c389b

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux
/dev/sdb2         4196352    10485759     3144704   83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@cloudera ~]# 

可以看到,/dev/sdb2的结束柱面已经增加上去了。

这一步,可能会认为数据会被破坏?
其实不会,增加,是不会的。

检查文件系统:

[root@cloudera ~]# e2fsck -f /dev/sdb2 
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sdb2: 13/65536 files (0.0% non-contiguous), 121615/261888 blocks
[root@cloudera ~]# 

作用到文件系统:

[root@cloudera ~]# resize2fs /dev/sdb2 
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/sdb2 to 786176 (4k) blocks.
The filesystem on /dev/sdb2 is now 786176 blocks long.

[root@cloudera ~]# 

重新挂载:

[root@cloudera ~]# mount /dev/sdb2 /mount_point/2/
[root@cloudera ~]# 
[root@cloudera ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        28G  7.0G   20G  27% /
devtmpfs        1.9G     0  1.9G   0% /dev
tmpfs           1.9G   84K  1.9G   1% /dev/shm
tmpfs           1.9G  8.9M  1.9G   1% /run
tmpfs           1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1       509M  164M  346M  33% /boot
tmpfs           378M   16K  378M   1% /run/user/42
tmpfs           378M     0  378M   0% /run/user/0
/dev/sdb2       3.0G  444M  2.4G  16% /mount_point/2
[root@cloudera ~]# 

可以看到,空间已经扩展成功了。

来看看其中的数据:

[root@cloudera ~]# ls -ltr /mount_point/2/
total 451040
-rw-r--r-- 1 root root 142039186 Oct 10 09:48 oracle-j2sdk1.7-1.7.0+update67-1.x86_64.rpm
-rw-r--r-- 1 root root 319800836 Oct 24 17:58 jdk-9.0.1_linux-x64_bin.rpm
drwx------ 2 root root     16384 Oct 30 10:46 lost+found
[root@cloudera ~]# 
[root@cloudera ~]# du -sh /mount_point/2/
441M    /mount_point/2/
[root@cloudera ~]# 
[root@cloudera ~]# du -sh /mount_point/2/*
305M    /mount_point/2/jdk-9.0.1_linux-x64_bin.rpm
16K     /mount_point/2/lost+found
136M    /mount_point/2/oracle-j2sdk1.7-1.7.0+update67-1.x86_64.rpm
[root@cloudera ~]# 
[root@cloudera ~]# cksum /mount_point/2/jdk-9.0.1_linux-x64_bin.rpm 
2567089852 319800836 /mount_point/2/jdk-9.0.1_linux-x64_bin.rpm
[root@cloudera ~]# 
[root@cloudera ~]# cksum /mount_point/2/oracle-j2sdk1.7-1.7.0+update67-1.x86_64.rpm 
707345721 142039186 /mount_point/2/oracle-j2sdk1.7-1.7.0+update67-1.x86_64.rpm
[root@cloudera ~]# 

可以看到,数据都还在,没有损坏。

————————————————————————————————
Done。

说点什么

avatar

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

  Subscribe  
提醒