首页 > 代码库 > 2016-9-20第七周作业
2016-9-20第七周作业
fdisk:对于一块硬盘来讲,最多只能管理15个分区 fdisk -l [-u] [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 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 |
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 extendedp 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 #显示已有分区 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-b217266ede96You 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 [root@bogon ~]# mkdir /raid1 [root@bogon ~]# mount /dev/md0 /raid1 # [root@bogon ~]# mdadm -D /dev/md0 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 [root@bogon ~]# mkdir /backup [root@bogon ~]# mount -o noatime,acl /dev/md1 /backup [root@bogon ~]# blkid /dev/md1 # |
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 [root@localhost bin]# users.sh useradd: user ‘visitor1‘ already existsuseradd: 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 [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 |
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 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/sdaforcelba=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第七周作业