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)
上一篇 2016-11-16 22:43
下一篇 2016-11-17 17:52

相关推荐

  • 马哥教育网络班22期第二周课程练习

    1、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示? cp命令:copy 源文件;目标文件;    单源复制:cp [OPTION]… [-T] SOURCE DEST    多源复制:cp [OPTION]… SOURCE… DIRECTORY  …

    Linux干货 2016-08-22
  • 几个正则表达式和find查找语句事例

    显示当前系统上root、fedora、或user1用户的默认shell; ~]# grep -E "^(root|bin|hadop)" /etc/passwd | cut -d: -f7 找出/etc/rc.d/init.d/functions文件中某单词后面跟一组小括号的行,形如: hello(); ~]#grep -E -o &qu…

    Linux干货 2016-10-30
  • linux的终端类型

    1、什么是终端 终端(Terminal)也称终端设备,是计算机网络中处于网络最外围的设备,主要用于用户信息的输入以及处理结果的输出等。 在早期计算机系统中,由于计算机主机昂贵,因此一个主机(IBM大型计算机)一般会配置多个终端,这些终端本身不具备计算能力,仅仅承担信息输入输出的工作,运算和处理均由主机来完成。 在个人计算机时代,个人计算机可以运行称为终端仿真…

    Linux干货 2016-10-19
  • 第二十一周作业

    1、回顾并详细总结MySQL的存储引擎、索引; 常用存储引擎的对比: 特点 MyISAM InnoDB MEMORY MERGE NDB 存储限制 有 64TB 有 没有 有 事务安全 支持 锁机制 表锁 行锁 表锁 表锁 行锁 B树索引 支持 支持 支持 支持 支持 哈希索引 支持 全文索引 支持 集群索引 支持 数据缓存 支持 支持 支持 索引缓存 支持…

    2017-07-19
  • Linux systemd管理

                                                      &nbsp…

    系统运维 2016-09-28
  • 运维学习笔记-看看别人家的Puppet代码

    这篇博客的目的是通过分析Forge上的Puppet模块来加深一些概念的理解,同时了解一些常用用法。 今天的例子是jfryman-nginx模块,它是原puppetlabs-nginx模块的升级版本,依赖3个Puppet公共模块:puppetlabs-apt,puppetlabs-stdlib和puppetlabs-concat。安装非常方便,puppet m…

    Linux干货 2016-06-23

评论列表(1条)

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

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