调整Linux磁盘分区大小

参考一:调整Linux磁盘分区大小


在使用linux的过程中, 有时会出现因为安装系统时分区不当导致有的分区空间不足,而有的分区空间过剩的情况.比如: 我在安装系统时给/usr/local分配器了5G的空间,但使用一段过程后发现, /usr/local最多只用到了1G.这样可以将/usr/local大小调整为1G. 空出4G留作他用.本文归纳了在不破快文件系统数据的前提下对文件系统大小进行调整的方法.这里采用的是"拆东墙, 补西墙"的方法. 当然, 如果你的磁盘中有未分区的空闲空间, 你就不用减小某个分区的空间了.

准备工作

(一) 备份

首先组好备份要更改的分区中的文件. 对磁盘的操作也许会导致数据的丢失. 开始工作之前最好将重要的数据保存到别的分区.

(二) 获取相关信息.

1, 运行 $ df , 查看文件系统信息. 记下你想要调整的分区对应的挂载点和设备文件.

这一步是为了查看/usr/local对应/dev/中的哪个设备文件. 本文以/dev/hda7为例.

2, 运行 # sudo fdisk /dev/sda . 进入fdisk , 按下p, 查看磁盘分区信息. 记下/dev/sda8的起始柱面号, 终止柱面号. 设他们分别为start, end. 并记下一个"Unit"包含多少字节.

注意, 文件系统大小和磁盘分区大小是不能等同的: 起码他们的计量单位不同. 在文件系统中, 一般用KB, MB, GB为单位. 但在fdisk中, 是以磁盘的"unit"值(即一个柱面中包含的字节数)来计算的. 我们运行 # fdisk /dev/sda后, fdisk显示如下信息:

Disk /dev/sda: 163.9 GB, 163928604672 bytes
255 heads, 63 sectors/track, 19929 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

(三)计算终止柱面号

e.g: 原先/usr/local的大小为5000 MB, 起始柱面号为2550, 终止柱面号为3157. 那么我现在要将它的大小改为1000MB, 应该这样计算终止柱面号( [3.2] = 4 ):

终止柱面号 = 2550 + [(1000 * 1024 * 1024 ) / 8225280] = 2678

使用fdisk删除原来的分区(以前分区的数据不会丢失!). 然后新建一个分区, 新分区于旧分区唯一的区别就在于分区大小不一样, 这表现在他们终止柱面号不同.

增加分区大小: 终止柱面号设置为空闲空间的最后一个柱面号.减小分区大小: 终止柱面号由计算得出.

开始调整
在开始调整之前, 最好拿一张纸记下要调整的分区的下列信息:

调整前大小, 调整后大小. 它挂载在哪个目录, 对应于/dev/中的哪个设备. 起始, 终止柱面.

e.g: 调整前: 5G, 调整后: 1G, 挂载点: /usr/local, 对应于/dev/hda7. 起始,终止柱面分别为2550, 3157.

1, 卸载分区
最好在runlevel = 1的情况下卸载.
# umount /dev/

2, 检查文件系统的一致性
# fsck -n /dev/

3, 如够是ext3系统, 还要去除日志
# tune2fs -O ^has_journal /dev/

注意: 取出日志后的ext3系统是无法挂载的, 使用
# tune2fs -j /dev/重建日志.

4, 调整文件系统大小
# resize_reiserfs -s 1000M /dev/hda7

调整完文件系统的大小以后,再使用以下命令以文件系统进行全面的检查:
# reiserfsck --check --fix-fixable /dev/hda7

5, 进入fdisk, 调整分区大小.
# fdisk /dev/hda

由于内核仍然使用旧的分区表, 重启之后再进行后面的步骤.

通过以上步骤,我们已经完成了硬盘分区大小调整的操作,接下来回到重新调整文件系统大小的问题上来。 由于硬盘分区是按照柱面来计划的,我们要在 resize_reiserfs步骤上就一步到位调整文件系统大小与硬盘分区大小正好相等是比较困难的事情。所以一般是调硬盘分区比文件系统要大一点点, 然后再通过以下命令进行同步(当然如果你不在意硬盘分区里面存在部分空间的话也可以忽略以下步骤):

# umount /home/
# resize_reiserfs /dev/hda6


参考二: esize2fs - ext2 file system resizer

resize2fs 是 ext2 文件系统大小调整工具
可以扩大 可以缩小原ext2分区(当然 ext3只是多了 Journal的ext2 也可以)

需要注意的是 增大的话
起始柱面不能变

步骤就是:
先fdisk 调整分区
所谓调整 也就是删了相关的分区 再重建分区
最后在分区上再用 resize2fs 调整文件系统大小

所以一个概念 或者说对照就是
文件系统(file system)是在分区(Partition)基础上的

# rpm -qf /sbin/resize2fs
e2fsprogs-1.32-15.1

# rpm -qil e2fsprogs
Name        : e2fsprogs                    Relocations: (not relocatable)
Version     : 1.32                              Vendor: Red Hat, Inc.
Release     : 15.1                          Build Date: Mon 04 Oct 2004 10:23:16 PM CST
Install Date: Mon 17 Jan 2005 03:36:34 PM CST      Build Host: tweety.build.redhat.com
Group       : System Environment/Base       Source RPM: e2fsprogs-1.32-15.1.src.rpm
Size        : 1069923                          License: GPL
Signature   : DSA/SHA1, Thu 18 Nov 2004 05:35:43 AM CST, Key ID 219180cddb42a60e
Packager    : Red Hat, Inc.
URL         : http://e2fsprogs.sourceforge.net/
Summary     : Utilities for managing the second extended (ext2) filesystem.
Description :
The e2fsprogs package contains a number of utilities for creating,
checking, modifying, and correcting any inconsistencies in second
extended (ext2) filesystems. E2fsprogs contains e2fsck (used to
repair filesystem inconsistencies after an unclean shutdown), mke2fs
(used to initialize a partition to contain an empty ext2 filesystem),
debugfs (used to examine the internal structure of a filesystem, to
manually repair a corrupted filesystem, or to create test cases for
e2fsck), tune2fs (used to modify filesystem parameters), and most of
the other core ext2fs filesystem utilities.

You should install the e2fsprogs package if you need to manage the
performance of an ext2 filesystem.
/lib/evms/libe2fsim.1.2.1.so
/lib/libcom_err.so.2
/lib/libcom_err.so.2.0
/lib/libe2p.so.2
/lib/libe2p.so.2.3
/lib/libext2fs.so.2
/lib/libext2fs.so.2.4
/lib/libss.so.2
/lib/libss.so.2.0
/lib/libuuid.so.1
/lib/libuuid.so.1.2
/sbin/badblocks
/sbin/debugfs
/sbin/dumpe2fs
/sbin/e2fsck
/sbin/e2image
/sbin/e2label
/sbin/findfs
/sbin/fsck
/sbin/fsck.ext2
/sbin/fsck.ext3
/sbin/mke2fs
/sbin/mkfs.ext2
/sbin/mkfs.ext3
/sbin/resize2fs
/sbin/tune2fs
/usr/bin/chattr
/usr/bin/lsattr
/usr/bin/uuidgen
/usr/sbin/mklost+found
/usr/share/doc/e2fsprogs-1.32
/usr/share/doc/e2fsprogs-1.32/README
/usr/share/doc/e2fsprogs-1.32/RELEASE-NOTES
/usr/share/man/man1/chattr.1.gz
/usr/share/man/man1/lsattr.1.gz
/usr/share/man/man1/uuidgen.1.gz
/usr/share/man/man3/libuuid.3.gz
/usr/share/man/man3/uuid_clear.3.gz
/usr/share/man/man3/uuid_compare.3.gz
/usr/share/man/man3/uuid_copy.3.gz
/usr/share/man/man3/uuid_generate.3.gz
/usr/share/man/man3/uuid_generate_random.3.gz
/usr/share/man/man3/uuid_generate_time.3.gz
/usr/share/man/man3/uuid_is_null.3.gz
/usr/share/man/man3/uuid_parse.3.gz
/usr/share/man/man3/uuid_time.3.gz
/usr/share/man/man3/uuid_unparse.3.gz
/usr/share/man/man8/badblocks.8.gz
/usr/share/man/man8/debugfs.8.gz
/usr/share/man/man8/dumpe2fs.8.gz
/usr/share/man/man8/e2fsck.8.gz
/usr/share/man/man8/e2image.8.gz
/usr/share/man/man8/e2label.8.gz
/usr/share/man/man8/findfs.8.gz
/usr/share/man/man8/fsck.8.gz
/usr/share/man/man8/fsck.ext2.8.gz
/usr/share/man/man8/fsck.ext3.8.gz
/usr/share/man/man8/mke2fs.8.gz
/usr/share/man/man8/mkfs.ext2.8.gz
/usr/share/man/man8/mkfs.ext3.8.gz
/usr/share/man/man8/mklost+found.8.gz
/usr/share/man/man8/resize2fs.8.gz
/usr/share/man/man8/tune2fs.8.gz

# fdisk /dev/sdb
The number of cylinders for this disk is set to 8924.
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): p

Disk /dev/sdb: 73.4 GB, 73407868928 bytes
255 heads, 63 sectors/track, 8924 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot    Start       End    Blocks   Id  System
/dev/sdb1   *         1       522   4192933+  82  Linux swap
/dev/sdb2           523       783   2096482+  83  Linux
/dev/sdb3           784      1436   5245222+  83  Linux
/dev/sdb4          1437      8924  60147360    f  Win95 Ext'd (LBA)
/dev/sdb5          1437      8924  60147328+  83  Linux

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

Command (m for help): p

Disk /dev/sdb: 73.4 GB, 73407868928 bytes
255 heads, 63 sectors/track, 8924 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot    Start       End    Blocks   Id  System
/dev/sdb1   *         1       522   4192933+  82  Linux swap
/dev/sdb2           523       783   2096482+  83  Linux
/dev/sdb4          1437      8924  60147360    f  Win95 Ext'd (LBA)
/dev/sdb5          1437      8924  60147328+  83  Linux

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

Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (523-8924, default 523):
Using default value 523
Last cylinder or +size or +sizeM or +sizeK (523-1436, default 1436):
Using default value 1436

Command (m for help): p

Disk /dev/sdb: 73.4 GB, 73407868928 bytes
255 heads, 63 sectors/track, 8924 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot    Start       End    Blocks   Id  System
/dev/sdb1   *         1       522   4192933+  82  Linux swap
/dev/sdb2           523      1436   7341705   83  Linux
/dev/sdb4          1437      8924  60147360    f  Win95 Ext'd (LBA)
/dev/sdb5          1437      8924  60147328+  83  Linux

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

Calling ioctl() to re-read partition table.
Syncing disks.

# resize2fs -p /dev/sdb2 7341705
resize2fs 1.32 (09-Nov-2002)
The containing partition (or device) is only 1835426 blocks.
You requested a new size of 7341705 blocks.

Syncing disks.

# resize2fs 1.32 (09-Nov-2002)
The containing partition (or device) is only 1835426 blocks.
You requested a new size of 7341705 blocks.

Syncing disks.

# e2fsck -f /dev/sdb2
e2fsck 1.32 (09-Nov-2002)
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
/var: 599/262144 files (4.5% non-contiguous), 74698/524120 blocks

# resize2fs -p /dev/sdb2
resize2fs 1.32 (09-Nov-2002)
Begin pass 1 (max = 40)
Extending the inode table     XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/sdb2 is now 1835008 blocks long.

然后 mount 上 ls 一下 就发现 原来 sdb2 的区大了 还有 sdb3 的东西也在

RESIZE2FS(8)                                                      RESIZE2FS(8)

NAME
       resize2fs - ext2 file system resizer

SYNOPSIS
       resize2fs [ -d debug-flags ] [ -f ] [ -F ] [ -p ] device [ size ]

DESCRIPTION
       The resize2fs program will resize ext2 file systems.  It can be used to
       enlarge or shrink an ext2 file system located on device so that it will
       have  size  blocks.   If  the  size parameter is not specified, it will
       default to the size of the partition.  The size parameter may never  be
       larger than the size of the partition.

       The  resize2fs  program does not manipulate the size of partitions.  If
       you wish to enlarge a filesystem, you must  first  make  sure  you  can
       expand  the  size  of the underlying partition first.  This can be done
       using fdisk(8) by deleting the  partition  and  recreating  it  with  a
       larger  size.   When  recreating the partition, make sure you create it
       with the same starting disk cylinder as before!  Otherwise, the  resize
       operation  will  certainly  not  work,  and  you  may  lose your entire
       filesystem.

       If you wish to shrink an ext2 partition, first use resize2fs to  shrink
       the  size  of filesystem.  Then you may use fdisk(8) to shrink the size
       of the partition.  When shrinking the size of the partition, make  sure
       you do not make it smaller than the new size of the ext2 filesystem!

OPTIONS
       -d debug-flags
              Turns on various resize2fs debugging features, if they have been
              compiled into the binary.  debug-flags  should  be  computed  by
              adding  the  numbers  of the desired features from the following
              list:
                   1    - Print out all disk I/O
                   2    - Debug block relocations
                   8    - Debug inode relocations
                   16   - Debug moving the inode table

       -p     Prints out a percentage completion bars for each resize2fs oper-
              ation,  so  that  the user can keep track of what the program is
              doing.

       -f     Forces resize2fs to proceed with the  filesystem  resize  opera-
              tion,  overriding  some  safety  checks which resize2fs normally
              enforces.

       -F     Flush the filesystem device's buffer  caches  before  beginning.
              Only really useful for doing resize2fs time trials.

AUTHOR
       resize2fs was written by Theodore Ts'o .

COPYRIGHT
       Resize2fs  is Copyright 1998 by Theodore Ts'o and PowerQuest, Inc.  All
       rights reserved.  As of April,  2000  Resize2fs  may  be  redistributed
       under the terms of the GPL.

SEE ALSO
       fdisk(8), e2fsck(8), mke2fs(8)

E2fsprogs version 1.32           November 2002                    RESIZE2FS(8)


 

参考三:增加linux分区大小

前面已经安装了lvm 系统,现在来维护lvm系统

一、扩展分区空间

    分两种情况 1、卷组中无剩余空间,这样的话,先必须扩展卷组 ,方法就是添加硬盘,创建lvm 分区,在lvm上建立物理卷,,将物理卷添加到卷组中去,就是前面所说的,这里就不说了。      2、若卷组中有剩余空间,就直接扩展卷组的逻辑卷,就不要添加物理卷了。

   扩展逻辑卷之后,就可以对相应的文件系统进行容量扩展了。

    由于我的卷组lijunyi 中还有88M的容量,所以我直接把80M添加到逻辑卷meng中去,用lvdisplay,查看lv size 增加了80M如图:

逻辑卷是增加容量了。可是我的文件系统/lvm12怎么还没有变化呢?

这个是需要一个命令的 resize2fs

执行这个命令的时候是需要卸载文件系统的 #umount /lvm12 重新挂载后我们可以看到/lvm12的容量变大了。


如果你不是ext3系统而是jfs 或是其他的就得用其他的命令了。

刚才只是增加容量,用lvreduce命令可以减少逻辑卷大小的,还可以用vgreanme改变卷组名称,用lvrename改变逻辑卷名称

参考四:

Linux分区命令fdisk图文详解


本教程为在已使用的Linux系统中新加入一个硬盘。

1、fdisk –l 查看:看到新加入硬盘hdd

 

 Linux分区命令fdisk图文详解

2、输入:fdisk /dev/hdd

    Linux分区命令fdisk图文详解 

3、键入m查看有哪些命令:

 Linux分区命令fdisk图文详解

4、键入p查看一下硬盘hdd的分区情况:

Linux分区命令fdisk图文详解

5、键入n增加新的分区,系统提示建立分区类别,扩展分区还是主分区,输入e建立扩展分区,输入分区号,(1-4),开始柱面号和结束柱面号,或直接按回车,使用缺省值,把所有的空间分给扩展分区。

Linux分区命令fdisk图文详解

6、扩展分区就建成了,键入p查看一下分区情况:

Linux分区命令fdisk图文详解

7、hdd1就是我们建立的分区,扩展分区不能直接使用,还需要在上面建立逻辑分区,

键入n创建分区:

Linux分区命令fdisk图文详解

 

8、键入l创建逻辑分区,然后输入起止柱面号,如果在此扩展分区上只创建一个逻辑分区,按两次回车键,使用缺省值,把所有的空间分给此逻辑分区,本例中创建两个逻辑分区,第一个分1000个柱面。

Linux分区命令fdisk图文详解

9、可以键入p查看一下分区情况:

Linux分区命令fdisk图文详解

10、输入w保持操作,分区就完成了。中间如果做错了键入q,退出重来。

Linux分区命令fdisk图文详解


参考四:

linux增加根分区大小

分类: Linux&Unix 1549人阅读 评论(0) 收藏 举报

以下操作以root身份运行


1.增加一个新分区(从原有硬盘分,或增加一个新硬盘并进行分区fdisk)
  格式化成ext3(mkfs.ext3 /dev/sdb1,假设为/dev/sdb1)


2.将新的分区挂载到一个临时目录,如:/mnt/tmp
  mount /dev/sdb1 /mnt/tmp


3.du -sh /
  查看一个根目录中每个目录的大小,并选择一个大小合适的目录
  如:/usr目录
  cp -r /usr /mnt/tmp


4.修改/etc/fstab
  /dev/sdb1               /usr       ext3         defaults           0 0
5.重启,如果系统运行正常,将/etc/fstab中新加的一行注释掉,则再重启后/usr又变回原来的位置,将其删除rm -rf /usr/* ;如果不正常,查找原因


6.修改/etc/fstab,将注释去掉,重启即可


  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值