当前位置:首页 > Linux
Linux 系统下给根分区扩容
来源:靑龍一笑的博客  作者:靑龍一笑  发布时间:2022-12-20 19:48:54  点击量:87  评论:0

    近日,在进行服务器硬盘更换过程中,发现早期不合理的硬盘分区情况。

[root@RicenOS data]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        14G  1.3G   12G  10% /
tmpfs           499M     0  499M   0% /dev/shm
/dev/sda3        14G  316M   13G   3% /data
[root@RicenOS data]# fdisk -l
Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000b98ad

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1         262     2097152   82  Linux swap / Solaris
Partition 1 does not end on cylinder boundary.
/dev/sda2   *         262        2089    14679040   83  Linux
/dev/sda3            2089        3916    14678054   83  Linux

    可以看到,/dev/sda1 是交换分区,/dev/sda2 是根分区,/dev/sda3 是一个数据分区。
    现在要做的是,挂载一块新的硬盘,将数据分区上的数据拷贝到新硬盘上,然后将 /dev/sda2 和 /dev/sda3 这两个分区合并,以达到根分区扩容的目的。

[root@RicenOS ~]# fdisk -l
Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000b98ad

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1         262     2097152   82  Linux swap / Solaris
Partition 1 does not end on cylinder boundary.
/dev/sda2   *         262        2089    14679040   83  Linux
/dev/sda3            2089        3916    14678054   83  Linux

Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

    新硬盘是 /dev/sdb,创建一个目录用来挂载之前的数据分区,即 /dev/sda3。

[root@RicenOS ~]# mkdir -pv /old_data

    卸载 /dev/sda3:

[root@RicenOS ~]# umount /data/

    重新挂载 /dev/sda3:

[root@RicenOS ~]# mount /dev/sda3 /old_data/

    对新硬盘进行 lvm 分区,并挂载到 /data 目录上,详细的操作请参考我的另一篇文章《给 Linux 系统添加新硬盘,并创建 LVM 分区》。

[root@RicenOS ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              14G  1.3G   12G  10% /
tmpfs                 499M     0  499M   0% /dev/shm
/dev/sda3              14G  316M   13G   3% /old_data
/dev/mapper/data-data 7.8G   18M  7.4G   1% /data

    将旧数据迁移到新硬盘上:

[root@RicenOS ~]# ls /data/
lost+found
[root@RicenOS ~]# ls /old_data/
lost+found  ricen_file
[root@RicenOS ~]# mv /old_data/ricen_file/ /data/
[root@RicenOS ~]# ls /data/
lost+found  ricen_file
[root@RicenOS ~]# ls /old_data/
lost+found

    卸载旧分区:

[root@RicenOS ~]# umount /old_data/

    删除没用的挂载点:

[root@RicenOS ~]# rm -rf /old_data/

    扩容根分区:

[root@RicenOS ~]# fdisk -cu /dev/sda
Command (m for help): p

Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders, total 62914560 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 identifier: 0x000b98ad

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048     4196351     2097152   82  Linux swap / Solaris
/dev/sda2   *     4196352    33554431    14679040   83  Linux
/dev/sda3        33554432    62910539    14678054   83  Linux

Command (m for help): d
Partition number (1-4): 3

Command (m for help): d
Partition number (1-4): 2

Command (m for help): p

Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders, total 62914560 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 identifier: 0x000b98ad

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048     4196351     2097152   82  Linux swap / Solaris

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First sector (4196352-62914559, default 4196352): 
Using default value 4196352
Last sector, +sectors or +size{K,M,G} (4196352-62914559, default 62914559): 
Using default value 62914559

Command (m for help): p

Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders, total 62914560 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 identifier: 0x000b98ad

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048     4196351     2097152   82  Linux swap / Solaris
/dev/sda2         4196352    62914559    29359104   83  Linux

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: 设备或资源忙.
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@RicenOS ~]# reboot

    调整 /dev/sda2 分区大小:

[root@RicenOS ~]# resize2fs /dev/sda2
[root@RicenOS ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              28G  1.3G   25G   5% /
tmpfs                 499M     0  499M   0% /dev/shm
/dev/mapper/data-data 7.8G  299M  7.1G   4% /data
版权所有 © 2005-2023 靑龍一笑的博客  Powered by C.S.Ricen
Copyright © 2005-2023 by www.ricensoftwares.com.cn  All Rights Reserved.

欢迎光临本站,这里是靑龍一笑的博客。

因资金匮乏,本站已迁到国外的免费空间,可能导致本站的访问速度较慢,由此给您带来的不便,敬请谅解。

您可以通过下方的“支持本站建设”链接,给本站提供资金支持。

Free Web Hosting