马哥教育网络班21期+第7周课程练习
-
创建一个10G分区,并格式为ext4文件系统;
a.要求block大小为2048,预留空间百分比为2,卷标为MYDATA,默认挂载属性包含acl;
b.挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不能更新文件的访问时间戳;
# 创建10G的分区 [root@CentOS-7 ~]# fdisk /dev/sda Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): n Partition type: p primary (2 primary, 0 extended, 2 free) e extended Select (default p): p Partition number (3,4, default 3): 3 First sector (25591808-251658239, default 25591808): Using default value 25591808 Last sector, +sectors or +size{K,M,G} (25591808-251658239, default 251658239): +10G Partition 3 of type Linux and of size 10 GiB is set 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: Device or resource busy. 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@CentOS-7 ~]# partx -a /dev/sda partx: /dev/sda: error adding partitions 1-2 [root@CentOS-7 ~]# partx -a /dev/sda partx: /dev/sda: error adding partitions 1-3 [root@CentOS-7 ~]# cat /proc/partitions major minor #blocks name 8 0 125829120 sda 8 1 204800 sda1 8 2 12590080 sda2 8 3 10485760 sda3 11 0 1048575 sr0 253 0 10485760 dm-0 253 1 2097152 dm-1 # 格式化为ext4,block大小为2048,预留空间百分比为2,卷标为MYDATA; [root@CentOS-7 ~]# mke2fs -t ext4 -b 2048 -m 2 -L "MYDATA" /dev/sda3 # 创建挂载目录 [root@CentOS-7 ~]# mkdir -p /data/mydata # 默认挂载属性包含acl,挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不能更新文件的访问时间戳; [root@CentOS-7 ~]# mount -t ext4 -o noexec,noatime,acl /dev/sda3 /data/mydata/
2. 创建一个大小为1G的swap分区,并创建好文件系统,并启用之;
# 首先新建1G的swap类型分区 #先创建逻辑分区,使用所有空间,然后在逻辑分区上创建1G swap分区; [root@CentOS-7 ~]# fdisk /dev/sda Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): n Partition type: p primary (3 primary, 0 extended, 1 free) e extended Select (default e): e Selected partition 4 First sector (46563328-251658239, default 46563328): Using default value 46563328 Last sector, +sectors or +size{K,M,G} (46563328-251658239, default 251658239): Using default value 251658239 Partition 4 of type Extended and of size 97.8 GiB is set Command (m for help): n All primary partitions are in use Adding logical partition 5 First sector (46565376-251658239, default 46565376): Using default value 46565376 Last sector, +sectors or +size{K,M,G} (46565376-251658239, default 251658239): +1G Partition 5 of type Linux and of size 1 GiB is set Command (m for help): l 0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris 1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT- 2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT- 3 XENIX usr 3c PartitionMagic 84 OS/2 hidden C: c6 DRDOS/sec (FAT- 4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx 5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data 6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / . 7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility 8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt 9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi eb BeOS fs e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD ee GPT f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/ 10 OPUS 55 EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b 11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f1 SpeedStor 12 Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor 14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f2 DOS secondary 16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS 17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE 18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto 1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep 1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT 1e Hidden W95 FAT1 80 Old Minix Command (m for help): t Partition number (1-5, default 5): 5 Hex code (type L to list all codes): 82 Changed type of partition 'Linux' to 'Linux swap / Solaris' 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: Device or resource busy. 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@CentOS-7 ~]# partx -a /dev/sda partx: /dev/sda: error adding partitions 1-3 [root@CentOS-7 ~]# partx -a /dev/sda partx: /dev/sda: error adding partitions 1-5 [root@CentOS-7 ~]# cat /proc/partitions major minor #blocks name 8 0 125829120 sda 8 1 204800 sda1 8 2 12590080 sda2 8 3 10485760 sda3 8 4 1 sda4 8 5 1048576 sda5 11 0 1048575 sr0 253 0 10485760 dm-0 253 1 2097152 dm-1 # 创建swap文件系统并启用 [root@CentOS-7 ~]# free -m total used free shared buff/cache available Mem: 977 552 96 16 328 197 Swap: 2047 57 1990 [root@CentOS-7 ~]# [root@CentOS-7 ~]# mkswap /dev/sda5 Setting up swapspace version 1, size = 1048572 KiB no label, UUID=cd5c8885-e09e-478d-8915-275ff2ba2d0f [root@CentOS-7 ~]# swapon /dev/sda5 [root@CentOS-7 ~]# free -m total used free shared buff/cache available Mem: 977 553 95 16 328 196 Swap: 3071 57 3014
3. 写一个脚本
a. 获取并列出当前系统上所有磁盘设备;
b.显示每个磁盘设备上每个分区相关的空间使用信息;
#!/bin/bash Disk_name=$(fdisk -l | awk -F'[ :]+' '/^Disk \/dev\/sd?/ {print $2}') for i in ${Disk_name}; do fdisk -l $i done
4. 总结RAID各个级别及其组合方式和性能的不同;
5. 创建一个大小为10G的RAID1,要求有一个空闲盘,而且CHUNK大小为128k;
# 首先在虚拟机添加3块10G的磁盘 [root@CentOS-7 ~]# fdisk -l /dev/sd[b-d] | grep '^Disk /dev' Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors Disk /dev/sdc: 10.7 GB, 10737418240 bytes, 20971520 sectors Disk /dev/sdd: 10.7 GB, 10737418240 bytes, 20971520 sectors # 创建3个新分区 [root@CentOS-7 ~]# fdisk /dev/sdb Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): First sector (2048-20971519, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): Using default value 20971519 Partition 1 of type Linux and of size 10 GiB is set Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@CentOS-7 ~]# fdisk /dev/sdc [root@CentOS-7 ~]# fdisk /dev/sdd # 10G的RAID1,要求有一个空闲盘,而且CHUNK大小为128k; [root@CentOS-7 ~]# partx -a /dev/sdb partx: /dev/sdb: error adding partition 1 [root@CentOS-7 ~]# partx -a /dev/sdc partx: /dev/sdc: error adding partition 1 [root@CentOS-7 ~]# partx -a /dev/sdd partx: /dev/sdd: error adding partition 1 [root@CentOS-7 ~]# mdadm -C /dev/md0 -n 2 -x 1 -l 1 -a yes -c 128 /dev/sd{b1,c1,d1} 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@CentOS-7 ~]# mdadm -D /dev/md0 /dev/md0: Version : 1.2 Creation Time : Sun Aug 21 17:24:21 2016 Raid Level : raid1 Array Size : 10476544 (9.99 GiB 10.73 GB) Used Dev Size : 10476544 (9.99 GiB 10.73 GB) Raid Devices : 2 Total Devices : 3 Persistence : Superblock is persistent Update Time : Sun Aug 21 17:25:14 2016 State : clean Active Devices : 2 Working Devices : 3 Failed Devices : 0 Spare Devices : 1 Name : CentOS-7:0 (local to host CentOS-7) UUID : 3520aab9:bf556c43:c26a1a76:c634588a Events : 17 Number Major Minor RaidDevice State 0 8 17 0 active sync /dev/sdb1 1 8 33 1 active sync /dev/sdc1 2 8 49 - spare /dev/sdd1
6. 创建一个大小为4G的RAID5设备,chunk大小为256k,格式化为ext4文件系统,要求开机可自动挂载至/backup目录,而且不更新访问时间戳,且支持acl功能;
# 首先新建4个2G的分区sda[5-8],将每个分区看作是一个独立的磁盘,一个作为空闲盘 # RAID种每个磁盘大小可以不相同(但为了不浪费空间,建议磁盘大小相同) [root@CentOS-7 ~]# fdisk -l /dev/sda Disk /dev/sda: 128.8 GB, 128849018880 bytes, 251658240 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x0004aa3c Device Boot Start End Blocks Id System /dev/sda1 * 2048 411647 204800 83 Linux /dev/sda2 411648 25591807 12590080 8e Linux LVM /dev/sda3 25591808 251658239 113033216 5 Extended /dev/sda5 25593856 29788159 2097152 83 Linux /dev/sda6 29790208 33984511 2097152 83 Linux /dev/sda7 33986560 38180863 2097152 83 Linux /dev/sda8 38182912 42377215 2097152 83 Linux [root@CentOS-7 ~]# mdadm -C /dev/md1 -n 3 -l 5 -a yes -c 256 -x 1 /dev/sda{5,6,7,8} mdadm: cannot open /dev/sda5: No such file or directory [root@CentOS-7 ~]# partx -a /dev/sda partx: /dev/sda: error adding partitions 1-2 [root@CentOS-7 ~]# partx -a /dev/sda partx: /dev/sda: error adding partitions 1-3 partx: /dev/sda: error adding partitions 5-8 [root@CentOS-7 ~]# cat /proc/partitions major minor #blocks name 8 0 125829120 sda 8 1 204800 sda1 8 2 12590080 sda2 8 3 1 sda3 8 5 2097152 sda5 8 6 2097152 sda6 8 7 2097152 sda7 8 8 2097152 sda8 8 16 10485760 sdb 8 17 10484736 sdb1 8 32 10485760 sdc 8 33 10484736 sdc1 8 48 10485760 sdd 8 49 10484736 sdd1 11 0 4228096 sr0 253 0 10485760 dm-0 253 1 2097152 dm-1 9 0 10476544 md0 [root@CentOS-7 ~]# mdadm -C /dev/md1 -n 3 -l 5 -a yes -c 256 -x 1 /dev/sda{5,6,7,8} mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md1 started. [root@CentOS-7 ~]# mdadm -D /dev/md1 /dev/md1: Version : 1.2 Creation Time : Sun Aug 21 17:56:32 2016 Raid Level : raid5 Array Size : 4190208 (4.00 GiB 4.29 GB) Used Dev Size : 2095104 (2046.34 MiB 2145.39 MB) Raid Devices : 3 Total Devices : 4 Persistence : Superblock is persistent Update Time : Sun Aug 21 17:56:43 2016 State : clean, degraded, recovering Active Devices : 2 Working Devices : 4 Failed Devices : 0 Spare Devices : 2 Layout : left-symmetric Chunk Size : 256K Rebuild Status : 18% complete Name : CentOS-7:1 (local to host CentOS-7) UUID : 0f18450d:cc3b25ac:d7034547:b1f0821c Events : 3 Number Major Minor RaidDevice State 0 8 5 0 active sync /dev/sda5 1 8 6 1 active sync /dev/sda6 4 8 7 2 spare rebuilding /dev/sda7 3 8 8 - spare /dev/sda8 # 格式化为ext4文件系统,要求开机可自动挂载至/backup目录,而且不更新访问时间戳,且支持acl功能; [root@CentOS-7 ~]# mke2fs -t ext4 /dev/md1 [root@CentOS-7 ~]# mkdir -p /backup [root@CentOS-7 ~]# blkid /dev/md1 /dev/md1: UUID="9de8fb0c-2211-4669-8a3b-a342c182e252" TYPE="ext4" [root@CentOS-7 ~]# echo "UUID="9de8fb0c-2211-4669-8a3b-a342c182e252" /backup ext4 defaults,noatime,acl 0 0" >> /etc/fstab [root@CentOS-7 ~]# mount -a [root@CentOS-7 ~]# df -h | grep backup /dev/md1 3.9G 16M 3.7G 1% /backup
7. 写一个脚本
a. 接收一个以上文件路径作为参数;
b. 显示每个文件拥有的行数;
c. 总结说明本次共为几个文件统计了行数;
[root@rhel-6-2 tmp]# cat 1.sh #!/bin/bash if [[ $# -eq 0 ]]; then echo -e "\nusage: sh $0 <FILE...>\n" exit 1 else for i in "$@"; do wc -l ${i} done echo "Total number of files: $#" fi [root@rhel-6-2 tmp]# sh 1.sh usage: sh 1.sh <FILE...> [root@rhel-6-2 tmp]# sh 1.sh /etc/fstab /etc/hosts 15 /etc/fstab 3 /etc/hosts Total number of files: 2
8. 写一个脚本
a. 传递两个以上字符串当作用户名;
b. 创建这些用户,且密码同用户名;
c. 总结说明共创建了几个用户;
[root@rhel-6-2 tmp]# cat 2.sh #!/bin/bash Num_user=0 if [[ $# -le 1 ]]; then echo -e "\nusage: sh $0 <arg1> <arg2>..." echo -e "Pls enter at least two strings.\n" exit 1 else for i in "$@"; do useradd ${i} &>/dev/null if [[ $? -eq 0 ]]; then let Num_user+=1 echo "${i}" | passwd --stdin ${i} &>/dev/null fi done echo "The number of users created: ${Num_user}" echo -e "User lists:" echo "$*" | sed -r 's/[[:space:]]+/\n/g' fi [root@rhel-6-2 tmp]# sh 2.sh usage: sh 2.sh <arg1> <arg2>... Pls enter at least two strings. [root@rhel-6-2 tmp]# sh 2.sh user1 user2 The number of users created: 2 User lists: user1 user2
9. 写一个脚本,新建20个用户,visitor1-visitor20;计算他们的ID之和。
[root@rhel-6-2 tmp]# cat 3.sh #!/bin/bash Sum=0 for i in visitor{1..20}; do useradd $i let Sum+=$(id -u $i) done echo "The sum of UID: $Sum" [root@rhel-6-2 tmp]# sh 3.sh The sum of UID: 10230
10. 写一个脚本,分别统计/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions、/etc/fstab文件中以#开头的行数之和以及总的空白行数。
[root@rhel-6-2 init.d]# cat 3.sh #!/bin/bash if [[ $# -ne 1 ]]; then echo 'Pls enter only one argument.' else echo "The number of #: $(grep '^#' $1 | wc -l)" echo "The number of blank lines: $(grep '^$' $1 | wc -l)" fi [root@rhel-6-2 init.d]# sh 3.sh Pls enter only one argument. [root@rhel-6-2 init.d]# sh 3.sh /etc/rc.d/rc.sysinit The number of #: 44 The number of blank lines: 96 [root@rhel-6-2 init.d]# sh 3.sh /etc/rc.d/init.d/functions The number of #: 40 The number of blank lines: 72 [root@rhel-6-2 init.d]# sh 3.sh /etc/fstab The number of #: 7 The number of blank lines: 1
11. 写一个脚本,显示当前系统上所有默认shell为bash的用户的用户名,UID以及此类所有用户的UID之和。
[root@rhel-6-2 init.d]# cat 4.sh #!/bin/bash Sum_uid=0 echo "UserName:UID lists" for i in $(grep '\<bash\>$' /etc/passwd | cut -d : -f1); do echo "${i}:$(id -u ${i})" let Sum_uid+=$(id -u ${i}) done echo "The sum of UID: $Sum_uid" [root@rhel-6-2 init.d]# sh 4.sh UserName:UID lists root:0 user1:500 user2:501 visitor1:502 ... ... visitor20:521 The sum of UID: 11231
12. 写一个脚本,显示系统上所有拥有附加组的用户的用户名,并说明共有多少个此类用户。
[root@rhel-6-2 tmp]# cat 5.sh #!/bin/bash Sum_user=0 echo "User list:" while read line; do User_name=$(echo ${line} | cut -d: -f1) Num=$(id -G ${User_name} | wc -w) if [[ ${Num} -eq 2 ]]; then echo "${User_name}" let Sum_user+=1 fi done < /etc/passwd echo "The number of user: ${Sum_user}" [root@rhel-6-2 tmp]# sh 5.sh User list: adm postfix user1 user2 The number of user: 4
13. 创建一个由至少两个物理卷组成的大小为20G的卷组;要求PE大小为8M;在卷组中创建一个大小为5G的逻辑卷mylv1,格式化为ext4文件系统,开机自动挂载至/users目录,支持acl;
# 查看现有磁盘,使用sdb,sdc创建20G卷组并完成实验 [root@CentOS-7 ~]# fdisk -l /dev/sd[b-c] Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk /dev/sdc: 10.7 GB, 10737418240 bytes, 20971520 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes # 首先创建分区并修改分区类型未lvm [root@CentOS-7 ~]# fdisk /dev/sdb Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Device does not contain a recognized partition table Building a new DOS disklabel with disk identifier 0x8a4a0c3b. Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): First sector (2048-20971519, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): Using default value 20971519 Partition 1 of type Linux and of size 10 GiB is set Command (m for help): t Selected partition 1 Hex code (type L to list all codes): 8e Changed type of partition 'Linux' to 'Linux LVM' Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. #同样操作sdc [root@CentOS-7 ~]# fdisk /dev/sdc [root@CentOS-7 ~]# partx -a /dev/sdb partx: /dev/sdb: error adding partition 1 [root@CentOS-7 ~]# partx -a /dev/sdc partx: /dev/sdc: error adding partition 1 [root@CentOS-7 ~]# cat /proc/partitions major minor #blocks name 8 16 10485760 sdb 8 17 10484736 sdb1 8 32 10485760 sdc 8 33 10484736 sdc1 8 0 125829120 sda 8 1 204800 sda1 8 2 12590080 sda2 11 0 4228096 sr0 253 0 10485760 dm-0 253 1 2097152 dm-1 # 查看分区状态 [root@CentOS-7 ~]# fdisk -l /dev/sd[b-c] Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x8a4a0c3b Device Boot Start End Blocks Id System /dev/sdb1 2048 20971519 10484736 8e Linux LVM Disk /dev/sdc: 10.7 GB, 10737418240 bytes, 20971520 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0xeef74501 Device Boot Start End Blocks Id System /dev/sdc1 2048 20971519 10484736 8e Linux LVM # 创建物理卷 [root@CentOS-7 ~]# pvcreate /dev/sdb1 Physical volume "/dev/sdb1" successfully created [root@CentOS-7 ~]# pvcreate /dev/sdc1 Physical volume "/dev/sdc1" successfully created # 查看物理卷状态 [root@CentOS-7 ~]# pvs [root@CentOS-7 ~]# pvdisplay # 创建卷组,要求PE大小为8M [root@CentOS-7 ~]# vgcreate myvg -s 8M /dev/sdb1 /dev/sdc1 Volume group "myvg" successfully created # 查看卷组状态 [root@CentOS-7 ~]# vgs myvg VG #PV #LV #SN Attr VSize VFree myvg 2 0 0 wz--n- 19.98g 19.98g [root@CentOS-7 ~]# vgdisplay myvg --- 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 19.98 GiB PE Size 8.00 MiB Total PE 2558 Alloc PE / Size 0 / 0 Free PE / Size 2558 / 19.98 GiB VG UUID 8jRp4h-2YgZ-34EB-d7Ld-LwW7-cd1f-nHxmjl # 在卷组myvg中创建一个大小为5G的逻辑卷mylv1; [root@CentOS-7 ~]# lvcreate -L 5G -n mylv1 myvg Logical volume "mylv1" created. [root@CentOS-7 ~]# ls /dev/mapper/ centos_centos--7-root centos_centos--7-swap control myvg-mylv1 # 查看lv状态 [root@CentOS-7 ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert root centos_centos-7 -wi-ao---- 10.00g swap centos_centos-7 -wi-ao---- 2.00g mylv1 myvg -wi-a----- 5.00g [root@CentOS-7 ~]# lvs /dev/myvg/mylv1 LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mylv1 myvg -wi-a----- 5.00g # 查看lv详细状态 lvdisplay [path/of/lv] [root@CentOS-7 ~]# lvdisplay /dev/myvg/mylv1 --- Logical volume --- LV Path /dev/myvg/mylv1 LV Name mylv1 VG Name myvg LV UUID XA0VWt-DbCi-VfXX-30Eu-06IQ-GPG3-sTpkwE LV Write Access read/write LV Creation host, time CentOS-7, 2016-08-21 15:00:58 +0800 LV Status available # open 0 LV Size 5.00 GiB Current LE 640 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:2 # 将新建lv格式化为ext4文件系统,开机自动挂载至/users目录,支持acl; [root@CentOS-7 ~]# mke2fs -t ext4 -L mylv1 /dev/myvg/mylv1 # 创建挂载点并配置开机自动挂载; # 首先查看分区信息 [root@CentOS-7 ~]# blkid /dev/myvg/mylv1 /dev/myvg/mylv1: LABEL="mylv1" UUID="e9508003-f147-408b-a93c-11c2e6820380" TYPE="ext4" [root@CentOS-7 ~]# mkdir -p /users # /etc/fstab中UUID=XXX也可替换为blkid输出中的/dev/myvg/mylv1或LABEL="mylv1" [root@CentOS-7 ~]# echo "UUID="e9508003-f147-408b-a93c-11c2e6820380" /users ext4 defaults,acl 0 0" >> /etc/fstab [root@CentOS-7 ~]# mount -a [root@CentOS-7 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos_centos--7-root 10G 4.7G 5.4G 47% / devtmpfs 474M 0 474M 0% /dev tmpfs 489M 84K 489M 1% /dev/shm tmpfs 489M 7.1M 482M 2% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda1 197M 127M 71M 65% /boot tmpfs 98M 20K 98M 1% /run/user/42 tmpfs 98M 0 98M 0% /run/user/0 /dev/mapper/myvg-mylv1 4.8G 20M 4.6G 1% /users
14. 新建用户magedu;其家目录为/users/magedu,而后切换至此用户,复制多个文件至家目录;
[magedu@CentOS-7 ~]$ cp /etc/hosts /etc/fstab ~ [magedu@CentOS-7 ~]$ ll /users/magedu/ 总用量 8 -rw-r--r--. 1 magedu magedu 528 8月 21 15:31 fstab -rw-r--r--. 1 magedu magedu 180 8月 21 15:31 hosts
15. 扩展mylv1至9G,确保扩展完成后所有原数据完全可用;
[root@CentOS-7 ~]# df -h | grep /users /dev/mapper/myvg-mylv1 4.8G 21M 4.6G 1% /users [root@CentOS-7 ~]# lvextend -L 9G /dev/myvg/mylv1 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@CentOS-7 ~]# df -h | grep /users /dev/mapper/myvg-mylv1 4.8G 21M 4.6G 1% /users # 扩展文件系统到指定大小 [root@CentOS-7 ~]# resize2fs /dev/myvg/mylv1 resize2fs 1.42.9 (28-Dec-2013) Filesystem at /dev/myvg/mylv1 is mounted on /users; on-line resizing required old_desc_blocks = 1, new_desc_blocks = 2 The filesystem on /dev/myvg/mylv1 is now 2359296 blocks long. [root@CentOS-7 ~]# df -h | grep /users /dev/mapper/myvg-mylv1 8.8G 23M 8.3G 1% /users # 检查文件是否丢失 [root@CentOS-7 ~]# ll /users/magedu/ total 8 -rw-r--r--. 1 magedu magedu 528 Aug 21 15:32 fstab -rw-r--r--. 1 magedu magedu 180 Aug 21 15:32 hosts [root@CentOS-7 ~]# cat /users/magedu/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.10.10.5 CentOS-7
16. 缩减mylv1至7G,确保缩减完成后原有数据完全可用;
# 卸载文件系统 [root@CentOS-7 ~]# umount /dev/myvg/mylv1 # 强制进行文件系统检测、修复 [root@CentOS-7 ~]# e2fsck -f /dev/myvg/mylv1 # 缩减文件系统到指定大小 [root@CentOS-7 ~]# resize2fs /dev/myvg/mylv1 7G # 缩减逻辑卷 [root@CentOS-7 ~]# lvreduce -L 7G /dev/myvg/mylv1 WARNING: Reducing active logical volume to 7.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce 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@CentOS-7 ~]# mount -a [root@CentOS-7 ~]# !df df -h | grep /users /dev/mapper/myvg-mylv1 6.8G 23M 6.5G 1% /users # 查看该文件系统的文件是否损坏 [root@CentOS-7 ~]# ll /users/magedu/ total 8 -rw-r--r--. 1 magedu magedu 528 Aug 21 15:32 fstab -rw-r--r--. 1 magedu magedu 180 Aug 21 15:32 hosts [root@CentOS-7 ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.10.10.5 CentOS-7
17. 对mylv1创建快照,并通过备份数据;要求保留原有的属主和属组等信息;
[root@CentOS-7 ~]# lvcreate -s -L 10M -n mylv1-snap -p r /dev/myvg/mylv1 Rounding up size to full physical extent 16.00 MiB Logical volume "mylv1-snap" created. [root@CentOS-7 ~]# mount /dev/myvg/mylv1-snap /mnt/ mount: /dev/mapper/myvg-mylv1--snap is write-protected, mounting read-only [root@CentOS-7 ~]# tree /mnt/ /mnt/ |-- lost+found `-- magedu |-- fstab `-- hosts 2 directories, 2 files [root@CentOS-7 ~]# mkdir -p /tmp/mylv1/ [root@CentOS-7 ~]# cp -a /mnt/* /tmp/mylv1/ [root@CentOS-7 ~]# tree /tmp/mylv1/ /tmp/mylv1/ |-- lost+found `-- magedu |-- fstab `-- hosts 2 directories, 2 files [root@CentOS-7 ~]# cat /tmp/mylv1/magedu/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.10.10.5 CentOS-7 # 备份完成无误后,卸载并删除快照 [root@CentOS-7 ~]# umount /mnt/ [root@CentOS-7 ~]# lvremove /dev/myvg/mylv1-snap Do you really want to remove active logical volume mylv1-snap? [y/n]: y Logical volume "mylv1-snap" successfully removed
原创文章,作者:N21_未来人,如若转载,请注明出处:http://www.178linux.com/38525
评论列表(1条)
写的很好,排版也很棒,加油