linux扩展分区

对于linux系统管理员来说,随着业务的发展,磁盘容量不足是需要面对的一个难题。这里通过两种方式来说明如果添加新的磁盘到linux系统当中去一是连接新的disk,对disk分区和格式化,然后合并以存在的filesystem和新增加的disk的filesystem二是通过linux logical volumn manager 来管理磁盘

 

添加新的disk到linux当中来

[完成的任务]将新增加的disk做出一个parition,然后将linux上已有磁盘上数据全部或部分迁移到新的磁盘上来

假设现在linux system中的filesystem的使用情况大致是:

[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

可以看到/var挂载点挂载的/dev/hda2 partition已经使用了99%。需要增加新的磁盘容量。

通过查看/proc/partitions proc 文件,可以查看当前linux系统中所有已知的partitions

[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]#

可以看到minor 值为0表示是整块磁盘, 磁盘上的分区的major同一样,minor是根据parition的增加而增加,这里hda1 hda2 … hda6的minor分别为1,2,…,6

Linux系统中hard disk命名规则, scsi磁盘以sd开头,ide磁盘以hd开头,scsi的第一个磁盘为sda, 依次其它的为sdb,sdc,sdd, IDE同样如此,hda, hdb.

sda上第一个parition为sda1,剩余的以此类推。

接下来,为新增加的disk hdb建立分区。

[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): 
============================================================
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):
可以看到目前hdb 上没有建立parition。
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):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-9729, default 9729):
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):

如果你在新建partition的时候出错,则你可以使用d命令来删除该parition,然后重新来过。 另外,linux最多允许四个primary partition, 如果你需要建立更多的分区,使用建立extended partition. 建立完extended partition之后,可以在extended partition上建立新的logical partition。

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 #logical parition on extended parition
/dev/hda6             845         913      554211   83  Linux # ditto

当你完成对hdb分区后, 查看下分区表分区是否正确 : 通过cat /proc/partitions 或 fdisk -l

接下来就需要对新增的disk hdb进行格式化,为其建立新的目录结构。

mkfs –t ext3 /dev/hdb1

然后为/dev/hdb1 建立挂载点,这里是/mnt/hdb1

修改/etc/fstab 将/dev/hdb1 挂载到/mnt/hdb1

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

最后别忘记挂载,挂载完成后就可以使用新增的disk了。

小结:新增disk基本需要:连接好disk,对disk分区, 格式化,最后建立挂载点,挂载

关于LVM,在Linux中扩展磁盘容量(2)—LVM 中总结。


场景,假设/home 下已经没有足够的空间,由于预算的问题,也不太可能购买一块足够大的磁盘,用来作为/home的空间。

1 备份你的数据

使用工具打包你/home目录下的数据,因为LVM会破坏其physical volumn上的数据。

因为/home下存放了你所有用户的数据,并且/home挂载在/dev/hde5 上, 所有为了在对/home进行操作的时候不受其他人的干扰,首先进入single-user mode

who命令查看同时登录的用户

wall “warning message” 通知其他登录的用户。

init 1 进入到single-user mode, 因为runlevel 1下需要网络service不可用所以,确保你是从VGA console登录到你的系统的。

接下来你就可以umount 你的/home 挂载点了。

2 查看你的partition的type ID

fdisk /dev/hde5

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命令来修改/dev/hde5 的type ID 以及新增加的盘/dev/hdf的type ID 同时对新增加的盘进行分区。

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):

Command (m for help): t

Partition number (1-6): 5

Hex code (type L to list codes): 8e # Linux LVM type ID



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):

检查下 确保你的修改已经生效

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):
 

保存对partition table 的修改

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#

创建PV (physical volumn)

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

扫描系统中存在的VG (Volumn Group) 命令VGscan 如果系统中没有存在的volumn group,则创建一个新的VG。

vgcreate lvm-hde /dev/hdf1 /dev/hde5

如果一切顺利,系统中一个新的VG “lvm-hde” 被创建出来,查看下VG的状态

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

接下了 就可以从VG “lvm-hde”中创建LV (Logical Volumn)

sh-2.05b# lvcreate -l 212 lvm-hde -n lvm0 #通过指定PE (Physical extends)来创建LV


Logical volume "lvm0" created
sh-2.05b# lvcreate -l 100%FREE -n lvm0 lvm-hde #通过指定VG的百分比大小来创建新的LV
sh-2.05b# lvcreate -l 50%VG -n lvm0 lvm-hde

当LV被创建完成以后,需要对其格式化后才可以被使用

格式化过程如下

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.

到这里,/dev/lvm-hde/lvm0这个LV创建,格式化完成,下面创建挂载点将其挂载。
mkdir /home
修改/etc/fstab
/dev/lvm-hde/lvm0   /home      ext3    defaults        1 2

mount –a 挂载/dev/lvm-hde/lvm0 到/home 挂载点下

到这时,你的新的/home空间大小是/dev/hde5 /dev/hdf1大小的和,所以你可以将你备份的/home目录下的历史数据迁移会/home目录下

总结

一, Logical Volumn创建的基本步骤:

1 初始化将被你用作PV的parition, 修改partition type ID –》 Linux LVM

2 使用创建好的PV来创建 VG

3 从VG中创建新的LV

image

4 为创建好的LV创建文件系统 (ext3, GFS , etc)

5 创建挂载点

6 挂载,如果需要自动挂载,修改/etc/fstab 配置文件。

完毕。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值