磁盤分區

磁盤分區
1、fdisk命令的使用(即分區方式)
[root@localhost ~]# fdisk -l

Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00013f3e

Device Boot Start End Blocks Id System
/dev/vda1 * 2048 20970332 10484142+ 83 Linux

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
[root@localhost ~]# fdisk /dev/vdb ##劃分/dev/vdb,未保存時信息保存在內存中
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xc7f34c27.

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 ##刪除設備
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
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 sykstem 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
Partition type: ##指定劃分分區設備的類型
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): ##指定使用的分區表號
First sector (2048-20971519, default 2048): ##分區起始塊,選擇默認
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +1G ##設備大小指定
Partition 1 of type Linux and of size 1GiB is set
Command (m for help): p ##顯示分區表

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xc7f34c27

Device Boot Start End Blocks Id System
/dev/vdb1 2048 2099199 1048576 83 Linux ##被劃分出來的分區

Command (m for help): wq 保存並退出
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

[root@localhost ~]# partprobe 同步系統分區表

[root@localhost ~]# cat /proc/partitions ##查看系統能識別的分區
major minor #blocks name

253 0 10485760 vda
253 1 10484142 vda1
253 16 10485760 vdb
253 17 1048576 vdb1
11 0 3655680 sr0
2、如何使用新建的分區?

格式化
掛載
vim /etc/fstab 實現永久掛載

**具體操作如下:
[root@localhost ~]# fdisk /dev/vdb ##劃分分區
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
First sector (2048-20971519, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +100M
Partition 1 of type Linux and of size 100 MiB is set

Command (m for help): wq
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

[root@localhost ~]# partprobe ##同步系統分區表
[root@localhost ~]# cat /proc/partitions ##查看系統能識別的分區
major minor #blocks name

253 0 10485760 vda
253 1 10484142 vda1
253 16 10485760 vdb
253 17 102400 vdb1
11 0 3655680 sr0

[root@localhost ~]# mkfs.ext1 /dev/vdb1 ##將設備/dev/vdb1格式化爲ext1格式
[root@localhost ~]# mount /dev/vdb1 /mnt/ ##臨時掛載設備
[root@localhost ~]# df -h ##查看所有掛載設備
Filesystem Size Used Avail Use% Mounted on
… … … …
/dev/vdb1 93M 1.6M 85M 2% /mnt

[root@localhost ~]# vim /etc/fstab ##開機自動掛載設備
10 /dev/vdb1 /mnt ext4 defaults 0 0

此處檢測mount -a 的功能,即重新讀取/etc/fstab文件的內容
[root@localhost ~]# umount /dev/vdb1
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
tmpfs 921M 0 921M 0% /sys/fs/cgroup
/dev/sr0 3.5G 3.5G 0 100% /run/media/root/RHEL-7.0 Server.x86_64
[root@localhost ~]# mount -a
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
… … … …
/dev/sr0 3.5G 3.5G 0 100% /run/media/root/RHEL-7.0 Server.x86_64
/dev/vdb1 93M 1.6M 85M 2% /mnt

3、刪除設備
卸載
刪除/etc/fstab重設備的自動掛載條目
用fdisk刪除分區
[root@localhost ~]# umount /dev/vdb1
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
… … … …
/dev/sr0 3.5G 3.5G 0 100% /run/media/root/RHEL-7.0 Server.x86_64
[root@localhost ~]# vim /etc/fstab ##刪除文件中該設備自動激活條目
[root@localhost ~]# fdisk /dev/vdb
… … … …
Command (m for help): d ##刪除設備
Selected partition 1
Partition 1 is deleted

Command (m for help): wq
The partition table has been altered!
[root@localhost ~]# partprobe
[root@localhost ~]# cat /proc/partitions
major minor #blocks name

253 0 10485760 vda
253 1 10484142 vda1
253 16 10485760 vdb
11 0 3655680 sr0

4、swap
[root@localhost ~]# swapon -s ##查看存在的swap分區

**(1)在不重裝系統的情況下擴大swap分區,即建立swap分區
建立swap分區
格式化爲swap格式
[root@localhost ~]# fdisk /dev/vdb 劃分設備並將其分區標籤改爲swap
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
First sector (2048-20971519, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +4G
Partition 1 of type Linux and of size 4 GiB is set

Command (m for help): t 改變設備分區的標籤
Selected partition 1
Hex code (type L to list all codes): L 查看分區標籤

0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
… …. …. …. ##此處省略部分內容
Hex code (type L to list all codes): 82 ##swap標籤的代碼爲82
Changed type of partition ‘Linux’ to ‘Linux swap / Solaris’

Command (m for help): p 顯示分區

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xc7f34c27

Device Boot Start End Blocks Id System
/dev/vdb1 2048 8390655 4194304 82 Linux swap / Solaris

Command (m for help): wq
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.

[root@localhost ~]# mkswap /dev/vdb1 格式化分區
Setting up swapspace version 1, size = 4194300 KiB
no label, UUID=b2d80d42-8a8b-41bb-902d-5ab17a1807ac
[root@localhost ~]# swapon -a /dev/vdb1 臨時激活swap

[root@localhost ~]# vim /etc/fstab 開機自動激活swap
10 /dev/vdb2 swap swap defaults 0 0
激活swap 文件格式爲swap
[root@localhost ~]# swapon -s ##查看系統中的swap分區
Filename Type Size Used Priority
/dev/vdb2 partition 4194300 0 -1
swapon -a的功能,即重新讀取配置文件
[root@localhost ~]# swapoff /dev/vdb1 ##關閉激活的swap分區
[root@localhost ~]# swapon -s ##在此處查看沒有swap分區
[root@localhost ~]# swapon -a ##讓系統重新讀取/etc/fstab文件的內容,swap分區又生效
[root@localhost ~]# swapon -s ##在此處查看有swap分區
Filename Type Size Used Priority
/dev/vdb2

**(2)刪除擴大的swap分區

[root@localhost ~]# vim /etc/fstab ##刪除文件中swap自動激活條目
[root@localhost ~]# swapoff /dev/vdb1 ##關閉激活的swap分區
[root@localhost ~]# swapon -s ##查看當前存在的swap分區

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章