1. 程式人生 > >linux--系統中的簡單分區和管理

linux--系統中的簡單分區和管理

分盤 分區 磁盤

一、添加文件系統空間

簡單分區和文件系統
1.認識
1> 存儲是每個計算機系統的基本需求。 Red Hat EnterpriseLinux 提供了一些強大的工具 , 它們能在大量的場景中管理多種類型的存儲設備

2> disk 是用於管理磁盤分區的實用程序。您可以通過選擇 -l選項和指定磁盤名稱 ( fdisk -cul /dev/vda ) 運行該實用程序 , 以查看磁盤及其分區。您可以通過交互式地運行該實用程序 , 並選擇相應的菜單選項 ( fdisk -cu /dev/vda ) 進行更改。 -c 禁用舊的 DOS 兼容模式 , -u 以扇區 ( 而不是柱面 , 已經過時 ) 的格式顯示輸出
xiaot


3> 查看系統設備信息
fdisk -l
顯示系統中所有可以使用的設備信息
blkid
顯示系統正在使用的設備 id

2.添加新設備
1> 新建分區
[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 0x115e3020.

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 system id ##修改分區功能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): 1 ##選擇建立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): 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: 0x115e3020

Device Boot Start End Blocks Id System
/dev/vdb1 2048 206847 102400 83 Linux

Command (m for help): n ##再次建立新的分區
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): e ##選擇建立新的擴展分區
Partition number (2-4, default 2): 2
First sector (206848-20971519, default 206848):
Using default value 206848
Last sector, +sectors or +size{K,M,G} (206848-20971519, default 20971519): +200M
Partition 2 of type Extended and of size 200 MiB 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: 0x115e3020

Device Boot Start End Blocks Id System
/dev/vdb1 2048 206847 102400 83 Linux
/dev/vdb2 206848 616447 204800 5 Extended

Command (m for help): n
Partition type:
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)
Select (default p): l ##新建邏輯分區
Adding logical partition 5
First sector (208896-616447, default 208896):
Using default value 208896
Last sector, +sectors or +size{K,M,G} (208896-616447, default 616447): +100M
Partition 5 of type Linux and of size 100 MiB 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: 0x115e3020

Device Boot Start End Blocks Id System
/dev/vdb1 2048 206847 102400 83 Linux
/dev/vdb2 206848 616447 204800 5 Extended
/dev/vdb5 208896 413695 102400 83 Linux


Command (m for help): w ##設置完畢後保存以上所有設置
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
253 18 1 vdb2
253 21 102400 vdb5
[root@localhost ~]#

圖示:fdisk顯示當前存在設備

技術分享

[root@localhost ~]# blkid ##當前可以使用的設備,因為新建的分區內被數據占用,未被系統認為可以使用的設備,所以沒有剛才新建的設備
/dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs"

2> 格式新建分區,激活設置可被使用
文件系統:
##mkfs.btrfs mkfs.ext2 mkfs.ext4 mkfs.minix mkfs.vfat
##mkfs.cramfs mkfs.ext3 mkfs.fat mkfs.msdos mkfs.xfs
技術分享
#
# /etc/fstab
# Created by anaconda on Wed May 7 01:22:57 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=9bf6b9f7-92ad-441b-848e-0257cbb883d1 / xfs defaults 1 1
/dev/vdb1 /mnt xfs defaults 0 0
設備 掛載位置 文件系統類型 0備份 0檢查



二、添加swap交換分區

換空間或交換區是磁盤驅動器上的空間 , 用作當前未使用部分內存的溢出。這樣 , 系統就能在主內存中留出空間用於儲存當前正在處理的數據 , 並在系統面臨主內存空間不足的風險時提供應急溢出

1.添加分區方式相同,在建立新的分區後,進行設置

示圖:t添加分區功能id,l查看分區功能各id


示圖:設置分區的swap功能id,將修改保存進硬盤,刷新


示圖:swap型文件格式化設備,成為可用的swap設備

2.管理交換分區
使用 fdisk 創建新分區。此外 , 在用 fdisk 保存更改之前 , 將分區類型更改為 “ 0x82 LinuxSwap”
mkswap /dev/vdaN 會準備好將分區用作交換區
blkid /dev/vdaN 將確定 UUID
將新交換空間添加到 /etc/fstab :UUID=uuid swap swap defaults 0 0
swapon -a 將激活新交換區
swapon -s 將顯示當前交換區的狀態
swapoff /dev/vdaN 將停用該特定交換區
[root@localhost ~]# swapon -a /dev/vdb[1,2] ##激活/dev/vdb1和vdb2交換區
[root@localhost ~]# swapon -s ##顯示當前交換區的狀態
Filename Type Size Used Priority ##Filename文件名,Type類型, Size大小, Used被使用大小, Priorty優先級
/dev/vdb1 partition 102396 0 -1
/dev/vdb2 partition 102396 0 -2

三、磁盤加密保護

1.認識
LUKS ( Linux 統一密鑰設置 ) 是標準的設備加密格式
LUKS 可以對分區或卷進行加密
必須首先對加密的卷進行解密 , 才能掛載其中的文件系統

2.步驟
1> 使用 fdisk 創建新分區

2> cryptsetup luksFormat /dev/vdaN 可對新分區進行加密 ,並設置解密密碼

[root@localhost ~]# cryptsetup luksFormat /dev/vdb1

WARNING!
========
This will overwrite data on /dev/vdb1 irrevocably.

Are you sure? (Type uppercase yes): YES
Enter passphrase:
Verify passphrase:


3> 您輸入正確的解密密碼之後 , cryptsetup luksOpen /dev/vdaN name 會將加密的卷 /dev/vdaN 解鎖為/dev/mapper/name
示圖:

4> 解密的卷上創建 xfs 文件系統 : mkfs -t xfs /dev/mapper/name
示圖:

5> 創建目錄掛載點 , 並掛載文件系統 : mkdir /mnt
– mount /dev/mapper/name /mnt


6> 完成之後 , umount /dev/mapper/name 並運行cryptsetup luksClose name 以鎖定加密的卷


本文出自 “13342594” 博客,請務必保留此出處http://13352594.blog.51cto.com/13342594/1978199

linux--系統中的簡單分區和管理