linux练习

1.创建一个至少有两个PV组成的大小为20G的名为testvg的VG;要求PE大小 为16MB, 而后在卷组中创建大小为5G的逻辑卷testlv;挂载至/users目录

1.准备两个10G的分区
[root@centos6 ~]lsblk
...
sdb      8:16   0   60G  0 disk 
├─sdb1   8:17   0   10G  0 part 
└─sdb2   8:18   0   10G  0 part
...

2.创建pv
[root@centos6 ~] pvcreate /dev/sdb1   
  Physical volume "/dev/sdb1" successfully created
[root@centos6 ~] pvcreate /dev/sdb2  
  Physical volume "/dev/sdb2" successfully created

3.创建vg
[root@centos6 ~]vgcreate -s 16M testvg /dev/sdb{1,2}    
  Volume group "testvg" successfully created
[root@centos6 ~] pvs
  PV         VG     Fmt  Attr PSize  PFree 
  /dev/sdb1  testvg lvm2 a--u 10.00g 10.00g
  /dev/sdb2  testvg lvm2 a--u 10.00g 10.00g
[root@centos6 ~] vgs
  VG     #PV #LV #SN Attr   VSize  VFree 
  testvg   2   0   0 wz--n- 20.00g 20.00g


4.创建lv和文件系统
[root@centos6 ~]lvcreate -n testlv -L 5G testvg
  Logical volume "testlv" created.
[root@centos6 ~]lvs
  LV     VG     Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  testlv testvg -wi-a----- 5.00g 
[root@centos6 ~]mkfs.ext4 /dev/testvg/testlv 
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 39 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

5.创建/users目录,修改/etc/fstab并挂载
[root@centos6 ~]mkdir /users
[root@centos6 ~]blkid 
...
/dev/mapper/testvg-testlv: UUID="32bd3d97-003a-4d2a-8e8e-e7ebe4decfe0" TYPE="ext4"
[root@centos6 ~]vim /etc/fstab
在最后添加一行
UUID="32bd3d97-003a-4d2a-8e8e-e7ebe4decfe0" /users ext4  defaults 0 0
[root@centos6 ~]mount -a
[root@centos6 ~]lsblk
...
sdb                        8:16   0   60G  0 disk 
├─sdb1                     8:17   0   10G  0 part 
│ └─testvg-testlv (dm-0) 253:0    0    5G  0 lvm  /users
└─sdb2                     8:18   0   10G  0 part 
...

2.新建用户archlinux,要求其家目录为/users/archlinux,而后su切换至 archlinux用户,复制/etc/pam.d目录至自己的家目录

1.创建用户和复制pam.d
[root@centos6 ~]useradd -d /users/archlinux archlinux
[root@centos6 ~]su - archlinux
[archlinux@centos6 ~]pwd
/users/archlinux
[archlinux@centos6 ~]cp -a /etc/pam.d .

2.比较大小查看是否完整完整复制
[archlinux@centos6 ~]du -sh /users/archlinux/pam.d /etc/pam.d
236K    /users/archlinux/pam.d
236K    /etc/pam.d

3.扩展testlv至7G,要求archlinux用户的文件不能丢失

1.扩展
[root@centos6 ~]lvresize -r -L +2G /dev/testvg/testlv
  Size of logical volume testvg/testlv changed from 5.00 GiB (320 extents) to 7.00 GiB (448 extents).
  Logical volume testlv successfully resized.
[root@centos6 ~]lvs
  LV     VG     Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  testlv testvg -wi-ao---- 7.00g

2.检查文件是否丢失
[root@centos6 ~]su - archlinux
[archlinux@centos6 ~]pwd
/users/archlinux
[archlinux@centos6 ~]du -sh /users/archlinux/pam.d /etc/pam.d
236K    /users/archlinux/pam.d
236K    /etc/pam.d

4.收缩testlv至3G,要求archlinux用户的文件不能丢失

1.收缩
[root@centos6 ~]umount /dev/testvg/testlv
[root@centos6 ~]e2fsck -f /dev/testvg/testlv
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/testvg/testlv: 84/458752 files (0.0% non-contiguous), 64515/1835008 blocks
[root@centos6 ~]resize2fs /dev/testvg/testlv 3G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/testvg/testlv to 786432 (4k) blocks.
The filesystem on /dev/testvg/testlv is now 786432 blocks long.
[root@centos6 ~]lvreduce -L 3G /dev/testvg/testlv
  WARNING: Reducing active logical volume to 3.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce testvg/testlv? [y/n]: y
  Size of logical volume testvg/testlv changed from 7.00 GiB (448 extents) to 3.00 GiB (192 extents).
  Logical volume testlv successfully resized.
[root@centos6 ~]mount -a
[root@centos6 ~]lvs
  LV     VG     Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  testlv testvg -wi-ao---- 3.00g 

2.检查文件是否丢失
[root@centos6 ~]su - archlinux
[archlinux@centos6 ~]pwd
/users/archlinux
[archlinux@centos6 ~]du -sh /users/archlinux/pam.d /etc/pam.d
236K    /users/archlinux/pam.d
236K    /etc/pam.d

5.对testlv创建快照,并尝试基于快照备份数据,验证快照的功能

1.创建快照并挂载
[root@centos6 ~]lvcreate -L 2G -s -n testlv-snapshot -p r /dev/testvg/testlv
  Logical volume "testlv-snapshot" created.
[root@centos6 ~]mkdir /mnt/snap
[root@centos6 ~]mount -o ro /dev/testvg/testlv-snapshot /mnt/snap
[root@centos6 ~]lsblk
...
sdb                                      8:16   0   60G  0 disk 
├─sdb1                                   8:17   0   10G  0 part 
│ ├─testvg-testlv-real (dm-1)          253:1    0    3G  0 lvm  
│ │ ├─testvg-testlv (dm-0)             253:0    0    3G  0 lvm  /users
│ │ └─testvg-testlv--snapshot (dm-3)   253:3    0    3G  1 lvm  /mnt/snap
│ └─testvg-testlv--snapshot-cow (dm-2) 253:2    0    2G  1 lvm  
│   └─testvg-testlv--snapshot (dm-3)   253:3    0    3G  1 lvm  /mnt/snap
└─sdb2                                   8:18   0   10G  0 part

2.测试快照
[root@centos6 ~]ll /users /mnt/snap
/mnt/snap:
total 20
drwx------. 5 archlinux archlinux  4096 Feb 21 09:28 archlinux
drwx------. 2 root      root      16384 Feb 21 09:03 lost+found

/users:
total 20
drwx------. 5 archlinux archlinux  4096 Feb 21 09:28 archlinux
drwx------. 2 root      root      16384 Feb 21 09:03 lost+found
[root@centos6 ~]rm -rf /users/archlinux/
[root@centos6 ~]ll /users  /mnt/snap
/mnt/snap:
total 20
drwx------. 5 archlinux archlinux  4096 Feb 21 09:28 archlinux
drwx------. 2 root      root      16384 Feb 21 09:03 lost+found

/users:
total 16
drwx------. 2 root root 16384 Feb 21 09:03 lost+found
[root@centos6 ~]umount /dev/testvg/testlv-snapshot
[root@centos6 ~]umount /dev/testvg/testlv
[root@centos6 ~]lvconvert --merge /dev/testvg/testlv-snapshot
  Merging of volume testlv-snapshot started.
  testlv: Merged: 100.0%
  testlv: Merged: 100.0%
  Merge of snapshot into logical volume testlv has finished.
  Logical volume "testlv-snapshot" successfully removed
[root@centos6 ~]mount -a
[root@centos6 ~]ls /users
archlinux  lost+found

6.写一个脚本,完成如下功能:

(1) 列出当前系统识别到的所有磁盘设备
(2) 如磁盘数量为1,则显示其空间使用信息 否则,则显示最后一个磁盘上的空间使用信息

1.编写脚本
[root@centos6 ~]vim listAllDisk.sh
#!/bin/bash
fdisk -l                                                                                                     
reg='[sh]d[a-z]'
diskNum=`fdisk -l /dev/${reg} | grep -o "Disk /dev/${reg}" | wc -l`
[ $diskNum -eq 1 ] && fdisk -l [sh]da || fdisk -l `fdisk -l /dev/${reg} | grep -o "Disk /dev/${reg}" | tail -1 | cut -d' ' -f2`
2.执行脚本
[root@centos6 bin]listAllDisk.sh 

Disk /dev/sda: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 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: 0x000df04f

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1         131     1048576   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2             131        6505    51200000   83  Linux
/dev/sda3            6505       10330    30720000   83  Linux
/dev/sda4           10330       26109   126745600    5  Extended
/dev/sda5           10330       10591     2097152   82  Linux swap / Solaris

Disk /dev/sdb: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 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: 0x54eb43f6

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1306    10490413+  83  Linux
/dev/sdb2            1307        2612    10490445   83  Linux

Disk /dev/mapper/testvg-testlv: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 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: 0x00000000

以下是最后一个磁盘上的空间使用信息
Disk /dev/sdb: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 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: 0x54eb43f6

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1306    10490413+  83  Linux
/dev/sdb2            1307        2612    10490445   83  Linux

7.创建一个可用空间为1G的RAID1设备,文件系统为ext4,有一个空闲盘,开机可自动挂载至/backup目录

1.准备三个1G的分区
[root@centos6 ~]lsblk
...
sdb      8:16   0   60G  0 disk 
├─sdb1   8:17   0    1G  0 part 
├─sdb2   8:18   0    1G  0 part 
└─sdb3   8:19   0    1G  0 part
2.创建
[root@centos6 ~]mdadm -C /dev/md0 -n 2 -l 1 -a yes -x 1 /dev/sdb{1,2,3}
[root@centos6 ~]mkfs.ext4 /dev/md0
[root@centos6 ~]mdadm -D -s >> /etc/mdadm.conf
[root@centos6 ~]mkdir /backup
[root@centos6 ~]blkid
...
/dev/sdb1: UUID="79104bf9-d200-1e84-7371-2a7817a67a87" UUID_SUB="6b7d5ac5-9c4b-23ae-d200-ba84e20c7258" LABEL="centos6.xm11211:0" TYPE="linux_raid_member" 
/dev/sdb2: UUID="79104bf9-d200-1e84-7371-2a7817a67a87" UUID_SUB="99ee0abb-f130-66c2-8ca4-165c50b9ca33" LABEL="centos6.xm11211:0" TYPE="linux_raid_member" 
/dev/sdb3: UUID="79104bf9-d200-1e84-7371-2a7817a67a87" UUID_SUB="11cc4774-0b52-ff29-f415-e3dd5d5f24aa" LABEL="centos6.xm11211:0" TYPE="linux_raid_member" 
/dev/md0: UUID="01fa1870-423d-4878-a7b6-d866c2f3b87f" TYPE="ext4"
[root@centos6 ~]vim /etc/fstab
在最后添加一行
UUID=01fa1870-423d-4878-a7b6-d866c2f3b87f /backup ext4 defaults 0 0
[root@centos6 ~]mount -a
[root@centos6 ~]lsblk
sdb       8:16   0   60G  0 disk  
├─sdb1    8:17   0    1G  0 part  
│ └─md0   9:0    0    1G  0 raid1 /backup
├─sdb2    8:18   0    1G  0 part  
│ └─md0   9:0    0    1G  0 raid1 /backup
└─sdb3    8:19   0    1G  0 part  
  └─md0   9:0    0    1G  0 raid1 /backup

你可能感兴趣的:(linux练习)