1. 程式人生 > >linux---磁碟管理

linux---磁碟管理

1).本地儲存裝置的識別

df -h		  --->  檢視系統正在掛載的裝置
fdisk -l	  --->  檢視系統中真實存在的裝置
cat /proc/partitions	  --->  檢視系統識別的裝置
blkid			  --->  檢視系統可以使用的裝置
mount			  --->  檢視系統中的掛載資訊

2).裝置的掛載和解除安裝

  • 裝置名稱
/dev/xdx	  --->  /dev/hd0 /dev/hd1 /dev/sda
/dev/sr0	  --->  光碟機
/dev/mapper/*	  --->  虛擬裝置(用軟體模擬出來的)
  • 裝置的掛載
mount	裝置		掛載點
mount	/dev/sdb1	/mnt	  --->  掛載sdb1到mnt
umount	/mnt|/dev/sd1		  --->  解除安裝
mount	-o,ro /dev/sdb1	/mnt	  --->  只讀掛載
mount				  --->  檢視掛載資訊
mount	-o,remount rw /dev/sdb1|/mnt	  --->  重新讀寫掛載
  • 解決裝置正忙問題
[[email protected] mnt]# umount /mnt	  --->  解除安裝裝置;失敗
umount: /mnt: target is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))

解決方法1:

[[email protected] mnt]# lsof /mnt	  --->  檢視使用裝置的程序
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
      Output information may be incomplete.
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
bash    2650 root  cwd    DIR   8,17    16384    1 /mnt
lsof    3933 root  cwd    DIR   8,17    16384    1 /mnt
kill -9 2650		  --->  殺死使用裝置的程序
umount	/mnt		  --->  解除安裝裝置

解決方法2:

fuser -vm	/mnt	  --->  檢視使用裝置的程序
fuser -kvm	/mnt	  --->  檢視並結束使用裝置的程序
[[email protected] ~]# fuser -vm /mnt/	
                     USER        PID ACCESS COMMAND
/mnt:                root     kernel mount /mnt
                     kiosk      3699 ..c.. bash
                     root       4136 ..c.. bash

3).磁碟分割槽

fdisk -l		  --->  檢視磁碟分割槽
fdisk /dev/vdb		  --->  編輯磁碟分割槽
[[email protected] ~]# 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 0x669d0c95.

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):	  --->  主分割槽id用預設
First sector (2048-20971519, default 2048):	  --->  分割槽起始
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +500M		  --->  分割槽大小
Partition 1 of type Linux and of size 500 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: 0x669d0c95

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048     1026047      512000   83  Linux
<<<當系統已經有三個主分割槽時>>>
Command (m for help): n
Partition type:
   p   primary (3 primary, 0 extended, 1 free)
   e   extended
Select (default e): 	  --->  當有3個主分割槽時需要先劃分擴充套件分割槽
Using default response e
Selected partition 4
First sector (3074048-20971519, default 3074048): 
Using default value 3074048
Last sector, +sectors or +size{K,M,G} (3074048-20971519, default 20971519):		  --->  預設把所有剩餘空間都給擴充套件分割槽 
Using default value 20971519
Partition 4 of type Extended and of size 8.5 GiB is set
ommand (m for help): p		  --->  檢視/dev/vdb的磁碟分割槽

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: 0x669d0c95

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048     1026047      512000   83  Linux
/dev/vdb2         1026048     2050047      512000   83  Linux
/dev/vdb3         2050048     3074047      512000   83  Linux
/dev/vdb4         3074048    20971519     8948736    5  Extended
Command (m for help): wq	  --->  儲存磁碟分割槽並退出
The partition table has been altered!

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

4).掛載檔案系統,格式化

[[email protected] ~]# mkfs.		  --->  雙擊tab健,檢視掛載檔案系統
mkfs.btrfs   mkfs.ext3    mkfs.minix   mkfs.xfs
mkfs.cramfs  mkfs.ext4    mkfs.msdos   
mkfs.ext2    mkfs.fat     mkfs.vfat    
[[email protected] ~]# mkfs.xfs /dev/vdb1	  --->  格式化/dev/vdb1

[[email protected] ~]# vim /etc/fstab		  --->  編輯配置檔案;開機自動掛載

掛載磁碟      掛載地址	掛載檔案系統	預設		不備份;不檢測
/dev/vdb1       /mnt        xfs          defaults        0 0

[[email protected] ~]# mount -a		  --->  檢查所有配置檔案

5).swap分割槽管理

  • 分割槽標籤的改變
Command (m for help): t
Partition number (1-6, default 6): 6
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-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi eb  BeOS fs        
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT            
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1 80  Old Minix      
Hex code (type L to list all codes): 82 
Changed type of partition 'Linux' to 'Linux swap / Solaris'
  • swap分割槽建立
劃分分割槽並設定分割槽標籤為82
mkswap /dev/vdb6  ----> 將準備好的分割槽用作交換區
swapon -a /dev/vdb6	----> 開啟/dev/vdb6的swap分割槽
[[email protected] ~]# swapon -s		----> 檢視swap分割槽
Filename				Type		Size	Used	Priority
/dev/vdb6                              	partition	8434684	0	-1
  • 分割槽的自動掛載
vim /etc/fstab		----> 編輯配置檔案,開機自動啟動

  /dev/vdb6       swap    swap    defaults        0 0
  • swap分割槽的刪除
vim /etc/fstab		----> 刪除配置檔案關於swap的編輯
swapoff /dev/vdb6	----> 停用該特定交換區的狀態
swapon -s		----> 顯示當前交換區的狀態

6).給使用者配置使用額度

[[email protected] ~]# mkfs.xfs /dev/vdb5	----> 格式化/dev/vdb5
[[email protected] ~]#mkdir /kai	---->  建立一個目錄
[[email protected] ~]#mount -o usrquota /dev/vdb5 /kai	

[[email protected] ~]#edpuota -u student	---->  編輯檔案
	Disk quotas for user student (uid 1000):
	  Filesystem                   blocks       soft       hard     inodes     soft     hard
	  /dev/vdb5                     20480          0      20480          1        0        0
[[email protected] ~]#vim /etc/fstab		---->  編輯檔案
	/dev/vdb5       /kai    xfs     defaults,usrquota       0 0

測試:

[[email protected] home]# su - student
[[email protected] ~]$ dd if=/dev/zero of=/kai/studentfile bs=1M count=30
dd: error writing ‘/kai/studentfile’: Disk quota exceeded
21+0 records in
20+0 records out
20971520 bytes (21 MB) copied, 0.0183663 s, 1.1 GB/s
[[email protected] ~]$ du -sh /kai/studentfile 
20M	/kai/studentfile

7).磁碟加密

[[email protected] home]# cryptsetup luksFormat /dev/vdb3

WARNING!

========
This will overwrite data on /dev/vdb3 irrevocably.

Are you sure? (Type uppercase yes): YES
Enter passphrase: 
Verify passphrase: 
[[email protected] home]# ll /dev/vdb3
brw-rw----. 1 root disk 253, 19 Oct 28 16:32 /dev/vdb3
[[email protected] home]# mount /dev/vdb3 /mnt
mount: unknown filesystem type 'crypto_LUKS'
[[email protected] home]# cryptsetup open /dev/vdb3 kai
Enter passphrase for /dev/vdb3: 
[[email protected] home]# ll /dev/mapper/kai 
lrwxrwxrwx. 1 root root 7 Oct 28 16:34 /dev/mapper/kai -> ../dm-0
[[email protected] home]# mkfs.xfs /dev/mapper/kai
[[email protected] home]# mount /dev/mapper/kai /mnt
[[email protected] home]# df
[[email protected] home]# touch /mnt/file{1..10}
[[email protected] home]# ll /mnt/
[[email protected] home]# umount /mnt
[[email protected] home]# df
[[email protected] home]# cryptsetup close kai	  --->  注意:關閉之後,/dev/mapper/kai檔案消失,掛載原始裝置也不能檢視其中內容
[[email protected] home]# cryptsetup open /dev/vdb3 xin
Enter passphrase for /dev/vdb3: 
[[email protected] home]# mount /dev/mapper/xin /mnt