wuliws 发表于 2018-5-19 14:39:27

Linux第七周

  1、创建一个10G分区,并格式为ext4文件系统;
  (1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl;
  (2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳;
# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xa0b19c72.
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-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.
# partx -a /dev/sdb
BLKPG: Device or resource busy
error adding partition 1
# cat /proc/partitions
major minor#blocksname
   8      0125829120 sda
   8      1   512000 sda1
   8      2    2048000 sda2
   8      3123268096 sda3
   8       16   83886080 sdb
   8       17   10490413 sdb1
# mke2fs -t ext4 -b 2048 -m 2 -L 'MYDATA'/dev/sdb1               
mke2fs 1.41.12 (17-May-2010)
文件系统标签=MYDATA
操作系统:Linux
块大小=2048 (log=1)
分块大小=2048 (log=1)
Stride=0 blocks, Stripe width=0 blocks
657408 inodes, 5245206 blocks
104904 blocks (2.00%) reserved for the super user
第一个数据块=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
正在写入inode表: 完成                           
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
This filesystem will be automatically checked every 29 mounts or
180 days, whichever comes first.Use tune2fs -c or -i to override.
# mkdir -p /data/mydata
# echo 'LABEL=MYDATA/data/mydataext4defaults,acl0 0' >> /etc/fstab
# mount -a  2、创建一个大小为1G的swap分区,并创建好文件系统,并启用之;
# fdisk /dev/sdb
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): 2
First cylinder (6529-10443, default 6529):
Using default value 6529
Last cylinder, +cylinders or +size{K,M,G} (6529-10443, default 10443): +1G
Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 82
Changed system type of partition 2 to 82 (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: 设备或资源忙.
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.
# partx -a /dev/sdb
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
# mkswap /dev/sdb2
Setting up swapspace version 1, size = 1060284 KiB
no label, UUID=54ef09ac-5e9e-4453-9269-84936c6c1c19
# swapon /dev/sdb2  3、写一个脚本
     (1)、获取并列出当前系统上的所有磁盘设备;
     (2)、显示每个磁盘设备上每个分区相关的空间使用信息;
# cat t3.sh
#!/bin/bash
#
i=1
fdisk -l /dev/d | grep "Disk /"
for DevName in $(ls /dev/d);do         
   if [ $i -eq 1 ] ;then
         df -h $DevName | awk -v OFS="    " '{print $1,$2,$3,$4,$5,$6}'
         let i++
   else
         df -h $DevName | grep -v "File" | awk -v OFS="    " -v name=$DevName '{print name,$2,$3,$4,$5,$6}'
   fi
done  4、总结RAID的各个级别及其组合方式和性能的不同;
  
RAID级别RAID0RAID1RAID5RAID6RAID10RAID01容错无有有有有有冗余无镜像奇偶校验奇偶校验镜像镜像读性能有所提高有所提高有所提高有所提高有所提高有所提高写性能有所提高略有下降
有所提高有所提高有所提高有所提高最少磁盘数
2,2+2,2+3,3+4,4+
4,4+4,4+允许磁盘最多损坏数0112每组1个
每组1个可用容量
N*min(s1,s2,...)N*min(s1,s2,...)/2
(N-1)*min(s1,s2,...)
(N-2)*min(s1,s2,...)N*min(s1,s2,...)/2N*min(s1,s2,...)/2  

  5、创建一个大小为10G的RAID1,要求有一个空闲盘,而且CHUNK大小为128k;
# fdisk/dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x75d40809.
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-10443, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-10443, default 10443): +10G
Command (m for help): n
Command action
   e   extended
   p   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): +10G
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (2613-10443, default 2613):
Using default value 2613
Last cylinder, +cylinders or +size{K,M,G} (2613-10443, default 10443): +10G
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
# 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
# cat /proc/partitions
major minor#blocksname
   8      0125829120 sda
   8      1   512000 sda1
   8      2    2048000 sda2
   8      3123268096 sda3
   8       16   83886080 sdb
   8       17   10490413 sdb1
   8       18   10490445 sdb2
   8       19   10490445 sdb3
# mdadm -C /dev/md0 -n 2 -l 1 -c 128 -x 1 /dev/sdb{1,2,3}
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.
# mdadm -D /dev/md0
/dev/md0:
      Version : 1.2
Creation Time : Mon Aug1 20:19:54 2016
   Raid Level : raid1
   Array Size : 10482176 (10.00 GiB 10.73 GB)
Used Dev Size : 10482176 (10.00 GiB 10.73 GB)
   Raid Devices : 2
Total Devices : 3
    Persistence : Superblock is persistent
    Update Time : Mon Aug1 20:20:40 2016
          State : clean, resyncing
Active Devices : 2
Working Devices : 3
Failed Devices : 0
Spare Devices : 1
Resync Status : 87% complete
         Name : localhost.localdomain:0(local to host localhost.localdomain)
         UUID : 2277076e:bc9b6d70:eabfb522:cee50958
         Events : 14
    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  6、创建一个大小为4G的RAID5设备,chunk大小为256k,格式化ext4文件系统,要求可开机自动挂载至/backup目录,而且不更新访问时间戳,且支持acl功能;
# fdisk /dev/sdb
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-10443, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-10443, default 10443): +4G
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (524-10443, default 524):
Using default value 524
Last cylinder, +cylinders or +size{K,M,G} (524-10443, default 10443): +4G
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (1047-10443, default 1047):
Using default value 1047
Last cylinder, +cylinders or +size{K,M,G} (1047-10443, default 10443): +10G
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
# mdadm -C /dev/md5 -n 3 -l 5 -c 256 /dev/sdb{1,2,3}
mdadm: /dev/sdb1 appears to be part of a raid array:
       level=raid1 devices=2 ctime=Mon Aug1 20:19:54 2016
mdadm: largest drive (/dev/sdb3) exceeds size (4196864K) by more than 1%
Continue creating array?
Continue creating array? (y/n) y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md5 started.
# mdadm -D /dev/md5
/dev/md5:
      Version : 1.2
Creation Time : Mon Aug1 20:35:52 2016
   Raid Level : raid5
   Array Size : 8393728 (8.00 GiB 8.60 GB)
Used Dev Size : 4196864 (4.00 GiB 4.30 GB)
   Raid Devices : 3
Total Devices : 3
    Persistence : Superblock is persistent
    Update Time : Mon Aug1 20:36:12 2016
          State : clean, degraded, recovering
Active Devices : 2
Working Devices : 3
Failed Devices : 0
Spare Devices : 1
         Layout : left-symmetric
   Chunk Size : 256K
Rebuild Status : 8% complete
         Name : localhost.localdomain:5(local to host localhost.localdomain)
         UUID : 8d09334e:eb6c0810:f773a7f9:2030c941
         Events : 2
    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      spare rebuilding   /dev/sdb3
# mkfs.ext4 /dev/md5
# mkdir /backup
# echo "/dev/md5    /backup   ext4defaults,acl,nodiratime0 0">> /etc/fstab  7、写一个脚本

     (1) 接受一个以上文件路径作为参数;
     (2) 显示每个文件拥有的行数;
     (3) 总结说明本次共为几个文件统计了其行数;
# cat test7.sh
#!/bin/bash
if [ $# -eq 0 ];then
      echo "Please give a file name !"
      exit 1
fi
for i in $*
do
      echo "$i have $(wc -l $i | cut -d' ' -f1) lines"
done
echo"本次共为$#个文件统计了其行数!"  8、写一个脚本
     (1) 传递两个以上字符串当作用户名;
     (2) 创建这些用户;且密码同用户名;
     (3) 总结说明共创建了几个用户;
#!/bin/bash
if [ $# -lt 2 ];then
      echo "At least give two username !"
      exit 1
fi
for i in $*
do
      useradd $i
      echo "$i:$i" | chpasswd
done
echo "Total Add $# users !"  9、写一个脚本,新建20个用户,visitor1-visitor20;计算他们的ID之和;

# cat test9.sh
#!/bin/bash
declare -i tid
for i in {1..20}
do
      useradd visitor$i
      let tid+=$(id -u visitor$i)
done
echo "Total id is $tid !"  10、写一脚本,分别统计/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以#号开头的行数之和,以及总的空白行数;
# cat test10.sh
#!/bin/bash
declare -i l1
declare -i space
for i in /etc/{rc.d/{rc.sysinit,init.d/functions},fstab}
do
      let l1+=$(grep '^#' $i | wc -l | cut -d' ' -f1)
      let space+=$(grep'^[[:space:]]*$' $i | wc -l | cut -d' ' -f1)
done
echo "Comment lines beginning with a '#' total $l1 lines!"
echo "Blank lines total $space lines !"  11、写一个脚本,显示当前系统上所有默认shell为bash的用户的用户名、UID以及此类所有用户的UID之和;
# cat test11.sh
#!/bin/bash
declare -i uid
for i in $(grep bash$ /etc/passwd | cut -d: -f1)
do
      echo "$i, `id -u $i`"
      let uid+=`id -u $i`
done
echo "Total uid is $uid !"  12、写一个脚本,显示当前系统上所有,拥有附加组的用户的用户名;并说明共有多少个此类用户;
#!/bin/bash
awk '{FS=":"} $3 != $4 {print $1;n++}END{ print n}' /etc/passwd  13、创建一个由至少两个物理卷组成的大小为20G的卷组;要求,PE大小为8M;而在卷组中创建一个大小为5G的逻辑卷mylv1,格式化为ext4文件系统,开机自动挂载至/users目录,支持acl;

  

  # fdisk /dev/sdb
  Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
  Building a new DOS disklabel with disk identifier 0xdb484fda.
  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-1305, default 1):
  Using default value 1
  Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305):
  Using default value 1305
  

  Command (m for help): w
  The partition table has been altered!
  

  Calling ioctl() to re-read partition table.
  Syncing disks.
  # fdisk /dev/sdc
  Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
  Building a new DOS disklabel with disk identifier 0xcbb0c025.
  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-1305, default 1):
  Using default value 1
  Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305):
  Using default value 1305
  

  Command (m for help): w
  The partition table has been altered!
  

  Calling ioctl() to re-read partition table.
  Syncing disks.
  # pvcreate /dev/sd{b,c}1
  Physical volume "/dev/sdb1" successfully created
  Physical volume "/dev/sdc1" successfully created
  # vgcreate -s 8m myvg /dev/sd{b,c}1
  Volume group "myvg" successfully created
  # lvcreate -L 5g -n mlv1 myvg
  Logical volume "mlv1" created
  # mkfs.ext4 /dev/myvg/mlv1
  # echo "/dev/myvg/mlv1/usersext4defaults,acl 0 0" >> /etc/fstab

  # vgdisplay
  --- Volume group ---
  VG Name               myvg
  System ID
  Format                lvm2
  Metadata Areas      2
  Metadata Sequence No2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  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       640 / 5.00 GiB
  FreePE / Size       1918 / 14.98 GiB
  VG UUID               Yrb5ku-mOoE-cdk3-E0QW-QmyY-hM39-BRGBjk
  # lvdisplay
  --- Logical volume ---
  LV Path                /dev/myvg/mlv1
  LV Name                mlv1
  VG Name                myvg
  LV UUID                oqYvWw-FpYI-Ccd4-KWRG-V2Dq-pcNS-apoGJ6
  LV Write Access      read/write
  LV Creation host, time localhost.localdomain, 2016-08-02 22:23:21 +0800
  LV Status            available
  # open               1
  LV Size                5.00 GiB
  Current LE             640
  Segments               1
  Allocation             inherit
  Read ahead sectors   auto
  - currently set to   256
  Block device         253:0
  14、新建用户magedu;其家目录为/users/magedu,而后su切换至此用户,复制多个文件至家目录;
# useradd -d /users/magedu magedu
# echo magedu:123456 | chpasswd
# su - magedu
$ pwd
/users/magedu
$ cp /etc/{passwd,group} .
$ ls
grouppasswd  15、扩展mylv1至9G,确保扩展完成后原有数据完全可用;
# lvextend -L 9G /dev/myvg/mlv1
Size of logical volume myvg/mlv1 changed from 5.00 GiB (640 extents) to 9.00 GiB (1152 extents).
Logical volume mlv1 successfully resized
# resize2fs /dev/myvg/mlv1
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/myvg/mlv1 is mounted on /users; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/myvg/mlv1 to 2359296 (4k) blocks.
The filesystem on /dev/myvg/mlv1 is now 2359296 blocks long.
# lvdisplay
--- Logical volume ---
LV Path                /dev/myvg/mlv1
LV Name                mlv1
VG Name                myvg
LV UUID                oqYvWw-FpYI-Ccd4-KWRG-V2Dq-pcNS-apoGJ6
LV Write Access      read/write
LV Creation host, time localhost.localdomain, 2016-08-02 22:23:21 +0800
LV Status            available
# open               1
LV Size                9.00 GiB
Current LE             1152
Segments               1
Allocation             inherit
Read ahead sectors   auto
- currently set to   256
Block device         253:0
# ls /users/magedu/
grouppasswd  16、缩减mylv1至7G,确保缩减完成后原有数据完全可用;
# lvreduce -L 7G /dev/myvg/mlv1
WARNING: Reducing active and open logical volume to 7.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce mlv1? : y
Size of logical volume myvg/mlv1 changed from 9.00 GiB (1152 extents) to 7.00 GiB (896 extents).
Logical volume mlv1 successfully resized
# resize2fs /dev/myvg/mlv1
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/myvg/mlv1 is mounted on /users; on-line resizing required
On-line shrinking from 2359296 to 1835008 not supported.
# lvdisplay
--- Logical volume ---
LV Path                /dev/myvg/mlv1
LV Name                mlv1
VG Name                myvg
LV UUID                oqYvWw-FpYI-Ccd4-KWRG-V2Dq-pcNS-apoGJ6
LV Write Access      read/write
LV Creation host, time localhost.localdomain, 2016-08-02 22:23:21 +0800
LV Status            available
# open               1
LV Size                7.00 GiB
Current LE             896
Segments               1
Allocation             inherit
Read ahead sectors   auto
- currently set to   256
Block device         253:0
# ls /users/magedu/
grouppasswd  17、对mylv1创建快照,并通过备份数据;要求保留原有的属主属组等信息;
# lvcreate -L 5G -p r -s -n mylv1_snapshot /dev/myvg/mlv1
Logical volume "mylv1_snapshot" created  


  
页: [1]
查看完整版本: Linux第七周