最近在編譯內(nèi)核的時(shí)候获枝,遇到了/usr/src/
硬盤空間不足的問題省店,此前遇到硬盤分區(qū)的問題總是用圖形化工具進(jìn)行雹舀,這樣雖然高效可是總感覺太傻瓜说榆,正好湊著這個(gè)機(jī)會(huì)總結(jié)一下稍味。
Linux相關(guān)命令介紹
本文主要介紹在沒有使用LVM的情況下如何對(duì)空間進(jìn)行擴(kuò)容操作模庐,在介紹具體步驟之前掂碱,需要先了解一下Linux有關(guān)磁盤空間的命令疼燥。
1、df命令
linux中df命令的功能是用來檢查linux的文件系統(tǒng)的磁盤空間占用情況披诗。可以利用該命令來獲取硬盤被占用了多少空間,目前還剩下多少空間等信息粒竖。具體的參數(shù)利用--help
參數(shù)獲得即可蕊苗,常用的是-h
選項(xiàng)朽砰,文件大小采用GB熔掺,MB等方式顯示。
命令格式:df [選項(xiàng)] [文件]
[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命令是對(duì)文件和目錄磁盤使用的空間的查看鬓催,還是和df命令有一些區(qū)別的肺素。常用選項(xiàng)有-h
,文件采用GB宇驾,MB方式顯示倍靡;-s
只顯示路徑的總文件大小。
命令格式:du [選項(xiàng)][文件]
[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磁盤空間擴(kuò)容
相關(guān)操作介紹
1课舍、查看當(dāng)前分區(qū)信息
[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塌西、分區(qū)操作
[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 :設(shè)置可引導(dǎo)標(biāo)記
b :修改bsd的磁盤標(biāo)簽
c :設(shè)置DOS操作系統(tǒng)兼容標(biāo)記
d :刪除一個(gè)分區(qū)
l :顯示已知的分區(qū)類型,其中82為Linux swap分區(qū)捡需,83為Linux分區(qū)
m :顯示幫助信息
n :增加一個(gè)新的分區(qū)
o :創(chuàng)建一個(gè)新的空白的DOS分區(qū)表
p :顯示磁盤當(dāng)前的分區(qū)表
q :退出fdisk程序饰剥,不保存任何修改
s :創(chuàng)建一個(gè)新的空白的Sun磁盤標(biāo)簽
t :改變一個(gè)分區(qū)的系統(tǒng)號(hào)碼(比如把Linux Swap分區(qū)改為Linux分區(qū))
u :改變顯示記錄單位
v :對(duì)磁盤分區(qū)表進(jìn)行驗(yàn)證
w :保存修改結(jié)果并退出fdisk程序
x :特殊功能
(1)刪除分區(qū)
Command (m for help): d //輸入命令d
Partition number (1-4): 3 //這里輸入的數(shù)字根據(jù)fdisk -l結(jié)果中的順序確定
Command (m for help): p //查看當(dāng)前分區(qū)情況古沥,可以看到sda3已經(jīng)刪除
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)創(chuàng)建主分區(qū)
Command (m for help): n //輸入n創(chuàng)建新分區(qū)
Command action
e extended
p primary partition (1-4)
p //創(chuàng)建主分區(qū),主分區(qū)最多能創(chuàng)建4個(gè)
Partition number (1-4): 3 //fdisk -l的結(jié)果顯示已經(jīng)有sda1 sda2從sda3開始創(chuàng)建
First cylinder (2354-5221, default 2354): //起始柱面岗憋,采用默認(rèn)即可,回車
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 //打印觀察當(dāng)前分區(qū)信息,可以看到sda3被創(chuàng)建
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)創(chuàng)建邏輯分區(qū)
擴(kuò)展分區(qū)是無法使用的,必須在擴(kuò)展分區(qū)的基礎(chǔ)上創(chuàng)建邏輯分區(qū),具體步驟如下所示:
Command (m for help): n
Command action
e extended
p primary partition (1-4)
e //創(chuàng)建邏輯分區(qū)
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不能直接使用需要在此基礎(chǔ)上創(chuàng)建邏輯分區(qū)
Command (m for help): n //創(chuàng)建邏輯分區(qū)
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邏輯分區(qū)被創(chuàng)建
(4)創(chuàng)建swap交換分區(qū)
Command (m for help): n //創(chuàng)建分區(qū)
Command action
e extended
p primary partition (1-4)
p //創(chuàng)建主分區(qū)
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命令將分區(qū)轉(zhuǎn)化成swap分區(qū)
Partition number (1-4): 3
Hex code (type L to list codes): 82 //swap分區(qū)的類型號(hào)為82福铅,Linux分區(qū)類型號(hào)為83环揽,LVM類型分區(qū)號(hào)為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已經(jīng)成為swap分區(qū)
(5)保存退出操作
Command (m for help): w //保存操作退出序无,需要重啟電腦分區(qū)信息才能生效
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)格式化分區(qū)
格式化Linux分區(qū)
[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分區(qū)
[root@localhost Desktop]# mkswap /dev/sda4 //格式化sda4為swap分區(qū)格式
Setting up swapspace version 1, size = 2056316 KiB
no label, UUID=648482ec-0b42-4e14-af17-070742831f19
[root@localhost Desktop]#
(7)掛載分區(qū)
臨時(shí)掛載
[root@localhost Desktop]# mount /dev/sda3 /mnt/ //將mnt掛載到sda3上
[root@localhost Desktop]# df -h //查看當(dāng)前掛載信息
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文件椰苟,在結(jié)尾添加/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
磁盤擴(kuò)容具體步驟
有了上一小節(jié)的鋪墊,磁盤擴(kuò)容就簡單多了,下面以Vmware下Cent OS為例進(jìn)行擴(kuò)大/usr/local/
文件夾的磁盤空間哩俭。以下步驟是將Vmware虛擬機(jī)中的Linux進(jìn)行磁盤擴(kuò)展讳苦,真機(jī)的過程大同小異咐扭,真機(jī)不需要第一步虛擬機(jī)的相關(guān)操作辛馆,不做詳細(xì)介紹。
一、虛擬機(jī)相關(guān)操作
關(guān)閉相關(guān)虛擬機(jī)--->虛擬機(jī)--->設(shè)置--->擴(kuò)展(輸入硬盤容量最大值)--->完成
二乙濒、系統(tǒng)中的相關(guān)分區(qū)操作
1亏掀、使用fdisk -l查詢當(dāng)前系統(tǒng)分區(qū)情況
[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巩割、創(chuàng)建主分區(qū)
[root@localhost Desktop]# fdisk /dev/sda //對(duì)sda進(jìn)行操作
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 //創(chuàng)建新分區(qū)
Command action
e extended
p primary partition (1-4)
p //主分區(qū)
Selected partition 4 //主分區(qū)最多創(chuàng)建4個(gè)牙言,由于系統(tǒng)已經(jīng)有3個(gè)主分區(qū)入挣,自動(dòng)選擇partition 4
First cylinder (2611-5221, default 2611): //起始柱面默認(rèn) 回車
Using default value 2611
Last cylinder, +cylinders or +size{K,M,G} (2611-5221, default 5221): //截止柱面默認(rèn) 回車 也可以+20G
Using default value 5221
Command (m for help): p //查看最新分區(qū)信息
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勋拟、保存退出分區(qū)操作
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. //虛擬機(jī)需要重啟才能識(shí)別新分區(qū)
4亲怠、格式化相關(guān)分區(qū)
重啟后图毕,格式化分區(qū)
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嚷掠、掛載分區(qū)
注意: 將指定文件夾掛載到新分區(qū)犬耻,相關(guān)內(nèi)容會(huì)被清空学密,比如要將/usr/local
下的內(nèi)容掛載到/dev/sda4
款青,需要將/usr/local
下的相關(guān)內(nèi)容備份,掛載結(jié)束后钝诚,在復(fù)制到/usr/local
下垫蛆。
修改/etc/fstab文件,在結(jié)尾添加/dev/sda4 /usr/local/ ext4 defaults 0 0
重啟機(jī)器决左,利用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/
已經(jīng)掛載到/dev/sda4
上继找,空間為20G边臼,將備份的的內(nèi)容重新復(fù)制到/usr/local/
即可鸣戴。
LVM介紹
由于當(dāng)時(shí)裝系統(tǒng)的時(shí)候沒有采用LVM方式,導(dǎo)致無法直接對(duì)/
目錄進(jìn)行擴(kuò)展年栓,以后裝系統(tǒng)的時(shí)候還是采用LVM管理磁盤空間吧某抓,LVM的優(yōu)點(diǎn)就不在這里詳細(xì)敘述纸兔,下邊對(duì)LVM進(jìn)行簡單的總結(jié),以便日后方便使用否副。
一汉矿、LVM簡介
LVM是 Logical Volume Manager(邏輯卷管理)的簡寫。LVM將一個(gè)或多個(gè)硬盤的分區(qū)在邏輯上集合备禀,相當(dāng)于一個(gè)大硬盤來使用洲拇,當(dāng)硬盤的空間不夠使用的時(shí)候,可以繼續(xù)將其它的硬盤的分區(qū)加入其中曲尸,這樣可以實(shí)現(xiàn)磁盤空間的動(dòng)態(tài)管理赋续,相對(duì)于普通的磁盤分區(qū)有很大的靈活性。
如下圖所示:由四個(gè)磁盤分區(qū)可以組成一個(gè)很大的空間队腐,然后在這些空間上劃分一些邏輯分區(qū)蚕捉,當(dāng)一個(gè)邏輯分區(qū)的空間不夠用的時(shí)候,可以從剩余空間上劃分一些空間給空間不夠用的分區(qū)使用柴淘。
二、LVM常用術(shù)語
1秘通、物理存儲(chǔ)介質(zhì)(The physical media):這里指系統(tǒng)的存儲(chǔ)設(shè)備:硬盤为严,如:/dev/hda1、/dev/sda等等肺稀,是存儲(chǔ)系統(tǒng)最低層的存儲(chǔ)單元第股。
2、物理卷(physical volume):物理卷就是指硬盤分區(qū)或從邏輯上與磁盤分區(qū)具有同樣功能的設(shè)備(如RAID)话原,是LVM的基本存儲(chǔ)邏輯塊夕吻,但和基本的物理存儲(chǔ)介質(zhì)(如分區(qū)、磁盤等)比較繁仁,卻包含有與LVM相關(guān)的管理參數(shù)涉馅。
3、卷組(Volume Group):LVM卷組類似于非LVM系統(tǒng)中的物理硬盤黄虱,其由物理卷組成稚矿。可以在卷組上創(chuàng)建一個(gè)或多個(gè)“LVM分區(qū)”(邏輯卷),LVM卷組由一個(gè)或多個(gè)物理卷組成晤揣。
4桥爽、邏輯卷(logical volume):LVM的邏輯卷類似于非LVM系統(tǒng)中的硬盤分區(qū),在邏輯卷之上可以建立文件系統(tǒng)(比如/home或者/usr等)昧识。
5钠四、PE(physical extent):每一個(gè)物理卷被劃分為稱為PE(Physical Extents)的基本單元,具有唯一編號(hào)的PE是可以被LVM尋址的最小單元跪楞。PE的大小是可配置的形导,默認(rèn)為4MB。
6习霹、LE(logical extent):邏輯卷也被劃分為被稱為LE(Logical Extents) 的可被尋址的基本單位朵耕。在同一個(gè)卷組中,LE的大小和PE是相同的淋叶,并且一一對(duì)應(yīng)阎曹。
簡單來說就是:
PV:是物理的磁盤分區(qū)
VG:LVM中的物理的磁盤分區(qū),也就是PV煞檩,必須加入VG处嫌,可以將VG理解為一個(gè)倉庫或者是幾個(gè)大的硬盤。
LV:也就是從VG中劃分的邏輯分區(qū)
參考文獻(xiàn)
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