linux系统加硬盘容量,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邏輯關系
參考文獻
總結
以上是生活随笔為你收集整理的linux系统加硬盘容量,Linux系统扩展硬盘空间的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux中menu命令,menucof
- 下一篇: linux内核__force,Linux