8月9日 磁盘管理 2

1、swap分区管理

  • 用磁盘创建swap分区
[root@centos7 ~]#gdisk /dev/sdb   ---创建分区,并把code改成8200
[root@centos7 ~]#lsblk  ---分区创建完毕
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0   50G  0 part /
├─sda3   8:3    0   50G  0 part /app
├─sda4   8:4    0  512B  0 part 
└─sda5   8:5    0    2G  0 part [SWAP]
sdb      8:16   0   20G  0 disk 
└─sdb1   8:17   0    2G  0 part 
sdc      8:32   0  150G  0 disk 
├─sdc1   8:33   0   10G  0 part 
├─sdc2   8:34   0    5G  0 part 
└─sdc3   8:35   0    1K  0 part 
sdd      8:48   0  120G  0 disk 
sr0     11:0    1  7.7G  0 rom  /run/media/root/CentOS 7 x86_64
[root@centos7 ~]#mkswap /dev/sdb1   ---创建swap文件系统
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=e32dc38a-b7cf-425d-b27c-0b99c4af4592
[root@centos7 ~]#vim /etc/fstab   ---更改配置文件
  1 
  2 #
  3 # /etc/fstab
  4 # Created by anaconda on Fri Jul 14 11:16:04 2017
  5 #
  6 # Accessible filesystems, by reference, are maintained under '/dev/disk'
  7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
  8 #
  9 UUID=e7e1738b-58ae-49d3-9f7e-c62988ead392 /                       xfs     defaults        0 0
 10 UUID=348930fe-f95e-40e6-90ff-83c66b3f2e9a /app                    xfs     defaults        0 0
 11 UUID=dfbab032-681e-4e34-a16e-2f5e1eda5f32 /boot                   xfs     defaults        0 0
 12 UUID=6145ae5c-6ebd-4655-a52f-e90f9b5a9e58 swap                    swap    defaults        0 0
[root@centos7 ~]#swapon -a   ---激活交换分区
[root@centos7 ~]#cat /proc/swaps    ----查看swap分区
Filename                Type        Size    Used    Priority
/dev/sda5                               partition   2097148 0   -1
/dev/sdb1                               partition   2097148 0   -2
[root@centos7 ~]#swapon -s   ---查看swap分区
Filename                Type        Size    Used    Priority
/dev/sda5                               partition   2097148 0   -1
/dev/sdb1                               partition   2097148 0   -2
[root@centos7 ~]#vim /etc/fstab  ---修改优先级,值在0-32767之间,值越大优先级越高,因为/dev/sdb1为sdb磁盘的第一个分区,是在外道,速度比较快,所以尽量先使用sdb1的swap分区
  1 
  2 #
  3 # /etc/fstab
  4 # Created by anaconda on Fri Jul 14 11:16:04 2017
  5 #
  6 # Accessible filesystems, by reference, are maintained under '/dev/disk'
  7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
  8 #
  9 UUID=e7e1738b-58ae-49d3-9f7e-c62988ead392 /                       xfs     defaults        0 0
 10 UUID=348930fe-f95e-40e6-90ff-83c66b3f2e9a /app                    xfs     defaults        0 0
 11 UUID=dfbab032-681e-4e34-a16e-2f5e1eda5f32 /boot                   xfs     defaults        0 0
 12 UUID=6145ae5c-6ebd-4655-a52f-e90f9b5a9e58 swap                    swap    defaults        0 0
 13 UUID=e32dc38a-b7cf-425d-b27c-0b99c4af4592 swap                     swap    pri=10        0 0
[root@centos7 ~]#swapoff /dev/sdb1   ---先禁用
[root@centos7 ~]#swapon -s
Filename                Type        Size    Used    Priority
/dev/sda5                               partition   2097148 0   -1
[root@centos7 ~]#swapon -a   ---再激活
[root@centos7 ~]#swapon -s   ---最后才能生效
Filename                Type        Size    Used    Priority
/dev/sda5                               partition   2097148 0   -1
/dev/sdb1                               partition   2097148 0   10
  • 用文件创建swap分区
[root@centos7 ~]#fdisk /dev/sdc  ---创建一个独立的分区
[root@centos7 ~]#lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0   50G  0 part /
├─sda3   8:3    0   50G  0 part /app
├─sda4   8:4    0  512B  0 part 
└─sda5   8:5    0    2G  0 part [SWAP]
sdb      8:16   0   20G  0 disk 
└─sdb1   8:17   0    2G  0 part [SWAP]
sdc      8:32   0  150G  0 disk 
└─sdc1   8:33   0   20G  0 part   ---一个20G的独立分区创建完毕
sdd      8:48   0  120G  0 disk 
sr0     11:0    1  7.7G  0 rom  /run/media/root/CentOS 7 x86_64
[root@centos7 ~]#mkfs.xfs /dev/sdc1  ---创建文件系统
[root@centos7 ~]#mkdir /mnt/sdc1  
[root@centos7 ~]#mount /dev/sdc1 /mnt/sdc1  ---把文件系统挂载到目录下
[root@centos7 ~]#df
Filesystem     1K-blocks     Used Available Use% Mounted on
/dev/sda2       52403200 15338292  37064908  30% /
devtmpfs          485268        0    485268   0% /dev
tmpfs             499980       88    499892   1% /dev/shm
tmpfs             499980     7292    492688   2% /run
tmpfs             499980        0    499980   0% /sys/fs/cgroup
/dev/sda3       52403200   540240  51862960   2% /app
/dev/sda1        1038336   172284    866052  17% /boot
tmpfs             100000       12     99988   1% /run/user/0
  ev/sr0         8086368  8086368         0 100% /run/media/root/CentOS 7 x86_64
▽dev/sdc1       20961280    32944  20928336   1% /mnt/sdc1
[root@centos7 ~]#cd /mnt/sdc1  ---进入这个目录
[root@centos7 sdc1]#dd if=/dev/zero of=swapfile bs=1M  count=1024   ---创建一个大的文件,将来作为swap分区使用
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 13.2999 s, 80.7 MB/s
[root@centos7 sdc1]#mkswap swapfile     ---创建swap文件系统
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=45d3ea5d-fac8-4cbe-9588-210d999a7802
 1 
  2 #
  3 # /etc/fstab
  4 # Created by anaconda on Fri Jul 14 11:16:04 2017
  5 #
  6 # Accessible filesystems, by reference, are maintained under '/dev/disk'
  7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
  8 #
  9 UUID=e7e1738b-58ae-49d3-9f7e-c62988ead392 /                       xfs     defaults        0 0
 10 UUID=348930fe-f95e-40e6-90ff-83c66b3f2e9a /app                    xfs     defaults        0 0
 11 UUID=dfbab032-681e-4e34-a16e-2f5e1eda5f32 /boot                   xfs     defaults        0 0
 12 UUID=6145ae5c-6ebd-4655-a52f-e90f9b5a9e58 swap                    swap    defaults        0 0
 13 UUID=e32dc38a-b7cf-425d-b27c-0b99c4af4592 swap                     swap    pri=10        0 0
 14 UUID=45d3ea5d-fac8-4cbe-9588-210d999a7802  swap                    swap   default        0 0   ---修改配置文件
[root@centos7 sdc1]#swapon -a   ---激活分区。有以下提示,建议把权限修改为0600
swapon: /mnt/sdc1/swapfile: insecure permissions 0644, 0600 suggested.
[root@centos7 sdc1]#chmod 0600 swapfile ---修改权限
[root@centos7 sdc1]#ll  ---权限被更改
total 1048576
-rw-------. 1 root root 1073741824 Aug  9 20:17 swapfile
[root@centos7 sdc1]#swapon -s  分区创建成功
Filename                Type        Size    Used    Priority
/dev/sda5                               partition   2097148 0   -1
/dev/sdb1                               partition   2097148 0   10
/mnt/sdc1/swapfile                      file    1048572 0   -2

2、创建ISO文件

cd /app
mkdir centos6
cd centos6/
cp -r  /media/CentOS_6.9_Final/.* . ---先将点打头的文件复制过来
cp -r /media/CentOS_6.9_Final/* .   ---centos6.9第一张盘
cp -r /media/CentOS_6.9_Final/* .  ---centos6.9第二张盘
mkisofs -r -o /app/CentOS-6-x86_64-Everything-0809.iso /app/centos6/   ----把目录里的内容制作成iso镜像文件,此时制作的ISO文件不能做为启动使用,只能用于yumf源。
[root@centos6 app]#rz  ---上传mkdvdiso.sh脚本
[root@centos6 app]#ls
mkdvdiso.sh
[root@centos6 app]#chmod a+x mkdvdiso.sh  
[root@centos6 app]#./mkdvdiso.sh --help ---查帮助
Error: /usr/bin/implantisomd5 Not Found!
Please install isomd5sum and try again.
[root@centos6 app]#yum search isomd5sum  ---搜索安装包
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
centos6                                   | 4.0 kB     00:00 ... 
==================== N/S Matched: isomd5sum =====================
isomd5sum-devel.i686 : Development headers and library for using
                     : isomd5sum
isomd5sum-devel.x86_64 : Development headers and library for
                       : using isomd5sum
isomd5sum.x86_64 : Utilities for working with md5sum implanted in
                 : ISO images    ---为这个安装包
  Name and summary matches only, use "search all" for everything.
yum install isomd5sum
./mkdvdiso.sh /app/centos6/ /app/CentOS-6-x86_64-Everything-0809-used.iso  ---执行这个脚本可以将目录中的内容做成启动功能的iso文件,与mkisofs命令不同的是,源在前,目标在后

3、一些常用的小工具

  • free
[root@centos6 cd]#free  ---默认以k为单位
             total       used       free     shared    buffers     cached
Mem:       1004112     819232     184880       3280      73812     347900
-/+ buffers/cache:     397520     606592
Swap:      2097148          0    2097148
[root@centos6 cd]#free -m
             total       used       free     shared    buffers     cached
Mem:           980        800        180          3         72        339
-/+ buffers/cache:        388        592
Swap:         2047          0       2047
[root@centos6 cd]#free -g
             total       used       free     shared    buffers     cached
Mem:             0          0          0          0          0          0
-/+ buffers/cache:          0          0
Swap:            1          0          1
[root@centos6 cd]#free -h
             total       used       free     shared    buffers     cached
Mem:          980M       800M       180M       3.2M        72M       339M
-/+ buffers/cache:       388M       592M
Swap:         2.0G         0B       2.0G
[root@centos7 app]#free  ---在centos7中都是给计算好的
              total        used        free      shared  buff/cache   available
Mem:        2048540      547780     1038912       14160      461848     1390260
Swap:       2097148           0     2097148

总结:buffers和cached都是缓存,前面的是元数据的缓存,后面的是数据的缓存,缓存里的内容可以清除,实际上用掉的内存为800-(72+339)=338 剩余的内存为180+(72+339)=592。

  • df

文件系统空间占用等信息的查看工具:
df[OPTION]... [FILE]...
-H 以1000为单位
-T 文件系统类型
-h: human-readable
-i:inodesinstead of blocks
-P: 以Posix兼容的格式输出 ---假如前面设备名比较长,造成不和后面的同行,可以使用-p选项,让他们在一行

[root@centos6 app]#df
Filesystem           1K-blocks    Used Available Use% Mounted on
/dev/sda2             50264772 4750828  42953944  10% /
tmpfs                   502056      76    501980   1% /dev/shm
/dev/sda3             40185208 1097616  37039592   3% /app
/dev/sda1               991512   34904    905408   4% /boot
/dev/sdb1             10194676   23420   9646736   1% /home
/dev/sr0               6049912 6049912         0 100% /media/CDROM
/app/cenos6.9everyting-asddgggghhsds.osaddgdh
                        999320    1284    945608   1% /mnt/f1
[root@centos6 app]#df -P
Filesystem                                    1024-blocks    Used Available Capacity Mounted on
/dev/sda2                                        50264772 4750828  42953944      10% /
tmpfs                                              502056      76    501980       1% /dev/shm
/dev/sda3                                        40185208 1097616  37039592       3% /app
/dev/sda1                                          991512   34904    905408       4% /boot
/dev/sdb1                                        10194676   23420   9646736       1% /home
/dev/sr0                                          6049912 6049912         0     100% /media/CDROM
/app/cenos6.9everyting-asddgggghhsds.osaddgdh      999320    1284    945608       1% /mnt/f1
  • du

查看某目录总体空间占用状态:
du [OPTION]... DIR ---只统计目录的大小,以k为单位
-h: human-readable
-s: summary ---只统计目录的总大小,不单独统计里面的子目录。

[root@centos6 app]#du -sh /* ---统计根下的一级子目录的大小

-dd

[root@centos6 app]#echo abcdefg >f1
[root@centos6 app]#echo 123456789 >f2
[root@centos6 app]#ls
f1  f2
[root@centos6 app]#cat f1 f2
abcdefg
123456789
[root@centos6 app]#dd if=f1 of=f2 bs=1 count=2 skip=3 seek=4 conv=notrunc  ---skip=3表示跳过读入的文件的前三个字节;seek=4表示跳过输出到的文件的前四个字节,从第5个字节处开始输入,conv=notrunc表示不截断
2+0 records in
2+0 records out
2 bytes (2 B) copied, 0.000318369 s, 6.3 kB/s
[root@centos6 app]#cat f1 f2
abcdefg
1234de789
[root@centos6 app]#cat f1 f2
abcdefg
1234de789
[root@centos6 app]#dd if=f1 of=fA conv=ucase   ---表示把小写转换成大写
0+1 records in
0+1 records out
8 bytes (8 B) copied, 0.000174666 s, 45.8 kB/s
[root@centos6 app]#cat fA
ABCDEFG
[root@centos6 app]#cat fA
ABCDEFG
[root@centos6 app]#dd if=fA of=fa conv=lcase  --大写转换成小写
0+1 records in
0+1 records out
8 bytes (8 B) copied, 0.000171626 s, 46.6 kB/s
[root@centos6 app]#cat fa
abcdefg
dd if=/dev/urandom of=/dev/sda1---x销毁磁盘数据
dd if=/dev/sda of=/dev/sda ---修复磁盘
[root@centos6 app]#dd if=/dev/zero of=f3 bs=1M count=1024  ---测试磁盘的写速度
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 5.35934 s, 200 MB/s
[root@centos6 app]#dd if=/app/f3 of=/dev/null bs=1M count=1024  ---测试磁盘的读速度
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 3.03901 s, 353 MB/s

总结:/dev/zero和/dev/null都是内存中的数据,读和写的时候很快。通过从文件中读写到/dev/null可以测磁盘的读速度;通过从/dev/zero读写到文件中可以测磁盘的写速度。

你可能感兴趣的:(8月9日 磁盘管理 2)