Quick HOWTO : Ch27 : Expanding Disk Capacity/zh:修订间差异

来自Ubuntu中文
跳到导航跳到搜索
Xiaoliangzi留言 | 贡献
以内容'{{Translation}} {{From|http://www.linuxhomenetworking.com/wiki/index.php/Quick_HOWTO_:_Ch27_:_Expanding_Disk_Capacity}} {{Translator | Xiaoliangzi}} …'创建新页面
 
Xiaoliangzi留言 | 贡献
无编辑摘要
 
(未显示同一用户的13个中间版本)
第1行: 第1行:
{{Translation}}
{{Translation}}
{{From|http://www.linuxhomenetworking.com/wiki/index.php/Quick_HOWTO_:_Ch27_:_Expanding_Disk_Capacity}}
{{From|http://www.linuxhomenetworking.com/wiki/index.php/Quick_HOWTO_:_Ch27_:_Expanding_Disk_Capacity}}
{{Translator | [[User:Xiaoliangzi | Xiaoliangzi]]}}
{{Translator|[[User:Xiaoliangzi | Xiaoliangzi]]}}
{{Languages|Quick HOWTO : Ch27 : Expanding Disk Capacity}}
{{Languages|Quick HOWTO : Ch27 : Expanding_Disk_Capacity}}
= &nbsp; <br> =
= &nbsp; <br> =


= 概述 =
= 概述 =


The lack of available disk storage frequently plagues Linux systems administrators. The most common reasons for this are expanding databases, increasing numbers of users, and the larger number of tasks your Linux server is expected to perform until a replacement is found.
现有硬盘容量的不足经常困扰着 Linux 系统管理员,最常见的原因是不断膨胀的数据和数量不断增加的用户以及在找到替代以前 Linux 服务器还必须正常执行大量的任务。


This chapter explores how to add a disk to a Linux system in two ways. The first is by moving directories from a full partition to an empty one made available by the new disk and then linking the directory structures of the two disks together. The second is by merging the partitions together to create a combined partition using the Linux Logical Volume Manager (LVM).
本篇讲述了如何利用两种方式将硬盘添加到Linux系统中。第一种方式是,将文件夹从一个已满分区移动到另一个空的分区中,然后将文件夹目录链接到两块硬盘中。另一种方式是,利用Linux逻辑分区管理器(LVM)合并几个分区为一个分区。


=添加硬盘到Linux=
=添加硬盘到 Linux=


At some stage you'll be faced with the task of installing an additional hard drive into your Linux server. Perhaps an existing device failed, or maybe you ran out of available space. To provide more space, this section will cover adding a hard disk with only one partition and will then explain how to migrate data from the full disk to the new one.
在一些场合下您会遇到将一块新硬盘添加到 Linux 服务器中的问题,原因也许是现有的硬盘坏了或者没有空间了。为了提供更多的空间,这部分将会讲述如何添加一块只有一个分区的硬盘以及怎样将数据从已满的硬盘转移到新的硬盘中。


==Scenario==
==引言==


Things are getting crowded on bigboy: Even after you removed all unwanted data, the /var partition is full. You need to add a new hard drive to the system. You can verify this situation with the df -k command's output, which also shows that the other partitions are too full to accept any more data.
有些文件夹总是很拥挤:即使你删掉了所有不需要的数据,/var 分区仍然是满的,这时你需要增加一块新的硬盘驱动器到系统中。你可以通过调用命令 df -k 来验证,下面显示出其他分区也已经满了不能再接受新数据。


  [root@bigboy tmp]# df -k
  [root@bigboy tmp]# df -k
第29行: 第29行:
  [root@bigboy tmp]#
  [root@bigboy tmp]#


A new hard disk was added according to the manufacturer's instructions, but you now need to know how to proceed.
参照生产厂家的说明新的硬盘可以添加到系统中,但是你需要知道怎样去做。


==确定硬盘的类型==
==确定硬盘的类型==


Linux stores the names of all known disk partitions in the /proc/partitions file. The entire hard disk is represented by an entry with a minor number of 0, and all the partitions on the drive are sequentially numbered after that. In the example, the system has two hard disks; disk /dev/hda has been partitioned, but the new disk (/dev/hdb) needs to be prepared to accept data.
/proc/partitons 文件中 Linux 存储了所有已知硬盘分区的名字。整个硬盘用次数字为0的镜像表示,硬盘上的所有分区从1开始顺序排列。下面的例子中系统有两个硬盘,硬盘/dev/hda 已经分过区,而新的硬盘(/dev/hdb)需要设置以接收数据。


  [root@bigboy tmp]# cat /proc/partitions
  [root@bigboy tmp]# cat /proc/partitions
第48行: 第48行:
  [root@bigboy tmp]#
  [root@bigboy tmp]#


'''Note:''' Linux hard disk device names follow a specific standard. SCSI disks all start with sd and IDE disks with hd. After this comes a letter that identifies the unit number of the disk, so for example, the first disk would be a, the second would be b, the third would be c, and so on. Finally, a two-digit number defines the partition number. Using this convention the fifth partition on the fourth IDE drive would be /dev/hdd5.
'''注意:'''Linux 硬盘设备的命名遵循一定的标准。SCSI 硬盘以 sd 开头而 IDE 硬盘以 hd 开头,之后以一个字母来确定唯一的一块硬盘,比如,第一块硬盘是 a,第二块是 b 第三块是 c 以此类推。 最后,一个两位数确定分区号。利用该规则第四块 IDE 驱动器的第五分区应该是/dev/hdd5。


==在新硬盘上准备分区==
==在新硬盘上准备分区==


Linux partition preparation is very similar to that in a Windows environment, because both operating systems share the fdisk partitioning utility. The steps are:
Linux 上分区的准备与 Windows 环境下非常相似,因为这两种操作系统都用 fdisk 分区工具。使用步骤如下:




1) The first Linux step in adding a new disk is to partition it in preparation of adding a filesystem to it. Type the fdisk command followed by the name of the disk. You want to run fdisk on the /dev/hdb disk, so the command is:
1) Linux 下添加一块新的硬盘的第一步是对其进行分区以便以后在上面添加文件系统。
输入 fdisk 命令后面跟着硬盘的名字。比如如果你想在硬盘 /dev/hdb 上运行 fdisk,命令如下:


  [root@bigboy tmp]# fdisk /dev/hdb
  [root@bigboy tmp]# fdisk /dev/hdb
第68行: 第69行:
  Command (m for help):  
  Command (m for help):  


2) Just to make sure you're on the correct device, issue the p command to print all the known partitions on the disk. In this case, there are none which is good.
2) 要确保你在操作正确的硬盘,输入命令 p 显示硬盘上的所有分区。下面的例子中硬盘中没有分区。


  Command (m for help): p
  Command (m for help): p
第80行: 第81行:
  Command (m for help):
  Command (m for help):


3) The fdisk m command prints a small help manual of valid commands. You will see that n is the command to add a new partition. Add a new primary partition, number 1, and use the defaults to make the partition occupy the entire disk.
3) fdisk 中的 m 命令显示所有有效命令的简介,可以看出 n 是增加一个新分区的命令。增加一个编号为1的新分区,使用默认值使该分区覆盖整个硬盘。


  Command (m for help): n
  Command (m for help): n
第92行: 第93行:
  Last cylinder or +size or +sizeM or +sizeK (1-9729, default 9729):
  Last cylinder or +size or +sizeM or +sizeK (1-9729, default 9729):


4) Run the print (p) command to confirm that you successfully created the partition partition.
4) 运行显示命令(p)确认已经成功建立了新分区。


  Command (m for help): p
  Command (m for help): p
第105行: 第106行:
  Command (m for help):
  Command (m for help):


'''Tip:''' If you make a mistake, you can use the d command to delete the partition and start over. The t command enables you to change partition type from the default of 83 for regular Linux partitions to something else, such as 82 for swap space. In most cases, this won't be necessary, the default value is sufficient.
'''提示:''' 如果操作错误,可以使用命令 d 删除该分区然后重新开始。命令 t 可以用来改变分区类型,从常规的 Linux 分区83到其他,比如82是 swap 空间。大多数情况下,你不必改变分区类型,默认的值已经可以了。


'''Note:''' When you created the new partition, you may have noticed that fdisk queried you as to whether it was going to be a primary or secondary partition. Linux allows only four primary partitions; if you need more, you can convert one of the primary ones into an extended one. Here is an example of a partition table that includes an extended partition followed by two regular partitions within it.
'''注意:''' 在创建新的分区时也许你已经注意到 fdisk 询问将要创建的分区是一个主分区还是扩展分区。 Linux 只允许存在四个主分区,如果需要更多的,可以将其中一个主分区转换成扩展分区。下面例子的分区表显示存在一个扩展分区其中有两个普通分区。


  Command (m for help): p
  Command (m for help): p
第125行: 第126行:
  Command (m for help):
  Command (m for help):


Adding more partitions is just a question of repeating the previous steps the required number of times, while remembering that at some stage, you may need to add an extended partition.
添加更多的分区可按照上面所述,记住,在一些情况下你可能需要添加一个扩展分区。


5) Changes won't be made to the disk's partition table until you use the w command to write, or save, the changes. Do that now, and, when finished, exit with the q command.
5) 在使用命令 w 来保存所做的更改前对分区表的更改并不会生效。完成后请用命令 w 保存更改,并输入命令 q 退出。


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


After this is complete you'll need to verify your work and start migrating your data to the new disk. These steps will be covered next.
完成以上步骤后,你需要验证以下然后开始将数据转移到新硬盘中,后面将介绍这些步骤。


==Verifying Your New Partition==
==验证新分区==


You can take a look at the /proc/partitions file or use the fdisk -l command to see the changes to the disk partition structure of your system:
查看 /proc/partitions 文件或者调用命令 fdisk -l 确认硬盘上所做的更改。


  [root@bigboy tmp]# cat /proc/partitions
  [root@bigboy tmp]# cat /proc/partitions
第162行: 第163行:
==在新分区上放置文件夹目录==
==在新分区上放置文件夹目录==


You now need to format the partition, giving it a new directory structure by using the mkfs command. The Fedora installation procedure defaults to an ext3 type, which is what you should use here.
现在要格式化分区并利用命令 mkfs 给分区一个新的文件结构。Fedora 系统上默认使用 ext3 类型,如下所示:


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


Next, you must create special mount point directory, to which the new partition will be attached. Create directory /mnt/hdb1 for this purpose.
下一步,你必须创建一个特殊的挂载点目录,以便新的分区挂载。创建文件夹 /mnt/hdb1以便挂载。


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


When Linux boots, it searches the /etc/fstab file for a list of all partitions and their mounting characteristics, and then it mounts the partitions automatically. You'll have to add an entry for your new partition that looks like this:
当 LInux 启动时会搜索 /etc/fstab 文件获取所有分区及其挂载特性,然后系统会自动挂在这些分区。你需要在该文件中添加一行如下所示:


  #
  #
第177行: 第178行:
  /dev/hdb1  /mnt/hdb1  ext3  defaults 1 2
  /dev/hdb1  /mnt/hdb1  ext3  defaults 1 2


The first entry is the name of the partition followed by the mount point directory and the filesystem type. The fourth entry defines the mounting options, which need be only default for most scenarios. The fifth entry governs whether the dump filesystem backup command can be used for the filesystem. A value of 0 means no, and 1 means yes. The final entry defines the order in which a filesystem check is done at boot time. The check is done twice. The root (or master) filesystem has a value of 1 and is checked on the first pass, all other filesystems should have a value of 2. If you are not familiar with the /etc/fstab file use the man fstab command to get a full explanation of its various options.
第一段是分区的名字,后面是挂在点目录及文件系统类型。第四段定义了挂在选项,大多数情况下只需要是 defaults 即可。第五段描述的是整理文件系统命令是否可用,0为不可用,1为可用。最后字段定义了启动时文件系统检查的顺序,该检查将会执行两遍。主文件系统取值为1将会首先检查,其他所有文件系统应当取为2。如果对 /etc/fstab 文件不熟悉可以输入命令 man fstab 来获取各个选项的详细解释。


You don't have to wait for a reboot to mount your partition. You can use the mount command with the -a option to read the /etc/fstab file for new entries.
你不必等待重启来挂载分区,可以使用带 -a 选项的 mount 命令来读取 /etc/fstab 并更新。


  [root@bigboy tmp]# mount -a
  [root@bigboy tmp]# mount -a


You are now able to access your new partition as device /mnt/hdb1.
现在你可以用 /mnt/hdb1 读取新的分区了。


==Migrating Data Over To your New Partition==
==转移数据到新分区==


As you remember from investigating with the df -k command, the /var partition is almost full.
还记得之前调用命令 df -k 检查磁盘分区吗,分区 /var 几乎已经满了。


  [root@bigboy tmp]# df -k
  [root@bigboy tmp]# df -k
第199行: 第200行:
  [root@bigboy tmp]#
  [root@bigboy tmp]#


The du -sk * command shows the disk usage of all subdirectories in a directory. You can recursively use the command by using the cd command to step down through all the subdirectories until you discover the one with the greatest file usage. In this case, you only had to go to the /var directory to see that the /var/transactions directory was the culprit.
命令 du -sk * 显示一个文件夹中所有子文件夹占用的磁盘空间。通过命令 cd 进入各个子文件夹然后循环调用前面的命令直到发现最大的文件。这种情况下,你只须进入 /var 文件夹然后找到元凶 /var/transactions。


  [root@bigboy tmp]# cd /var
  [root@bigboy tmp]# cd /var
第213行: 第214行:
  [root@bigboy var]#
  [root@bigboy var]#


As a solution, the /var partition will be expanded to the new /dev/hdb1 partition mounted on the /mnt/hdb1 directory mount point. To migrate the data, use these steps:
作为解决办法,分区 /var 需要扩展成新的 /dev/hdb1 分区,而这个分区挂在在文件夹 /mnt/hdb1上。按照以下步骤转移数据:


1) Back up the data on the partition you are about to work on.
1) 备份要操作的分区上的数据。


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


  [root@bigboy tmp]# who
  [root@bigboy tmp]# who
第230行: 第231行:
  [root@bigboy tmp]#
  [root@bigboy tmp]#


3) Log into the VGA console, and enter single-user mode.
3) 登陆到 VGA 终端,进入单用户模式。


  [root@bigboy tmp]# init 1
  [root@bigboy tmp]# init 1


4) Rename the /var/transactions directory /var/transactions-save to make sure you have an easy to restore backup of the data, not just the tapes.
4) 将目录 /var/transactions 重命名为 /var/transactions-save 以保证以后能够恢复轻松地恢复这些数据。


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


5) Create a new, empty /var/transactions directory; this will later act as a mount point.
5) 创建一个新的空目录 /var/transactions; 这个目录后面将作为挂载点。


  sh-2.05b# mkdir /var/transactions
  sh-2.05b# mkdir /var/transactions


6) Copy the contents of the /var/transactions-save directory to the root directory of /dev/hdb1, which is actually /mnt/hdb1.
6) 拷贝目录 /var/transactions-save 里的内容到 /dev/hdb1 的根目录下,即 /var/hdb1 里面。
 


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


7) Unmount the new /dev/hdb1 partition.
7) 将分区 /dev/hdb1 取消挂载。


  sh-2.05b# umount /mnt/hdb1
  sh-2.05b# umount /mnt/hdb1


8) Edit the /etc/fstab file, removing our previous entry for /dev/hdb1 replacing it with one using the new mount point.
8) 编辑文件 /etc/fstab,移除先前的关于 /dev/hdb1 的条目并且用新的挂载点条目代替之。


  #
  #
第260行: 第262行:
  /dev/hdb1  /var/transactions  ext3  defaults 1 2
  /dev/hdb1  /var/transactions  ext3  defaults 1 2


9) Remount /dev/hdb1 on the new mount point using the mount -a command, which reads /etc/fstab and automatically mounts any entries that are not mounted already.
9) 利用命令 mount -a 重新挂载 /dev/hdb1 到新的挂载点,该命令会调用文件 /etc/fstab 并且自动挂载任何还没挂载的条目。


  sh-2.05b# mount -a
  sh-2.05b# mount -a


10) Test to make sure that the contents of the new /var/transactions directory is identical to /var/transactions-save.
10) 测试下以便确认新的文件夹 /var/transactions 里的内容跟 /var/transactions-save 是相同的。


11) Return to multi-user mode by typing exit. The system will return to its default runlevel.
11) 输入命令 exit 返回多用户模式,系统就会返回默认的运行级别。


  sh-2.05b# exit
  sh-2.05b# exit


12) Make sure your applications are working correctly and delete both the /var/transactions-save directory and the /mnt/hdb1 mount point directory at some later date.
12) 确认应用程序能够正常工作,之后可以删除目录 /var/transactions-save /mnt/hdb1 挂载点目录。


This exercise showed you how to migrate the entire contents of a subdirectory to a new disk. Linux also allows you to merge partitions together, to create a larger combined one. The reasons and steps for doing so will be explained next.
这个练习展示了如何将一个子目录中的所有数据转移到一个新的硬盘中。Linux 也允许合并分区以创建一个更大的分区。下面将会讲述原因和操作步骤。


=利用LVM扩展分区=
=利用LVM扩展分区=


The Logical Volume Manager (LVM) enables you to resize your partitions without having to modify the partition tables on your hard disk. This is most useful when you find yourself running out of space on a filesystem and want to expand into a new disk partition versus migrating all or a part of the filesystem to a new disk.
逻辑分区管理器(LVM)能够在不改变硬盘分区表的前提下重新调整各个分区的大小。当你发现空间不够用而想拓展成一个新的分区,相对于转移所有或者部分数据到一个新硬盘中这是最有用的办法。


* '''Physical Volume''': A physical volume (PV) is another name for a regular physical disk partition that is used or will be used by LVM.
* "物理分区": 物理分区(PV),在LVM中又称为常规物理磁盘分区。
* '''Volume Group''': Any number of physical volumes (PVs) on different disk drives can be lumped together into a volume group (VG). Under LVM, volume groups are analogous to a virtual disk drive.
* "分区组": 任何数量在不同硬盘驱动器上的物理分区(PVs)可以合称为一个分区组(VG)。在 LVM 中,分区组可以看做一个虚拟硬盘驱动器。
* '''Logical Volumes''': Volume groups must then be subdivided into logical volumes. Each logical volume can be individually formatted as if it were a regular Linux partition. A logical volume is, therefore, like a virtual partition on your virtual disk drive.
* "逻辑分区" 分区组必须再分割成逻辑分区。每一个逻辑分区可以独立地进行格式化就好像是一个常规物理分区一样。因此,逻辑分区就好像虚拟硬盘上的虚拟分区一样。


:This may seem complicated, but it allows you to create new virtual partitions with sizes you can change from groups of real disk partitions whose sizes you probably cannot change. Another advantage of LVM is that this can all be done without disturbing other partitions on your hard disks.
: 这看起来很复杂,但是LVM允许创建新的虚拟分区,它的容量大小可以从真实的硬盘分区组中调整,而真实磁盘分区的大小可能你并不像改变。LVM 的另一个好处是,在做这些更改时可以不影响硬盘中的其他分区。


* '''Physical Extent''': Real disk partitions are divided into chunks of data called physical extents (PEs) when you add them to a logical volume. PEs are important as you usually have to specify the size of your volume group not in gigabytes, but as a number of physical extents.
* "物理内容": 真实的硬盘分区被分割成一个个数据块,当把它们添加进逻辑分区时被称为物理内容(PEs)。物理内容重要是因为当描述分区组的大小时不是用多少G字节,而是用多少物理内容。


Make sure you understand these terms fully as they will be used repeatedly in many of the following sections. Lets go!
确保你对这些术语完全理解了,因为后面我们会频繁地用到它们。下面我们要出发了~


==配置LVM设备==
==配置LVM设备==
第408行: 第410行:
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.
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!
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!
第428行: 第430行:
  sh-2.05b#
  sh-2.05b#


===Create A Volume Group For the PVs===
===为多个物理分区创建一个组===


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.
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.
第438行: 第440行:
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.
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.


===Create A Logical Volume From The Volume Group===
===从组中创建一个逻辑分区===


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).
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).
第478行: 第480行:
  sh-2.05b# lvcreate -l 50%VG -n lvm0 lvm-hde
  sh-2.05b# lvcreate -l 50%VG -n lvm0 lvm-hde


===Format The Logical Volume===
===格式化该逻辑分区===


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.
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.
第505行: 第507行:
  sh-2.05b#
  sh-2.05b#


===Create A Mount Point===
===创建挂载点===


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.
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.
第511行: 第513行:
  sh-2.05b# mkdir /home
  sh-2.05b# mkdir /home


===Update The /etc/fstab File===
===更新 /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.
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.
第522行: 第524行:
  #/dev/hdf1      /data2        ext3    defaults        1 2
  #/dev/hdf1      /data2        ext3    defaults        1 2


===Mount The Volume===
===挂在该分区===


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
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
第531行: 第533行:
  sh-2.05b#
  sh-2.05b#


===Restore Your Data===
===复原数据===


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


===Get Out Of Single User Mode===
===切出单用户模式===


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


=Conclusion=
=总结=


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

2010年9月6日 (一) 19:49的最新版本

{{#ifexist: :Quick HOWTO : Ch27 : Expanding Disk Capacity/zh/zh | | {{#ifexist: Quick HOWTO : Ch27 : Expanding Disk Capacity/zh/zh | | {{#ifeq: {{#titleparts:Quick HOWTO : Ch27 : Expanding Disk Capacity/zh|1|-1|}} | zh | | }} }} }} {{#ifeq: {{#titleparts:Quick HOWTO : Ch27 : Expanding Disk Capacity/zh|1|-1|}} | zh | | }}

 

概述

现有硬盘容量的不足经常困扰着 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.