1、创建一个10G分区,并格式为ext4文件系统;
(1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl;
[root@localhost ~]# fdisk /dev/sdc # 创建一个10G分区 Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel , Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) 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): 1 First cylinder (1-121583, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-121583, default 121583): +10G Command (m for help): p Disk /dev/sdc: 1000.1 GB, 1000056291328 bytes 255 heads, 63 sectors/track, 121583 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: 0xe34b702b Device Boot Start End Blocks Id System /dev/sdc1 1 1306 10490413+ 83 Linux Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@localhost ~]# mke2fs -t ext4 -b 2048 -m 2 -L 'MYDATA' -O acl /dev/sdc1 mke2fs 1.41.12 (17-May-2010) #其中的-O是启动指定特性 Invalid filesystem option set: acl # 提示:无效的文件系统选项设置,说明设置失败 [root@localhost ~]# mke2fs -t ext4 -b 2048 -m 2 -L 'MYDATA' /dev/sdc1 # -t指定格式为ext4,-b 2048 指定block大小为2048, -m 2预留空间百分比为2, # -L 'MYDATA'卷标为MYDATA, 默认挂载属性包含acl; mke2fs 1.41.12 (17-May-2010) Filesystem label=MYDATA OS type: Linux Block size=2048 (log=1) Fragment size=2048 (log=1) Stride=0 blocks, Stripe width=0 blocks 657408 inodes, 5245206 blocks 104904 blocks (2.00%) reserved for the super user First data block=0 Maximum filesystem blocks=543162368 321 block groups 16384 blocks per group, 16384 fragments per group 2048 inodes per group Superblock backups stored on blocks: 16384, 49152, 81920, 114688, 147456, 409600, 442368, 802816, 1327104, 2048000, 3981312 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 37 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [root@localhost ~]# tune2fs -o acl /dev/sdc1 # 其中-o acl调整文件系统的默认挂载选项为acl tune2fs 1.41.12 (17-May-2010)
(2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳;
[root@localhost ~]# mount -o noexec,noatime /dev/sdc1 /data/mydata # 挂载时noexec禁止程序自动运行,noatime不更新文件的访问时间戳 # 通过查看/etc/mtab文件显示当前文件已挂载的设备信息 /dev/sdc1 on /data/mydata type ext4 (rw,noexec,noatime) [root@localhost ~]# cat /proc/mounts | grep '/dev/sdc1' # 查看内核追踪到的已挂载的所有设备信息 /dev/sdc1 /data/mydata ext4 rw,seclabel,noexec,noatime,barrier=1,data=ordered 0 0 [root@localhost ~]# mount | grep '/dev/sdc1'
2、创建一个大小为1G的swap分区,并创建好文件系统,并启用之;
(动态调整交换分区大小)
第一种方法:新建分区,将新建的分区增加到交换分区
[root@localhost ~]# fdisk /dev/sdb # 创建一个1G的swap分区 Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0xadfeb0da. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) 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'). #注意:分出一个区做为交换分区,分区类型应为primary,extend不能做为交换分区。 Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-2610, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +1G Command (m for help): t # 转换为82 (Linux swap / Solaris) Selected partition 1 Hex code (type L to list codes): 82 Changed system type of partition 1 to 82 (Linux swap / Solaris) Command (m for help): p # 查看分区情况 Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 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: 0xadfeb0da Device Boot Start End Blocks Id System /dev/sdb1 1 132 1060258+ 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@localhost ~]# partx -a /dev/sdb # 通知内核重新读取硬盘分区表 BLKPG: Device or resource busy error adding partition 1 [root@localhost ~]# partx -a /dev/sdb BLKPG: Device or resource busy error adding partition 1 [root@localhost ~]# mkswap /dev/sdb1 # 创建交换分区 Setting up swapspace version 1, size = 1060252 KiB no label, UUID=0d83dd2a-248c-4307-a5a0-e0b2a49d4f18 [root@localhost ~]# swapon /dev/sdb1 # 挂载交换分区,启用 [root@localhost ~]# free –m # 以MB为单位查看内存空间使用状态 total used free shared buffers cached Mem: 988 515 473 0 26 194 -/+ buffers/cache: 294 694 Swap: 2059 0 2059 [root@localhost ~]# swapoff /dev/sdb1 #禁用交换分区 [root@localhost ~]# free –m # 可以看到禁用后swap分区变小了 total used free shared buffers cached Mem: 988 512 476 0 26 194 -/+ buffers/cache: 292 696 Swap: 1023 0 1023
第二种方法:建立虚拟内存文件
#先创建1G的空文件 [root@localhost ~]# dd if=/dev/zero of=/tmp/swap1 bs=1024 count=1024000 1024000+0 records in 1024000+0 records out 1048576000 bytes (1.0 GB) copied, 3.38308 s, 310 MB/s [root@localhost ~]# mkswap /tmp/swap1 # 创建交换分区 Setting up swapspace version 1, size = 1023996 KiB no label, UUID=98dc9904-bbb1-40e7-841b-3e86a506324f [root@localhost ~]# swapon /tmp/swap1 # 挂载交换分区,启用 [root@localhost ~]# free –m # 以MB为单位查看内存空间使用状态
3、写一个脚本
(1)、获取并列出当前系统上的所有磁盘设备;
(2)、显示每个磁盘设备上每个分区相关的空间使用信息;
#!/bin/bash echo "all disk as follows:" fdisk -l | grep '^Disk[[:space:]]/dev/' | cut -d ' ' -f 2 | cut -d : -f1 for disk in $(fdisk -l | grep '^/dev/sd*' | cut -d ' ' -f 1);do echo "$disk partion useages:" df -h $disk done
4、总结RAID的各个级别及其组合方式和性能的不同;
RAID-0:条带卷,strip; 读、写性能提升;(IO速度最快) 可用空间:N*min(S1,S2,...) 无容错能力 最少磁盘数:2, 2+(两个或两个以上) RAID-1: 镜像卷,mirror; 读性能提升、写性能略有下降; 可用空间:1*min(S1,S2,...) 有冗余能力(容错性最好) 最少磁盘数:2, 2+ RAID-4: 读、写性能提升 可用空间:(N-1)*min(S1,S2,...) 有容错能力:最多坏1块磁盘 最少磁盘数:3, 3+ RAID-5:(性能与数据备份的综合考虑) 读、写性能提升 可用空间:(N-1)*min(S1,S2,...) 有容错能力:最多坏1块磁盘 最少磁盘数:3, 3+ RAID-6: 读、写性能提升 可用空间:(N-2)*min(S1,S2,...) 有容错能力:允许坏2块磁盘 最少磁盘数:4, 4+ RAID-10: 读、写性能提升 可用空间:N*min(S1,S2,...)/2 有容错能力:每组镜像最多只能坏1块磁盘; 最少磁盘数:4, 4+ RAID-01: 读、写性能提升 可用空间:N*min(S1,S2,...)/2 有容错能力:最多坏1块 最少磁盘数:4, 4+ RAID-50: RAID 5与RAID 0的组合,先作RAID 5,再作RAID 0。 读、写性能提升 可用空间:(N-1)*min(S1,S2,...)/2 有容错能力:每组镜像最多只能坏1块磁盘; 最少磁盘数:6,6+ JBOD:(Just a Bunch Of Disks) 将多块磁盘的空间合并一个大的连续空间使用; 可用空间:sum(S1,S2,...)
5、创建一个大小为10G的RAID1,要求有一个空闲盘,而且CHUNK大小为128k;
[root@localhost ~]# fdisk /dev/sdb # 创建/dev/sdb{1,2,3}分区,都是10G Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) 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): 1 First cylinder (1-13054, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-13054, default 13054): +10G Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (1307-13054, default 1307): Using default value 1307 Last cylinder, +cylinders or +size{K,M,G} (1307-13054, default 13054): +10G Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 3 First cylinder (2613-13054, default 2613): Using default value 2613 Last cylinder, +cylinders or +size{K,M,G} (2613-13054, default 13054): +10G # 将新建的sdb{1,23}调整为fd( Linux raid autodetect)类型 Command (m for help): t Partition number (1-4): 1 Hex code (type L to list codes): fd Changed system type of partition 1 to fd (Linux raid autodetect) Command (m for help): t Partition number (1-4): 2 Hex code (type L to list codes): fd Changed system type of partition 2 to fd (Linux raid autodetect) Command (m for help): t Partition number (1-4): 3 Hex code (type L to list codes): fd Changed system type of partition 3 to fd (Linux raid autodetect) Command (m for help): p #查看新建的分区 Disk /dev/sdb: 107.4 GB, 107374182400 bytes 255 heads, 63 sectors/track, 13054 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: 0x7bbe0efa Device Boot Start End Blocks Id System /dev/sdb1 1 1306 10490413+ fd Linux raid autodetect /dev/sdb2 1307 2612 10490445 fd Linux raid autodetect /dev/sdb3 2613 3918 10490445 fd Linux raid autodetect Command (m for help): w #保存 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@localhost ~]# partx -a /dev/sdb # 通知内核重读硬盘分区表 BLKPG: Device or resource busy error adding partition 1 BLKPG: Device or resource busy error adding partition 2 BLKPG: Device or resource busy error adding partition 3 [root@localhost ~]# mdadm -C /dev/md0 -a yes -c 128 -n 2 -x 1 -l 1 /dev/sdb{1,2,3} # 创建/dev/md0,-a自动创建目标RAID设备的设备文件,-c指明块chunk为128k,-n使用2个 设备来创建此RAID,-x指明1个空闲盘,-l指明要创建的RAID的级别为RAID1 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 ~]# mdadm -D /dev/md0 # 显示RAID的详细信息 /dev/md0: Version : 1.2 Creation Time : Tue Jul 19 11:30:27 2016 #RAID被创建的时间 Raid Level : raid1 # RAID的等级为raid1 Array Size : 10489317 (10.00 GiB 10.74 GB) #此RAID可用的磁盘容量 Used Dev Size : 10489317 (10.00 GiB 10.74 GB) # 每个设备可用容量 Raid Devices : 2 # 用作raid的可用设备 Total Devices : 3 # 全部的设备数量 Persistence : Superblock is persistent Update Time : Tue Jul 19 11:30:50 2016 State : clean, resyncing Active Devices : 2 # 启动的设备数量 Working Devices : 3 Failed Devices : 0 Spare Devices :1 # 空闲磁盘数量 Rebuild Status : 38% complete # 创建进度 Name : localhost.localdomain:0 (local to host localhost.localdomain) UUID : cec50dc5:adfd8f8f:573c7429:dd1414f9 Events : 6 Number Major Minor RaidDevice State 0 8 17 0 active sync /dev/sdb1 1 8 18 1 active sync /dev/sdb2 2 8 19 - spare /dev/sdb3 [root@localhost ~]# cat /proc/mdstat #查看内核是否已经识别新的分区 Personalities : [raid1] md0 : active raid1 sdb3[2](S) sdb2[1] sdb1[0] 10489317 blocks super 1.2 [2/2] [UU] [==================>..] resync = 92.3% (9684544/10489317) finish=0.0min speed=184684K/sec unused devices: <none> [root@localhost ~]# cat /proc/mdstat # 查看目前磁盘阵列的状态 Personalities : [raid1] md0 : active raid1 sdb3[2](S) sdb2[1] sdb1[0] 10489317 blocks super 1.2 [2/2] [UU] unused devices: <none>
6、创建一个大小为4G的RAID5设备,chunk大小为256k,格式化ext4文件系统,要求可开机自动挂载至/backup目录,而且不更新访问时间戳,且支持acl功能;
[root@localhost ~]# fdisk /dev/sdb # 创建/dev/sdb{1,2,3}分区,都是4G Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0xca63c7a1. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) 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): 1 First cylinder (1-2610, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +4G Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (524-2610, default 524): Using default value 524 Last cylinder, +cylinders or +size{K,M,G} (524-2610, default 2610): +4G Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 3 First cylinder (1047-2610, default 1047): Using default value 1047 Last cylinder, +cylinders or +size{K,M,G} (1047-2610, default 2610): +4G # 将新建的sdb{1,23}调整为fd( Linux raid autodetect)类型 Command (m for help): t Partition number (1-4): 1 Hex code (type L to list codes): fd Changed system type of partition 1 to fd (Linux raid autodetect) Command (m for help): t Partition number (1-4): 2 Hex code (type L to list codes): fd Changed system type of partition 2 to fd (Linux raid autodetect) Command (m for help): t Partition number (1-4): 3 Hex code (type L to list codes): fd Changed system type of partition 3 to fd (Linux raid autodetect) Command (m for help): p Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 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: 0xca63c7a1 Device Boot Start End Blocks Id System /dev/sdb1 1 523 4200966 fd Linux raid autodetect /dev/sdb2 524 1046 4200997+ fd Linux raid autodetect /dev/sdb3 1047 1569 4200997+ fd Linux raid autodetect Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@localhost ~]# partx -a /dev/sdb # # 通知内核重读硬盘分区表 BLKPG: Device or resource busy error adding partition 1 BLKPG: Device or resource busy error adding partition 2 BLKPG: Device or resource busy error adding partition 3 [root@localhost ~]# mdadm -C /dev/md0 -a yes -c 256 -n 3 -l 5 /dev/sdb{1,2,3} # 创建/dev/md0,-a自动创建目标RAID设备的设备文件,-c指明块chunk为256k,-n使用3个设备来创建此RAID, -l指明要创建的RAID的级别为RAID5 mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md0 started. [root@localhost ~]# mdadm -D /dev/md0 # 显示RAID的详细信息 /dev/md0: Version : 1.2 Creation Time : Thu Jul 21 16:14:48 2016 Raid Level : raid5 Array Size : 8393216 (8.00 GiB 8.59 GB) Used Dev Size : 4196608 (4.00 GiB 4.30 GB) Raid Devices : 3 Total Devices : 3 Persistence : Superblock is persistent Update Time : Thu Jul 21 16:18:12 2016 State : clean Active Devices : 3 Working Devices : 3 Failed Devices : 0 Spare Devices : 0 Layout : left-symmetric Chunk Size : 256K Name : localhost.localdomain:0 (local to host localhost.localdomain) UUID : 1f2c8e69:2a3725f0:a194712e:5d6ddf6c Events : 22 Number Major Minor RaidDevice State 0 8 17 0 active sync /dev/sdb1 1 8 18 1 active sync /dev/sdb2 3 8 19 2 active sync /dev/sdb3 [root@localhost ~]# cat /proc/mdstat # #查看内核是否已经识别新的分区 Personalities : [raid6] [raid5] [raid4] md0 : active raid5 sdb3[3] sdb2[1] sdb1[0] 8393216 blocks super 1.2 level 5, 256k chunk, algorithm 2 [3/3] [UUU] unused devices: <none> [root@localhost ~]# mke2fs -t ext4 /dev/md0 # 将/dev/md0格式化为ext4 mke2fs 1.41.12 (17-May-2010) 文件系统标签= 操作系统:Linux 块大小=4096 (log=2) 分块大小=4096 (log=2) Stride=64 blocks, Stripe width=128 blocks 525200 inodes, 2098304 blocks 104915 blocks (5.00%) reserved for the super user 第一个数据块=0 Maximum filesystem blocks=2151677952 65 block groups 32768 blocks per group, 32768 fragments per group 8080 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 33 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [root@localhost ~]# vim /etc/fstab # 设置开机自动挂载,noatime不更新访问时间戳 添加:/dev/md0 /backup ext4 acl,noatime 0 0 [root@localhost ~]# mkdir /backup [root@localhost ~]# mount -a [root@localhost ~]# df -lh | grep '/dev/md0' # 查看挂载信息 /dev/md0 7.9G 147M 7.4G 2% /backup
7、写一个脚本
(1) 接受一个以上文件路径作为参数;
(2) 显示每个文件拥有的行数;
(3) 总结说明本次共为几个文件统计了其行数;
#!/bin/bash # read -p "please input the file path: " -a line if [ ${#line[@]} -lt 1 ];then echo "please input file path, but now you haven't input anything!" else for i in $(seq 0 $[${#line[*]}-1]);do if [ -f ${line[$i]} ];then echo "${line[$i]} lines is:`wc -l ${line[$i]} | cut -d ' ' -f 1`" else echo "please input a true file path!" exit 1 fi done fi echo "file total number:${#line[@]}"
8、写一个脚本
(1) 传递两个以上字符串当作用户名;
(2) 创建这些用户;且密码同用户名;
(3) 总结说明共创建了几个用户;
#!/bin/bash for i in $@;do useradd $i echo $i | passwd --stdin $i done echo "create total users: $#"
9、写一个脚本,新建20个用户,visitor1-visitor20;计算他们的ID之和;
#!/bin/sbin 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"
10、写一脚本,分别统计/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以#号开头的行数之和,以及总的空白行数;
#!/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
11、写一个脚本,显示当前系统上所有默认shell为bash的用户的用户名、UID以及此类所有用户的UID之和;
#!/bin/bash echo "The users 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 "The uid sum is $sumuid ."
12、写一个脚本,显示当前系统上所有,拥有附加组的用户的用户名;并说明共有多少个此类用户;
#!/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: $sum"
13、创建一个由至少两个物理卷组成的大小为20G的卷组;要求,PE大小为8M;而在卷组中创建一个大小为5G的逻辑卷mylv1,格式化为ext4文件系统,开机自动挂载至/users目录,支持acl;
[root@localhost ~]# fdisk /dev/sdb # 先创建2个分区大小都为10G的PV Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0x1358b443. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) 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): 1 First cylinder (1-13054, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-13054, default 13054): +10G Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (1307-13054, default 1307): Using default value 1307 Last cylinder, +cylinders or +size{K,M,G} (1307-13054, default 13054): +10G Command (m for help): t Partition number (1-4): 1 Hex code (type L to list codes): 8e Changed system type of partition 1 to 8e (Linux LVM) Command (m for help): t Partition number (1-4): 2 Hex code (type L to list codes): 8e Changed system type of partition 2 to 8e (Linux LVM) Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@localhost ~]# partx -a /dev/sdb # 通知内核重新读取硬盘分区表 BLKPG: Device or resource busy error adding partition 1 BLKPG: Device or resource busy error adding partition 2 [root@localhost ~]# partx -a /dev/sdb # 通知内核重新读取硬盘分区表 BLKPG: Device or resource busy error adding partition 1 BLKPG: Device or resource busy error adding partition 2 [root@localhost ~]# pvcreate /dev/sdb{1,2} # 将分区创建为物理卷 Physical volume "/dev/sdb1" successfully created Physical volume "/dev/sdb2" successfully created [root@localhost ~]# pvs # # 简要查看创建的物理卷 pvdisplay 可查看详细PV信息 PV VG Fmt Attr PSize PFree /dev/sdb1 lvm2 a-- 10.00g 10.00g /dev/sdb2 lvm2 a-- 10.00g 10.00g [root@localhost ~]# vgcreate -s 8M myvg /dev/sdb{1,2} Volume group "myvg" successfully created # 将物理卷创建为卷组名为myvg,PE大小为8M [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 3UWOY0-Vm57-4ges-tnX1-Q98N-6haM-sEbIVe [root@localhost ~]# lvcreate -L 5G -n mylv1 myvg Logical volume "mylv1" created # 在卷组中创建大小为5G的逻辑卷mylv1 [root@localhost ~]# mke2fs -t ext4 /dev/myvg/mylv1 # 将mylv1格式化 mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 327680 inodes, 1310720 blocks 65536 blocks (5.00%) reserved for the super user First data block=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 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 21 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [root@localhost ~]# mkdir /users # 创建/users目录 [root@localhost ~]# vim /etc/fstab # 设为开机自动挂载至/users 添加 /dev/myvg/mylv1 /users ext4 acl 0 0 [root@localhost ~]# mount -a [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/sda2 18G 2.5G 15G 16% / tmpfs 504M 72K 504M 1% /dev/shm /dev/sda1 291M 33M 244M 12% /boot /dev/mapper/myvg-mylv1 5.0G 138M 4.6G 3% /users
14、新建用户magedu;其家目录为/users/magedu,而后su切换至此用户,复制多个文件至家目录;
[root@localhost ~]# useradd -d /usr/magedu/ magedu [root@localhost ~]# su - magedu [magedu@localhost root]$ cp -r /etc/pam.d /users/magedu #复制/etc/pam.d目录至用户magedu的家目录
15、扩展mylv1至9G,确保扩展完成后原有数据完全可用;
[magedu@localhost root]$ su - # 切换至root用户 Password: [root@localhost ~]# pvs # 查看pv信息,看物理卷是否能扩展 PV VG Fmt Attr PSize PFree /dev/sdb1 myvg lvm2 a--u 10.00g 5.00g /dev/sdb2 myvg lvm2 a--u 10.00g 10.00g [root@localhost ~]# vgs # 查看vg信息,看卷组是否能扩展 VG #PV #LV #SN Attr VSize VFree 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/sda2 18G 3.0G 14G 19% / tmpfs 504M 232K 503M 1% /dev/shm /dev/sda1 291M 59M 217M 22% /boot /dev/mapper/myvg-mylv1 5.0G 138M 4.6G 3% /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/sda2 18G 3.0G 14G 19% / tmpfs 504M 232K 503M 1% /dev/shm /dev/sda1 291M 59M 217M 22% /boot /dev/mapper/myvg-mylv1 # 已更新,但不一定真正达,9G的10%以内是可以接受的 8.9G 140M 8.3G 2% /users [root@localhost ~]# ls /users/magedu/pam.d/ ## 查看megedu用户文件是否丢失
16、缩减mylv1至7G,确保缩减完成后原有数据完全可用;
[root@localhost ~]# umount /dev/myvg/mylv1 # 卸载分区 [root@localhost ~]# resize2fs /dev/myvg/mylv1 7G # 缩减逻辑卷大小 resize2fs 1.41.12 (17-May-2010) Please run 'e2fsck -f /dev/myvg/mylv1' first. [root@localhost ~]# e2fsck -f /dev/myvg/mylv1 e2fsck 1.41.12 (17-May-2010) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information /dev/myvg/mylv1: 121/589824 files (0.0% non-contiguous), 72769/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 ~]# 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 mylv1 myvg -wi-a----- 7.00g [root@localhost ~]# mount /dev/myvg/mylv1 /users/magedu/ # 查看逻辑卷 [root@localhost ~]# cd !$ # 查看magedu用户文件是否丢失 cd /users/magedu/ [root@localhost magedu]# ls lost+found magedu [root@localhost magedu]# cd magedu [root@localhost magedu]# ls pam.d
17、对mylv1创建快照,并通过备份数据;要求保留原有的属主属组等信息;
[root@localhost magedu]# cp /etc/issue /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 ~]# mount /dev/myvg/snaplv /mnt # 将快照文件挂载至/mnt mount: block device /dev/mapper/myvg-snaplv is write-protected, mounting read-only [root@localhost ~]# cd /mnt # /查看mnt下挂载的issue [root@localhost mnt]# ls hgfs issue sdb sdc sdd [root@localhost mnt]# cat issue CentOS Linux release 6.0 (Final) Kernel \r on an \m [root@localhost mnt]# vim /users/issue # 编辑源卷的issue文件 [root@localhost mnt]# cat !$ # 查看修改后的issue文件 cat /users/issue CentOS Linux release 6.0 (Final) Kernel \r on an \m New line. # 这一句是添加的。 [root@localhost mnt]# pwd # 再查看/mnt下快照里面的issue /mnt [root@localhost mnt]# cat issue # 下面并没有变化,还是原来的文件内容 CentOS Linux release 6.0 (Final) Kernel \r on an \m [root@localhost mnt]# cp /etc/rc.d/init.d/functions /users # 复制某文件至/users [root@localhost mnt]# cd /users/ [root@localhost users]# ls # 发现/users源卷仍可以新增文件等操作 magedu functions issue lost+found
原创文章,作者:二极管,如若转载,请注明出处:http://www.178linux.com/25876
评论列表(1条)
写的很好,排版也很棒,加油