本文转载自:https://blog.csdn.net/cruise_h/article/details/22403529

本文讨论如何再不丢失数据的情况下调整已有ext3分区的大小,包括:

  1. 压缩已有分区
  2. 增大已有分区
  3. 合并两个ext3分区

这在没有使用LVM(逻辑卷管理),而已有分区规划不能满足要求时,非常有用。

前提

1.拥有root权限

用root登录,或者sudo操作

2.被操作的分区的文件系统必须已被卸载(umount)

2.1对于不包含系统重要文件的分区,如/home分区,可以直接umount。
2.2对于包含系统重要文件的分区,如/分区,往往不可能在使用时umount,则需要借助Live CD。

如果在生产环境中实施,一定记得备份好你的数据!以防万一因新分区尺寸等问题导致数据意味丢失。

以下以较复杂情况(使用Live CD)为例,说明调整ext3分区的步骤。

案例1:压缩一个ext3分区

shell>df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1             9.5G  4.1G  4.9G  46% /
varrun                 94M  132K   94M   1% /var/run
varlock                94M     0   94M   0% /var/lock
udev                   10M   52K   10M   1% /dev
devshm                 94M     0   94M   0% /dev/shm
lrm                    94M   18M   77M  19% /lib/modules/2.6.17-10-generic/volatile

本案例意在压缩分区/dev/sda1。

shell>fdisk -l
Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        1247    10016496   83  Linux
/dev/sda2            1248        1305      465885    5  Extended
/dev/sda5            1248        1305      465853+  82  Linux swap / Solaris
shell>fdisk -s /dev/sda1
10016496

接着,我们重启系统,使用Live CD进入系统(如果你需要修改的分区不包含重要系统文件,则无需此步骤,可以直接操作)。

shell>shutdown -r now

运行Live CD进入系统后,切换成root,进行后续操作:

shell>su 
shell>umount /dev/sda1 #确保需要调整的分区被umount
shell>fsck -n /dev/sda1

fsck 1.38 (30-Jun-2005)
e2fsck 1.38 (30-Jun-2005)
/dev/sda1: clean, 159037/1254176 files, 1095299/2504124 blocks

接下来我们移除/dev/sda1上的文件系统日志信息,将/dev/sda1分区上的文件系统降为ext2

shell>tune2fs -O ^has_journal /dev/sda1

tune2fs 1.38 (30-Jun-2005)

shell>e2fsck -f /dev/sda1

e2fsck 1.38 (30-Jun-2005)
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/sda1: 164178/1254176 files (0.6% non-contiguous), 1051617/2504124 blocks

检查无误后,我们使用resize2fs调整文件系统大小。resize2fs只能调整ext2文件系统,这就是为什么我们之前将/dev/sda1转化成了ext2

shell>resize2fs /dev/sda1 6000M 
resize2fs 1.38 (30-Jun-2005)
Resizing the filesystem on /dev/sda1 to 1536000 (4k) blocks.
The filesystem on /dev/sda1 is now 1536000 blocks long.

需要记录下这里的blocks数目1536000和他们的大小4k,我们后面会使用到。
接下来我们删除/dev/sda1分区(不用担心,不会有数据丢失),且创建一个新的,较小(适合我们调整后的文件系统)的分区。

shell>fdisk /dev/sda   (注意:不是sda1!是针对整个磁盘的操作)
The number of cylinders for this disk is set to 1305.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)
#看下帮助
Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition’s system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

#删除分区

Command (m for help): d
Partition number (1-5): 1    #指定分区号

#创建新分区/dev/sda1,它之前是主分区,现在依然选择主分区p

Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
p
Partition number (1-4): 1

#接下来是关键的步骤,我们需要指定新分区的大小,第一个柱面是确定的(通过fdisk -l可以查到),最后一个柱面需要计算,我们根据resize2fs的输出进行计算,为了确保分区够大,我们加上%3~5%:1536000 * 4k * 1.03 = 6328320k

First cylinder (1-1305, default 1): 1
Last cylinder or +size or +sizeM or +sizeK (1-1247, default 1247): +6328320K #大写K

我们原有的/dev/sda1有boot标记,即开机启动,新分区也加上:

Command (m for help): a
Partition number (1-5): 1

接着我们将之前变更写入分区表,并退出:

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.
Syncing disks.

重启系统,再次进入Live CD。

shell>shutdown -r now
shell>su
shell>fsck -n /dev/sda1

 fsck 1.38 (30-Jun-2005)
e2fsck 1.38 (30-Jun-2005)
/dev/sda1: clean, 159036/765536 files, 1047239/1536000 blocks

shell>tune2fs -j /dev/sda1 #为ext2加上日志,转化为ext3
tune2fs 1.38 (30-Jun-2005)
Creating journal inode: done
This filesystem will be automatically checked every 30 mounts or
0 days, whichever comes first. Use tune2fs -c or -i to override.

#再次重启,取出Live CD,进入到原系统

shell>shutdown -r now
shell>df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1             5.8G  4.1G  1.6G  73% /
varrun                 94M  132K   94M   1% /var/run
varlock                94M     0   94M   0% /var/lock
udev                   10M   52K   10M   1% /dev
devshm                 94M     0   94M   0% /dev/shm
lrm                    94M   18M   77M  19% /lib/modules/2.6.17-10-generic/volatile
shell>fdisk -l
Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1         789     6337611   83  Linux
/dev/sda2            1248        1305      465885    5  Extended
/dev/sda5            1248        1305      465853+  82  Linux swap / Solaris
shell>fdisk -s /dev/sda1
6337611

案例2:扩展一个ext3分区

经过案例1,我们空出了4G的未使用空间,在分区/dev/sda1之后。这里我们期望将这部分空间添加到我们的/dev/sda1分区上(这只有在未使用空间正好紧邻该分区时才可以实现!)。

先查看下现有分区信息:

shell>df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1             5.8G  4.1G  1.6G  73% /
varrun                 94M  132K   94M   1% /var/run
varlock                94M     0   94M   0% /var/lock
udev                   10M   52K   10M   1% /dev
devshm                 94M     0   94M   0% /dev/shm
lrm                    94M   18M   77M  19% /lib/modules/2.6.17-10-generic/volatile
shell>fdisk -l
Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1         789     6337611   83  Linux
/dev/sda2            1248        1305      465885    5  Extended
/dev/sda5            1248        1305      465853+  82  Linux swap / Solaris
shell>fdisk -s /dev/sda1
6337611

#增大分区依然要求分区被umount,与案例1类似,重启,使用Live CD,进入后切换为root后执行

shell>umount /dev/sda1
shell>fsck -n /dev/sda1
fsck 1.38 (30-Jun-2005)
e2fsck 1.38 (30-Jun-2005)
/dev/sda1: clean, 159036/765536 files, 1080014/1536000 blocks
shell>tune2fs -O ^has_journal /dev/sda1  #转化为ext2文件系统

tune2fs 1.38 (30-Jun-2005)

接下来删除分区(数据并不会丢失),并创建较大的新分区(包含要扩展的部分)
shell>fdisk /dev/sda
The number of cylinders for this disk is set to 1305.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)
Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition’s system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)
Command (m for help): p

Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1         789     6337611   83  Linux
/dev/sda2            1248        1305      465885    5  Extended
/dev/sda5            1248        1305      465853+  82  Linux swap / Solaris
Command (m for help): d   #删除分区
Partition number (1-5): 1    
Command (m for help): n   #新建分区
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
p   #设置为主分区
Partition number (1-4): 1
First cylinder (1-1305, default 1): 1
Last cylinder or +size or +sizeM or +sizeK (1-1247, default 1247): 1247   #这里fdisk检测到能够到达的最大值,使用该值即可
Command (m for help): p

Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1        1247    10016496   83  Linux
/dev/sda2            1248        1305      465885    5  Extended
/dev/sda5            1248        1305      465853+  82  Linux swap / Solaris
Command (m for help): a  #添加boot标记
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.
Syncing disks.

然后我们重启系统,继续进入Live CD,切换为root:

shell>e2fsck -f /dev/sda1
shell>resize2fs /dev/sda1  #不指定大小,就会最大的可用空间,所以可以不计算
resize2fs 1.38 (30-Jun-2005)
Resizing the filesystem on /dev/sda1 to 2504124 (4k) blocks.
The filesystem on /dev/sda1 is now 2504124 blocks long.
shell>fsck -n /dev/sda1
fsck 1.38 (30-Jun-2005)
e2fsck 1.38 (30-Jun-2005)
/dev/sda1: clean, 159036/1254176 files, 1062544/2504124 blocks
shell>tune2fs -j /dev/sda1  #添加日志,再次转化为ext3
tune2fs 1.38 (30-Jun-2005)
Creating journal inode: done
This filesystem will be automatically checked every 30 mounts or
0 days, whichever comes first. Use tune2fs -c or -i to override.

之后就可以重启进入原系统,查看新的分区信息

shell>df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1             9.5G  4.1G  5.0G  45% /
varrun                 94M  132K   94M   1% /var/run
varlock                94M     0   94M   0% /var/lock
udev                   10M   52K   10M   1% /dev
devshm                 94M     0   94M   0% /dev/shm
lrm                    94M   18M   77M  19% /lib/modules/2.6.17-10-generic/volatile
shell>fdisk -l
Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        1247    10016496   83  Linux
/dev/sda2            1248        1305      465885    5  Extended
/dev/sda5            1248        1305      465853+  82  Linux swap / Solaris
shell>fdisk -s /dev/sda1
10016496

小结

经过上述案例,我们可以了解到调整ext3分区的基本思路:

步骤1)umount 需要调整的分区

步骤2)将ext3降为ext2,删除日志功能

步骤3)牢记,文件系统是建立在分区之上的。所以,如果要缩小分区,先缩小文件系统,再缩小分区;要扩展分区,先扩展分区,再扩展文件系统!

调整文件系统步骤:先使用tune2f将ext3降为ext2,然后使用resize2fs调整ext2文件系统大小,最终tune2f再将ext2升级为ext3;

调整分区步骤:删除原分区,创建新分区,注意分区大小的指定,以及是否boot,是否主分区等。

案例3:合并两个ext3分区

有了上述两个案例做铺垫,我们可以延伸操作本案例。首先前提,两个分区是相邻分区才能够进行合并。

基本思路:

  1. 将后一个分区的文件系统umount,且删除该分区(因数据会被清除,如果需要保留,提前备份!),这样就有了空闲空间,形成于案例2一样的情况。
  2. 按照案例2的操作步骤依次处理即可。

主要注意的是:

1)如果被删除的分区,在/etc/fstab上有记录,删除之!
2)被删除分区的数据也会被删除,再次提醒,如果需要,备份!

具体操作不再详述,参考前两个案例。

参考:http://www.howtoforge.com/linux_resizing_ext3_partitions

Linux下调整ext3分区大小【转】的更多相关文章

  1. Win7系统下调整硬盘分区大小给C盘更多的空间

    电脑安装了很多程序,C盘空间越来越小了.如何给C盘调整更多的空间,其实只要调整硬盘分区大小便可解决这个问题,下面有个小技巧,需要的朋友照做就可以了 Win7系统下如何调整硬盘分区大小,以前装系统的时候 ...

  2. 如何在macOS下调整磁盘分区大小?

    可以在“macOS”下利用磁盘工具并且不抹掉主分区的情况下,随意更改磁盘分区大小的方法.“OS X”经过几次大版本升级以后,也改名为“macOS”,而且系统自带的“磁盘工具”无论是功能和界面也有很大的 ...

  3. Linux下修改Swap分区大小

    据了解Linux下可以有两种方法创建交换空间,一种是创建交换分区,另一种是创建交换文件.本文记录的是创建交换文件的方法,因为我用的是这种方法.. 添加交换文件步骤: 1.找个地方创建一个.swap的文 ...

  4. liunx系统下调整Swap分区大小

    作者:邓聪聪 添加swap交换空间的步骤如下:第一步:确保系统中有足够的空间来用做swap交换空间,准备在一个独立的文件系统中添加一个swap交换文件,在/tmp中添加1G的swap交换文件第二步:添 ...

  5. 嵌入式linux下获取flash分区大小

    在嵌入式系统中,由于flash存储空间有限,或者是存储数据,实现数据的循环删除,需要获取到分区的使用情况,可以通过系统下的函数statfs来获取使用情况:实现代码如下: flashInfo.cpp # ...

  6. 调整swap分区大小-Linux下安装Oracle时报swap不够解决方法

    调整swap分区大小 方法一:如果磁盘有剩余的空间,用分区工具新建一个swap分区.并写到/etc/fstab里面.再 #swapon -a方法二:可以用一个文件做交换分区. su root cd / ...

  7. (转)Linux下增加交换分区的大小

    场景:最近在Linux环境安装ELK相关软件时候发现机器特别的卡,所以就查看了Linux机器的内存使用情况,发现是内存和交换分区空间太小了. 对于虚拟机中的内存问题,可以直接通过更改虚拟机的硬件进行解 ...

  8. linux下硬盘的分区:

    提到硬盘的分区,以前就是很乱,有什么主分区/扩展分区/逻辑分区等;它们有什么区别呢?现在简单的了解一下: 由于在MBR的主引导记录中的分区表里面最多只能记录4个分区记录,这个不够用啊,为了解决这个问题 ...

  9. Linux下的GPT分区,使用parted命令

    Linux下的GPT分区,这是另外一种分区,针对MBR分区,它有很多优点: (1)几乎突破了分区个数的限制. 在GPT分区表中最多可以支持128个主分区. (2)单个分区容量几乎没有限制. 单个分区最 ...

随机推荐

  1. netcore swagger xml发布丢失问题

    1.netcore2.1.2,swagger.aspnetcore 1.1.0版本.发布netcore项目时swagger的接口文档xml 遗漏,始终发布不上去.后来查阅资料,讲的好像是netcore ...

  2. kali漏洞扫描

    nmap (apt-get install nmap)          nmap从初级到高级 ------------------------------ Nessus (dpkg -i Nessu ...

  3. uvalive 3353 Optimal Bus Route Design

    题意: 给出n个点,以及每个点到其他点的有向距离,要求设计线路使得每一个点都在一个环中,如果设计的线路拥有最小值,那么这个线路就是可选的.输出这个最小值或者说明最小线路不存在. 思路: 在DAG的最小 ...

  4. GitHub 代码上传

    方法一 登录GitHub后,点击下面的图 New responsitory 按钮 或者点击绿色按钮 New repository,新建一个新建一个远程仓库(remote repository),点击后 ...

  5. linux常用命令:cd 命令

    Linux cd 命令可以说是Linux中最基本的命令语句,其他的命令语句要进行操作,都是建立在使用 cd 命令上的.所以,学习Linux 常用命令,首先就要学好 cd 命令的使用方法技巧. 1. 命 ...

  6. iframe使用

    iframe是一个前端页面的内联框架(即行内框架),使用很方便, <!--嵌套子页面--> <script type="text/x-template" id=& ...

  7. 20165215 2017-2018-2 《Java程序设计》第八周学习总结

    20165215 2017-2018-2 <Java程序设计>第八周学习总结 教材学习内容总结 进程与线程 进程是程序的一次动态执行过程,它对应了从代码加载.执行至执行完毕的一个完整过程. ...

  8. Linux下几种重启Nginx的方式,找出nginx配置文件路径和测试配置文件是否正确

    Linux下几种重启Nginx的方式,找出nginx配置文件路径和测试配置文件是否正确 目录在/etc/ngnix/conf.d下找出nginx配置文件路径和测试配置文件是否正确# /usr/sbin ...

  9. 什么是Satoshi?和比特币中本聪有什么关系?

    Satoshi Nakamoto(中本聪)是发起比特币和原始比特币客户端创建者.不过,我经常听到“Satoshi ”这个词,好像它是一个货币单位. 什么是Satoshi呢?Satoshi是0.0000 ...

  10. Symfony2 UserSecurityEncoder实现自己的验证方式

    fosuserbundle默认使用sha512加密 如果要实现自己的加密方式 需要继承Symfony\Component\Security\Core\Encoder\BasePasswordEncod ...