Linux系统扩展硬盘空间

最近在编译内核的时候,遇到了/usr/src/硬盘空间不足的问题,此前遇到硬盘分区的问题总是用图形化工具进行,这样虽然高效可是总感觉太傻瓜,正好凑着这个机会总结一下。


Linux相关命令介绍

本文主要介绍在没有使用LVM的情况下如何对空间进行扩容操作,在介绍具体步骤之前,需要先了解一下Linux有关磁盘空间的命令。
1、df命令
linux中df命令的功能是用来检查linux的文件系统的磁盘空间占用情况。可以利用该命令来获取硬盘被占用了多少空间,目前还剩下多少空间等信息。具体的参数利用--help参数获得即可,常用的是-h选项,文件大小采用GB,MB等方式显示。
命令格式:df [选项] [文件]

[root@localhost Desktop]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        18G   12G  4.7G  72% /
tmpfs           947M   80K  947M   1% /dev/shm
/dev/sda1       283M   91M  177M  34% /boot
/dev/sda3        20G  1.0G   18G   6% /usr/local

2、du命令
Linux du命令也是查看使用空间的,但是与df命令不同的是Linux du命令是对文件和目录磁盘使用的空间的查看,还是和df命令有一些区别的。常用选项有-h,文件采用GB,MB方式显示;-s只显示路径的总文件大小。
命令格式:du [选项][文件]

[root@localhost Desktop]# du -h /usr/local/
4.0K    /usr/local/games
96K /usr/local/libexec/git-core/mergetools
650M    /usr/local/libexec/git-core
650M    /usr/local/libexec
980M    /usr/local/

[root@localhost Desktop]# du -sh /usr/local/
980M    /usr/local/

Linux磁盘空间扩容

相关操作介绍

1、查看当前分区信息

[root@localhost Desktop]# fdisk -l

Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0006fead

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          39      307200   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              39        2354    18598912   83  Linux
/dev/sda3            2354        4965    20974226+  83  Linux
/dev/sda4            4966        5221     2056320   82  Linux swap / Solaris

2、分区操作

[root@localhost Desktop]# fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
     switch off the mode (command 'c') and change display units to
     sectors (command 'u').

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

a :设置可引导标记
b :修改bsd的磁盘标签
c :设置DOS操作系统兼容标记
d :删除一个分区
l :显示已知的分区类型,其中82为Linux swap分区,83为Linux分区
m :显示帮助信息
n :增加一个新的分区
o :创建一个新的空白的DOS分区表
p :显示磁盘当前的分区表
q :退出fdisk程序,不保存任何修改
s :创建一个新的空白的Sun磁盘标签
t :改变一个分区的系统号码(比如把Linux Swap分区改为Linux分区)
u :改变显示记录单位
v :对磁盘分区表进行验证
w :保存修改结果并退出fdisk程序
x :特殊功能

(1)删除分区

Command (m for help): d            //输入命令d
Partition number (1-4): 3          //这里输入的数字根据fdisk -l结果中的顺序确定

Command (m for help): p       //查看当前分区情况,可以看到sda3已经删除

Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0006fead

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          39      307200   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              39        2354    18598912   83  Linux
/dev/sda4            4966        5221     2056320   82  Linux swap / Solaris

(2)创建主分区

Command (m for help): n       //输入n创建新分区
Command action
   e   extended
   p   primary partition (1-4)
p                //创建主分区,主分区最多能创建4个
Partition number (1-4): 3        //fdisk -l的结果显示已经有sda1 sda2从sda3开始创建
First cylinder (2354-5221, default 2354):   //起始柱面,采用默认即可,回车
Using default value 2354
Last cylinder, +cylinders or +size{K,M,G} (2354-5221, default 5221): +20G  //截止柱面,采用+size的形式,+20G代表增加20G的空间

Command (m for help): p   //打印观察当前分区信息,可以看到sda3被创建

Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0006fead

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          39      307200   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              39        2354    18598912   83  Linux
/dev/sda3            2354        2374      20201900   83  Linux

Command (m for help): 

(3)创建逻辑分区
扩展分区是无法使用的,必须在扩展分区的基础上创建逻辑分区,具体步骤如下所示:

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
e       //创建逻辑分区
Selected partition 4
First cylinder (2375-5221, default 2375): 
Using default value 2375
Last cylinder, +cylinders or +size{K,M,G} (2375-5221, default 5221): 
Using default value 5221

Command (m for help): p

Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0006fead

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          39      307200   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              39        2354    18598912   83  Linux
/dev/sda3            2354        2374      162019   83  Linux
/dev/sda4            2375        5221    22868527+   5  Extended   //sda4为Extended不能直接使用需要在此基础上创建逻辑分区

Command (m for help): n    //创建逻辑分区
First cylinder (2375-5221, default 2375): 
Using default value 2375
Last cylinder, +cylinders or +size{K,M,G} (2375-5221, default 5221): 
Using default value 5221

Command (m for help): p

Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0006fead

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          39      307200   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              39        2354    18598912   83  Linux
/dev/sda3            2354        2374      162019   83  Linux
/dev/sda4            2375        5221    22868527+   5  Extended
/dev/sda5            2375        5221    22868496   83  Linux   //可以看到sda5逻辑分区被创建

(4)创建swap交换分区

Command (m for help): n    //创建分区
Command action
   e   extended
   p   primary partition (1-4)
p   //创建主分区
Partition number (1-4): 3
First cylinder (2354-5221, default 2354): 
Using default value 2354
Last cylinder, +cylinders or +size{K,M,G} (2354-5221, default 5221): +2G

Command (m for help): t    //利用t命令将分区转化成swap分区
Partition number (1-4): 3
Hex code (type L to list codes): 82     //swap分区的类型号为82,Linux分区类型号为83,LVM类型分区号为8e
Changed system type of partition 3 to 82 (Linux swap / Solaris)

Command (m for help): p

Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0006fead

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          39      307200   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              39        2354    18598912   83  Linux
/dev/sda3            2354        2615     2097851+  82  Linux swap / Solaris   //可以看到sda3已经成为swap分区

(5)保存退出操作

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 or after you run partprobe(8) or kpartx(8)
Syncing disks.


Command (m for help): q    //不保存退出

[root@localhost Desktop]# 

(6)格式化分区

格式化Linux分区

[root@localhost Desktop]# mkfs.ext4 /dev/sda3  //格式化sda3为ext4格式
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1311184 inodes, 5243556 blocks
262177 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=0
161 block groups
32768 blocks per group, 32768 fragments per group
8144 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
    4096000

Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 28 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@localhost Desktop]# 

格式化Swap分区

[root@localhost Desktop]# mkswap /dev/sda4   //格式化sda4为swap分区格式
Setting up swapspace version 1, size = 2056316 KiB
no label, UUID=648482ec-0b42-4e14-af17-070742831f19
[root@localhost Desktop]# 

(7)挂载分区

临时挂载

[root@localhost Desktop]# mount /dev/sda3 /mnt/   //将mnt挂载到sda3上
[root@localhost Desktop]# df -h   //查看当前挂载信息
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        18G   13G  3.7G  78% /
tmpfs           947M  228K  947M   1% /dev/shm
/dev/sda1       283M   91M  177M  34% /boot
/dev/sda3        20G   44M   19G   1% /mnt
[root@localhost Desktop]# 

永久挂载
修改/etc/fstab文件,在结尾添加/dev/sda3 /usr/local/ ext4 defaults 0 0

 #/etc/fstab
 #Created by anaconda on Tue Dec 23 05:42:01 2014

 #Accessible filesystems, by reference, are maintained under '/dev/disk'
 #See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

UUID=4be66d01-fba7-496d-94e0-0727d44cdc07 /                       ext4    defaults        1 1
UUID=4a6343e8-80d2-4c56-93e9-9e0eca314ce0 /boot                   ext4    defaults        1 2
UUID=78f620c5-f8e1-457f-8a3d-dbb629ea7616 swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/sda3               /usr/local/             ext4    defaults       0 0

磁盘扩容具体步骤

有了上一小节的铺垫,磁盘扩容就简单多了,下面以Vmware下Cent OS为例进行扩大/usr/local/文件夹的磁盘空间。以下步骤是将Vmware虚拟机中的Linux进行磁盘扩展,真机的过程大同小异,真机不需要第一步虚拟机的相关操作,不做详细介绍。
一、虚拟机相关操作
关闭相关虚拟机--->虚拟机--->设置--->扩展(输入硬盘容量最大值)--->完成
二、系统中的相关分区操作
1、使用fdisk -l查询当前系统分区情况

[root@localhost Desktop]# fdisk -l 

Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0006fead

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          39      307200   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              39        2354    18598912   83  Linux
/dev/sda3            2354        2611     2064384   82  Linux swap / Solaris

2、创建主分区

[root@localhost Desktop]# fdisk /dev/sda  //对sda进行操作

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

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): n    //创建新分区
Command action
   e   extended
   p   primary partition (1-4)
p    //主分区
Selected partition 4    //主分区最多创建4个,由于系统已经有3个主分区,自动选择partition 4
First cylinder (2611-5221, default 2611):    //起始柱面默认  回车
Using default value 2611
Last cylinder, +cylinders or +size{K,M,G} (2611-5221, default 5221):  //截止柱面默认 回车  也可以+20G         
Using default value 5221

Command (m for help): p   //查看最新分区信息

Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0006fead

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          39      307200   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              39        2354    18598912   83  Linux
/dev/sda3            2354        2611     2064384   82  Linux swap / Solaris
/dev/sda4            2611        5221    20966162+  83  Linux

3、保存退出分区操作

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 or after you run partprobe(8) or kpartx(8)
Syncing disks.   //虚拟机需要重启才能识别新分区

4、格式化相关分区
重启后,格式化分区

root@localhost Desktop]# mkfs.ext4 /dev/sda4   //将sda4格式成ext4格式
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1310720 inodes, 5241540 blocks
262077 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=0
160 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
    4096000

Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 22 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

5、挂载分区
注意: 将指定文件夹挂载到新分区,相关内容会被清空,比如要将/usr/local下的内容挂载到/dev/sda4,需要将/usr/local下的相关内容备份,挂载结束后,在复制到/usr/local下。

修改/etc/fstab文件,在结尾添加/dev/sda4 /usr/local/ ext4 defaults 0 0
重启机器,利用df -h查看磁盘情况

 Size  Used Avail Use% Mounted on
/dev/sda2        18G   13G  3.7G  78% /
tmpfs           947M  224K  947M   1% /dev/shm
/dev/sda1       283M   91M  177M  34% /boot
/dev/sda4        20G   44M   19G   1% /usr/local

可以看到/usr/local/已经挂载到/dev/sda4上,空间为20G,将备份的的内容重新复制到/usr/local/即可。


LVM介绍

由于当时装系统的时候没有采用LVM方式,导致无法直接对/目录进行扩展,以后装系统的时候还是采用LVM管理磁盘空间吧,LVM的优点就不在这里详细叙述,下边对LVM进行简单的总结,以便日后方便使用。

一、LVM简介
LVM是 Logical Volume Manager(逻辑卷管理)的简写。LVM将一个或多个硬盘的分区在逻辑上集合,相当于一个大硬盘来使用,当硬盘的空间不够使用的时候,可以继续将其它的硬盘的分区加入其中,这样可以实现磁盘空间的动态管理,相对于普通的磁盘分区有很大的灵活性。
如下图所示:由四个磁盘分区可以组成一个很大的空间,然后在这些空间上划分一些逻辑分区,当一个逻辑分区的空间不够用的时候,可以从剩余空间上划分一些空间给空间不够用的分区使用。


LVM模型

二、LVM常用术语
1、物理存储介质(The physical media):这里指系统的存储设备:硬盘,如:/dev/hda1、/dev/sda等等,是存储系统最低层的存储单元。
2、物理卷(physical volume):物理卷就是指硬盘分区或从逻辑上与磁盘分区具有同样功能的设备(如RAID),是LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘等)比较,却包含有与LVM相关的管理参数。
3、卷组(Volume Group):LVM卷组类似于非LVM系统中的物理硬盘,其由物理卷组成。可以在卷组上创建一个或多个“LVM分区”(逻辑卷),LVM卷组由一个或多个物理卷组成。
4、逻辑卷(logical volume):LVM的逻辑卷类似于非LVM系统中的硬盘分区,在逻辑卷之上可以建立文件系统(比如/home或者/usr等)。
5、PE(physical extent):每一个物理卷被划分为称为PE(Physical Extents)的基本单元,具有唯一编号的PE是可以被LVM寻址的最小单元。PE的大小是可配置的,默认为4MB。
6、LE(logical extent):逻辑卷也被划分为被称为LE(Logical Extents) 的可被寻址的基本单位。在同一个卷组中,LE的大小和PE是相同的,并且一一对应。
简单来说就是:
PV:是物理的磁盘分区
VG:LVM中的物理的磁盘分区,也就是PV,必须加入VG,可以将VG理解为一个仓库或者是几个大的硬盘。
LV:也就是从VG中划分的逻辑分区


PV VG LV逻辑关系

参考文献

http://www.tuicool.com/articles/AfM7Nn6
http://www.linuxidc.com/Linux/2012-06/61873.htm
http://www.cnblogs.com/gaojun/archive/2012/08/22/2650229.html
http://www.osyunwei.com/archives/9368.html

你可能感兴趣的:(Linux系统扩展硬盘空间)