zoukankan      html  css  js  c++  java
  • unit3&4&5

    ###########第三单元系统延迟及定时机制

    一、计划定期任务

    at 命令

    at 命令可以指定某一任务在将来的特定时间运行。该作业可能是一次备份、对您系统的检查或者特定时间发送的通知。那些需要花费很长时间才可完成的作业正适合 at 命令。仅需使用 at 命令将任务设置为在一分钟或两分钟之后运行。然后您便可以安全注销 , 因为该任务会在与 shell 会话断开连接的情况下运行

    • at 命令必须指定任务应运行的时间。该指定可以是具体时间和 / 或日期 ( 例如星期一 , 10:00pm 或 7 月 15 日 ) 。也可以是当前时间的相对时间 ( 现在时间加上 5 分钟 , 现在时间加上 3 天或 4:00pm 加上1 周 ) 通过添加其他选项 , 您可以在任务完成是发送邮件 ( -m ), 或者从文件中读取任务 ( -f 文件 ), 而无需通过标准输入进行

    • 在键入 at 命令行之后 , 按 Enter 并继续键入作业中包含的其他命令。任务可由多个命令组成。在完成键入要运行的命令时 , 在单独出现的一行中按 Ctrl+d 完成任务

    at 命令示例

    [root@serverX ~]# at now +2 min ##在当前时间2分钟后执行

    at> echo "Hello from the at command" >/dev/pts/0

    at> <EOT> ##按“Ctrl+d”完成任务

    job 4 at 2011-08-10 14:05

    :

    在将某一 at 作业设置为运行之后 , 可以通过使用 atq 命令列出作业编号以及次数。若要查看作业中所包含的命令 , 键入 at -c #( # 号为作业编号 ) 。键入 Atrm # ( # 号为作业编号 ) 则是删除指定作业。

    eg:

    [root@localhost mnt]# at -l

    [root@localhost mnt]# at 21:20

    at> poweroff

    at> <EOT>

    job 3 at Wed Apr  5 21:20:00 2017

    [root@localhost mnt]# at -l

    3 Wed Apr  5 21:20:00 2017 a root

    [root@localhost mnt]# atrm 3

    [root@localhost mnt]# at -l

    [root@localhost mnt]# mail

    Heirloom Mail version 12.5 7/5/10.  Type ? for help.

    "/var/spool/mail/root": 1 message 1 unread

    >U  1 root                  Wed Apr  5 21:19  15/534   "Output from your job "

    & 2

    2: Invalid message number

    & q

    Held 1 message in /var/spool/mail/root

    您在 /var/spool/mail/root 中有邮件

    二、管理定期任务

    • cron 设备管理必须按计划定期重复运行的程序。后台程序crond 每分钟唤醒一次 , 以运行计划的任何任务用户使用crontab 命令计划个人任务。系统管理员可以在系统范围配置文件中设置任务

    • 单个用户使用称为 crontab ( cron 表 ) 的文本文件登记任务

    • crontab -l 列出文件

    • crontab -r 删除文件

    • crontab -e 编辑文件

    • crontab -e 使用默认编辑器是 vi

    eg:

    [root@localhost mnt]# systemctl status crond.service  ##查看crond服务状态

    crond.service - Command Scheduler

       Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled)

       Active: active (running) since Wed 2017-04-05 20:44:09 EDT; 1h 0min ago

     Main PID: 531 (crond)

       CGroup: /system.slice/crond.service

               └─531 /usr/sbin/crond -n

    Apr 05 20:44:09 localhost systemd[1]: Started Command Scheduler.

    Apr 05 20:44:10 localhost crond[531]: (CRON) INFO (RANDOM_DELAY will be sca....)

    Apr 05 20:44:11 localhost crond[531]: (CRON) INFO (running with inotify support)

    Hint: Some lines were ellipsized, use -l to show in full.

    [root@localhost mnt]# crontab -u root -e  ##以root身份编辑 crontab 文件

    crontab: no changes made to crontab

    [root@localhost mnt]# touch file{1..10} ##在写好crontab 文件之后,新建文件会执行

    [root@localhost mnt]# crontab -u root -l   ##列出crontab文件

    * * * * * rm -fr /mnt/*

    [root@localhost cron]# crontab -u root -e

    crontab: installing new crontab

    [root@localhost cron]# touch file{1..10}

    [root@localhost cron]# cd /mnt

    [root@localhost mnt]# touch file{1..10}

    [root@localhost mnt]# crontab -u root -l

    */2 09-17 * * *  rm -fr /mnt/*

    三、corn 时间表示方式

    • 1* 2* 3* 4* 5* command

    – 1*---------- 分钟

    – 2*---------- 小时

    – 3*---------- 天

    – 4*---------- 月

    – 5*---------- 周

    时间表示示例

    cron语法 命令何时运行

    05 * * * * 每小时整点过5分钟

    05 02 * * * 每天2:05am

    30 08 01 * * 每月第一天8:30pm

    00 07 25 12 * 12月25日7:00am

    30 16 * * 5 每个星期五4:30pm

    */5* * * * 5分钟(0、5、10....50、55)

    */10 9-17 1,15 * * 在每月第一天和第十五天9:00am至5:00pm之间(在5:00pm不会运行,最后一次运行是在4:50pm)每隔10分钟

    0 0 1 jan 0 1月1日午夜和1月的每个星期日(不仅是1月1日所在的星期日)

    orgcrontab 注意事项

    • 属于系统的任务通常保存在 /etc/crontab 文件中而不是使用 crontab -e 编辑保存在个人文件中。更好的做法是用文本编辑器创建 crontab 文件并保存到 /etc/cron.d 中 ( 避免更新系统软件包 cronie 时出现问题 ) 。

    /etc/cron.d/crontab 文件在日期说明后另加一个字段 , 表示将运行作业的用户

    • 对于需要每天、每周或每月运行一次的脚本 , 可以直接将其设置为可执行文件 , 并保存在 /etc/cron.{daily,weekly,monthly} 中相应目录下。这些任务由/etc/anacrontab 配置的系统anacron 服务运行 anacron是 cron 的集成组件 , 用于更好地管理作业 , 并确保在作业因计算机关闭而未运行的情况下 , 在系统引导后运行作业

    eg:

    [root@localhost mnt]# cd /etc/cron.d

    [root@localhost cron.d]# vim westos

    [root@localhost cron.d]# echo "* * * * * root rm -fr /mnt/*" >/etc/cron.d/westos

    [root@localhost cron.d]# touch file{1..10} ##在写好文件之后新建文件会执行,

    [root@localhost cron.d]# touch /mnt/file{1..10}

    [root@localhost cron.d]# cat /etc/cron.d/westos

    * * * * * root rm -fr /mnt/*

    [root@localhost cron.d]# echo "*/2 09-17 * * * * root rm -fr /mnt/*" >/etc/cron.d/westos

    [root@localhost cron.d]# touch /mnt/file{1..10}[root@localhost cron.d]# useradd westos

    [root@localhost cron.d]# touch /etc/cron.deny

    [root@localhost cron.d]# vim /etc/cron.deny

    [root@localhost cron.d]# su - student

    上一次登录:Wed Apr  5 21:31:48 EDT 2017pts/2 上

    [student@localhost ~]$ crontab -e

    You (student) are not allowed to use this program (crontab)

    See crontab(1) for more information

    [student@localhost ~]$ exit

    logout

    [root@localhost cron.d]# su - westos

    [westos@localhost ~]$ crontab -e

    no crontab for westos - using an empty one

    crontab: no changes made to crontab

    [westos@localhost ~]$ exit

    logout

    [root@localhost cron.d]# vim /etc/cron.allow

    [root@localhost cron.d]# su - westos

    上一次登录:Wed Apr  5 22:11:07 EDT 2017pts/2 上

    [westos@localhost ~]$ crontab -e

    no crontab for westos - using an empty one

    crontab: no changes made to crontab

    [westos@localhost ~]$ exit

    logout

    [root@localhost cron.d]# su - student

    上一次登录:Wed Apr  5 22:10:37 EDT 2017pts/2 上

    [student@localhost ~]$ crontab -e

    You (student) are not allowed to use this program (crontab)

    See crontab(1) for more information

    [student@localhost ~]$ exit

    logout

    四、cron 权限设定

    • 在系统中默认所有用户可以设定 cron

    • 用户黑名单

    – /etc/cron.deny

    • 用户白名单

    – /etc/cron.allow

    eg:

    [root@localhost mnt]# vim /etc/at.deny

    student

    [root@localhost mnt]# su - student

    [student@localhost ~]$ at now +1min

    You do not have permission to use at.

    [student@localhost ~]$ exit

    logout

    [root@localhost mnt]# touch /etc/at.allow

    [root@localhost mnt]# vim /etc/at.allow

    student

    [root@localhost mnt]# su - student

    上一次登录:Wed Apr  5 21:30:02 EDT 2017pts/2 上

    [student@localhost ~]$ at now +1min

    at> exit

    at> <EOT>

    job 5 at Wed Apr  5 21:33:00 2017

    [student@localhost ~]$ atrm 5

    [student@localhost ~]$ logout

    [root@localhost mnt]# ls -l /etc/at.allow /etc/at.deny

    -rw-r--r--. 1 root root 9 Apr  5 21:31 /etc/at.allow

    -rw-r--r--. 1 root root 9 Apr  5 21:29 /etc/at.deny

    五、系统中的临时文件

    • 系统中服务在正常运行时会产生临时文件

    • 在系统中 /usr/lib/tmpfiles.d/ 标实服务的临时文件存放位置

    • 文件示例

    • vim /usr/lib/tmpfiles.d/test.conf

    d /run/test 0700 root root 5s

    六、控制系统中的临时文件

    • systemd-tmpfiles

    – --create /usr/lib/tmpfiles.d/test.conf

    – --clean /usr/lib/tmpfiles.d/test.conf

    eg:

    [root@localhost mnt]# mkdir haha

    [root@localhost mnt]# touch haha/file{1..10}

    [root@localhost mnt]# vim /usr/lib/tmpfiles.d/haha.conf

    d /mnt/haha 0700 root root 3s

    [root@localhost mnt]# systemd-tmpfiles --create /usr/lib/tmpfiles.d/haha.conf

    [root@localhost mnt]# systemd-tmpfiles --clean /usr/lib/tmpfiles.d/hello.conf

    #################第四单元管理系统中的简单分区和文件系统

    一、简单分区和文件系统

    • 存储是每个计算机系统的基本需求。 Red Hat EnterpriseLinux 提供了一些强大的工具 , 它们能在大量的场景中管理多种类型的存储设备

    • disk 是用于管理磁盘分区的实用程序。您可以通过选择 -l选项和指定磁盘名称 ( fdisk -cul /dev/vda ) 运行该实用程序 , 以查看磁盘及其分区。您可以通过交互式地运行该实用程序 , 并选择相应的菜单选项 ( fdisk -cu /dev/vda ) 进行更改。 -c 禁用旧的 DOS 兼容模式 , -u 以扇区 ( 而不是柱面 , 已经过时 ) 的格式显示输出

    0磁道1扇区512字节

    512-446(mbr)=66=64(mpt)+2(55aa:磁盘有效标识bios)

    二、查看系统设备信息

    • fdisk -l

    [root@localhost cron.d]# fdisk -l

    磁盘 /dev/vda:10.7 GB, 10737418240 字节,20971520 个扇区

    Units = 扇区 of 1 * 512 = 512 bytes

    扇区大小(逻辑/物理):512 字节 / 512 字节

    I/O 大小(最小/最佳):512 字节 / 512 字节

    磁盘标签类型:dos

    磁盘标识符:0x00013f3e

       设备 Boot      Start         End      Blocks   Id  System

    /dev/vda1   *        2048    20970332    10484142+  83  Linux  ##*表示不可更改n

    磁盘 /dev/vdb:10.7 GB, 10737418240 字节,20971520 个扇区

    Units = 扇区 of 1 * 512 = 512 bytes

    扇区大小(逻辑/物理):512 字节 / 512 字节

    I/O 大小(最小/最佳):512 字节 / 512 字节

    [root@localhost cron.d]# fdisk /dev/vdb

    欢迎使用 fdisk (util-linux 2.23.2)。

    更改将停留在内存中,直到您决定将更改写入磁盘。

    使用写入命令前请三思。

    Device does not contain a recognized partition table

    使用磁盘标识符 0x9cc9eaa5 创建新的 DOS 磁盘标签。

    命令(输入 m 获取帮助):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

       u   change display/entry units

       v   verify the partition table

       w   write table to disk and exit

       x   extra functionality (experts only)

    命令(输入 m 获取帮助):n  ##   n表示新建一个磁盘

    Partition type:

       p   primary (0 primary, 0 extended, 4 free)

       e   extended

    Select (default p): p  ##p首要分区,e逻辑分区

    分区号 (1-4,默认 1):1

    起始 扇区 (2048-20971519,默认为 2048):2048

    Last 扇区, +扇区 or +size{K,M,G} (2048-20971519,默认为 20971519):+1G

    分区 1 已设置为 Linux 类型,大小设为 1 GiB

    命令(输入 m 获取帮助):wq  ##退出保存

    The partition table has been altered!

    Calling ioctl() to re-read partition table.

    正在同步磁盘。

    [root@localhost cron.d]# partprobe   ##同步磁盘

    – 显示系统中所有可以使用的设备信息

    • blkid

    [root@localhost mnt]# blkid

    /dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs"

    /dev/sr0: UUID="2015-10-30-11-11-49-00" LABEL="RHEL-7.2 Server.x86_64" TYPE="iso9660" PTTYPE="dos"

    – 显示系统正在使用的设备 id

    [root@localhost cron.d]# cat /proc/partitions

    major minor  #blocks  name

     253        0   10485760 vda

     253        1   10484142 vda1

     253       16   10485760 vdb

     253       17    1048576 vdb1

    三、创建新分区

    四、文件系统比较

    • ext4 是 Red Hat Enterprise Linux 6 的标准文件系统。它非常强大可靠 , 具有多项可以提高现代工作量处理性能的功能

    • ext2 是常用于 Linux 中的较旧的文件系统。它简单可靠 , 非常适合小型存储设备 , 但是效率低于 ext4

    • vfat 支持包括一系列相关文件系统 ( VFAT/FAT16 和FAT32 ), 这些文件系统针对较旧版本的 Microsoft Windows开发 , 在大量的系统和设备上受支持

    • xfs 在 Red Hat Enterprise Linux 7 的标准文件系统其具备数据完全性 、性能稳定、扩展性强( 18eb )、传输速率高( 7G/s )

    五、文件系统创建示例

    # mkfs -t ext4|xfs /dev/vda3

    # blkid /dev/vda3

    /dev/vda3: UUID="cffde973-ab02-4b52-a09c-2a7ee610255c" TYPE="ext4"

    # mkdir /test

    /etc/fstab 添加一个条目 :

    UUID="cffde973-ab02-4b52-a09c-2a7ee610255c" /test [ext4|xfs] defaults 1 2

    测试挂载 :

    # mount /test

    删除现有文件系统

    1. 通过使用 umount /mountpoint 卸载文件系统。

    2. 在 /etc/fstab 中删除相应条目。

    3. 删除挂载点目录 : rmdir /mountpoint

    [root@localhost cron.d]# mount /dev/vdb1 /mnt/ ##将/dev/vdb1设备挂到/mnt下

    mount: /dev/vdb1 写保护,将以只读方式挂载

    mount: 未知的文件系统类型“(null)”

    [root@localhost cron.d]# df

    文件系统          1K-块    已用    可用 已用% 挂载点

    /dev/vda1      10473900 3809552 6664348   37% /

    devtmpfs         927072       0  927072    0% /dev

    tmpfs            942660      84  942576    1% /dev/shm

    tmpfs            942660   17052  925608    2% /run

    tmpfs            942660       0  942660    0% /sys/fs/cgroup

    /dev/vdb1       1038336   32928 1005408    4% /mnt  

    [root@localhost cron.d]# vim /etc/fstab

    /dev/vdb1    /mnt     xfs   defaults      0        0

    要挂载的设备    挂载点      挂载格式  文件系统参数      dump选项   fsck选项 (0是不要检验,1是要检验,2也是要检验,1会比2早被检验)

    [root@localhost cron.d]# umount  /mnt/  ##不再使用/mnt

    [root@localhost cron.d]# df

    文件系统          1K-块    已用    可用 已用% 挂载点

    /dev/vda1      10473900 3809552 6664348   37% /

    devtmpfs         927072       0  927072    0% /dev

    tmpfs            942660      84  942576    1% /dev/shm

    tmpfs            942660   17052  925608    2% /run

    tmpfs            942660       0  942660    0% /sys/fs/cgroup

    [root@localhost cron.d]# mount -a  ##挂载fstab下的所有选项

    [root@localhost cron.d]# df

    文件系统          1K-块    已用    可用 已用% 挂载点

    /dev/vda1      10473900 3809552 6664348   37% /

    devtmpfs         927072       0  927072    0% /dev

    tmpfs            942660      84  942576    1% /dev/shm

    tmpfs            942660   17052  925608    2% /run

    tmpfs            942660       0  942660    0% /sys/fs/cgroup

    /dev/vdb1       1038336   32928 1005408    4% /mnt

    六、swap 交换分区

    • 换空间或交换区是磁盘驱动器上的空间 , 用作当前未使用部分内存的溢出。这样 , 系统就能在主内存中留出空间用于储存当前正在处理的数据 , 并在系统面临主内存空间不足的风险时提供应急溢出

    七、管理交换分区

    • 使用 fdisk 创建新分区。此外 , 在用 fdisk 保存更改之前 , 将分区类型更改为 “ 0x82 LinuxSwap”

    • mkswap /dev/vdaN 会准备好将分区用作交换区

    • blkid /dev/vdaN 将确定 UUID

    • 将新交换空间添加到 /etc/fstab :UUID=uuid swap swap defaults 0 0

    • swapon -a 将激活新交换区

    • swapon -s 将显示当前交换区的状态

    • swapoff /dev/vdaN 将停用该特定交换区

    八、swap 交换分区建议设定

    系统RAM 建议的最小交换空间

    最大4GB 至少2GB

    4GB到16GB 至少4GB

    16GB到64GB 至少8GB

    64GB到256GB 至少16GB

    九、磁盘加密保护

    • LUKS ( Linux 统一密钥设置 ) 是标准的设备加密格式

    • LUKS 可以对分区或卷进行加密

    • 必须首先对加密的卷进行解密 , 才能挂载其中的文件系统

    十、创建新加密的卷

    • 使用 fdisk 创建新分区

    • cryptsetup luksFormat /dev/vdaN 可对新分区进行加密 ,并设置解密密码

    • 您输入正确的解密密码之后 , cryptsetup luksOpen /dev/vdaN name 会将加密的卷 /dev/vdaN 解锁为/dev/mapper/name

    • 解密的卷上创建 xfs 文件系统 : mkfs -t xfs /dev/mapper/name

    • 创建目录挂载点 , 并挂载文件系统 : mkdir /secret

    – mount /dev/mapper/name /secret

    • 完成之后 , umount /dev/mapper/name 并运行cryptsetup luksClose name 以锁定加密的卷

    eg:

    新建分区后

    [root@localhost ~]# cryptsetup luksFormat /dev/vdb1

    WARNING!

    ========

    This will overwrite data on /dev/vdb1 irrevocably.

    Are you sure? (Type uppercase yes): YES  ##大写

    Enter passphrase:   ##密码不少于 8 个字符

    Verify passphrase:

    [root@localhost ~]# cryptsetup open /dev/vdb1 westos11  ##输入正确的解密密码之后 , cryptsetup luksOpen /dev/vdb1westos11会将加密的卷 /dev/vdb1 解锁为/dev/mapper/westos11

    Enter passphrase for /dev/vdb1:

    [root@localhost ~]# ls -l /dev/mapper/

    总用量 0

    crw-------. 1 root root 10, 236 Apr  6 01:40 control

    lrwxrwxrwx. 1 root root       7 Apr  6 02:32 westos11 -> ../dm-0  ##解锁后的文件

    [root@localhost ~]# mkfs.xfs /dev/mapper/westos11  ##创建westos11文件系统

    meta-data=/dev/mapper/westos11   isize=256    agcount=4, agsize=65408 blks

             =                       sectsz=512   attr=2, projid32bit=1

             =                       crc=0

    data     =                       bsize=4096   blocks=261632, 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@localhost ~]# mount /dev/mapper/westos11 /mnt/ ##挂载

    [root@localhost ~]# touch /mnt/file{1..5}

    [root@localhost ~]# ls /mnt/

    file1  file2  file3  file4  file5  ##挂载成功

    [root@localhost ~]# umount /mnt/

    [root@localhost ~]# df

    文件系统          1K-块    已用    可用 已用% 挂载点

    /dev/vda1      10473900 3810472 6663428   37% /

    devtmpfs         927072       0  927072    0% /dev

    tmpfs            942660      80  942580    1% /dev/shm

    tmpfs            942660   17004  925656    2% /run

    tmpfs            942660       0  942660    0% /sys/fs/cgroup

    [root@localhost ~]# cryptsetup close westos11   ##创建westos11文件系统

    [root@localhost ~]# ll /dev/mapper/

    总用量 0

    crw-------. 1 root root 10, 236 Apr  6 01:40 control

    [root@localhost ~]# ls /mnt/

    tt  west

    [root@localhost ~]# ls -l /dev/mapper/

    总用量 0

    crw-------. 1 root root 10, 236 Apr  6 01:40 control

    [root@localhost ~]# cryptsetup close westos11  ##关闭westos11文件系统

    [root@localhost ~]# mkfs.xfs /dev/vdb1

    mkfs.xfs: /dev/vdb1 appears to contain an existing filesystem (crypto_LUKS).

    mkfs.xfs: Use the -f option to force overwrite.

    [root@localhost ~]# mkfs.xfs /dev/vdb1 -f

    meta-data=/dev/vdb1              isize=256    agcount=4, agsize=65536 blks

             =                       sectsz=512   attr=2, projid32bit=1

             =                       crc=0

    data     =                       bsize=4096   blocks=262144, imaxpct=25

             =                       sunit=0      swidth=0 blks

    naming   =version 2              bsize=4096   ascii-ci=0 ftype=0

    log      =internal log           bsize=4096   blocks=2560, version=2

             =                       sectsz=512   sunit=0 blks, lazy-count=1

    realtime =none                   extsz=4096   blocks=0, rtextents=0

    [root@localhost ~]# fdisk /dev/vdb

    欢迎使用 fdisk (util-linux 2.23.2)。

    更改将停留在内存中,直到您决定将更改写入磁盘。

    使用写入命令前请三思。

    命令(输入 m 获取帮助):d  ##删除分区

    已选择分区 1

    分区 1 已删除

    命令(输入 m 获取帮助):p  ##打印当前分区

    磁盘 /dev/vdb:10.7 GB, 10737418240 字节,20971520 个扇区

    Units = 扇区 of 1 * 512 = 512 bytes

    扇区大小(逻辑/物理):512 字节 / 512 字节

    I/O 大小(最小/最佳):512 字节 / 512 字节

    磁盘标签类型:dos

    磁盘标识符:0x9cc9eaa5

       设备 Boot      Start         End      Blocks   Id  System

    命令(输入 m 获取帮助):wq

    The partition table has been altered!

    Calling ioctl() to re-read partition table.

    正在同步磁盘。

    [root@localhost ~]# partprobe

    [root@localhost ~]# mdadm -C /dev/md0 -a yes -l 1 -n 2 -x 1 /dev/vdb{1..3}

    mdadm: Note: this array has metadata at the start and

        may not be suitable as a boot device.  If you plan to

        store '/boot' on this device please ensure that

        your boot-loader understands md/v1.x metadata, or use

        --metadata=0.90

    Continue creating array? y

    mdadm: Defaulting to version 1.2 metadata

    mdadm: array /dev/md0 started.

    [root@localhost ~]# mount /dev/md0  /mnt/

    mount: /dev/md0 写保护,将以只读方式挂载

    mount: 未知的文件系统类型“(null)”

    [root@localhost ~]# df

    文件系统          1K-块    已用    可用 已用% 挂载点

    /dev/vda1      10473900 3810484 6663416   37% /

    devtmpfs         927072       0  927072    0% /dev

    tmpfs            942660      80  942580    1% /dev/shm

    tmpfs            942660   17072  925588    2% /run

    tmpfs            942660       0  942660    0% /sys/fs/cgroup

    [root@localhost ~]# mkfs.xfs /dev/md0

    meta-data=/dev/md0               isize=256    agcount=4, agsize=65500 blks

             =                       sectsz=512   attr=2, projid32bit=1

             =                       crc=0

    data     =                       bsize=4096   blocks=262000, 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@localhost ~]# df

    文件系统          1K-块    已用    可用 已用% 挂载点

    /dev/vda1      10473900 3810484 6663416   37% /

    devtmpfs         927072       0  927072    0% /dev

    tmpfs            942660      80  942580    1% /dev/shm

    tmpfs            942660   17072  925588    2% /run

    tmpfs            942660       0  942660    0% /sys/fs/cgroup

    [root@localhost ~]# mount /dev/md0  /mnt/

    [root@localhost ~]# df

    文件系统          1K-块    已用    可用 已用% 挂载点

    /dev/vda1      10473900 3810576 6663324   37% /

    devtmpfs         927072       0  927072    0% /dev

    tmpfs            942660      80  942580    1% /dev/shm

    tmpfs            942660   17072  925588    2% /run

    tmpfs            942660       0  942660    0% /sys/fs/cgroup

    /dev/md0        1044588   32928 1011660    4% /mnt

    [root@localhost ~]# mdadm /dev/md0 -f /dev/vdb2

    mdadm: set /dev/vdb2 faulty in /dev/md0

    [root@localhost ~]# mdadm /dev/md0 -r /dev/vdb2

    mdadm: hot removed /dev/vdb2 from /dev/md0

    [root@localhost ~]# mdadm /dev/md0 -a /dev/vdb2

    mdadm: added /dev/vdb2

    第五单元

    使用逻辑卷管理器管理灵活存储

    一、查看 LVM 定义

    • 物理分区或磁盘是 LVM 的第一构建块。这些可以是分区、完整磁盘、 RAID 集或 SAN 磁盘

    • 物理卷是 LVM 所使用的基础 “ 物理 ” 存储。这通常是块设备 ,例如分区或完整磁盘。设备必须初始化为 LVM 物理卷 , 才能与 LVM 结合使用

    • 卷组是存储池 , 由一个或多个物理卷组成

    • 物理区块是物理卷中存储的小型数据区块 , 用作 LVM 存储的后端

    • 逻辑区块映射到物理区块 , 构成 LVM 存储的前端。默认情况下 , 每个逻辑区块映射到一个物理区块。启用某些选项将更改此映射。例如 , 镜像会导致每个逻辑区块映射到两个物理区块

    • 逻辑卷是逻辑区块组。逻辑卷可以像硬盘驱动器分区一样使用

    物理卷区-->pv-->vg-->lvm

    二、为什幺使用逻辑卷

    • 逻辑卷和逻辑卷管理有助于更加轻松地管理磁盘空间。如果文件系统需要更多的空间 ,可以将其卷组的可用空间分配给逻辑卷 , 并且可以调整文件系统的大小。如果磁盘开始出现错误 , 可以通过卷组将替换磁盘注册为物理卷 , 并且逻辑卷的范围可迁移到新磁盘。

    三、使用命令行工具实施LVM存储

    • 准备物理卷

    – disk 用于创建与 LVM 结合使用的新分区。在要与 LVM 结合使用的分区上 , 始终将“ 类型 ” 设置为 “ 0x8e Linux LVM”

    – pvcreate /dev/vdaN 用于初始化分区 ( 或其他物理设备 ), 使其作为物理卷与 LVM 结合使用。直接在物理卷中创建用于存储 LVM 配置数据的表头

    • 创建卷组

    – vgcreate vgname /dev/vdaN 将创建名为 vgname 且由物理卷 /dev/vdaN 组成的卷组。您可以在创建时指定其他物理卷 ( 以空格隔开 ), 也可以随后使用 vgextend 添加新物理卷

    • 创建和使用新逻辑卷

    – lvcreate -n lvname -L 2G vgname 从 vgname 上可用物理区块创建名为 lvname 、容量为 2GB 的新逻辑卷

    eg:

    命令(输入 m 获取帮助):t

    分区号 (1,2,默认 2):

    Hex 代码(输入 L 列出所有代码):8e

    已将分区“Linux”的类型更改为“Linux LVM”

       设备 Boot      Start         End      Blocks   Id  System

    /dev/vdb1            2048     2099199     1048576   8e  Linux LVM

    /dev/vdb2         2099200     4196351     1048576   8e  Linux LVM

    命令(输入 m 获取帮助):wq

    The partition table has been altered!

    Calling ioctl() to re-read partition table.

    正在同步磁盘。

    [root@localhost ~]# partprobe

    [root@localhost ~]# pvcreate /dev/vdb1  ##用于初始化分区

      Physical volume /dev/vdb1 not found

      Can't open /dev/vdb1 exclusively.  Mounted filesystem?

    [root@localhost ~]# mdadm -S /dev/md0

    mdadm: stopped /dev/md0

    [root@localhost ~]# pvcreate /dev/vdb1

      Physical volume /dev/vdb1 not found

    WARNING: linux_raid_member signature detected on /dev/vdb1 at offset 4096. Wipe it? [y/n] y

      Wiping linux_raid_member signature on /dev/vdb1.

    WARNING: xfs signature detected on /dev/vdb1 at offset 0. Wipe it? [y/n] y

      Wiping xfs signature on /dev/vdb1.

      Physical volume "/dev/vdb1" successfully created

    [root@localhost ~]# vgcreate vg0 /dev/vdb1

      Volume group "vg0" successfully created

    [root@localhost ~]# lvcreate -L 100M -n lv0 vg0

      Logical volume "lv0" created

    [root@localhost ~]# mkfs.xfs /dev/vg0/lv0

    meta-data=/dev/vg0/lv0           isize=256    agcount=4, agsize=6400 blks

             =                       sectsz=512   attr=2, projid32bit=1

             =                       crc=0

    data     =                       bsize=4096   blocks=25600, 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@localhost ~]# mount /dev/vg0/lv0 /mnt/

    [root@localhost ~]# ll /dev/vg0/lv0

    lrwxrwxrwx. 1 root root 7 Apr  6 04:02 /dev/vg0/lv0 -> ../dm-0

    [root@localhost ~]# ll /dev/mapper/vg0-lv0

    lrwxrwxrwx. 1 root root 7 Apr  6 04:02 /dev/mapper/vg0-lv0 -> ../dm-0

    watch -n 1 'pvs;echo ===;vgs;echo ===;lvs;echo ===;df -H /mnt'

    [root@localhost ~]# lvextend -L 500M /dev/vg0/lv0

      Extending logical volume lv0 to 500.00 MiB

      Logical volume lv0 successfully resized

    [root@localhost ~]# xfs_growfs /dev/vg0/lv0

    meta-data=/dev/mapper/vg0-lv0    isize=256    agcount=4, agsize=6400 blks

             =                       sectsz=512   attr=2, projid32bit=1

             =                       crc=0

    data     =                       bsize=4096   blocks=25600, imaxpct=25

             =                       sunit=0      swidth=0 blks

    naming   =version 2              bsize=4096   ascii-ci=0 ftype=0

    log      =internal               bsize=4096   blocks=853, version=2

             =                       sectsz=512   sunit=0 blks, lazy-count=1

    realtime =none                   extsz=4096   blocks=0, rtextents=0

    data blocks changed from 25600 to 128000

    [root@localhost ~]# lvextend -L 1500M /dev/vg0/lv0

      Extending logical volume lv0 to 1.46 GiB

      Insufficient free space: 250 extents needed, but only 130 available

    [root@localhost ~]# pvcreate /dev/vdb2

      Physical volume /dev/vdb2 not found

    WARNING: linux_raid_member signature detected on /dev/vdb2 at offset 4096. Wipe it? [y/n] y

      Wiping linux_raid_member signature on /dev/vdb2.

    WARNING: swap signature detected on /dev/vdb2 at offset 4086. Wipe it? [y/n] y

      Wiping swap signature on /dev/vdb2.

      Physical volume "/dev/vdb2" successfully created

    [root@localhost ~]# lvextend -L 1500M /dev/vg0/lv0   ##使用部分或全部可用区块扩展逻辑卷

      Extending logical volume lv0 to 1.46 GiB

      Insufficient free space: 250 extents needed, but only 130 available

    [root@localhost ~]# vgextend vg0 /dev/vdb2

      Volume group "vg0" successfully extended

    [root@localhost ~]# lvextend -L 1500M /dev/vg0/lv0

      Extending logical volume lv0 to 1.46 GiB

      Logical volume lv0 successfully resized

    [root@localhost ~]# xfs_growfs /dev/vg0/lv0

    meta-data=/dev/mapper/vg0-lv0    isize=256    agcount=20, agsize=6400 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               bsize=4096   blocks=853, version=2

             =                       sectsz=512   sunit=0 blks, lazy-count=1

    realtime =none                   extsz=4096   blocks=0, rtextents=0

    data blocks changed from 128000 to 384000

    – mkfs -t ext4 /dev/vgname/lvname 将在新逻辑卷上创建 ext4 文件系统

    – mkdir /data 建立所需目录作为挂载点

    – 向 /etc/fstab 文件中添加一下条目:

    /dev/mapper/vgname-lvname /data ext4 defaults 1 2

    – 运行 mount -a 可在 /etc/fstab 中挂载所有文件系统 , 包括您刚刚添加的条目

    四、查看 LVM 状态信息

    • pvdisplay /dev/vdaN 将显示有关特定物理卷的信息

    • vgdisplay vgname 将显示有关特定卷组的信息

    • lvdisplay /dev/vgname/lvname 将显示有关特定逻辑卷的信息

    五、扩展逻辑卷和xfs文件系统

    • 验证已挂载文件系统 /data 的当前大小

    – df -h /data

    • 验证 “ 可用物理区块 ” 是否够用

    – vgdisplay vgname

    • 使用部分或全部可用区块扩展逻辑卷

    – lvextend -l 128 /dev/vgname/lvname

    • 扩展在 /data 上挂载的关联文件系统

    – xfs_growfs

    /dev/vgname/lvname

    • 验证已挂载文件系统 /data 的新的大小

    – df -h /data

    六、创建快照以便于数据备份

    • 快照逻辑卷是 LVM 存储的另一项灵活功能。LVM 快照是临时保留所更改的逻辑卷的原始数据的逻辑卷。快照提供原始卷的静态视图 , 从而能够以一致状态备份其数据

    • 快照卷大小只需足以存储在它存在期间更改的数据即可

    • 如果数据更改量大于快照存储容量 , 则快照将自动变为不可用。 ( 原始卷原封不动 , 仍然需要从卷组中手动解除挂载和删除不可用的快照。 )

    七、创建和使用快照进行备份

    • 创建 /dev/vgname/lvname 的名为 snaplvname 、大小为 20M 的新快照卷

    – lvcreate -s -n snaplv -L 20M /dev/vgname/lvname

    • 如果备份软件要求 , 请挂载该快照 , 并使备份程序指向新挂载点

    – mkdir /snapmount

    – mount -o ro /dev/vgname/snaplv /snapmount    ##用唯读方式挂上

    • 验证快照逻辑卷的状态

    – lvs /dev/vgname/snaplv

    • 不再使用快照时 , 请解除挂载并删除它

    – umount /snapmount

    – lvremove /dev/vgname/snaplv

    eg:

    [root@localhost mnt]# lvcreate  -s -n kuaizhao -L 20M /dev/vg0/lvo

      Logical volume "kuaizhao" created

    [root@localhost mnt]# mount -o ro /dev/vg0/kuaizhao /mnt/

    [root@localhost mnt]# lvs /dev/vg0/kuaizhao

      LV       VG   Attr       LSize  Pool Origin Data%  Move Log Cpy%Sync Convert

      kuaizhao vg0  swi-aos--- 20.00m      lvo     10.10                          

    [root@localhost mnt]# umount /mnt/

    [root@localhost mnt]# lvremove /dev/vg0/kuaizhao

    Do you really want to remove active logical volume kuaizhao? [y/n]: y

      Logical volume "kuaizhao" successfully removed

  • 相关阅读:
    JS代码判断IE不同版本
    极简技术类录--正则表达式
    Java读取系统Properties
    极简技术类录--maven
    极简技术类录--git
    JVM字节码增强
    如何避免死锁?
    有三个线程T1,T2,T3,怎么确保它们按顺序执行?
    编写两个线程,顺序输出自然顺序:1,2,3,4,...,99,100
    对文本单词进行技数,并倒序列出计数统计
  • 原文地址:https://www.cnblogs.com/yangying2017/p/6685681.html
Copyright © 2011-2022 走看看