个人工具

Quick HOWTO : Ch27 : Expanding Disk Capacity/zh

来自Ubuntu中文

Xiaoliangzi讨论 | 贡献2010年9月6日 (一) 19:49的版本

(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航, 搜索

 

概述

现有硬盘容量的不足经常困扰着 Linux 系统管理员,最常见的原因是不断膨胀的数据和数量不断增加的用户以及在找到替代以前 Linux 服务器还必须正常执行大量的任务。

本篇讲述了如何利用两种方式将硬盘添加到Linux系统中。第一种方式是,将文件夹从一个已满分区移动到另一个空的分区中,然后将文件夹目录链接到两块硬盘中。另一种方式是,利用Linux逻辑分区管理器(LVM)合并几个分区为一个分区。

添加硬盘到 Linux

在一些场合下您会遇到将一块新硬盘添加到 Linux 服务器中的问题,原因也许是现有的硬盘坏了或者没有空间了。为了提供更多的空间,这部分将会讲述如何添加一块只有一个分区的硬盘以及怎样将数据从已满的硬盘转移到新的硬盘中。

引言

有些文件夹总是很拥挤:即使你删掉了所有不需要的数据,/var 分区仍然是满的,这时你需要增加一块新的硬盘驱动器到系统中。你可以通过调用命令 df -k 来验证,下面显示出其他分区也已经满了不能再接受新数据。

[root@bigboy tmp]# df -k
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/hda3               505636    118224    361307  25% /
/dev/hda1               101089     14281     81589  15% /boot
none                     63028         0     63028   0% /dev/shm
/dev/hda5               248895      6613    229432   3% /tmp
/dev/hda7              3304768   2720332    416560  87% /usr
/dev/hda2              3304768   3300536      4232  99% /var
[root@bigboy tmp]#

参照生产厂家的说明新的硬盘可以添加到系统中,但是你需要知道怎样去做。

确定硬盘的类型

在/proc/partitons 文件中 Linux 存储了所有已知硬盘分区的名字。整个硬盘用次数字为0的镜像表示,硬盘上的所有分区从1开始顺序排列。下面的例子中系统有两个硬盘,硬盘/dev/hda 已经分过区,而新的硬盘(/dev/hdb)需要设置以接收数据。

[root@bigboy tmp]# cat /proc/partitions
major minor  #blocks  name
 
   3     0    7334145 hda
   3     1     104391 hda1
   3     2    1052257 hda2
   3     3    2040255 hda3
   3     4          1 hda4
   3     5    3582463 hda5
   3     6     554211 hda6
  22     0   78150744 hdb
[root@bigboy tmp]#

注意:Linux 硬盘设备的命名遵循一定的标准。SCSI 硬盘以 sd 开头而 IDE 硬盘以 hd 开头,之后以一个字母来确定唯一的一块硬盘,比如,第一块硬盘是 a,第二块是 b 第三块是 c 以此类推。 最后,一个两位数确定分区号。利用该规则第四块 IDE 驱动器的第五分区应该是/dev/hdd5。

在新硬盘上准备分区

Linux 上分区的准备与 Windows 环境下非常相似,因为这两种操作系统都用 fdisk 分区工具。使用步骤如下:


1) 在 Linux 下添加一块新的硬盘的第一步是对其进行分区以便以后在上面添加文件系统。 输入 fdisk 命令后面跟着硬盘的名字。比如如果你想在硬盘 /dev/hdb 上运行 fdisk,命令如下:

[root@bigboy tmp]# fdisk /dev/hdb
 
The number of cylinders for this disk is set to 9729.
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): 

2) 要确保你在操作正确的硬盘,输入命令 p 显示硬盘上的所有分区。下面的例子中硬盘中没有分区。

Command (m for help): p
 
Disk /dev/hdb: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
 
   Device Boot      Start         End      Blocks   Id   System
 
Command (m for help):

3) fdisk 中的 m 命令显示所有有效命令的简介,可以看出 n 是增加一个新分区的命令。增加一个编号为1的新分区,使用默认值使该分区覆盖整个硬盘。

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
Partition number (1-4): 1
First cylinder (1-9729, default 1):<RETURN>
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-9729, default 9729):

4) 运行显示命令(p)确认已经成功建立了新分区。

Command (m for help): p
 
Disk /dev/hdb: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
 
   Device Boot      Start         End      Blocks   Id   System
/dev/hdb1               1        9726    78148161   83  Linux
 
Command (m for help):

提示: 如果操作错误,可以使用命令 d 删除该分区然后重新开始。命令 t 可以用来改变分区类型,从常规的 Linux 分区83到其他,比如82是 swap 空间。大多数情况下,你不必改变分区类型,默认的值已经可以了。

注意: 在创建新的分区时也许你已经注意到 fdisk 询问将要创建的分区是一个主分区还是扩展分区。 Linux 只允许存在四个主分区,如果需要更多的,可以将其中一个主分区转换成扩展分区。下面例子的分区表显示存在一个扩展分区其中有两个普通分区。

Command (m for help): p
 
Disk /dev/hda: 7510 MB, 7510164480 bytes
255 heads, 63 sectors/track, 913 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
 
   Device Boot      Start         End      Blocks   Id   System
/dev/hda1   *           1          13      104391   83  Linux
/dev/hda2              14         144     1052257+  83  Linux
/dev/hda3             145         398     2040255   82  Linux swap
/dev/hda4             399         913     4136737+   5  Extended
/dev/hda5             399         844     3582463+  83  Linux
/dev/hda6             845         913      554211   83  Linux
 
Command (m for help):

添加更多的分区可按照上面所述,记住,在一些情况下你可能需要添加一个扩展分区。

5) 在使用命令 w 来保存所做的更改前对分区表的更改并不会生效。完成后请用命令 w 保存更改,并输入命令 q 退出。

Command (m for help): w
Command (m for help): q

完成以上步骤后,你需要验证以下然后开始将数据转移到新硬盘中,后面将介绍这些步骤。

验证新分区

查看 /proc/partitions 文件或者调用命令 fdisk -l 确认硬盘上所做的更改。

[root@bigboy tmp]# cat /proc/partitions
major minor  #blocks  name
...
...
...
  22     0   78150744 hdb
  22     1   78150744 hdb1
[root@bigboy tmp]#
 
 
[root@bigboy tmp]# fdisk -l
...
...
...
Disk /dev/hdb: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
 
   Device Boot      Start         End      Blocks   Id  System
/dev/hdb1              1         9729    76051710   83  Linux
[root@bigboy tmp]#

在新分区上放置文件夹目录

现在要格式化分区并利用命令 mkfs 给分区一个新的文件结构。Fedora 系统上默认使用 ext3 类型,如下所示:

[root@bigboy tmp]# mkfs -t ext3 /dev/hdb1

下一步,你必须创建一个特殊的挂载点目录,以便新的分区挂载。创建文件夹 /mnt/hdb1以便挂载。

[root@bigboy tmp]# mkdir /mnt/hdb1

当 LInux 启动时会搜索 /etc/fstab 文件获取所有分区及其挂载特性,然后系统会自动挂在这些分区。你需要在该文件中添加一行如下所示:

#
# File: /etc/fstab
#
/dev/hdb1  /mnt/hdb1  ext3  defaults 1 2

第一段是分区的名字,后面是挂在点目录及文件系统类型。第四段定义了挂在选项,大多数情况下只需要是 defaults 即可。第五段描述的是整理文件系统命令是否可用,0为不可用,1为可用。最后字段定义了启动时文件系统检查的顺序,该检查将会执行两遍。主文件系统取值为1将会首先检查,其他所有文件系统应当取为2。如果对 /etc/fstab 文件不熟悉可以输入命令 man fstab 来获取各个选项的详细解释。

你不必等待重启来挂载分区,可以使用带 -a 选项的 mount 命令来读取 /etc/fstab 并更新。

[root@bigboy tmp]# mount -a

现在你可以用 /mnt/hdb1 读取新的分区了。

转移数据到新分区

还记得之前调用命令 df -k 检查磁盘分区吗,分区 /var 几乎已经满了。

[root@bigboy tmp]# df -k
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/hda3               505636    118224    361307  25% /
/dev/hda1               101089     14281     81589  15% /boot
none                     63028         0     63028   0% /dev/shm
/dev/hda5               248895      6613    229432   3% /tmp
/dev/hda7              3304768   2720332    416560  87% /usr
/dev/hda2              3304768   3300536      4232  99% /var
[root@bigboy tmp]#

命令 du -sk * 显示一个文件夹中所有子文件夹占用的磁盘空间。通过命令 cd 进入各个子文件夹然后循环调用前面的命令直到发现最大的文件。这种情况下,你只须进入 /var 文件夹然后找到元凶 /var/transactions。

[root@bigboy tmp]# cd /var
[root@bigboy var]# du -sk *
2036    cache
4       db
8       empty
...
...
133784  transactions
...
...
[root@bigboy var]#

作为解决办法,分区 /var 需要扩展成新的 /dev/hdb1 分区,而这个分区挂在在文件夹 /mnt/hdb1上。按照以下步骤转移数据:

1) 备份要操作的分区上的数据。

2) 调用命令 who 查看哪些用户正在登陆。如果其他用户在的话,利用命令 wall 给他们发送信息告诉他们系统即将关机。

[root@bigboy tmp]# who
root     pts/0        Nov  6 14:46 (192-168-1-242.my-site.com)
bob      pts/0        Nov  6 12:01 (192-168-1-248.my-site.com)
bunny    pts/0        Nov  6 16:25 (192-168-1-250.my-site.com)
[root@bigboy tmp]# wall The system is shutting down now!
 
Broadcast message from root (pts/0) (Sun Nov  7 15:04:27 2004):
 
The system is shutting down now!
[root@bigboy tmp]#

3) 登陆到 VGA 终端,进入单用户模式。

[root@bigboy tmp]# init 1

4) 将目录 /var/transactions 重命名为 /var/transactions-save 以保证以后能够恢复轻松地恢复这些数据。

sh-2.05b# mv /var/transactions /var/transactions-save

5) 创建一个新的空目录 /var/transactions; 这个目录后面将作为挂载点。

sh-2.05b# mkdir /var/transactions

6) 拷贝目录 /var/transactions-save 里的内容到 /dev/hdb1 的根目录下,即 /var/hdb1 里面。


sh-2.05b# cp -a /var/transactions-save/* /mnt/hdb1

7) 将分区 /dev/hdb1 取消挂载。

sh-2.05b# umount /mnt/hdb1

8) 编辑文件 /etc/fstab,移除先前的关于 /dev/hdb1 的条目并且用新的挂载点条目代替之。

#
# File: /etc/fstab
#
 
#/dev/hdb1  /mnt/hdb1  ext3  defaults 1 2
 
/dev/hdb1  /var/transactions  ext3  defaults 1 2

9) 利用命令 mount -a 重新挂载 /dev/hdb1 到新的挂载点,该命令会调用文件 /etc/fstab 并且自动挂载任何还没挂载的条目。

sh-2.05b# mount -a

10) 测试下以便确认新的文件夹 /var/transactions 里的内容跟 /var/transactions-save 是相同的。

11) 输入命令 exit 返回多用户模式,系统就会返回默认的运行级别。

sh-2.05b# exit

12) 确认应用程序能够正常工作,之后可以删除目录 /var/transactions-save 和 /mnt/hdb1 挂载点目录。

这个练习展示了如何将一个子目录中的所有数据转移到一个新的硬盘中。Linux 也允许合并分区以创建一个更大的分区。下面将会讲述原因和操作步骤。

利用LVM扩展分区

逻辑分区管理器(LVM)能够在不改变硬盘分区表的前提下重新调整各个分区的大小。当你发现空间不够用而想拓展成一个新的分区,相对于转移所有或者部分数据到一个新硬盘中这是最有用的办法。

  • "物理分区": 物理分区(PV),在LVM中又称为常规物理磁盘分区。
  • "分区组": 任何数量在不同硬盘驱动器上的物理分区(PVs)可以合称为一个分区组(VG)。在 LVM 中,分区组可以看做一个虚拟硬盘驱动器。
  • "逻辑分区" 分区组必须再分割成逻辑分区。每一个逻辑分区可以独立地进行格式化就好像是一个常规物理分区一样。因此,逻辑分区就好像虚拟硬盘上的虚拟分区一样。
这看起来很复杂,但是LVM允许创建新的虚拟分区,它的容量大小可以从真实的硬盘分区组中调整,而真实磁盘分区的大小可能你并不像改变。LVM 的另一个好处是,在做这些更改时可以不影响硬盘中的其他分区。
  • "物理内容": 真实的硬盘分区被分割成一个个数据块,当把它们添加进逻辑分区时被称为物理内容(PEs)。物理内容重要是因为当描述分区组的大小时不是用多少G字节,而是用多少物理内容。

确保你对这些术语完全理解了,因为后面我们会频繁地用到它们。下面我们要出发了~

配置LVM设备

It is probably best to learn about the features of LVM through a scenario. Suppose a small company needs to expand disk capacity, but there isn't the budget to purchase an adequately sized hard drive. The /home filesystem, which resides on /dev/hde5, has become too full. You just added a new hard drive /dev/hdf with 50% of the capacity of /dev/hde5 into which you want to expand /home. The device /dev/hdf has a single partition named /dev/hdf1 into which /dev/hde5 will be merged. Take a look at the required steps.

备份数据

Use the tar command or some other method to backup your data in /home. The LVM process will destroy the data on all physical volumes.

弹出/home文件系统

As /home stores most users' data, you'll need to do some preparatory work before unmounting the filesystem.

1) Use the who command to see who's logged in. If other users are present, send a message with the wall command informing them that the system is about to shutdown.

[root@bigboy tmp]# who
root     pts/0        Nov  6 14:46 (192-168-1-242.my-site.com)
bob      pts/0        Nov  6 12:01 (192-168-1-248.my-site.com)
bunny    pts/0        Nov  6 16:25 (192-168-1-250.my-site.com)
[root@bigboy tmp]# wall The system is shutting down now!
 
Broadcast message from root (pts/0) (Sun Nov  7 15:04:27 2004):
 
The system is shutting down now!
[root@bigboy tmp]#

2) Log into the VGA console, and enter single-user mode.

[root@bigboy tmp]# init 1

3) Unmount the filesystem.

sh-2.05b# umount /home

Now we're ready to start modifying the partitions which is covered next.

确定分区类型

You have to change each LVM partition used to be of type 8e (Linux LVM). You can test this with the fdisk -l command. Here is an example using /dev/hde that shows your target partitions are of the incorrect type.

sh-2.05b# fdisk -l /dev/hde
 
Disk /dev/hde: 4311 MB, 4311982080 bytes
16 heads, 63 sectors/track, 8355 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
 
   Device Boot    Start       End    Blocks   Id  System
/dev/hde1             1      4088   2060320+  fd  Linux raid autodetect
/dev/hde2          4089      5713    819000   83  Linux
/dev/hde3          5714      6607    450576   83  Linux
/dev/hde4          6608      8355    880992    5  Extended
/dev/hde5          6608      7500    450040+  83  Linux
sh-2.05b#

启动 FDISK

You can change the partition type using fdisk with the disk name as its argument. Use it to modify both partitions /dev/hde5 and /dev/hdf1. The fdisk examples that follow are for /dev/hde5; repeat them for /dev/hdf1.

sh-2.05b# fdisk /dev/hde
 
The number of cylinders for this disk is set to 8355.
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):

设置 ID Type 为 8e

You now need to set the partition types to the LVM value of 8e. Partitions /dev/hde5 and /dev/hdf1 are the fifth and sixth partitions on disk /dev/hde. Modify their type using the t command, and then specify the partition number and type code. You can also use the L command to get a full listing of ID types in case you forget.

Command (m for help): t
Partition number (1-6): 5
Hex code (type L to list codes): 8e
Changed system type of partition 5 to 8e (Linux LVM)
 
Command (m for help): t
Partition number (1-6): 6
Hex code (type L to list codes): 8e
Changed system type of partition 6 to 8e (Linux LVM)
 
Command (m for help):

确定更改生效

Use the p command to get the new proposed partition table.

Command (m for help): p
 
Disk /dev/hde: 4311 MB, 4311982080 bytes
16 heads, 63 sectors/track, 8355 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
 
   Device Boot    Start       End    Blocks   Id  System
/dev/hde1             1      4088   2060320+  fd  Linux raid autodetect
/dev/hde2          4089      5713    819000   83  Linux
/dev/hde3          5714      6607    450576   83  Linux
/dev/hde4          6608      8355    880992    5  Extended
/dev/hde5          6608      7500    450040+  8e  Linux LVM
 
Command (m for help):

保存更改

Use the w command to permanently save the changes to disk /dev/hde.

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.
sh-2.05b#

The error above will occur if any of the other partitions on the disk is mounted. This shouldn't be grave as you are already in single user mode in which most of the system's processes that would be accessing the partition have been shutdown.

定义每一个物理分区

After modifying the partition tables of /dev/hde and /dev/hdf, initialize the target partitions with the pvcreate command. This wipes out all the data on them in preparation for the next step. If you haven't backed up your data yet, do it now!

sh-2.05b# pvcreate /dev/hde5
pvcreate -- physical volume "/dev/hde5" successfully created
 
sh-2.05b# pvcreate /dev/hdf1
pvcreate -- physical volume "/dev/hdf1" successfully created
 
sh-2.05b#

运行 VGscan

The next step is to make Linux scan for any new LVM disk partitions and automatically create the LVM configuration files in the /etc directory. To do this, use the vgscan command.

sh-2.05b# vgscan
vgscan -- reading all physical volumes (this may take a while...)
sh-2.05b#

为多个物理分区创建一个组

Use the vgcreate command to combine the two physical volumes into a single unit called a volume group. The LVM software effectively tricks the operating system into thinking the volume group is a new hard disk. In the example, the volume group is called lvm-hde.

sh-2.05b# vgcreate lvm-hde /dev/hdf1 /dev/hde5
Volume group "lvm-hde" successfully created
sh-2.05b#

Therefore, the vgcreate syntax uses the name of the volume group as the first argument followed by the partitions that it will be comprised of as all subsequent arguments.

从组中创建一个逻辑分区

Now you're ready to partition the volume group into logical volumes with the lvcreate command. Like hard disks, which are divided into blocks of data, logical volumes are divided into units called physical extents (PEs).

You'll have to know the number of available PEs before creating the logical volume. This is done with the vgdisplay command using the new lvm-hde volume group as the argument.

sh-2.05b# vgdisplay lvm-hde
--- Volume group ---
VG Name               lvm-hde
VG Access             read/write
VG Status             available/resizable
VG #                  0
MAX LV                256
Cur LV                0
Open LV               0
MAX LV Size           255.99 GB
Max PV                256
Cur PV                2
Act PV                2
VG Size               848 MB
PE Size               4 MB
Total PE              212
Alloc PE / Size       0 / 0
Free  PE / Size       212 / 848 MB
VG UUID               W7bgLB-lAFW-wtKi-wZET-jDJF-8VYD-snUaSZ
 
sh-2.05b#

As you can see, 212 PEs are available as free. You can now use all 212 of them to create a logical volume named lvm0 from volume group lvm-hde.

sh-2.05b# lvcreate -l 212 lvm-hde -n lvm0
Logical volume "lvm0" created
sh-2.05b#

Note: You can also define percentages of the volume group to be used. The first example defines the use of 100% of the volume group's free space and the second example specifies using 50% of the total volume group.

sh-2.05b# lvcreate -l 100%FREE -n lvm0 lvm-hde
sh-2.05b# lvcreate -l 50%VG -n lvm0 lvm-hde

格式化该逻辑分区

After the logical volume is created, you can format it as if it were a regular partition. In this case, use the -t switch to specify to the mkfs formatting program that you want a type ext3 partition.

sh-2.05b# mkfs -t ext3 /dev/lvm-hde/lvm0
mke2fs 1.32 (09-Nov-2002)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
108640 inodes, 217088 blocks
10854 blocks (5.00%) reserved for the super user
First data block=0
7 block groups
32768 blocks per group, 32768 fragments per group
15520 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840
 
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
 
This filesystem will be automatically checked every 38 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
sh-2.05b#

创建挂载点

When you formatted the /dev/hde5 partition, you lost the /home directory. Now you have to recreate /home on which you'll later mount your new logical volume.

sh-2.05b# mkdir /home

更新 /etc/fstab

The /etc/fstab file lists all the partitions that need to be automatically mounted when the system boots. This snippet configures the newly labeled partition to be mounted on the /home mount point.

/dev/lvm-hde/lvm0   /home      ext3    defaults        1 2

The /dev/hde5 and /dev/hdf1 partitions are replaced by the combined /lvm0 logical volume. You, therefore, don't want the old partitions to be mounted again. Make sure that any reference to them in this file has either been commented a # character at the beginning of each line or deleted entirely.

#/dev/hde5       /data1        ext3    defaults        1 2
#/dev/hdf1       /data2        ext3    defaults        1 2

挂在该分区

The mount -a command reads the /etc/fstab file and mounts all the devices that haven't been mounted already. After mounting, test the volume by listing its directory contents. It should just contain the lost+found directory

sh-2.05b# mount -a
sh-2.05b# ls /home
lost+found
sh-2.05b#

复原数据

You can now restore your backed up data to /home.

切出单用户模式

Return to your original run state by using either the init 3 or init 5 commands. The exit command will make you return to your default runlevel.

总结

The demise of the hard disk has been predicted for many years. Faster, denser memory chips were supposed to eliminate their need, but hard disk technology has evolved, dramatically increasing their speed and capacity too. They will be around for a long time to come.

It seems as if when drives get bigger, so does the data they are intended to store. Expanding the existing disk capacity of your server may become an everyday occurrence and the tools described in this chapter should make the task easier.