日期:2014-05-16  浏览次数:20722 次

Linux对ext3分区大小进行无损更改的步骤
1 缩小一个ext3分区

这个章节介绍如何缩小一个ext3分区。在这个例子中,我将缩小我的/dev/sda5。首先让我们来看看目前系统的一些情况:
fdisk -l
df

如果你的分区中没有任何系统文件,则继续下面的步骤,否则,请重新启动系统.
当然,我们还是手工umount一次:
umount /dev/sda5

接下来运行:
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中去除,等于将其转成ext2系统:
tune2fs -O ^has_journal /dev/sda5

输出是这样的:
tune2fs 1.38 (30-Jun-2005)

接下来运行:
e2fsck -f /dev/sda5

运行结果:
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来改变文件系统的大小。这个程序只能改变ext2文件系统的大小,这就是为什么刚才我们要先将ext3转换成ex2的原因。当前/dev/sda1已用4.1GB(刚才的df输出可以看到),因此,将10GB的分区缩小到6GB是比较安全的(不能转换到4.1GB以下,否则会丢失数据)。我们运行:
resize2fs /dev/sda5 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.

请注意这些信息:块的数量(1536000)和大小(4k),一会我们将用到。接下来,我们将删除/dev/sda1分区,并创建一个新的,更小的分区。别害怕,不会有数据损失的。我们在fdisk下进行:
fdisk /dev/sda

注意阿,是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)

现在,我们来删除/dev/sda5:
Command (m for help): d
Partition number (1-5): 5

然后创建一个新的/dev/sda5。由于之前它是一个主分区,所以这里我们选择p,并且是一号分区:
Command (m for help): n

Partition number (1-4): 5

现在,就到了关键步骤了:我们被要求输入新分区的大小。首柱面号不是问题,这可以从文章开始时fdisk -l的输出中获得,但我们还不清楚新分区的末柱面号是多少。幸运的是,我们能够确定以k位单位的分区大小,然后按照这个公式计算:

1536000 * 4k * 1.03 = 6328320k

然后,我们在结果前面加上一个+号,并将小写的k换成大写的K,再输入:
Last cylinder or +size or +sizeM or +sizeK (1-1247, default 1247): +6328320K

现在,写入新的分区表并退出:
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环境中(或者rescue模式),用su命令再次切换成root用户,然后运行:
fsck -n /dev/sda5

输出应该是这样的:
fsck 1.38 (30-Jun-2005)
e2fsck 1.38 (30-Jun-2005)
/dev/sda1: clean, 159036/765536 files, 1047239/1536000 blocks

接下来,在新的/dev/sda1上创建日志文件,转换回ext3文件系统:
tune2fs -j /dev/sda1

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.

好了,这下完成了。再次重启,启动到你原来的系统中,看看:

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

fdisk -s /dev/sda1

6337611 搞定!
2 扩大一个ext3分区

在这里,我们拥有一个大约是6GB的/dev/sda1分区,在它后面,有一个大约4GB的未用空间(刚刚分出来的,呵呵)。现在,我们要做的就是把这 4GB的空闲空间增加到/dev/sda1分区中。请注意,如果这4GB的空余空间不在/dev/sda1后面,而是在硬盘的其它地方,就无法实现这个目的!
首先,我们收集一些当前分区的资料。

fdisk -l

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