linux系统磁盘管理

###linux系统中的磁盘管理###

##1.本地储存设备的识别
fdisk  -l                 ##真实存在的设备
cat   /proc/partition     ##系统识别的设备
blkid                     ##系统可使用的设备
df                        ##系统正在挂载的设备

linux系统磁盘管理
##2.设备的挂载和卸载
1.设备名称
/dev/xdx        ##/dev/hd0 /dev/hd1 /dev/sda /dev/sdb /dev/sda1 /dev/sda2
/dev/sr0        ##光驱
/dev/mapper/*   ##虚拟设备

2.设备的挂载
mount     设备    挂载点
mount   /dev/sdb1  /mnt            ##挂载sdb1到mnt
umount   /mnt | /dev/sdb1          ##卸载
mount -o ro  /dev/sdb1  /mnt       ##只读挂载
mount                              ##查看挂载信息
mount -o remount,rw  /dev/sdb1 | /mnt  ##重新读写挂载

linux系统磁盘管理

3.卸载设备时会出现的问题

[root[email protected] Desktop]# 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:
fuser -kvm /mnt
umount /mnt

linux系统磁盘管理

解决方法2:
lsof /mnt
[[email protected] Desktop]# 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    7069 root  cwd    DIR   8,17    16384    1 /mnt


kill -9 7069
umount /mnt

linux系统磁盘管理

##3.磁盘分区###
1.硬盘0磁道1扇区的512个字节中记录的信息如下

512=446      +       64        +        2
     ^                ^                 ^
  mbr(主引导记录)   mpt(主分区标)   55aa(硬盘的有效性表识)

2.分区步骤
fdisk -l
fdisk   /dev/vdb
m
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): +200M          ##分区大小
Partition 1 of type Linux 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: 0xa053b3e5

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      411647      204800   83  Linux

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

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

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.

执行:

[[email protected] ~]# partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
[[email protected] ~]#


当系统有三个分区时:

Command (m for help): n
Partition type:
   p   primary (3 primary, 0 extended, 1 free)
   e   extended
Select (default e): e                                                               ##建立的扩展分区
Selected partition 4
First sector (1230848-20971519, default 1230848):
Using default value 1230848
Last sector, +sectors or +size{K,M,G} (1230848-20971519, default 20971519):                  ##把剩余的内存都分给扩展分区
Using default value 20971519
Partition 4 of type Extended and of size 9.4 GiB 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: 0xa053b3e5

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      411647      204800   83  Linux
/dev/vdb2          411648      821247      204800   83  Linux
/dev/vdb3          821248     1230847      204800   83  Linux
/dev/vdb4         1230848    20971519     9870336    5  Extended

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

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

linux系统磁盘管理

###4.给设备安装文件系统###
ext3   rhel5及之前的版本  最多支持32TB的文件系统和2t文件,实际2tb文件系统,16G文件
ext4   rhel6                      1EB          16TB  
xfs    rhel7                      18EB         9EB    

mkfs.xfs    /dev/vdb1
mount    /dev/vdb1  /mnt

永久挂载:

vim  /etc/fstab
设备           挂载点      文件系统     挂载参数     是否备份   是否检测
/dev/vdb1      /mnt         xfs          defaults      0           0

linux系统磁盘管理

mount -a

linux系统磁盘管理

####5.swap分区管理####
1)
划分分区并设定分区标签未82
mkswap     /dev/vdb5
swapon  -a  /dev/vdb5
swapon  -s

linux系统磁盘管理
vim /etc/fstab
/dev/vdb5    swap     swap    defaults   0   0

linux系统磁盘管理

2)swap分区删除
vim  /etc/fstab

linux系统磁盘管理

swapoff  /dev/vdb5
swapon -s

linux系统磁盘管理

###6.配额###
配额是针对分区的。

mount -o usrquota  /dev/vdb6   /public
chmod 777 /public
edquota  -u  student

Disk quotas for user student (uid 1000):

   设备                        已存在文件     软额度      最大额度      存在文件数目
  Filesystem                   blocks           soft            hard                  inodes     soft     hard
  /dev/vdb6                         0                  0                0                             0        0        0

linux系统磁盘管理

vim /etc/fstab
/dev/vdb7   /public   xfs     defaults   0   0

测试
student用户执行   dd if=/dev/zero of=/public/studentfile bs=1M count=200

linux系统磁盘管理

###7.磁盘加密#####

cryptsetup luksFormat  /dev/vdb7                       ##加密
cryptsetup open  /dev/vdb7  cooffee

linux系统磁盘管理
mkfs.xfs   /dev/mapper/cooffee

linux系统磁盘管理
mount   /dev/mapper/cooffee  /mnt/
touch   /mnt/file{1..10}
umount  /mnt/
cryptsetup close  cooffee

cryptsetup open /dev/vdb8 cooffee
mount /dev/mapper/cooffee /mnt/

linux系统磁盘管理

加密磁盘开机自动挂载
vim  /etc/fstab
/dev/mapper/cooffee    /mnt  xfs   defaults     0    0

linux系统磁盘管理

vim  /etc/crypttab
cooffee  /dev/vdb7    /root/cooffeekey

linux系统磁盘管理

vim /root/cooffeekey
2018cooffee

linux系统磁盘管理

cryptsetup luksAddKey    /dev/vdb8  /root/cooffeekey

linux系统磁盘管理