Linux基础—磁盘管理

1、创建一个10G分区、并格式化为ext4文件系统

1)要求其block大小为2048,预留空间百分比2,卷标MYDATA,默认挂载属性包含acl 2)挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳

#mkfs.ext4 -b 2048 -m 2 -L mydata /dev/sda5
#mount /dev/sda5 /data/mydata -o noatime,noexec

2、创建一个大小为1G的swap分区,并创建好文件系统,并启用之

#fdisk /dev/sda
Command (m for help): n
All primary partitions are in use
Adding logical partition 6
First sector (341858304-976773167, default 341858304): 
Using default value 341858304
Last sector, +sectors or +size{K,M,G} (341858304-976773167, default 976773167): +1G
Partition 6 of type Linux and of size 1 GiB is set\
Command (m for help): t
Partition number (1-6, default 6): 6
Hex code (type L to list all codes): 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      
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!
Hex code (type L to list all codes): 82    
[root@localhost ~]# mkswap /dev/sda
[root@localhost ~]# mkswap /dev/sda6    
[root@localhost ~]# free -h
              total        used        free      shared  buff/cache   available
    Mem:           3.7G         93M        3.4G        8.4M        245M        3.4G
    Swap:          3.0G          0B        3.0G

3、写一个脚本

1)获取并列出当前系统上的所有磁盘设备 2)显示每个磁盘设备上的每个分区相关的空间使用信息 #!/bin/bash # lsblk df -h 4、总结raid的各个级别及其组合方式和性能的不同

5、创建一个大小为10G的raid1,要求一个空闲盘,而且chunk大小为128K

[root@localhost ~]# fdisk /dev/sda
Command (m for help): n
All primary partitions are in use
Adding logical partition 6
First sector (341858304-976773167, default 341858304): 
Using default value 341858304
Last sector, +sectors or +size{K,M,G} (341858304-976773167, default 976773167): +10G
Partition 6 of type Linux and of size 10 GiB is set

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

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

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

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

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

Command (m for help): w
The partition table has been altered!
[root@localhost ~]# mdadm --create /dev/md0 --auto=yes --level=1 --chunk=128K \
> --raid-devices=2 --spare-devices=1 /dev/sda{6,7,8}
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/n) y
[root@localhost ~]# mkfs.ext4 /dev/md0 
[root@localhost ~]# mount /dev/md0 /media/
[root@localhost ~]# df -h /media/
Filesystem      Size  Used Avail Use% Mounted on
/dev/md0        9.8G   37M  9.2G   1% /media

6、创建一个大小为4G的raid5设备,chunk大小为256k,格式化ext4文件系统,要求可开机自动挂载至/backup目录,而且不更新访问时间戳,且支持acl功能;

直接调用5题中的三块盘配置raid5,大小为20G
[root@localhost ~]# mdadm --create /dev/md1 --auto=yes --level=5 --chunk=256K --raid-devices=3 /dev/sda{6,7,8}
[root@localhost ~]# mkfs.ext4 /dev/md1
[root@localhost ~]# lsblk /dev/md1 
NAME MAJ:MIN RM SIZE RO TYPE  MOUNTPOINT
md1    9:1    0  20G  0 raid5 
#vi /etc/fstab
/dev/md1    /backup     ext4        noatime,acl     0   0

7、写一个脚本 1)接受一个以上文件路径作为参数 2)显示每个文件拥有的行数 3)总结说明本次共为几个文件统计了其行数;

#!/bin/bash
if [ $# -eq 0 ] ;then
    echo "You must input a file-full-directory at least!"
    else
            SUM=0;
            FILENUM=0;
            for file in $*;
            do
             if [ ! -f $file ];then
                    echo "THE $file is not a file,please input again!"
            else
                    LINE=$(cat $file|wc -l)
                    echo $file has $LINE lines
                    SUM=$(($SUM+$LINE))
                    FILENUM=$(($FILENUM+1))
            fi
            done
            echo the file-num is $FILENUM;
            echo the tatol lines is $SUM;
fi

8、写一个脚本 1)传递两个以上字符串当做用户名 2)创建这些用户,且密码同用户名 3)总结说明共创建了几个用户

#!/bin/bash
declare sum=0;
if [ $# -lt 2 ];then
    echo -e "\033[031m you must insert more string !\033[0m"
else
        for name in $*
           do
             id $name>>/dev/null 2>&1
             if [ $? -eq 0 ]; then
                   echo -e "\033[031m the user $name is exist!\033[0m"
             else
                   useradd $name && echo $name |passwd --stdin $name
                   sum=$(($sum+1))
        fi
        done
        echo "$sum users is created!"
fi

9、写一个脚本,新建20个用户,visitor1- visitor20,计算他们的ID之和

#!/bin/bash
#       create 20 users and count the sum of users-id
#       20161108    
declare sum=0;  
for (( i=1;i<=20;i++ ))
    do
    id visitor$i >> /dev/null 2>&1
    if [ $? -eq 0 ];then
        echo "The visitor$i is exist!"
    else
        useradd visitor$i && echo visitor$i |passwd --stdin visitor$i >>/dev/null 2>&1
        uid=$(id -u visitor$i)
        sum=$(($sum+$uid))
     fi
done
echo "the sum of user_id is $sum."

10、写一个脚本,分别统计/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以#号开头的行数之和,以及总的空白行数

#!/bin/bash
#
#       count the lines in /etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions、/etc/fstab
#       20161109
declare SUM1=0;
declare SUM2=0;
for file in /etc/rc.d/rc.sysinit /etc/rc.d/init.d/functions /etc/fstab
        do
        if [ -f $file ];then
                line1=$(grep "^#" $file|wc -l) >> /dev/null 2>&1
                line2=$(grep "[[:space:]]\+" $file|wc -l) >> /dev/null 2>&1
        echo "the $file the lines head by # is $line1,the space lines is $line2"             
                SUM1=$(($SUM1+$line1))
                SUM2=$(($SUM2+$line2))
        else echo -e "\033[031m the $file is not exist or wrong \033[0m"
        fi
        done
                echo "the tatol of lines head by # is $SUM1,the tatol space lines is $SUM2"

~ 
11、写一个脚本,显示当前系统上所有默认shell为bash的用户的用户名,UID以及此类所有用户的UID之和

#!/bin/bash
#       Display the user whose shell is bash,and its uid and the sum of uid
#       20161108

declare sum=0;
for USER_NAME in $(grep "bash$" /etc/passwd|cut -d":" -f 1)
    do
        Uid=$(id -u $USER_NAME)
        echo "$USER_NAME's uid is $Uid"
        sum=$(($sum+$Uid))
done
        echo "The sum of user_id is $sum"

~ 
12、写一个脚本,显示当前系统上所有,拥有附加组的用户的用户名,并说明共有多少个此类用户

#!/bin/bash
#       Display the user who have two subordinate-group
#       20161108

USER=$(cat /etc/passwd|cut -d':' -f 1)
declare SUM=0;
for NAME in $USER
   do
        if [ `id -G $NAME |wc -w` -ge 2 ] ;then
           SUM=$(($SUM+1))
           GROUP=$(id $NAME|cut -d' ' -f3)
           echo "The user $NAME groups is $GROUP"
        fi
done
        echo "these are $SUM users"

13、创建一个由至少两个物理卷组成的大小为20G的卷组,要求:PE大小为8M,而在卷组中创建一个大小为5G的逻辑卷mylv1,格式化为ext4文件,开机自动挂载至/users目录,支持acl

#pvcreate /dev/sd{c1,d1,e1}
#vgcreate -s 8M  testvg /dev/sd{c1,d1,e1}
#lvcreate -L +5G -n mylv1 testvg
#mkfs.ext4 /dev/testvg/mylv1
#vi /etc/fstab
/dev/testvg/mylv1       /users  ext4        default,acl 0  0
#mount /dev/testvg/mylv1  /users
#df -h /users 
Filesystem                Size  Used Avail Use% Mounted on
/dev/mapper/testvg-mylv1  4.8G   21M  4.6G   1% /users

14、新建用户magedu,其家目录/users/magedu,而后su切换至此目录,复制多个文件至家目录

#useradd -d /users/magedu magedu
#cp -r /var/* /users
#ls /users
adm    crash  empty  games   kerberos  local  log         mail  opt       run    tmp  yp
cache  db     ftp    gopher  lib       lock   lost+found  nis   preserve  spool  var

15、扩展mylv1至9G,确保扩展完成后原有数据完全可用

[root@localhost ~]# df -h /users/
Filesystem                Size  Used Avail Use% Mounted on
/dev/mapper/testvg-mylv1  4.8G  314M  4.3G   7% /users
[root@localhost ~]# umount /users/

[root@localhost ~]# e2fsck /dev/testvg/mylv1 
e2fsck 1.42.9 (28-Dec-2013)
/dev/testvg/mylv1: clean, 8488/327680 files, 133517/1310720 blocks

[root@localhost ~]# lvextend -L 6G /dev/testvg/mylv1 
  Size of logical volume testvg/mylv1 changed from 5.00 GiB (640 extents) to 6.00 GiB (768 extents).
  Logical volume mylv1 successfully resized.

[root@localhost ~]# e2fsck -f /dev/testvg/mylv1 
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/testvg/mylv1: 8488/327680 files (0.2% non-contiguous), 133517/1310720 blocks
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# resize2fs /dev/testvg/mylv1 
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/testvg/mylv1 to 1572864 (4k) blocks.
The filesystem on /dev/testvg/mylv1 is now 1572864 blocks long.

[root@localhost ~]# mount /dev/testvg/mylv1 /users/
[root@localhost ~]# df -h /users/
Filesystem                Size  Used Avail Use% Mounted on
/dev/mapper/testvg-mylv1  5.8G  314M  5.2G   6% /users

[root@localhost ~]# ls /users/
adm    crash  empty  games   kerberos  local  log         mail  opt       run    tmp  yp
cache  db     ftp    gopher  lib       lock   lost+found  nis   preserve  spool  var

16、缩减mylv1至4G,确保缩减完成后数据可用

[root@localhost ~]#umount /users
[root@localhost ~]# e2fsck /dev/testvg/mylv1            /*检查下数据完整性
e2fsck 1.42.9 (28-Dec-2013)

[root@localhost ~]# resize2fs /dev/testvg/mylv1 4G        /*如果是缩小空间,应先缩小其数据空间
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/testvg/mylv1 to 1048576 (4k) blocks.
The filesystem on /dev/testvg/mylv1 is now 1048576 blocks long.

lvreduce -L 4G /dev/testvg/mylv1                    /*然后再缩小其lv逻辑空间
  WARNING: Reducing active logical volume to 4.00 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce mylv1? [y/n]: y
  Size of logical volume testvg/mylv1 changed from 6.00 GiB (768 extents) to 4.00 GiB (512 extents).
  Logical volume mylv1 successfully resized.
[root@localhost ~]# mount /dev/testvg/mylv1 /users/ /*挂载

[root@localhost ~]# df -h /users/                   /*查看空间利用率,依然是314M
Filesystem                Size  Used Avail Use% Mounted on
/dev/mapper/testvg-mylv1  3.9G  314M  3.3G   9% /users
[root@localhost ~]# ls /users/                      /*查看内容依然还在
adm    crash  empty  games   kerberos  local  log         mail  opt       run    tmp  yp
cache  db     ftp    gopher  lib       lock   lost+found  nis   preserve  spool  var

17、对mylv1创建快照,并通过备份数据,要求保留原有的属主属组等信息

[root@localhost ~]# lvcreate -L 3.5G -n mylv1_snap -s /dev/testvg/mylv1 
  Logical volume "mylv1_snap" created.
[root@localhost ~]# lvs
  LV         VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root       centos -wi-ao---- 27.46g                                                    
  swap       centos -wi-ao----  2.00g                                                    
  mylv1      testvg owi-aos---  4.00g                                                    
  mylv1_snap testvg swi-a-s---  3.50g      mylv1  0.00   

[root@localhost ~]# mount /dev/testvg/mylv1_snap /mnt
[root@localhost ~]# ls /mnt/
adm    db     games     lib    log         nis       run    var
cache  empty  gopher    local  lost+found  opt       spool  yp
crash  ftp    kerberos  lock   mail        preserve  tmp

原创文章,作者:396064847,如若转载,请注明出处:http://www.178linux.com/59480

(0)
396064847396064847
上一篇 2016-11-16
下一篇 2016-11-17

相关推荐

  • linux中bash的基础特性和基本文件管理命令

    linux的文件管理类命令 linux系统下的涉及到目录管理命令,主要有 mkdir,rmdir linux系统下也会用到许多文件查看类命令,包括cat,tac,head,tail,more,less等 linux系统下涉及的文件管理类命令主要有cp,mv,rm 本节我们主要说明文件管理类命令的使用方法。 cp命令的使用方法 首先,cp这个命令的作用是可以复…

    Linux干货 2016-09-26
  • Linux基础知识之文本处理三剑客sed

    处理文本的工具sed     1.sed是一种流编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”。接着用sed命令处理缓冲区中的内容,完成处理后,把缓冲区中的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出。sed主要用来自动编…

    Linux干货 2016-08-11
  • 马哥linux 0726作业

    1,课堂练习 显示当前日期,格式:2016-08-08 显示前天是星期几 设置当前日期为2008-08-08 08:00 使用字符端登陆时,显示当前登录终端号,主机名和当前时间 1,首先我们知道当前登录信息的配置文件在/etc/issue中 2,然后我们查询帮助看看issue的具体描述查到了相关的mingetty的命令当中关于当中的一些选项就是我们需要的三个…

    Linux干货 2016-08-04
  • 单用户模式破解密码与密码的加密

    当你坐在一台CentOS 6主机前,但是却不知道密码,要怎样破解掉密码进入系统呢? 答案很简单: 1、启动系统,当出现如下界面时,按任意键 2、你会看到这个画面 3、敲击“a”键,执行modifiy the kernel arguments 4、键入“1”键,进入单用户模式 5、至此,你已经成功进入系统,并修改了密码! 是不是觉得Centos6的系统这样安全…

    Linux干货 2016-09-13
  • Linux之函数

      Linux之函数     函数介绍函数function是由若干条shell命令组成的语句块,实现代码重用和模块化编程。它与shell程序形式上是相似的,不同的是它不是一个单独的进程,不能独立运行,而是shell程序的一部分。函数和shell程序比较相似,区别在于:Shell程序在子Shell中运行而Shell函数在…

    Linux干货 2016-08-25
  • 数据库基础

    数据库基础 一、数据模型 数据库模型图 1、层次模型、2、网状模型、 3、关系模型二维关系: 表:就是一个关系及属性的描述,如:学生(学好,姓名,性别,班级)       行:row, entity       列:colume,…

    Linux干货 2016-10-19

评论列表(1条)

  • 马哥教育
    马哥教育 2016-11-17 10:01

    写的很好,排版也很漂亮,期待你的下一次佳作,完全可以当作模版博客来推荐了