linux root------系统的磁盘管理

1.本地查看

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

[root@march ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/vda3        8709120 2980288   5728832  35% /
devtmpfs          493920       0    493920   0% /dev
tmpfs             508456     160    508296   1% /dev/shm
tmpfs             508456    7144    501312   2% /run
tmpfs             508456       0    508456   0% /sys/fs/cgroup
/dev/vda1         201388  141732     59656  71% /boot
tmpfs             101692       8    101684   1% /run/user/42
tmpfs             101692       8    101684   1% /run/user/0

2.设备的挂载和卸载

1.设备名称

/dev/hd0 # #表示IDE设备,即并口硬盘,是老式的硬盘
/dev/xdx # /dev/sda /dev/sdb....
/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 #重新读写挂载

[root@foundation22 ~]# 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))

方法:另开一个SHELL窗口,vim /mnt/file

解决方法1
fuser -kvm /mnt
umount /mnt

解决方法2
lsof /mnt
[root@foundation22 ~]# 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
vim 3500 kiosk 4u REG 8,17 12288 2137 /mnt/.file.swp
[root@foundation22 ~]# kill -9 3500
[root@foundation22 ~]# umount /mnt

linux root------系统的磁盘管理

3.磁盘分区

1.硬盘0磁道1扇区的512个字节中记录的信息如下
512= 446 + 64 + 2
mbr:主引导记录,它负责从活动分区中装载,并运行系统引导程序。
mpt:分区表,含4 个分区项, 每个分区表项长16 个字节,共64 字节。
55aa:结束标志字

4.分区步骤

1.fdisk -l #查看分区

2.建立主分区

[root@localhost ~]# 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   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: 0xed32f7e0

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048     1026047      512000   83  Linux

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

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

当3个主分区已经建立

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 (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

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: 0xed32f7e0
   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    #把所有空间都给Extended

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

Calling ioctl() to re-read partition table.

linux root------系统的磁盘管理
linux root------系统的磁盘管理

5.给设备安装文件系统

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

ext3 rhel5及之前的版本格式化,最多支持32TB的文件系统和2T文件,实际2TB文件系统,16G文件
ext4 rhel6 1EB 16TB
xfs rhel7 18EB 9EB 7G/s 4G/s

mkfs.xfs    /dev/vdb1#格式化硬盘格式为xfs
mount    /dev/vdb1    /mnt    #临时挂载
vim /etc/fstab    #开机时自动挂载的分区
设备        挂载点        文件系统        挂载参数        是否备份    是否检测
/dev/vdb1    /mnt        xfs        defaults    0    0
mount -a

linux root------系统的磁盘管理

出错:

[root@localhost ~]# vim /etc/fstab 
/dev/vdb1      /mnt    xfs     defauts 0 0

[root@localhost ~]# reboot

partprobe #同步分区表
cat /proc/partitions

linux root------系统的磁盘管理

再次启动时,虚拟机会出错

linux root------系统的磁盘管理

-->输入PASSWD
-->vim /etc/fstab

#/dev/vdb1      /mnt    xfs     defauts 0 0

-->reboot
正常启动

6.swap分区管理

1.swap建立
划分分区并设定分区标签82
mkswap /dev/vdb6 #格式化
swapon -a /dev/vdb6 #启动
swapon -s #显示swap信息
[root@localhost ~]# swapon -s
Filename Type Size UsedPriority
/dev/vdb6 partition 1048572 0 -1

vim /etc/fstab #永久挂载在swap分区
/dev/sdv6 swap swap defaults 0 0

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

swapoff /dev/vdb6
swapon -s

linux root------系统的磁盘管理

7.配额

是针对分区的
mkdir /public
新建/etc/vdb7
mkfs.xfs /etc/vdb7

mount -o usrquota /dev/vdb7 /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 20480 1 0 0

vim /etc/fstab #永久配额
/dev/vdb7 /public xfs defaults,usrquota 0 0

测试:

[root@localhost ~]# su - student
Last login: Wed Aug  1 22:52:55 EDT 2018 on pts/0
[student@localhost ~]$ dd if=/dev/zero of=/public/studentfile bs=1M count=10
10+0 records in
10+0 records out
10485760 bytes (10 MB) copied, 0.00694435 s, 1.5 GB/s
[student@localhost ~]$ dd if=/dev/zero of=/public/studentfile bs=1M count=100
dd: error writing ‘/public/studentfile’: Disk quota exceeded
21+0 records in
20+0 records out
20971520 bytes (21 MB) copied, 0.224504 s, 93.4 MB/s
[student@localhost ~]$ du -sh /public/studentfile 
20M    /public/studentfile

linux root------系统的磁盘管理

8.磁盘加密

新建分区/dev/vdb8
partprobe

[root@localhost ~]# ll /dev/vdb8
brw-rw----. 1 root disk 253, 24 Aug  3 23:36 /dev/vdb8
cryptsetup luksFormat /dev/vdb8    #加密/dev/vdb8
cryptsetup open /dev/vdb8 sheen    #打开加密设备通口,命名为sheen
mkfs.xfs /dev/mapper/sheen    #/etc/mapper虚拟设备
mount /dev/mapper/sheen /mnt/    #挂载
touch /mnt/file{1..10}
umount /mnt/
cryptsetup close sheen        #关闭加密设备通口

cryptsetup open /dev/vdb8 sheen    
mount /dev/mapper/sheen /mnt/
ls /mnt/

[root@localhost ~]# ll /dev/vdb8
brw-rw----. 1 root disk 253, 23 Aug 1 23:24 /dev/vdb7
[root@localhost ~]# cryptsetup luksFormat /dev/vdb
warnning!
"============"
This will overwrite data on /dev/vdb7 irrevocably.

Are you sure? (Type uppercase yes): YES
Enter passphrase:
Verify passphrase:
[root@localhost ~]# cryptsetup open /dev/vdb8 sheen
Enter passphrase for /dev/vdb8:
[root@localhost ~]# ll /dev/mapper/sheen
lrwxrwxrwx. 1 root root 7 Aug 1 23:25 /dev/mapper/sheen -> ../dm-0
[root@localhost ~]# mount /dev/mapper/sheen /mnt/
[root@localhost ~]# touch /mnt/file{1..4}
[root@localhost ~]# ls /mnt/
file1 file2 file4
[root@localhost ~]# umount /mnt/
[root@localhost ~]# ls /mnt/
[root@localhost ~]# cryptsetup close sheen
[root@localhost ~]# ls /mnt/
[root@localhost ~]# cryptsetup open /dev/vdb8 sheen
Enter passphrase for /dev/vdb8:
[root@localhost ~]# cryptsetup open /dev/vdb8 sheen
Enter passphrase for /dev/vdb8:
[root@localhost ~]# ls /mnt/
[root@localhost ~]# mount /dev/mapper/sheen /mnt/
[root@localhost ~]# ls /mnt/
file1 file2 file3 file4

linux root------系统的磁盘管理
linux root------系统的磁盘管理

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

vim /etc/crypttab
sheen /dev/vdb8 /root/sheenkeys

vim /root/sheenkeys
sheenstar13

cryptsetup luksAddKey /dev/vdb8 /root/sheenkeys

测试:
重启虚拟reboot

df

相关推荐