马哥Linux——第四周作业

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

创建主目录/users/

[root@centos7 ~]#mkdir /users

创建用户,并指定家目录

[root@centos7 ~]#useradd archlinux -d /users/archlinux

切换用户

[root@centos7 ~]#su archlinux

复制

[archlinux@centos7 ~]$cp -a /etc/pam.d/ /users/archlinux/

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

创建两个10G,且类型为LVM的分区

[root@centos7 ~]#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
All primary partitions are in use
Adding logical partition 6
First sector (174069760-419430399, default 174069760): 
Using default value 174069760
Last sector, +sectors or +size{K,M,G} (174069760-419430399, default 419430399): +10G
Partition 6 of type Linux and of size 10 GiB is set

Command (m for help): n
All primary partitions are in use
Adding logical partition 7
First sector (195043328-419430399, default 195043328): 
Using default value 195043328
Last sector, +sectors or +size{K,M,G} (195043328-419430399, default 419430399): +10G
Partition 7 of type Linux and of size 10 GiB is set

Command (m for help): t
Partition number (1-7, default 7): 6
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): t
Partition number (1-7, default 7): 
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 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: 0x0001c749

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   106956799    52428800   83  Linux
/dev/sda3       106956800   169871359    31457280   83  Linux
/dev/sda4       169871360   419430399   124779520    5  Extended
/dev/sda5       169873408   174067711     2097152   82  Linux swap / Solaris
/dev/sda6       174069760   195041279    10485760   8e  Linux LVM
/dev/sda7       195043328   216014847    10485760   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: 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@centos7 ~]#partprobe /dev/sda

创建物理卷

[root@centos7 ~]#pvcreate /dev/sda6
  Physical volume "/dev/sda6" successfully created.
[root@centos7 ~]#pvcreate /dev/sda7
  Physical volume "/dev/sda7" successfully created.

创建PE大小为16MB的卷组

[root@centos7 ~]#vgcreate -s 16M testvg /dev/sda{6,7}
  Volume group "testvg" successfully created

创建大小为5G的逻辑卷

[root@centos7 ~]#lvcreate -L 5G -n testlv testvg
  Logical volume "testlv" created.

创建ext4文件系统

[root@centos7 ~]#mkfs.ext4 /dev/testvg/testlv 
mke2fs 1.42.9 (28-Dec-2013)
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

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done 

挂载

[root@centos7 ~]#mount /dev/testvg/testlv /users/

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

扩展逻辑卷至7G

[root@centos7 ~]#lvextend -L 7G /dev/testvg/testlv 
  Size of logical volume testvg/testlv changed from 5.00 GiB (320 extents) to 7.00 GiB (448 extents).
  Logical volume testvg/testlv successfully resized.

同步文件系统

[root@centos7 ~]#resize2fs /dev/testvg/testlv 
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/testvg/testlv is mounted on /users/archlinux; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/testvg/testlv is now 1835008 blocks long.

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

取消挂载

[root@centos7 ~]#umount /users/

检查文件系统完整性

fsck -f

[root@centos7 ~]#fsck -f /dev/mapper/testvg-testlv 
fsck from util-linux 2.23.2
e2fsck 1.42.9 (28-Dec-2013)
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/mapper/testvg-testlv: 11/458752 files (0.0% non-contiguous), 67327/1835008 blocks

收缩文件系统

[root@centos7 ~]#resize2fs /dev/mapper/testvg-testlv 3G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/mapper/testvg-testlv to 786432 (4k) blocks.
The filesystem on /dev/mapper/testvg-testlv is now 786432 blocks long.

收缩逻辑卷

[root@centos7 ~]#lvreduce /dev/testvg/testlv -L 3G
  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 testvg/testlv successfully resized.

挂载

[root@centos7 ~]#mount /dev/testvg/testlv /users/

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

创建大小为3G、权限为只读的的快照逻辑卷

[root@centos7 ~]#lvcreate -L 1G -s -n testsnap -p r /dev/testvg/testlv
  Using default stripesize 64.00 KiB.
  Logical volume "testsnap" created.

创建挂载目录

[root@centos7 ~]#mkdir /mnt/testsnap

挂载

[root@centos7 ~]#mount /dev/testvg/testsnap /mnt/testsnap/
mount: /dev/mapper/testvg-testsnap is write-protected, mounting read-only

取消挂载

[root@centos7 ~]#umount /mnt/testsnap/ /users/

把快照的数据合并到逻辑卷中

[root@centos7 ~]#lvconvert --merge /dev/testvg/testsnap 
  Merging of volume testvg/testsnap started.
  testlv: Merged: 100.00%

挂载

[root@centos7 ~]#mount /dev/testvg/testlv /users/

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

(1)列出当前系统识别到的所有磁盘设备;

(2)如磁盘数量为1,则显示其空间使用信息,否则,则显示最后一个磁盘上的空间使用信息

[root@centos7 ~]#cat /root/bin/disk.sh 
#!/bin/bash

DISKLIST=`df | grep "^/dev/s[hd].*" | tr -s " " | cut -d " " -f 1`
echo $DISKLIST

DISKNUM=`df | grep "^/dev/s[hd].*" | wc -l`
[ "$DISKNUM" -eq 1 ] && df | grep "^/dev/s[hd].*" | tr -s " " ":" | cut -d ":" -f 1,5
[ "$DISKNUM" -gt 1 ] && df | grep "^/dev/s[hd].*" | tr -s " " ":" | cut -d ":" -f 1,5 | tail -n 1

加执行权限

[root@centos7 ~]#chmod +x /root/bin/disk.sh 

运行

[root@centos7 ~]#disk.sh 
/dev/sda2 /dev/sda1 /dev/sda3
/dev/sda3:1%

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

创建3个1G,且类型为raid的分区

[root@centos7 ~]#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
All primary partitions are in use
Adding logical partition 8
First sector (216016896-419430399, default 216016896): 
Using default value 216016896
Last sector, +sectors or +size{K,M,G} (216016896-419430399, default 419430399): +1G
Partition 8 of type Linux and of size 1 GiB is set

Command (m for help): n
All primary partitions are in use
Adding logical partition 9
First sector (218116096-419430399, default 218116096): 
Using default value 218116096
Last sector, +sectors or +size{K,M,G} (218116096-419430399, default 419430399): +1G
Partition 9 of type Linux and of size 1 GiB is set

Command (m for help): n
All primary partitions are in use
Adding logical partition 10
First sector (220215296-419430399, default 220215296): 
Using default value 220215296
Last sector, +sectors or +size{K,M,G} (220215296-419430399, default 419430399): +1G
Partition 10 of type Linux and of size 1 GiB is set

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

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

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

Command (m for help): p

Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 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: 0x0001c749

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   106956799    52428800   83  Linux
/dev/sda3       106956800   169871359    31457280   83  Linux
/dev/sda4       169871360   419430399   124779520    5  Extended
/dev/sda5       169873408   174067711     2097152   82  Linux swap / Solaris
/dev/sda6       174069760   195041279    10485760   8e  Linux LVM
/dev/sda7       195043328   216014847    10485760   8e  Linux LVM
/dev/sda8       216016896   218114047     1048576   fd  Linux raid autodetect
/dev/sda9       218116096   220213247     1048576   fd  Linux raid autodetect
/dev/sda10      220215296   222312447     1048576   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: 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@centos7 ~]#partprobe /dev/sda

创建raid1

[root@centos7 ~]#mdadm -C /dev/md0 -a yes -l 1 -n 2 -x 1 /dev/sda{8,9,10}
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.

为raid设备创建ext4文件系统

[root@centos7 ~]#mkfs.ext4 /dev/md0 
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
65536 inodes, 261888 blocks
13094 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

创建挂载目录/backup

[root@centos7 ~]#mkdir /backup

挂载关系写入文件

[root@centos7 ~]#echo "UUID=915c0b4f-c35f-48c4-9f51-810585ee71f4 /backup ext4 defaults 0 0" >> /etc/fstab 

自动挂载

[root@centos7 ~]#mount -a

创建raid配置文件

[root@centos7 ~]#mdadm -Ds /dev/md0 >> /etc/mdadm.conf

8、总结linux上ip和dns配置方法

猜你喜欢

转载自www.cnblogs.com/keith946/p/11389435.html