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

相关推荐

  • Linux的文件系统的基础目录、帮助、部分命令介绍

    Linux的文件系统的基础目录、帮助、部分命令介绍 目录 linux基础目录 Linux获取帮助 相关控制命令,翻屏等 部分命令介绍 linux的基础目录 Bin: 共所有用户使用的基本命令,就是二进制程序 Sbin:系统的二进制程序。供管理员使用的 Boot:基本的加载器,引导加载器所依赖的各种静态文件 Dev:设备文件和特殊文件 其中设备分为两种类型,字…

    Linux干货 2016-10-29
  • DNS配置案例 二

    DNS配置案例 二 DNS配置案例 二 1 §·DNS的配置与案例 1 §·测试工具 3 §·主配置文件格式:/etc/named.conf 7 §·缓存名称服务器的配置: 8 §·配置解析一个正向区域 : 以loveme.com域为例: 10 §·配置解析一个反向区域 : 以loveme.com域为例: 15 §·配置从DNS服务器 19 §·问…

    Linux干货 2016-09-26
  • 磁盘和文件系统创建

    磁盘结构:主要是由盘体、控制电路、接口部件等组成。盘体里面封装了多个盘片的腔体;控制电路包含硬盘BIOS、主控芯片和硬件缓存等单元;接口部件包含电源、数据接口主从跳线等。读取硬盘数据时,主轴电机带动盘片旋转,副轴电机带动磁头臂将磁头放到相应的数据存放的柱面和磁道上,再由磁头读出相应扇区中的数据。(图1-1) 磁道(track):每个盘面被划分成了多个同心圆环…

    Linux干货 2016-02-14
  • 浅谈筛选日志中的IP地址信息

    作为运维人员,经常会需要会对日志中的某些重要信息进行筛选,比如说ip等参数。 案例一:筛选出IP地址信息 日志信息如下: [root@C67-X64-A1 hanghang]# cat test.txt  Jul 13 08:13:09 localhost sshd[14678]…

    系统运维 2016-07-22
  • vim编辑器

    一、vim编辑器简介     文本:ASCII,Unicode     文本编辑种类:     行编辑器:sed     全屏编辑器:nano,vi     v…

    Linux干货 2016-08-10
  • redis主/从配置及基于sentinel的故障转移

    一、NoSQL基础概念: ACID:原子性、一致性、隔离性、持久性;特性:数据量大、数据变化非常大(数据增长化、流量分布变化、数据间耦合结构变化)、数据源很多; CAP、BASECAP C:多个数据节点的的数据一致;A:用户发出请求后的有限时间范围内返回结果;P:network partition,网络发生分区后,服务是否依可用;CAP理论:一个分布式系统不…

    Linux干货 2017-01-27

评论列表(1条)

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

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