最近在编译内核的时候,遇到了/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逻辑关系

参考文献

linux系统加硬盘容量,Linux系统扩展硬盘空间相关推荐

  1. linux系统最大支持多大硬盘容量,LINUX操作系统对硬件支持有上限么?最大多少内存?多大硬盘容量?...

    32位的Linux的内存最大支持到4GB,64位的Linux的最大支持内存在TB级别上. (实际上最大支持多大的内容跟操作系统的种类无关,而是跟操作系统是几位的.还有CPU是几位的有关.) DOS是1 ...

  2. windows系统加了一个别的系统!------centos_6 by VMware

    如何在虚拟机中VMware安装centos_6 ----------------ef--------------- 链接在博文底下 一.安装准备 1.1.环境准备(文件下载ing) vmware下载最 ...

  3. linux扩充文件夹容量,Linux扩充磁盘空间 【附查看磁盘命令】

    今天手里一台raksmart要重做一下系统,但是做完以后发现磁盘空间只有2G-顿时感觉整个人不好了.于是乎练习客服处理,RAKSmart机器速度虽然一般,但是客服回复是真快,只需要一段命令就可以完美解 ...

  4. 监控系统加装存储服务器,监控系统加装存储服务器

    监控系统加装存储服务器 内容精选 换一换 本文介绍了裸金属服务器BMS产品新特性和对应的文档动态,新特性将在各个区域(Region)陆续发布,欢迎体验. 弹性云服务器(Elastic Cloud Se ...

  5. 计算机硬盘容量分盘计算,硬盘怎么规划分区大小才算科学合理? | 我爱分享网...

    硬盘怎么规划分区大小才算科学合理?这个问题通常电脑小白都会问,虽然有些非小白可能知道怎么分,但是下面有些内容你也不一定知道哦!我们常见硬盘大小为120G.500G.1T,下面就以这三种来介绍硬盘分区大 ...

  6. 计算机硬盘没容量了,电脑硬盘容量为什么偏少 硬盘空间容量有差异的原因

    相信很多人都会看到硬盘的实际容量与标识容量有差异的现象,一般1TB硬盘只有950G左右,那么为什么会这样呢?为什么硬盘容量无法达到标准容量呢?除了换算的差异之外还有其他原因吗?下面为大家详细解释硬盘容 ...

  7. linux 文件夹增加容量,Linux系统下对目录扩容的方法介绍

    导读 本文介绍了在Linux系统下对目录扩容的方法,一起来看一下吧. 1.现象: 日志服务器当初考虑不周,分区划分不太合理: 2.目标: 将/home磁盘空间缩减  并将新的磁盘分区扩充到/根目录 卸 ...

  8. 一加3t运行linux,一加3T手机A3010系统运行速度变慢变卡顿了_怎么进行刷机教程解决...

    如果咱们的这个一加手机3T也就是一加A3010的手机用着用着运行速度变慢变卡顿了,这个时候你会怎么办,你会用什么方法来进行处理呢,下面就来说一个比较常用的解决方法了,这个解决方法也是挺简单的,就是重新 ...

  9. linux恢复树莓派内存卡容量,树莓派自动扩展tf卡剩余空间

    在树莓派上开发了程序,配置了很多内容,然后现在需要做一个镜像出来,我的TF卡是16G的,传输起来实在太不方便.在网上找了很多给镜像瘦身的方法,然而并没有找到一个可靠的行之有效的方法,大部分的方法的确是 ...

最新文章

  1. Recyclerview 添加一个数组
  2. spring boot + redis 实现网站限流和接口防刷功能
  3. SharePoint 2013 新建网站集图解
  4. Java程序员在中年危机的时候,如何避免被“优化”掉呢?
  5. 2011辞职日志:辞职最关键时刻在下周一
  6. C++笔记:select多路复用机制
  7. win10下安装和卸载Ubuntu双系统
  8. python在cmd界面输入命令_python如何分别向两个cmd窗口输入指令?
  9. 浪潮“151计划”:信息安全 没有“偏远地区”
  10. 动手学Pytorch深度学习建模与应用
  11. scanf的用法大全
  12. JavaScript验证手机号码、电子邮箱格式
  13. node安装及环境配置
  14. 关于股票除权复权,前复权、后复权、不复权
  15. jquery图片3D旋绕效果 rotate3Di的操作
  16. cap 2 加州房价预测
  17. Github连接不上怎么办?
  18. 两台电脑如何直接用一根网线传数据
  19. 计算机管理usb出现问号,USB设备全部都是问号,求助?
  20. php直播源码,图片亮度

热门文章

  1. centos安装zendopcache
  2. spring定时任务
  3. (解题思路)Entity Framework 如动态创建表或者列
  4. 编写windows 控件需要注意的几个标签属性(Attribute)
  5. 年过30 ,这10条人生建议句句肺腑
  6. 数据去中心化的场景与流程
  7. 项目中常用的 iOS 第三方库
  8. java-并发-并发容器(3)
  9. /dev 设备文件属性解读
  10. Windows下lex 与 yacc的使用