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

相关推荐

  • 第十一周:OPENSSL和DNS

    详见我的51cto博客:http://afterdawn.blog.51cto.com/

    Linux干货 2016-12-09
  • N26-第五周作业-邢岩

    马哥门徒-N26-邢岩    我要继续今天的博客,《一万小时天才理论》里提到了髓鞘质,我是不是正在慢慢的给自己包裹一层髓鞘质呢?如果还没有,那么继续吧。    第一题:显示当前系统上root、fedora或user1用户的默认shell。      ~]# grep -E &#8…

    2017-02-24
  • 什么是CA??

       数字证书认证机构(英语:Certificate Authority,缩写为CA),也称为电子商务认证中心、电子商务认证授权机构,是负责发放和管理数字证书的权威机构,并作为电子商务交易中受信任的第三方,承担公钥体系中公钥的合法性检验的责任。 介绍     CA中心为每个使用公开密钥的用户发放一个…

    Linux干货 2017-07-17
  • 加密算法、https、dns(主从、视图)

    第九周 2018/1/29

    2018-01-29
  • 第二次作业

    一、只显示某目录下隐藏文件    答案1:#ls -d /tmp/*/    但是此答案是我在同学知道下完成,但是原理没明白 如果单独执行        #ls -d  &nbsp…

    Linux干货 2016-08-03
  • ☞文件服务器 – vsftpd

    文件服务器 – vsftpd 概述 FTP协议 vsftpd的配置文件 vsftpd + pam + file_db + virtual_vsftp_user vsftpd + pam + mysql + virtual_vsftp_user 登录时错误类型 总结 文件服务器是一种专供其他电脑访问文件和存储的程序。文件服务器通常比一般的个人电脑拥…

    Linux干货 2016-10-15

评论列表(1条)

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

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