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	  --->  重新读写挂载
  • 解决设备正忙问题
[root@zhu 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:

[root@zhu 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	  --->  查看并结束使用设备的进程
[root@zhu ~]# 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		  --->  编辑磁盘分区
[root@yu213 ~]# 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).挂载文件系统,格式化

[root@yu213 ~]# mkfs.		  --->  双击tab健,查看挂载文件系统
mkfs.btrfs   mkfs.ext3    mkfs.minix   mkfs.xfs
mkfs.cramfs  mkfs.ext4    mkfs.msdos   
mkfs.ext2    mkfs.fat     mkfs.vfat    
[root@yu213 ~]# mkfs.xfs /dev/vdb1	  --->  格式化/dev/vdb1

[root@yu213 ~]# vim /etc/fstab		  --->  编辑配置文件;开机自动挂载

挂载磁盘      挂载地址	挂载文件系统	默认		不备份;不检测
/dev/vdb1       /mnt        xfs          defaults        0 0

[root@yu213 ~]# 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分区
[root@yu213 ~]# 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).给用户配置使用额度

[root@yu213 ~]# mkfs.xfs /dev/vdb5	----> 格式化/dev/vdb5
[root@yu213 ~]#mkdir /kai	---->  创建一个目录
[root@yu213 ~]#mount -o usrquota /dev/vdb5 /kai	

[root@yu213 ~]#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
[root@yu213 ~]#vim /etc/fstab		---->  编辑文件
	/dev/vdb5       /kai    xfs     defaults,usrquota       0 0

测试:

[root@yu213 home]# su - student
[student@yu213 ~]$ 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
[student@yu213 ~]$ du -sh /kai/studentfile 
20M	/kai/studentfile

7).磁盘加密

[root@yu213 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: 
[root@yu213 home]# ll /dev/vdb3
brw-rw----. 1 root disk 253, 19 Oct 28 16:32 /dev/vdb3
[root@yu213 home]# mount /dev/vdb3 /mnt
mount: unknown filesystem type 'crypto_LUKS'
[root@yu213 home]# cryptsetup open /dev/vdb3 kai
Enter passphrase for /dev/vdb3: 
[root@yu213 home]# ll /dev/mapper/kai 
lrwxrwxrwx. 1 root root 7 Oct 28 16:34 /dev/mapper/kai -> ../dm-0
[root@yu213 home]# mkfs.xfs /dev/mapper/kai
[root@yu213 home]# mount /dev/mapper/kai /mnt
[root@yu213 home]# df
[root@yu213 home]# touch /mnt/file{1..10}
[root@yu213 home]# ll /mnt/
[root@yu213 home]# umount /mnt
[root@yu213 home]# df
[root@yu213 home]# cryptsetup close kai	  --->  注意:关闭之后,/dev/mapper/kai文件消失,挂载原始设备也不能查看其中内容
[root@yu213 home]# cryptsetup open /dev/vdb3 xin
Enter passphrase for /dev/vdb3: 
[root@yu213 home]# mount /dev/mapper/xin /mnt

猜你喜欢

转载自blog.csdn.net/hzyuhz/article/details/83618610
今日推荐