首页 > 代码库 > 2016-9-20第七周作业

2016-9-20第七周作业

fdisk:对于一块硬盘来讲,最多只能管理15个分区

fdisk -l [-u] [device...]

 fdisk device
子命令:管理功能
p:print,显示已有分区:
n:new,创建
d:delete,删除
w:write,写入磁盘并退出
q:quit,放弃更新并退出
m:获取帮助
l:列表,列出所有分区id
t:调整分区id

查看内核是否已经识别新的分区,
 cat /proc/partitions

如果不识别,通知内核重新读取硬盘分区表;
 partx -a /dev/DEVICE
-n M:N

kpartx -a /dev/DEVICE
-f:force

Centos 5:使用partprobe
partprobe [/dev/DEVICE]


创建文件系统:
mkfs命令:
(1) # mkfs.FS_TYPE(分区类型) /dev/DEVICE
                       ext4
                       xfs
                       btrfs
                       vfat
(2) # mkfs -t FS_TYPE /dev/DEVICE
                          ext4
                          xfs
                          btrfs
                          vfat

-L ‘LABEL‘:设定卷标

blkid /dev/DEVICE 显示分区属性

mke2fs:ext系列文件系统专用管理工具
-t {ext2|ext3|ext4}
-b {1024|2048|4096}(设置块大小)
-L ‘LABEL‘
-j:相当于 -t ext3
mkfs.ext3=mkfs -t ext3 = mke2fs -j = mke2fs -t ext3
-i #:为数据空间中每多少个字节创建一个inode;此大小不应该小于block的大小;
-N #:为数据空间创建多少个inode;
-m #:为管理人员预留的空间占据的百分比;
-O FEATURE[,...]:启用指定特性
-O ^FEATURE:关闭指定特性

mkswap:创建交换分区
mkswap [OPTIONS] device
-L ‘LABEL‘

前提:调整其分区的ID为82

其它常用工具:

blkid:块设备属性值信息查看
blkid [OPTION]... [DEVICE]
-U UUID:根据指定的UUID来查找对应的设备
-L LABEL:根据指定的LABEL来查找对应的设备

e2label:管理ext系列文件系统的LABEL
# e2label DEVICE [LABEL]

tune2fs:重新设定ext系列文件系统可调用参数的值
-l:查看指定文件系统超级块信息:super block
-L ‘LABEL‘:修改卷标
-m #:修改预留给管理员的空间百分比
-j:将ext2升级为ext3
-O:文件系统属性启用或禁用
-o:调整文件系统的默认挂载选项
-U UUID: 修改UUID号:

dumpe2fs: 既能查看超级块信息,又能查看整个文件的组织结构信息;又称查看布局信息
-h:查看超级块信息

文件系统检测:
fsck:File System CheCk
fsck.FS_TYPE
fsck -t FS_TYPE
-a:自动修复错误
-r:交互式修复错误

Note:FS_TYPE一定要与分区上已有的文件系统类型相同

e2fsck: ext系列文件专用的检测的工具
-y:自动回答为yes;
-f:强制修复;


挂载方法:mount DEVICE MOUNT_POINT
mount:通过查看/etc/mtab文件显示当前系统已挂载的所有设备
mount [-fnrsvw] [-t vfstype] [-o options] device dir
device:指明要挂载的设备:
(1) 设备文件:例如:/dev/sda5
(2) 卷标:-L ‘LABEL‘,例如 -L ‘MYDATA‘
(3) UUID, -U ‘UUID‘;例如 -u ‘0c50523c-43f1-45e7-85c0-a126711d406e‘
(4) 伪文件系统名称:proc,sysfs,devtmpfs,configfs
dir:挂载点
事先存在:建议使用空目录:
进程正在使用

常用选项:
-t vsftype:指定要挂载的设备上的文件系统类型;
-r:readnoly,只读挂载;
-w:read and write,读写挂载;
-n:不更新/etc/mtab;
-a:自动挂载所有支持自动挂载的设备:(定义在了/etc/fstab文件中,且挂载选项中有“自动挂载”功能)
-L ‘LABEL‘:以卷标指定挂载设备;
-U ‘UUID‘:以UUID指定要挂载的设备
-B --bind:绑定目录到另一个目录上

注意:查看内核追踪到的已挂载的所有设备:cat /proc/mounts

-o options:(挂载文件系统的选项)
    async:异步模式
    sync:同步模式
   atime/noatime:atime修改访问时间戳,noatime不修改访问时间戳(包含目录和文件)
   diratime/nodiratime:目录的访问时间戳的修改或不修改
   auto/noauto:是否支持自动挂载
   exec/noexec:是否支持将文件系统上应用程序运行为进程;
   dev/nodev:是否支持在此文件系统上使用设备文件
   suid/nosuid:是否运行suid文件
   remount:重新挂载
   ro:只读
   rw:读写
   user/nouser:是否允许普通用户挂载此设备
   acl:启用此文件系统上的acl功能(acl访问控制列表)

注意:上述选项可多个同时使用,彼此使用逗号分隔
默认挂载选项:defauts
defauts:rw,suid,dev,exec,auto,nouser,async

卸载命令:
# umount DEVICE
# umount MOUNT_POINT

查看正在访问指定文件系统的进程:
# fuser -v MOUNT_POINT

终止所有正在访问的文件系统的进程:
# fuser -km MOUNT_POINT

挂载交换分区:
启用:swapon
swapon [OPTION]... [DEVICE]
-a:激活所有的交换分区:
-p PRIORITY:指定优先级;
禁用:swapoff [OPTION]... [DEVICE]

内存空间使用状态:
free [OPTION]
-m:以MB为单位
-g:以GB为单位

文件系统空间占用等信息查看工具:
df:
-h: human-readable 以人类可读的
-i: inodes instead of blocks 显示inodes,而不是blocks(索引节点,而不是块)
-P:以Posix兼容的格式输出;

查看某目录总体占用空间状态:

du:
du [OPTION]... DIR
-h: human-readable 以人类可读的
-s:summary 查看文件的总体大小之和


mdadm:模式化的工具
命令的语法格式:mdadm [mode] <raiddevice> [options] <component-devices>
支持的RAID级别:LINEAR,RAID0,RAID1,RAID4,RAID5,RAID6,RAID10;

模式:
创建:-C
装配:-A
监控:-F
管理:-f,-r,-a

<raiddevice>: /dev/md#
<component-device>:任意块设备

-C:创建模式
-n #:使用#个块设备来创建此RAID;;
-l #:指明要创建的RAID的级别;
-a {yes|no}:自动创建目标RAID设备的设备文件;
-c CHUNK_SIZE:指明块大小;
-x #:指明空闲盘的个数:

-D:显示raid的详细信息:
mdadm -D /dev/md#

管理模式:
-f:标记指定磁盘为损坏;
-a:添加磁盘
-r:移除磁盘

观察md的状态:
cat /por/mdstat

停止md设备
mdadm -S /dev/md#

watch命令:
-n #:刷新间隔,单位是秒:

wach -n# ‘COMMAND‘
LVM: Logical Volume Manager,Version 2 逻辑卷管理器

dm: device mapper(设备映射),将一个或多个底层块设备组织成一个逻辑设备的模块
   /dev/dm-#

dev/mapper/VG_NAME(NAME=LV_NAME)
  /dev/mapper/vol0-root
/dev/VG_NAME/LV_NAME
  /dev/vol0/root

pv管理工具
pvs:简要pv信息显示
pvdisplay:显示pv的详细信息
pvcreate /dev/DEVICE:创建pv
pvremove 删除pv

vg管理工具:
vgs:简要vg信息显示
vgdisplay:显示vg的详细信息

vgcreate [-s](指定PE大小)#[bBsSkKmMgGtTpPeE](单位)  VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...]
vgextend VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...]  添加VG
vgextend VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...]  移除VG
     先做pvmove

vgremove 删除卷组

lv管理工具:
lvs:简要lv信息显示
lvdisplay:显示lv的详细信息

lvcreate -L #[mMgGtT] -n NAME VolumeGroup

lvremove /dev/VG_NAME 删除逻辑卷

扩展逻辑卷:
lvextend -L [+]#[mMgGtT] /dev/VG_NAME/LV_NAME
resize2fs /dev/VG_NAME/LV_NAME 修改逻辑边界

缩减逻辑卷
# umount /dev/VG_NAME/LV_NAME
# e2fsck -f /dev/VG_NAME/LV_NAME 强制检测修复文件系统
# resize2fs /dev/VG_NAME/LV_NAME #[mMgGtT]修改逻辑边界
# lverduce -L [-]#[mMgGtT] /dev/VG_NAME/LV_NAME 修改物理边界
# mount /dev/VG_NAME/LV_NAME /NAME

快照:snapshot
lvcreate -L #[mMgGtT] -p r(只读) -s(指明这是快照) -n(指明快照卷名字) snapshot_lv_name original_lv_name(源卷名字)

1、创建一个10G分区,并格式为ext4文件系统;

[root@localhost ~]# fdisk /dev/sdc
Command (m for help): n #新建分区
Command action
   e   extended
   p   primary partition (1-4)
p #主分区
Partition number (1-4): 1 #主分区号
First cylinder (1-10443, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-10443, default 10443): +10G #创建空间大小

Command (m for help): w  #保存并推出
The partition table has been altered!

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

   (1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl;

   (2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳;

[root@localhost ~]# mke2fs -t ext4 -b 2048 -m 2 -L ‘MYDATA‘ /dev/sdc1

[root@localhost ~]# mkdir /data/mydata

[root@localhost ~]# mount -o noauto,noatime,acl /dev/sdc1 /data/mydata/

[root@localhost ~]# mount
/dev/mapper/VolGroup-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
/dev/md127 on /mydata type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/sdc1 on /data/mydata type ext4 (rw,nodiratime,acl)

2、创建一个大小为1G的swap分区,并创建好文件系统,并启用之;

[root@bogon ~]# fdisk /dev/sdc

WARNING: DOS-compatible mode is deprecated. It‘s strongly recommended to
         switch off the mode (command ‘c‘) and change display units to
         sectors (command ‘u‘).

Command (m for help): n#新建分区

Command action

   e   extended
   p   primary partition (1-4)

p#主分区

Partition number (1-4): 2 #主分区号

First cylinder (1307-10443, default 1307):
Using default value 1307
Last cylinder, +cylinders or +size{K,M,G} (1307-10443, default 10443): +1G #创建空间大小

Command (m for help): t #调整磁盘类型

Partition number (1-4): 2  #主分区号

Hex code (type L to list codes): 82 #swap 类型

Changed system type of partition 2 to 82 (Linux swap / Solaris)


Command (m for help): p  #显示已有分区


Disk /dev/sdc: 85.9 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0dd03239

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1        1306    10490413+  83  Linux
/dev/sdc2            1307        1438     1060290   82  Linux swap / Solaris

Command (m for help): w #写入磁盘并退出

The partition table has been altered!


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

[root@bogon ~]# mkswap /dev/sdc2  #创建交换分区

Setting up swapspace version 1, size = 1060284 KiB

no label, UUID=880e15e9-f1f5-4682-861c-b217266ede96
You have new mail in /var/spool/mail/root
[root@bogon ~]# swapon /dev/sdc2
[root@bogon ~]# free -m

             total       used       free     shared    buffers     cached
Mem:           474        325        149          0         48        152
-/+ buffers/cache:        124        350
Swap:         1995          0       1995
[root@bogon ~]# swapoff /dev/sdc2
[root@bogon ~]# free -m

             total       used       free     shared    buffers     cached
Mem:           474        324        150          0         48        152
-/+ buffers/cache:        123        351
Swap:          959          0        959

3、写一个脚本

   (1)、获取并列出当前系统上的所有磁盘设备;

   (2)、显示每个磁盘设备上每个分区相关的空间使用信息;

[root@bogon bin]# diskfile.sh
All of the disk:
Disk /dev/sda
Disk /dev/sdb
Disk /dev/sdc
Each partition on disk devices related information use of space:
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       19G  5.7G   12G  33% /
tmpfs                 238M     0  238M   0% /dev/shm
/dev/sda1             477M   33M  419M   8% /boot
/dev/md127            9.7G   23M  9.2G   1% /mydata
/dev/sdc1             9.8G   13M  9.6G   1% /data/mydata
[root@bogon bin]# cat diskfile.sh
#!/bin/bash

disks=$(fdisk -l | grep -o ‘^Disk /dev/[sh]d[a-z]‘)
echo -e "All of the disk:\n$disks"
disks_partition=$(fdisk -l | grep -o ‘/dev/[sh]d[a-z][1-9]‘ | df -h)
echo -e "Each partition on disk devices related information use of space:\n$disks_partition"

4、总结RAID的各个级别及其组合方式和性能的不同;

RAID-0:读写性能提升;可用空间:N*min(S1,S2,....);无容错能力;最少2块磁盘。

RAID-1:读性能提升、写性能略有下降;可用空间1*min(s1,s2...);有冗余能力;最少2块磁盘。

RAID-4:有独立的硬盘做校验盘,

RAID-5:读写性能提升;可用空间:(n-1)*min(s1,s2,...);有容错能力,每组镜像最多只能坏一块磁盘;最少3块磁盘。

RAID-6:读写性能提升;可用空间(N-2)*min(s1,s2,...);有容错能力,每组镜像最多只能坏l两块磁盘;最少4块磁盘。

RAID-10:读写性能提升;可用空间:N*min(s1,s2,...)/2;有容错能力,每组镜像最多只能坏一块
最少4块磁盘。

5、创建一个大小为10G的RAID1,要求有一个空闲盘,而且CHUNK大小为128k;

[root@bogon ~]# fdisk /dev/sdc

WARNING: DOS-compatible mode is deprecated. It‘s strongly recommended to
         switch off the mode (command ‘c‘) and change display units to
         sectors (command ‘u‘).

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
e
Partition number (1-4): 3
First cylinder (1439-10443, default 1439):
Using default value 1439
Last cylinder, +cylinders or +size{K,M,G} (1439-10443, default 10443):
Using default value 10443

Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l
First cylinder (1439-10443, default 1439):
Using default value 1439
Last cylinder, +cylinders or +size{K,M,G} (1439-10443, default 10443): +10G

Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l
First cylinder (2745-10443, default 2745):
Using default value 2745
Last cylinder, +cylinders or +size{K,M,G} (2745-10443, default 10443): +10G

Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l
First cylinder (4051-10443, default 4051):
Using default value 4051
Last cylinder, +cylinders or +size{K,M,G} (4051-10443, default 10443): +10G

Command (m for help): p

Disk /dev/sdc: 85.9 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0dd03239

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1        1306    10490413+  83  Linux
/dev/sdc2            1307        1438     1060290   82  Linux swap / Solaris
/dev/sdc3            1439       10443    72332662+   5  Extended
/dev/sdc5            1439        2744    10490413+  83  Linux
/dev/sdc6            2745        4050    10490413+  83  Linux
/dev/sdc7            4051        5356    10490413+  83  Linux

Command (m for help): t
Partition number (1-7): 5
Hex code (type L to list codes): fd
Changed system type of partition 5 to fd (Linux raid autodetect)

Command (m for help): t
Partition number (1-7): 6
Hex code (type L to list codes): fd
Changed system type of partition 6 to fd (Linux raid autodetect)

Command (m for help): t
Partition number (1-7): 7
Hex code (type L to list codes): fd
Changed system type of partition 7 to fd (Linux raid autodetect)

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
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@bogon ~]# partx -a /dev/sdc
[root@bogon ~]# partx -a /dev/sdc

[root@bogon ~]# mdadm -C /dev/md0 -a yes -n 2 -x 1 -l 1 -c 128 /dev/sdc{5,6,7}
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=http://www.mamicode.com/0.90
Continue creating array? y
mdadm: Defaulting to version 1.2 metadata

mdadm: array /dev/md0 started.

[root@bogon ~]# mke2fs -t ext4 /dev/md0
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
655360 inodes, 2620544 blocks
131027 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=2684354560
80 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

正在写入inode表: 完成                            
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 32 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

[root@bogon ~]# mkdir /raid1

[root@bogon ~]# mount /dev/md0 /raid1
[root@bogon ~]# mount

/dev/mapper/VolGroup-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
/dev/md127 on /mydata type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/sdc1 on /data/mydata type ext4 (rw)
/dev/md0 on /raid1 type ext4 (rw)
[root@bogon ~]# blkid /dev/md0
/dev/md0: UUID="1b4e4817-45eb-4597-b062-1b0ee7944cda" TYPE="ext4"
[root@bogon ~]# vi /etc/fstab

#
# /etc/fstab
# Created by anaconda on Sun Jul 31 01:06:03 2016
#
# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root /                       ext4    defaults        1 1
UUID=dee2521e-80a4-4fc1-b3f4-45f742583238 /boot                   ext4    defaults        1 2
/dev/mapper/VolGroup-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
UUID="9cc3186e-2112-4115-a635-376ba327af54" /mydata     ext4    defaults        0 0
UUID="1b4e4817-45eb-4597-b062-1b0ee7944cda"    /raid1    ext4    defaults        0 0

[root@bogon ~]# mdadm -D /dev/md0
/dev/md0:
        Version : 1.2
  Creation Time : Sat Aug 20 04:03:05 2016
     Raid Level : raid1
     Array Size : 10482176 (10.00 GiB 10.73 GB)
  Used Dev Size : 10482176 (10.00 GiB 10.73 GB)
   Raid Devices : 2
  Total Devices : 3
    Persistence : Superblock is persistent

    Update Time : Sat Aug 20 04:07:40 2016
          State : clean
 Active Devices : 2
Working Devices : 3
 Failed Devices : 0
  Spare Devices : 1

           Name : bogon:0  (local to host bogon)
           UUID : fa4366c0:8c0c5848:61ddcd16:14e89429
         Events : 17

    Number   Major   Minor   RaidDevice State
       0       8       37        0      active sync   /dev/sdc5
       1       8       38        1      active sync   /dev/sdc6

       2       8       39        -      spare   /dev/sdc7
root@bogon ~]# df -lh
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       19G  5.8G   12G  34% /
tmpfs                 238M     0  238M   0% /dev/shm
/dev/sda1             477M   33M  419M   8% /boot
/dev/md127            9.7G   23M  9.2G   1% /mydata
/dev/sdc1             9.8G   13M  9.6G   1% /data/mydata
/dev/md0              9.8G   23M  9.2G   1% /raid1

[root@bogon ~]# cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4] [raid1]
md0 : active raid1 sdc7[2](S) sdc6[1] sdc5[0]
      10482176 blocks super 1.2 [2/2] [UU]
      
md127 : active raid5 sdb7[4] sdb8[3] sdb6[6](S) sdb5[5]
      10401792 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/3] [UUU]  

6、创建一个大小为4G的RAID5设备,chunk大小为256k,格式化ext4文件系统,要求可开机自动挂载至/backup目录,而且不更新访问时间戳,且支持acl功能;

[root@bogon ~]# fdisk /dev/sdc

WARNING: DOS-compatible mode is deprecated. It‘s strongly recommended to
         switch off the mode (command ‘c‘) and change display units to
         sectors (command ‘u‘).

Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l
First cylinder (5357-10443, default 5357):
Using default value 5357
Last cylinder, +cylinders or +size{K,M,G} (5357-10443, default 10443): +2G

Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l
First cylinder (5619-10443, default 5619):
Using default value 5619
Last cylinder, +cylinders or +size{K,M,G} (5619-10443, default 10443): +2G

Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l
First cylinder (5881-10443, default 5881): +2
Value out of range.
First cylinder (5881-10443, default 5881):
Using default value 5881
Last cylinder, +cylinders or +size{K,M,G} (5881-10443, default 10443): +2G

Command (m for help): t
Partition number (1-10): 8
Hex code (type L to list codes): fd
Changed system type of partition 8 to fd (Linux raid autodetect)

Command (m for help): t
Partition number (1-10): 9
Hex code (type L to list codes): fd
Changed system type of partition 9 to fd (Linux raid autodetect)

Command (m for help): t
Partition number (1-10): 10
Hex code (type L to list codes): fd
Changed system type of partition 10 to fd (Linux raid autodetect)

Command (m for help): pp^H

Disk /dev/sdc: 85.9 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0dd03239

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1        1306    10490413+  83  Linux
/dev/sdc2            1307        1438     1060290   82  Linux swap / Solaris
/dev/sdc3            1439       10443    72332662+   5  Extended
/dev/sdc5            1439        2744    10490413+  fd  Linux raid autodetect
/dev/sdc6            2745        4050    10490413+  fd  Linux raid autodetect
/dev/sdc7            4051        5356    10490413+  fd  Linux raid autodetect
/dev/sdc8            5357        5618     2104483+  fd  Linux raid autodetect
/dev/sdc9            5619        5880     2104483+  fd  Linux raid autodetect
/dev/sdc10           5881        6142     2104483+  fd  Linux raid autodetect

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
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@bogon ~]# partx -a /dev/sdc
[root@bogon ~]# partx -a /dev/sdc
[root@bogon ~]# mdadm -C /dev/md1 -a yes -n 3 -l 5 -c 256 /dev/sdc{8,9,10}

mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md1 started.
[root@bogon ~]# mke2fs -t ext4 /dev/md1
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=64 blocks, Stripe width=128 blocks
262944 inodes, 1051136 blocks
52556 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=1077936128
33 block groups
32768 blocks per group, 32768 fragments per group
7968 inodes per group
Superblock backups stored on blocks:
    32768, 98304, 163840, 229376, 294912, 819200, 884736

正在写入inode表: 完成                            
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 31 mounts or

180 days, whichever comes first.  Use tune2fs -c or -i to override.

[root@bogon ~]# mdadm -D /dev/md1
/dev/md1:
        Version : 1.2
  Creation Time : Sat Aug 20 04:43:19 2016
     Raid Level : raid5
     Array Size : 4204544 (4.01 GiB 4.31 GB)
  Used Dev Size : 2102272 (2.00 GiB 2.15 GB)
   Raid Devices : 3
  Total Devices : 3
    Persistence : Superblock is persistent

    Update Time : Sat Aug 20 04:48:29 2016
          State : clean
 Active Devices : 3
Working Devices : 3
 Failed Devices : 0
  Spare Devices : 0

         Layout : left-symmetric
     Chunk Size : 256K

           Name : bogon:1  (local to host bogon)
           UUID : d142b0c6:ebe08db0:7863ebb8:ef865982
         Events : 18

    Number   Major   Minor   RaidDevice State
       0       8       40        0      active sync   /dev/sdc8
       1       8       41        1      active sync   /dev/sdc9
       3       8       42        2      active sync   /dev/sdc10
[root@bogon ~]# cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4] [raid1]
md1 : active raid5 sdc10[3] sdc9[1] sdc8[0]
      4204544 blocks super 1.2 level 5, 256k chunk, algorithm 2 [3/3] [UUU]
      
md0 : active raid1 sdc7[2](S) sdc6[1] sdc5[0]
      10482176 blocks super 1.2 [2/2] [UU]
      
md127 : active raid5 sdb7[4] sdb8[3] sdb6[6](S) sdb5[5]
      10401792 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/3] [UUU]
      
unused devices: <none>

[root@bogon ~]# mkdir /backup

[root@bogon ~]# mount -o noatime,acl /dev/md1 /backup

[root@bogon ~]# blkid /dev/md1
/dev/md1: UUID="3c4756db-4d12-414c-8beb-51c8c6b0692d" TYPE="ext4"
[root@bogon ~]# vi /etc/fstab

#
# /etc/fstab
# Created by anaconda on Sun Jul 31 01:06:03 2016
#
# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root /                       ext4    defaults        1 1
UUID=dee2521e-80a4-4fc1-b3f4-45f742583238 /boot                   ext4    defaults        1 2
/dev/mapper/VolGroup-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
UUID="9cc3186e-2112-4115-a635-376ba327af54" /mydata     ext4    defaults        0 0
UUID="1b4e4817-45eb-4597-b062-1b0ee7944cda"    /raid    ext4    defaults        0 0
UUID="3c4756db-4d12-414c-8beb-51c8c6b0692d"   /backup   ext4   noatime,acl    0 0
[root@bogon ~]# df -lh | grep ‘^/dev/md1‘
/dev/md127            9.7G   23M  9.2G   1% /mydata
/dev/md1              3.9G   18M  3.7G   1% /backup
[root@bogon ~]# cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4] [raid1]
md1 : active raid5 sdc10[3] sdc9[1] sdc8[0]
      4204544 blocks super 1.2 level 5, 256k chunk, algorithm 2 [3/3] [UUU]
      
md0 : active raid1 sdc7[2](S) sdc6[1] sdc5[0]
      10482176 blocks super 1.2 [2/2] [UU]
      
md127 : active raid5 sdb7[4] sdb8[3] sdb6[6](S) sdb5[5]
      10401792 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/3] [UUU]
      
unused devices: <none>

7、写一个脚本

   (1) 接受一个以上文件路径作为参数;

   (2) 显示每个文件拥有的行数;

   (3) 总结说明本次共为几个文件统计了其行数;


[root@bogon bin]# cat file1.sh
#!/bin/bash
if [ $# -eq 0 ];then
echo "please input file path!"
#exit 1
fi
for i in $*;do
echo "$i lines is:$(wc -l $i | cut -d‘ ‘ -f1)"
done
echo "------------"
echo "files total number:$#"
[root@bogon bin]# file1.sh /etc/fstab
/etc/fstab lines is:18
------------
files total number:1

8、写一个脚本

   (1) 传递两个以上字符串当作用户名;

   (2) 创建这些用户;且密码同用户名;

   (3) 总结说明共创建了几个用户;

[root@bogon bin]# cat useradd.sh
#!/bin/bash
if [ $# -lt 2 ];then
        echo "please at lease two char!"
        exit 1
fi
for i in $*
do
        useradd $i
    echo $i | passwd --stdin $i
done
echo "Total Add $# users !"
[root@bogon bin]# useradd.sh oio po jkn
更改用户 oio 的密码 。
passwd: 所有的身份验证令牌已经成功更新。
更改用户 po 的密码 。
passwd: 所有的身份验证令牌已经成功更新。
更改用户 jkn 的密码 。
passwd: 所有的身份验证令牌已经成功更新。
Total Add 3 users !

9、写一个脚本,新建20个用户,visitor1-visitor20;计算他们的ID之和;

[root@localhost bin]# cat users.sh
#!/bin/bash
for i in {1..20};do
useradd visitor$i
uid=`grep "\<visitor$i\>" /etc/passwd | cut -d: -f3`
sumid=$[ $sumid + $uid ]
done
echo "ID sum:$sumid"

[root@localhost bin]# users.sh

useradd: user ‘visitor1‘ already exists
useradd: user ‘visitor2‘ already exists
useradd: user ‘visitor3‘ already exists
useradd: user ‘visitor4‘ already exists
useradd: user ‘visitor5‘ already exists
useradd: user ‘visitor6‘ already exists
useradd: user ‘visitor7‘ already exists
useradd: user ‘visitor8‘ already exists
useradd: user ‘visitor9‘ already exists
useradd: user ‘visitor10‘ already exists
useradd: user ‘visitor11‘ already exists
useradd: user ‘visitor12‘ already exists
useradd: user ‘visitor13‘ already exists
useradd: user ‘visitor14‘ already exists
useradd: user ‘visitor15‘ already exists
useradd: user ‘visitor16‘ already exists
useradd: user ‘visitor17‘ already exists
useradd: user ‘visitor18‘ already exists
useradd: user ‘visitor19‘ already exists
useradd: user ‘visitor20‘ already exists
ID sum:10190

10、写一脚本,分别统计/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以#号开头的行数之和,以及总的空白行数;

[root@localhost bin]# cat line.sh
#!/bin/bash
for i in {/etc/rc.d/rc.sysinit,/etc/rc.d/init.d/functions,/etc/fstab};do
line1=$(grep "^#" $i | wc -l)
line2=$(grep "^[[:space:]]" $i | wc -l)
echo "$i # start line total: $line1"
echo "$i space line total: $line2"
done

[root@localhost bin]# bash line.sh

/etc/rc.d/rc.sysinit # start line total: 44
/etc/rc.d/rc.sysinit space line total: 384
/etc/rc.d/init.d/functions # start line total: 40
/etc/rc.d/init.d/functions space line total: 623
/etc/fstab # start line total: 7
/etc/fstab space line total: 0

11、写一个脚本,显示当前系统上所有默认shell为bash的用户的用户名、UID以及此类所有用户的UID之和;

[root@localhost bin]# cat useruid.sh
#!/bin/bash
echo "User And Uid:"
grep "/bin/bash" /etc/passwd | cut -d: -f1,3
for i in $(grep "/bin/bash" /etc/passwd | cut -d: -f3);do
    let sumuid+=$i
done

echo "Uid sum is $sumuid."

[root@localhost bin]# useruid.sh
User And Uid:
root:0
mysql:27
visitor1:500
visitor2:501
visitor3:502
visitor4:503
visitor5:504
visitor6:505
visitor7:506
visitor8:507
visitor9:508
visitor10:509
visitor11:510
visitor12:511
visitor13:512
visitor14:513
visitor15:514
visitor16:515
visitor17:516
visitor18:517
visitor19:518
visitor20:519
Uid sum is 10217.

12、写一个脚本,显示当前系统上所有,拥有附加组的用户的用户名;并说明共有多少个此类用户;

[root@localhost bin]# cat userlist.sh
#!/bin/bash
declare i=1
userlist=`grep "[^:]$" /etc/group | cut -d: -f4`
for username in `cut -d : -f1 /etc/passwd`;do
    echo "$userlist" | grep -o "$username" &> /dev/null
    if [ $? == 0 ];then
        let sum+=i
    fi
    echo "$userlist" | grep -o "$username" | sort -u
done
echo "total users: $sum"
[root@localhost bin]# bash userlist.sh
bin
daemon
adm
mail
haldaemon
postfix
total users: 6
[root@localhost bin]#

13、创建一个由至少两个物理卷组成的大小为20G的卷组;要求,PE大小为8M;而在卷组中创建一个大小为5G的逻辑卷mylv1,格式化为ext4文件系统,开机自动挂载至/users目录,支持acl;

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

WARNING: DOS-compatible mode is deprecated. It‘s strongly recommended to
         switch off the mode (command ‘c‘) and change display units to
         sectors (command ‘u‘).

Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l
First cylinder (6143-10443, default 6143):
Using default value 6143
Last cylinder, +cylinders or +size{K,M,G} (6143-10443, default 10443): +10G

Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l
First cylinder (7449-10443, default 7449):
Using default value 7449
Last cylinder, +cylinders or +size{K,M,G} (7449-10443, default 10443): +10G

Command (m for help): t
Partition number (1-12): 11
Hex code (type L to list codes): 8e
Changed system type of partition 11 to 8e (Linux LVM)

Command (m for help): t
Partition number (1-12): 12
Hex code (type L to list codes): 8e
Changed system type of partition 12 to 8e (Linux LVM)

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
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@localhost ~]# partx -a /dev/sdc # 通知内核重新读取硬盘分区表
[root@localhost ~]# partx -a /dev/sdc # 通知内核重新读取硬盘分区表

[root@localhost ~]# pvcreate /dev/sdc{11,12} # 将分区创建为物理卷

  Physical volume "/dev/sdc11" successfully created

  Physical volume "/dev/sdc12" successfully created

[root@localhost ~]# pvs # 简要查看创建的物理卷  pvdisplay 可查看详细PV信息

  PV         VG       Fmt  Attr PSize  PFree

  /dev/sda2  VolGroup lvm2 a--u 19.51g     0
  /dev/sdc11          lvm2 ---- 10.00g 10.00g
  /dev/sdc12          lvm2 ---- 10.00g 10.00g
[root@localhost ~]# vgcreate -s 8M myvg /dev/sdc{11,12}
  Volume group "myvg" successfully created

[root@localhost ~]# vgdisplay #查看卷组vg的详细信息

  --- Volume group ---
  VG Name               myvg
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               20.00 GiB
  PE Size               8.00 MiB
  Total PE              2560
  Alloc PE / Size       0 / 0   
  Free  PE / Size       2560 / 20.00 GiB
  VG UUID               GeGT7X-iqbB-KtJx-8nbJ-djV8-Dd38-rLPbAy
   
  --- Volume group ---
  VG Name               VolGroup
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               19.51 GiB
  PE Size               4.00 MiB
  Total PE              4994
  Alloc PE / Size       4994 / 19.51 GiB
  Free  PE / Size       0 / 0   
  VG UUID               eP7T48-AO71-yMjl-kRhP-zd9I-onLM-8vFhH1
   
[root@localhost ~]# lvcreate -L 5G -n mylv1 myvg # 在卷组中创建大小为5G的逻辑卷mylv1

  Logical volume "mylv1" created.

[root@localhost ~]# mke2fs -t ext4 /dev/myvg/mylv1 # 将mylv1格式化

mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
    32768, 98304, 163840, 229376, 294912, 819200, 884736

正在写入inode表: 完成                            
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 25 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@localhost ~]# mkdir /users # 创建/users目录
[root@localhost ~]# mount -o acl /dev/myvg/mylv1 /users # 挂载至/users

[root@localhost ~]# vi /etc/fstab  # 设为开机自动挂载至/users
/dev/myvg/mylv1     /users             ext4     defaults,acl     0 0
[root@localhost ~]# mount | grep ‘^/dev/mapper/myvg-mylv1‘ #查看挂载情况
/dev/mapper/myvg-mylv1 on /users type ext4 (rw,acl) # 说明挂载成功
[root@localhost ~]# df -lh #
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root      19G  820M   17G   5% /
tmpfs                                            238M     0  238M   0% /dev/shm
/dev/sda1                                      477M   33M  419M   8% /boot
/dev/md127                                   9.7G   23M  9.2G   1% /mydata
/dev/sdc1                                      9.8G   13M  9.6G   1% /data/mydata
/dev/md0                                      9.8G   23M  9.2G   1% /raid1
/dev/md1                                      3.9G  1.7G  2.1G  45% /backup
/dev/mapper/myvg-mylv1             4.8G   10M  4.6G   1% /users

14、新建用户magedu;其家目录为/users/magedu,而后su切换至此用户,复制多个文件至家目录;

[root@localhost ~]# useradd -d /users/magedu magedu

[root@localhost magedu]# su - magedu

[[root@localhost ~]# cp -r /etc/sysconfig/ /users/magedu/

15、扩展mylv1至9G,确保扩展完成后原有数据完全可用;

[root@localhost ~]# pvs #查看pv信息,看物理卷是否能扩展
  PV         VG       Fmt  Attr PSize  PFree
  /dev/sda2  VolGroup lvm2 a--u 19.51g     0
  /dev/sdc11 myvg     lvm2 a--u 10.00g  5.00g
  /dev/sdc12 myvg     lvm2 a--u 10.00g 10.00g
[root@localhost ~]# vgs #查看vg信息,看卷组是否能扩展
  VG       #PV #LV #SN Attr   VSize  VFree
  VolGroup   1   2   0 wz--n- 19.51g     0
  myvg       2   1   0 wz--n- 20.00g 15.00g
[root@localhost ~]# lvextend -L 9G /dev/myvg/mylv1 #扩展mylv1至9G
  Size of logical volume myvg/mylv1 changed from 5.00 GiB (640 extents) to 9.00 GiB (1152 extents).
  Logical volume mylv1 successfully resized.
[root@localhost ~]# df -lh #查看硬盘信息,/dev/mapper/myvg-mylv1还是5G
Filesystem            Size  Used Avail Use% Mounted on
[root@localhost ~]# pvs #查看pv信息,看物理卷是否能扩展
  PV         VG       Fmt  Attr PSize  PFree
  /dev/sda2  VolGroup lvm2 a--u 19.51g     0
  /dev/sdc11 myvg     lvm2 a--u 10.00g  5.00g
  /dev/sdc12 myvg     lvm2 a--u 10.00g 10.00g
[root@localhost ~]# vgs #查看vg信息,看卷组是否能扩展
  VG       #PV #LV #SN Attr   VSize  VFree
  VolGroup   1   2   0 wz--n- 19.51g     0
  myvg       2   1   0 wz--n- 20.00g 15.00g
[root@localhost ~]# lvextend -L 9G /dev/myvg/mylv1 #扩展mylv1至9G
  Size of logical volume myvg/mylv1 changed from 5.00 GiB (640 extents) to 9.00 GiB (1152 extents).
  Logical volume mylv1 successfully resized.
[root@localhost ~]# df -lh #查看硬盘信息,/dev/mapper/myvg-mylv1还是5G
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root     19G  821M   17G   5% /
tmpfs                                         238M     0  238M   0% /dev/shm
/dev/sda1                                  477M   33M  419M   8% /boot
/dev/md127                                  9.7G   23M  9.2G   1% /mydata
/dev/sdc1                                      9.8G   13M  9.6G   1% /data/mydata
/dev/md0                                      9.8G   23M  9.2G   1% /raid1
/dev/md1                                      3.9G  1.7G  2.1G  45% /backup
/dev/mapper/myvg-mylv1             4.8G   11M  4.6G   1% /users
[root@localhost ~]# resize2fs -p /dev/myvg/mylv1 #通过resize2fs工具来修复文件系统,把新添加扒 文件系统更新,相当于卸载后重新挂载
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/myvg/mylv1 is mounted on /users; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/myvg/mylv1 to 2359296 (4k) blocks.
The filesystem on /dev/myvg/mylv1 is now 2359296 blocks long.

[root@localhost ~]# df -lh #查看硬盘信息,是否更新
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       19G  821M   17G   5% /
tmpfs                 238M     0  238M   0% /dev/shm
/dev/sda1             477M   33M  419M   8% /boot
/dev/md127            9.7G   23M  9.2G   1% /mydata
/dev/sdc1             9.8G   13M  9.6G   1% /data/mydata
/dev/md0              9.8G   23M  9.2G   1% /raid1
/dev/md1              3.9G  1.7G  2.0G  46% /backup
/dev/mapper/myvg-mylv1    8.8G   12M  8.3G   1% /users
[root@localhost ~]# ls /user/magedu/sysconfig/ # 查看magedu用户文件是否丢失
[root@localhost ~]# resize2fs -p /dev/myvg/mylv1 #通过resize2fs工具来修改逻辑边界
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/myvg/mylv1 is mounted on /users; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/myvg/mylv1 to 2359296 (4k) blocks.
The filesystem on /dev/myvg/mylv1 is now 2359296 blocks long.

[root@localhost ~]# df -lh #查看硬盘信息,是否更新
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root    19G  821M   17G   5% /
tmpfs                                         238M     0  238M   0% /dev/shm
/dev/sda1                                   477M   33M  419M   8% /boot
/dev/md127                                9.7G   23M  9.2G   1% /mydata
/dev/sdc1                                   9.8G   13M  9.6G   1% /data/mydata
/dev/md0                                   9.8G   23M  9.2G   1% /raid1
/dev/md1                                   3.9G  1.7G  2.0G  46% /backup
/dev/mapper/myvg-mylv1          8.8G   12M  8.3G   1% /users
[root@localhost ~]# ls /user/magedu/sysconfig/ # 查看magedu用户文件是否丢失

16、缩减mylv1至7G,确保缩减完成后原有数据完全可用;

[root@localhost ~]# umount /dev/myvg/mylv1 #卸载分区
[root@localhost ~]# resize2fs /dev/myvg/mylv1 7G #缩减逻辑卷大小

resize2fs 1.41.12 (17-May-2010)
请先运行 ‘e2fsck -f /dev/myvg/mylv1‘.

[root@localhost ~]# e2fsck -f /dev/myvg/mylv1
e2fsck 1.41.12 (17-May-2010)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第3步: 检查目录连接性
Pass 4: Checking reference counts
第5步: 检查簇概要信息
/dev/myvg/mylv1: 15/589824 files (0.0% non-contiguous), 72675/2359296 blocks
[root@localhost ~]# resize2fs /dev/myvg/mylv1 7G #缩减逻辑卷大小
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/myvg/mylv1 to 1835008 (4k) blocks.
The filesystem on /dev/myvg/mylv1 is now 1835008 blocks long.

[root@localhost ~]# e2fsck -f /dev/myvg/mylv1
e2fsck 1.41.12 (17-May-2010)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第3步: 检查目录连接性
Pass 4: Checking reference counts
第5步: 检查簇概要信息
/dev/myvg/mylv1: 15/458752 files (0.0% non-contiguous), 64451/1835008 blocks
[root@localhost ~]# lvreduce -L 7G /dev/myvg/mylv1 #缩减逻辑卷lv物理边界大小至3G
  WARNING: Reducing active logical volume to 7.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce myvg/mylv1? [y/n]: y
  Size of logical volume myvg/mylv1 changed from 9.00 GiB (1152 extents) to 7.00 GiB (896 extents).
  Logical volume mylv1 successfully resized.
[root@localhost ~]# lvs #查看逻辑卷
  LV      VG       Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv_root VolGroup -wi-ao----  18.57g                                                    
  lv_swap VolGroup -wi-ao---- 960.00m                                                    
  mylv1   myvg     -wi-a-----   7.00g                                                    
[root@localhost ~]# mount /dev/myvg/mylv1 /users/
[root@localhost ~]# ls /users

lost+found  magedu
[root@localhost ~]# ls /users/magedu/sysconfig/

17、对mylv1创建快照,并通过备份数据;要求保留原有的属主属组等信息;

[root@localhost /]# cp -r /etc/opt/ /users/
[root@localhost /]# lvcreate -s -L 30M -n snapLv -p r /dev/myvg/mylv1 #创建快照-s,-L指定快照.小为30M,-n指定快照名为snapLv,-p r只读权限。
  Rounding up size to full physical extent 32.00 MiB
  Logical volume "snapLv" created.
[root@localhost /]# mkdir /snapsh
[root@localhost ~]# mount /dev/myvg/snapLv /snapsh #将快照文件挂载至/snapsh

mount: block device /dev/mapper/myvg-snapLv is write-protected, mounting read-only
[root@localhost ~]# cd /snapsh/
[root@localhost snapsh]# ls
lost+found  magedu  opt
[root@localhost ~]# cat /snapsh/magedu/sysconfig/grub
boot=/dev/sda
forcelba=0

[root@localhost ~]# vi /users/magedu/sysconfig/grub  # 编辑源卷的grub文件

[root@localhost ~]# cat /users/magedu/sysconfig/grub # 查看修改后的grub文件

boot=/dev/sda
forcelba=0
welcome new line! #新增
[root@localhost ~]# cd /snapsh/

[root@localhost snapsh]# pwd # 再查看/snapsh下快照里面的grub

/snapsh

[root@localhost snapsh]# cat /snapsh/magedu/sysconfig/grub  # 文件内容没有变化

boot=/dev/sda

forcelba=0
[root@localhost snapsh]# cp -r /etc/init.d/ /users/ #复制某文件至/users
[root@localhost snapsh]# cd /users/
[root@localhost users]# ll # /users源卷仍可以新增文件等操作

总用量 32
-rw-r--r--. 1 root   root    1050 8月  20 21:20 fstab
drwxr-xr-x. 2 root   root    4096 8月  20 21:34 init.d
drwx------. 2 root   root   16384 8月  20 18:55 lost+found
drwx------. 3 magedu magedu  4096 8月  20 21:06 magedu
drwxr-xr-x. 2 root   root    4096 8月  20 21:10 opt


2016-9-20第七周作业