本文转载自:https://blog.csdn.net/cruise_h/article/details/22403529
本文讨论如何再不丢失数据的情况下调整已有ext3分区的大小,包括:
- 压缩已有分区
- 增大已有分区
- 合并两个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分区
有了上述两个案例做铺垫,我们可以延伸操作本案例。首先前提,两个分区是相邻分区才能够进行合并。
基本思路:
- 将后一个分区的文件系统umount,且删除该分区(因数据会被清除,如果需要保留,提前备份!),这样就有了空闲空间,形成于案例2一样的情况。
- 按照案例2的操作步骤依次处理即可。
主要注意的是:
1)如果被删除的分区,在/etc/fstab上有记录,删除之!
2)被删除分区的数据也会被删除,再次提醒,如果需要,备份!
具体操作不再详述,参考前两个案例。