1)發現系統中的設備
fdisk -l
cat /proc/partitions
2)系統發現的,但是沒有投入使用,隨時可以使用
blkid ##查看設備的ud信息
3)發現並在使用
df -h ##10^10
df -H ##10^20
/dev/xd*
x=s /dev/sd* ##sata硬盤,或者iscsi網絡存儲
x=v /dev/vd* ##虛擬硬盤,一般出現在虛擬機裏
x=h /dev/hd ##ida硬盤,一般出現在老式硬盤裏
/dev/sdal ##系統中的sata硬盤中的第一個分區
*=a~ 。。。。 ##/dev/vda,系統中的第一塊虛擬硬盤
/dev/cdrom,/dev/sr[0-...] ##系統中的光驅
/dev/maooer/* ##系統中的虛擬設備
3.對設備的使用(以主機爲例)
#設備掛載
blkid 識別可用設備
mount 設備 掛在點 ##設備的掛載
把系統中第二塊硬盤的第一個分區掛載到/mnt目錄上
mount /dev/sdb1 /mnt
只讀掛載第二塊硬盤的第一個分區掛載到/mnt目錄上
mount -o ro /dev/sdb1 /mnt
mount -o remount,rw /mnt|/dev/sdb1
#設備卸載
umount 設備|掛在點
umount /dev/sdb1 | /mnt
#注意:當卸載設備時出現下列情況
[root@foundation0 ~]# umount /dev/sdb1
umount: /home/kiosk/Desktop/photo: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1)) ##表示設備正在被某個程序使用
使用進程發現的方式
fuser -vm /dev/sdb1
lsof /dev/sdb1
使用進程結束的方式
kill -9 pid
fuser -kvm /dev/sdb1|/mnt/ ## k->kill v->顯示 m->掃描設備
卸載後,因爲是強制卸載,所以設備裏會存在未知文件,ls顯示後rm刪除即可
4.分區管理
主分區:分區表內的分區,只有4個
擴展分區:以邏輯分區的方式來使用,包含多個邏輯分區
邏輯分區:擴展分區裏的分區
##設備分區信息##
1)mbr主引導記錄446個字節 ##引導讀取分區
2)mpt主分區表64個字節 ##記錄分區信息
3)硬盤的有效性標識“55aa”2個字節
4)一個主分區佔用16個字節記錄分區信息
5)一塊硬盤上如果用mbr的分區最多可以存在4個主分區
##分區的添加
[root@server13 ~]# fdisk /dev/vdb
Command (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d #刪除分區
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 #新建分區
o create a new empty DOS partition table
p #顯示分區表信息
q #退出
s create a new empty Sun disklabel
t #修改分區id
u change display/entry units
v verify the partition table
w #將當前操作寫入硬盤操作
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): ##確定主分區id
First sector (2048-20971519, default 2048): ##分區起始塊的位置,用默認
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +100M ##分區結束塊的位置,用+大小的方式確定
Command (m for help): wq ##保存分區策略並退出fdisk界面
結果:
cat /proc/partitions ##查看設備是否被系統識別
mkfs.xfs /dev/vdb1 ##格式化設備,在設備上安裝系統xfs (必須格式化,不然無法掛載設備)
blkid ##查看可用設備,可以看到被格式化好的/dev/vdb1
mount /dev/vdb1 /mnt ##掛載設備,使設備投入使用
##設備永久掛載
vim /etc/fstab ##設備掛載策略文件
設備 掛載點 文件系統類型 掛載參數 是否備份設備 是否檢測設備
例:
/dev/vdb1 /mnt xfs defaults 0 0
mount -a ##讓fstab中未生效的策略生效
##刪除分區
umount /dev/vdb1
fdsik /dev/vdb1
:wq ##一定要記得退出保存
5.swap分區管理
swap 交換分區,系統自己使用,不需要掛載
1)swap分區的查看
[root@server13 ~]# swapon -s
Filename Type Size Used Priority
/dev/vdb1 partition 102396 0 -1
2)swap分區的建立(先建立一個分區,不用掛載,再修改id)
(1)劃分分區,並修改分區的id爲swap
fdisk /dev/vdb 建立分區
Device Boot Start End Blocks Id System
/dev/vdb1 2048 206847 102400 83 Linux ##默認情況下,此分區是linux系統的普通分區
Command (m for help): t ##修改分區的id
Selected partition 1
Hex code (type L to list all codes): l #列出所有id類型代碼
Hex code (type L to list all codes): 82 ##把/dev/vdb1的分區類型修改爲swap,swap類型代碼爲82
Changed type of partition 'FAT12' to 'Linux swap / Solaris'
Device Boot Start End Blocks Id System
/dev/vdb1 2048 2050047 1024000 82 Linux swap / Solaris
Command (m for help): wq
mkswap /dev/vdb1 ##格式化爲swap文件系統格式
swapon -a /dev/vdb1 ##激活swap設備,使系統利用此設備
vim /etc/fstab ##讓此設備開機自動激活
/dev/vdb1 swap swap defaults 0 0
##當磁盤全部被佔滿時,不能新建分區時,可以用文件來代替分區
dd if=/dev/zero of=/swapfile bs=1M count=100M
#此文件創建後的步驟和設備的步驟一致
mkswap /swapfile ##格式化
swapon -a /swapfile ##掛載
swapon -s ##查看
Filename Type Size Used Priority
/dev/vdb1 partition 102396 220 -1
/swapfile file 1023996 0 -2
#swap分區的刪除
swapoff /swapfile
##卸載
fdisk /dev/vdb ##d 刪除掛載
rm -fr /swapfile ##刪除文件
vim /etc/fstab ##刪除開機配載
6.磁盤配額
爲磁盤使用用戶分配配額,分區配額時正對於設備
1)激活設備配參數
mount -o usrquota /dev/vdb1 /mnt/
chmod 777 /mnt/
edquota -u student /dev/vdb1
Filesystem blocks soft hard inodes soft hard
/dev/vdb1 204800 0 204800 1 0 0
已存在文件大小 軟額度 最大額度 文件個數 軟額度 最大額度
檢查:
[student@localhost ~]$ dd if=/dev/zero of=/mnt/mm bs=1M count=201
dd: error writing ‘/mnt/mm’: Disk quota exceeded ##超過配額限制
201+0 records in
200+0 records out
209715200 bytes (210 MB) copied, 0.817757 s, 256 MB/s
##分區方式修改(需要將原來建立的分區刪掉)
mbr------>gpt
[root@localhost ~]# parted /dev/vdb
GNU Parted 3.1
Using /dev/vdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel ##修改設備分區方式標籤
New disk label type? ##雙擊tab鍵出現類型
aix amiga bsd dvh gpt loop mac msdos pc98 sun
New disk label type?
aix amiga bsd dvh gpt loop mac msdos pc98 sun
New disk label type? gpt|msdos ##把原有的dos標籤改成gpt格式 | 把gpt改爲mbr的
Warning: The existing disk label on /dev/vdb will be destroyed and all data on this disk will
be lost. Do you want to continue?
Yes/No? y
(parted) quit
Information: You may need to update /etc/fstab.
更改回來:
7.分區加密
1)建立分區
[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.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x116b0279.
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): +1000M
Partition 1 of type Linux and of size 1000 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: 0x116b0279
Device Boot Start End Blocks Id System
/dev/vdb1 2048 2050047 1024000 83 Linux
Command (m for help): wq
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
2)cryptsetup luksFormat /dev/vdb1 ##加密
3)cryptsetup open /dev/vdb1 westos ##開啓
4)mkfs.xfs /dev/mapper/westos
##格式化
5) mount /dev/mapper/westos /mnt/
##掛載
touch /mnt/file{1..4} ##開啓之後看見創建的內容
cryptsetup close westos ##關閉之後看不見創建的內容
7)cryptsetup open /dev/vdb1 westos ##用時,須再次打開
mount /dev/mapper/westos /mnt/ ##掛載,之後才能看見
##加密設備開機自動掛載
vim /etc/fstab ##開機自起
/dev/mapper/westos /mnt xfs defaults 0 0
vim /etc/crypttab ##寫名字及尋找密碼
westos /dev/vdb1 /root/passfile
vim /root/passfile ##寫密碼
2017westos
chmod 600 /root/passfile ##設置權限
cryptsetup luksAddKey /dev/vdb1 /root/passfile ##設置設備開機時與密碼關聯
##加密的清除
umount /dev/mapper/westos ##卸載
cryptsetup close westos ##關閉設備
mkfs.xfs /dev/vdb1 -f ##格式化
vim /etc/fstab ##刪除開機自啓
vim /etc/crypttab ##刪除名字
vim /root/passfile ##刪密碼