Unit 15 Linux的磁盘管理

1.本地存储设备的识别

查看本地设备信息有三种方法:

  • fdisk -l   ##真实存在的设备(不一定是系统识别)
  • cat /proc/partitions   #系统真实识别的设备
  • blkid  #系统可以使用的设备
[kiosk@foundation38 ~]$ fdisk -l
[kiosk@foundation38 ~]$ cat /proc/partitions
major minor  #blocks  name

   8        0  312571224 sda
   8        1  308636672 sda1
   8        2    3933184 sda2
  11        0    1048575 sr0
   7        0    3763200 loop0
   7        1    3655680 loop1
   7        2    3704832 loop2
  11        1       6010 sr1
[kiosk@foundation38 ~]$ blkid
/dev/sr1: UUID="2016-09-20-16-58-58-00" LABEL="M-eM-^MM-^NM-dM-8M-:M-fM-^IM-^KM-fM-^\M-:M-eM-^JM-)M-fM-^IM-^K" TYPE="iso9660" PTTYPE="mac"

df  #系统正在挂载的设备,下面两个命令在容量大小方面会有差异

-h  ##1K=1024

-H  ##1K=1000

[kiosk@foundation38 ~]$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1       295G   75G  220G  26% /
devtmpfs        1.9G     0  1.9G   0% /dev
tmpfs           1.9G  536K  1.9G   1% /dev/shm
tmpfs           1.9G  8.9M  1.9G   1% /run
tmpfs           1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/loop0      3.6G  3.6G     0 100% /var/www/html/rhel6.5
/dev/loop1      3.5G  3.5G     0 100% /var/www/html/rhel7.0
tmpfs           384M   32K  384M   1% /run/user/1000
/dev/loop2      3.6G  3.6G     0 100% /var/www/html/rhel7.3
/dev/sr1        5.9M  5.9M     0 100% /run/media/kiosk/华为手机助手
[kiosk@foundation38 ~]$ df -H
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1       316G   81G  236G  26% /
devtmpfs        2.0G     0  2.0G   0% /dev
tmpfs           2.1G  549k  2.1G   1% /dev/shm
tmpfs           2.1G  9.3M  2.1G   1% /run
tmpfs           2.1G     0  2.1G   0% /sys/fs/cgroup
/dev/loop0      3.9G  3.9G     0 100% /var/www/html/rhel6.5
/dev/loop1      3.8G  3.8G     0 100% /var/www/html/rhel7.0
tmpfs           403M   33k  403M   1% /run/user/1000
/dev/loop2      3.8G  3.8G     0 100% /var/www/html/rhel7.3
/dev/sr1        6.2M  6.2M     0 100% /run/media/kiosk/华为手机助手

umount出现设备正忙

解决方法一:

[kiosk@foundation38 ~]$ umount /dev/sr1
Error unmounting block device 11:1: GDBus.Error:org.freedesktop.UDisks2.Error.DeviceBusy: Error unmounting /dev/sr1: Command-line `umount  "/run/media/kiosk/??????"' exited with non-zero exit status 32: umount: /run/media/kiosk/??????: target is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))

[kiosk@foundation38 ~]$ fuser -vm /run/media/kiosk/华为手机助手
                     USER        PID ACCESS COMMAND
/run/media/kiosk/华为手机助手:
                     root     kernel mount /run/media/kiosk/华为手机助手
                     kiosk     13115 ..c.. bash
                     kiosk     13746 ..c.. vim

[kiosk@foundation38 ~]$ umount /dev/sr1    ##解除成功

[root@foundation38 ~]# mount /dev/sr1 /mnt    ##重新挂载
mount: /dev/sr1 is write-protected, mounting read-only
[root@foundation38 ~]# cd /mnt
[root@foundation38 mnt]# ls
Autorun.inf  Document  HiSuite.ico  华为手机助手安装向导.exe

解决方法二:

[root@foundation38 ~]# 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
gedit   14229 root  cwd    DIR   11,1     2048 1600 /mnt

[root@foundation38 ~]# kill -9 14229     ##删除对挂载地址/mnt中的占用
[root@foundation38 ~]# lsof /mnt/
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
      Output information may be incomplete.
[1]+  Killed                  gedit  (wd: /mnt)
(wd now: ~)
[root@foundation38 ~]# umount /mnt    ##解除成功

操作系统

磁盘分区

1.磁盘0磁道第1扇区的512字节的内容

512=446+64+2

446:mbr(主引导记录)

64:mpt(主分区表)

2:磁盘有效性标示/

eg:对磁盘vdb进行第二个分区并分配500M的空间

[root@client138 ~]# cat /proc/partitions
major minor  #blocks  name

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

 252        0     507904 dm-0 
[root@client138 ~]# 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.


Command (m for help): n     ##分区
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2):        ##第几个区
First sector (1026048-20971519, default 1026048):       ##起始位置
Using default value 1026048
Last sector, +sectors or +size{K,M,G} (1026048-20971519, default 20971519): +500M    ##分配大小
Partition 2 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: 0x09ee18d7

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048     1026047      512000   8e  Linux LVM
/dev/vdb2         1026048     2050047      512000   83  Linux

Command (m for help): wq      ##保存
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

[root@client138 /]# partprobe       ##同步分区
[root@client138 /]# cat /proc/partitions
major minor  #blocks  name

 253        0   10485760 vda
 253        1   10484142 vda1
 253       16   10485760 vdb
 253       17     512000 vdb1
 253       18     512000 vdb2            ##操作成功,分出了vdb第二个区
 252        0     507904 dm-0

[root@client138 /]# dd if=/dev/vdb of=/mnt/mbr2 bs=521 count=1    ##取出前512个位置的信息
1+0 records in
1+0 records out
521 bytes (521 B) copied, 0.000187945 s, 2.8 MB/s

[root@client138 /]# hexdump -C /mnt/mbr2
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000001b0  00 00 00 00 00 00 00 00  d7 18 ee 09 00 00 00 00  |................|     ##描黑部分一共64位
000001c0  21 02 8e 0e de f9 00 08  00 00 00 a0 0f 00 00 01  |!...............|      ##只能分四个区
000001d0  13 e8 83 09 5c cb 00 a8  0f 00 00 a0 0f 00 00 00  |....\...........|
000001e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00
55 aa  |..............U.|
00000200  00 00 00 00 00 00 00 00  00                       |.........|
00000209

分完区后不能直接挂载或使用

[root@client138 ~]# mount /dev/vdb2 /mnt ##报错
mount: /dev/vdb2 is write-protected, mounting read-only
mount: unknown filesystem type '(null)'

文件系统

是操作系统用于明确磁盘或分区上的文件的方法和数据结构,即在磁盘上组织文件的方式

操作系统中负责管理和存储文件信息的软件机构称为文件管理系统(文件系统)

文件系统由三部分组成:与文件管理有关的软件,被管理的文件及实施文件管理所需的数据结构

从系统角度来看,文件系统就是对文件存储空间进行组织和分配,负责文件存储并对存入的文件进行保护和检索的系统

文件系统 适应的Linux版本
ext3 rhel5之前的版本
ext4 rhel6
xfs rhel7

将文件系统赋予磁盘

mkfs.xfs /dev/vdb2

[root@client138 ~]# mkfs.xfs /dev/vdb2
meta-data=/dev/vdb2              isize=256    agcount=4, agsize=32000 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=128000, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@client138 ~]# mount /dev/vdb2 /mnt       ##成功挂载

[root@client138 ~]# df -h
Filesystem          Size  Used Avail Use% Mounted on
/dev/vda1            10G  3.2G  6.9G  32% /
devtmpfs            459M     0  459M   0% /dev
tmpfs               474M   84K  474M   1% /dev/shm
tmpfs               474M   13M  462M   3% /run
tmpfs               474M     0  474M   0% /sys/fs/cgroup
/dev/mapper/vg0-vo  473M  2.4M  442M   1% /home
/dev/vdb2           497M   26M  472M   6% /mnt

开机自动挂载设备

因为又要用一下vdb2磁盘,所以将刚才的先解除挂载

[root@client138 ~]# umount /mnt

自动挂载的配置文件是/etc/fstab,这个比rc.local要快,内核启动时会优先读取

[root@client138 ~]# vim /etc/fstab

配置内容:

/dev/vdb2 /mnt xfs defaults 0 0
设备名称 挂载点 文件系统类型 挂载参数 是否备份 是否检测

[root@client138 ~]# mount -a    ##挂载测试
[root@client138 ~]# df -h
Filesystem          Size  Used Avail Use% Mounted on
/dev/vda1            10G  3.2G  6.9G  32% /
devtmpfs            459M     0  459M   0% /dev
tmpfs               474M   84K  474M   1% /dev/shm
tmpfs               474M   13M  462M   3% /run
tmpfs               474M     0  474M   0% /sys/fs/cgroup
/dev/mapper/vg0-vo  473M  2.4M  442M   1% /home
/dev/vdb2           497M   26M  472M   6% /mnt          ##成功挂载

swap分区的管理

1.swap分区的建立

划分分区并设定分区的标签为82

[root@client138 ~]#  fdisk /dev/vdb    ##打开磁盘管理

Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'

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

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048     1026047      512000   8e  Linux LVM
/dev/vdb2         1026048     2050047      512000   82  Linux swap / Solaris

[root@client138 ~]# partprobe

设定swap分区

[root@client138 ~]# mkswap /dev/vdb2    
Setting up swapspace version 1, size = 511996 KiB
no label, UUID=c99e2c2d-b3d6-4500-a92a-6d166ea008c3
[root@client138 ~]# swapon -a /dev/vdb2      ##让系统识别swap分区

[root@client138 ~]# swapon -s    ##查看系统中的swap分区
Filename                Type        Size    Used    Priority
/dev/vdb2             partition    511996    0    -1

开机自动挂载

编辑/etc/fstab文件,加入配置

/dev/vdb2    swap   swap  defaults   0    0

2.swap分区的删除

[root@client138 ~]# vim /etc/fstab         ##首先删除开机自动挂载的设置

[root@client138 ~]# swapoff /dev/vdb2   ##解除vdb2的swap分区设置
[root@client138 ~]# swapon -s

磁盘的配额

磁盘vdb2已经配置过swap,首先需要解除标签并重新配置文件系统xfs

[root@client138 ~]# mkfs.xfs -f /dev/vdb2
meta-data=/dev/vdb2              isize=256    agcount=4, agsize=32000 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data  =                       bsize=4096   blocks=128000, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log    =internal log           bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

[root@client138 ~]# mount -o usrquota /dev/vdb2 /pub   ##加上配额参数进行挂载
[root@client138 ~]# edquota -u student    ##配额配置文件

  Filesystem       blocks       soft       hard     inodes     soft     hard
  /dev/vdb2          0              0          40000          0        0        0

  配置内容:

Filesystem blocks soft hard inodes soft    hard
/dev/vdb2 0 0 40000  0 0 0
设备 已存在文件 软限度 硬限度(最大额度) 存在文件数量

[root@client138 ~]# su - student

[student@client138 ~]$ dd if=/dev/zero of=/pub/student bs=1M count=21   ##当取出21M时很OK
21+0 records in
21+0 records out
22020096 bytes (22 MB) copied, 0.0126497 s, 1.7 GB/s
[student@client138 ~]$ dd if=/dev/zero of=/pub/student bs=1M count=40      ##取出超过40M时就会报错
dd: error writing ‘/pub/student’: Disk quota exceeded
40+0 records in
39+0 records out
40960000 bytes (41 MB) copied, 0.0491933 s, 833 MB/s

猜你喜欢

转载自blog.csdn.net/weixin_41884844/article/details/86561479