Linux-創建系統磁盤分區、swap分區

系統設備類型

Linux中,設備是以文件的形式存放在/etc目錄下,其中磁盤文件一般命名爲/etc/xdxn

第一個x:磁盤類型(s:sata硬盤、u盤等    h:IDE硬盤    v:虛擬硬盤)
第二個x:a...z    第幾塊硬盤
n爲數字,表示分區所在的個數      

硬盤分區命令:fdisk

[root@server ~]# fdisk -l            ##-l參數,列出指定設備的分區,如果沒有指定,使用/proc/patitions的內容
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors    ##v虛擬磁盤,a第一塊磁盤
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@server ~]# fdisk /dev/vdb                    ##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.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x5ebdcaac.
Command (m for help): m                            ##按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      ##創建一個GPT分區表
   G   create an IRIX (SGI) partition table        ##創建一個IRIX分區表
   l   list known partition types                  ##列出設備類型
   m   print this menu                             ##打印這個菜單
   n   add a new partition                         ##新建一個分區
   o   create a new empty DOS partition table      ##創建一個dos分區表
   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): g                      ##測試就選最新的,創建一個GPT分區表
Building a new GPT disklabel (GUID: 7AC1B0AC-BCB7-4F21-9ED0-A9A1A03D20A0)
Command (m for help): n                      ##創建一個新的分區
Partition number (1-128, default 1):         ##分區號
First sector (2048-20971486, default 2048):     ##分區起始扇區
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971486, default 20971486): +1G    ##分區結束扇區,+大小直接指定分區大小
Created partition 1                             ##創建成功

Command (m for help): p                         ##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: gpt

#         Start          End    Size  Type            Name
 1         2048      2099199      1G  Linux filesyste             ##被劃分出來的分區

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

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

系統能夠識別的

mount
分區/etc/patririons
[root@server ~]# cat /proc/partitions 
major minor  #blocks  name

 253        0   10485760 vda
 253        1   10484142 vda1
 253       16   10485760 vdb
 253       17    1048576 vdb1        ##vdb1就是我們新建的分區

分區建立以後,如果我們需要使用,還需要進行格式化和掛載操作

Linux中的文件系統有vfat、ext2、ext4、xfs等

xfs是RHEL7的標準文件系統:支持最大分區9EB、傳輸效率可達7G/s

格式化:
[root@server ~]# mkfs.xfs /dev/vdb1        ##將vdb1分區格式化爲xfs格式
meta-data=/dev/vdb1              isize=256    agcount=4, agsize=65536 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=262144, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

###查看可用分區:blkid###
[root@server ~]# blkid
/dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs" 
/dev/vdb1: UUID="8eacbc25-87a4-4ed6-9400-040491ded23a" TYPE="xfs" PARTUUID="d32de79e-e1f0-4f08-ae0e-fe749a9ed052" 

 掛載mount:
[root@server ~]# mount /dev/vdb1 /mnt               ##掛載/dev/vdb1到/mnt下
[root@server ~]# df           [root@server ~]# fdisk /dev/vdb
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
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): d
Partition number (1,2, default 2): 
Partition 2 is deleted

Command (m for help): d
Selected partition 1
Partition 1 is deleted[root@server ~]# fdisk /dev/vdb
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
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): d
Partition number (1,2, default 2): 
Partition 2 is deleted

Command (m for help): d
Selected partition 1
Partition 1 is deleted

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

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



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

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

                      ##顯示目前系統中文件系統的使用情況
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/vda1       10473900 3160848   7313052  31% /
devtmpfs          469332       0    469332   0% /dev
tmpfs             484920     140    484780   1% /dev/shm
tmpfs             484920   12796    472124   3% /run
tmpfs             484920       0    484920   0% /sys/fs/cgroup
/dev/vdb1        1038336   32928   1005408   4% /mnt    ##掛載點
[root@server ~]# touch /mnt/file1                       ##寫入成功
###我們可以通過添加參數來控制掛載的文件系統的權限###
[root@server ~]# mount -o ro /dev/vdb1 /mnt    ##-o ro只讀,rw可讀寫
###掛載後修改參數###
[root@server ~]# mount -o remount,ro /dev/vdb1 /mnt    

swap分區的建立

[root@server ~]# fdisk /dev/vdb
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
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 number (2-128, default 2):       ##分區號2
First sector (2099200-20971486, default 2099200): 
Last sector, +sectors or +size{K,M,G,T,P} (2099200-20971486, default 20971486): +1G    ##大小1G
Created partition 2

 Command (m for help): t                   ##改變分區標籤    
Partition number (1,2, default 2): 
Partition type (type L to list all types): l    ##列出分區標籤
  1 EFI System                     C12A7328-F81F-11D2-BA4B-00A0C93EC93B
  2 MBR partition scheme           024DEE41-33E7-11D3-9D69-0008C781F39F
 ......................................................................
 12 Linux filesystem               0FC63DAF-8483-4772-8E79-3D69D8477DE4
 13 Linux RAID                     A19D880F-05FC-4D3B-A006-743F0F84911E
 14 Linux swap                     0657FD6D-A4AB-43C4-84E5-0933C84B4F4F
 15 Linux LVM                      E6D6D379-F507-44C2-A23C-238F2A3DF928
 16 Linux reserved                 8DA63339-0007-60C0-C436-083AC8230908
 17 FreeBSD data                   516E7CB4-6ECF-11D6-8FF8-00022D09712B
 18 FreeBSD boot                   83BD6B9D-7F41-11DC-BE0B-001560B84F0F
.......................................................................

Partition type (type L to list all types): 14    ##14 Linux swap
Changed type of partition 'Linux filesystem' to 'Linux swap'
##### w保存退出
[root@server ~]# cat /proc/partitions         ##查看系統可識別的分區
major minor  #blocks  name

 253        0   10485760 vda
 253        1   10484142 vda1
 253       16   10485760 vdb
 253       17    1048576 vdb1

未顯示我們剛剛創建的swap分區,可以使用partprobe命令同步分區表

[root@server ~]# partprobe                    ##同步分區表
[root@server ~]# cat /proc/partitions         ##重新查看
major minor  #blocks  name

 253        0   10485760 vda
 253        1   10484142 vda1
 253       16   10485760 vdb
 253       17    1048576 vdb1
 253       18    1048576 vdb2                ##vdb2分區

swap分區的管理

[root@server ~]# swapon -s                   ##查看系統中的swap分區
[root@server ~]# mkswap /dev/vdb2            ##格式化/dev/vdb2分區爲swap分區格式
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=62d5d396-bd15-439b-a7c4-a07970410a8e
[root@server ~]# swapon -a /dev/vdb2         ##激活swap分區
[root@server ~]# swapon -s                   ##再次查看,swap分區生效
Filename				Type		Size	Used	Priority
/dev/vdb2                              	partition	1048572	0	-1

上面掛載分區,激活swap分區都是臨時性的操作,如果想要自動掛載,需要修改fstab文件

文件位置:/etc/fstab

##設備          掛載點   文件系統  掛載參數默認    不備份   不檢測
/dev/vdb1       /mnt    xfs     defaults        0       0
/dev/vdb2       swap    swap    defaults        0       0

清理實驗環境

[root@server ~]# vim /etc/fstab           ##刪除fstab中的自動掛載內容
[root@server ~]# umount /dev/vdb1         ##卸載vdb1分區
[root@server ~]# swapon -s
Filename                Type        Size    Used    Priority
/dev/vdb2                                  partition    1048572    0    -1
[root@server ~]# swapoff /dev/vdb2        ##關閉激活的swap分區
[root@server ~]# swapon -s
[root@server ~]# fdisk /dev/vdb           ##刪除創建的vdb1、vdb2分區
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
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): d            ##選項d 刪除分區
Partition number (1,2, default 2): 
Partition 2 is deleted
Command (m for help): d
Selected partition 1
Partition 1 is deleted
Command (m for help): w            ##寫入硬盤並退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
發佈了37 篇原創文章 · 獲贊 13 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章