Linux 练习 - 磁盘存储和文件系统

1、创建一个 2G 的文件系统,块大小为 2048byte,预留 1% 可用空间,文件系统 ext4,卷标为 TEST,要求此分区开机后自动挂载至 /test 目录,且默认有 acl 挂载选项;
# 1、先准备有空余空间的硬盘,此处为 /dev/sdb ,可用空间20G

# 2、用fdisk创建分区
[root@centos7 data]# fdisk -l /dev/sdb

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

[root@centos7 data]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x6f11fc49.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-41943039, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +2G
Partition 1 of type Linux and of size 2 GiB is set

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x6f11fc49

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux

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

Calling ioctl() to re-read partition table.
Syncing disks.

# 3、格式化分区,指定块大小、预留空间、卷标
[root@centos7 data]# mkfs.ext4 -b 2048 -m 1 -L TEST /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=TEST
OS type: Linux
Block size=2048 (log=1)
Fragment size=2048 (log=1)
Stride=0 blocks, Stripe width=0 blocks
131072 inodes, 1048576 blocks
10485 blocks (1.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=269484032
64 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

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done 

# 4、将挂载信息添加到 /etc/fstab ,挂载为acl
[root@centos7 data]# echo UUID=`blkid | sed -nr 's/^\/dev\/sdb1.*"(.*)" T.*$/\1/p'` /test ext4 acl 0 0 >> /etc/fstab

# 5、创建挂载目录,立即生效挂载信息,查看挂载信息
[root@centos7 data]# mkdir /test
[root@centos7 data]# mount -a remount
[root@centos7 data]# df -h
Filesystem                       Size  Used Avail Use% Mounted on
devtmpfs                         894M     0  894M   0% /dev
tmpfs                            910M     0  910M   0% /dev/shm
tmpfs                            910M   11M  900M   2% /run
tmpfs                            910M     0  910M   0% /sys/fs/cgroup
/dev/mapper/centos_centos7-root   50G  5.4G   45G  11% /
/dev/mapper/centos_centos7-home   47G   39M   47G   1% /home
/dev/sda1                       1014M  193M  822M  20% /boot
tmpfs                            182M   12K  182M   1% /run/user/42
tmpfs                            182M     0  182M   0% /run/user/1003
tmpfs                            182M     0  182M   0% /run/user/0
/dev/sdb1                        2.0G  9.1M  1.9G   1% /test
2、写一个脚本,完成如下功能:

(1) 列出当前系统识别到的所有磁盘设备

(2) 如磁盘数量为1,则显示其空间使用信息

否则,则显示最后一个磁盘上的空间使用信息

#!/bin/bash
echo "系统中的所有磁盘设备:"
lsblk | grep -o '^sd[[:alpha:]]'
LASTDISK=`lsblk | grep -o '^sd[[:alpha:]]' | tail -1`
echo "最后一块磁盘的空间使用信息:"
lsblk /dev/$LASTDISK
3、将 CentOS6 的 CentOS-6.10-x86_64-bin-DVD1.iso 和 CentOS-6.10-x86_64-bin-DVD2.iso 两个文件,合并成一个 CentOS-6.10-x86_64-Everything.iso 文件,并将其配置为 yum 源。
# 1、在虚拟机添加两个光驱,插入两张光盘,对应的设备为 /dev/sr0 /dev/sr1

# 2、创建挂载目录
[root@localhost ~]# mkdir /mnt/cd{1,2}

# 3、将挂载到对应的目录
[root@localhost ~]# mount /dev/sr0 /mnt/cd1
[root@localhost ~]# mount /dev/sr1 /mnt/cd2

# 4、合并文件
## 创建合并文件夹
[root@localhost ~]# mkdir CentOS-6.10-x86_64-Everything
## 复制 cd1 文件
[root@localhost ~]# cp -a /mnt/cd1/* ./CentOS-6.10-x86_64-Everything/
## 复制 cd2 文件
[root@localhost ~]# cp -a /mnt/cd2/Packages/* ./CentOS-6.10-x86_64-Everything/Packages/

## 创建 iso 文件
[root@localhost ~]# mkisofs -r -o CentOS-6.10-x86_64-Everything.iso ./CentOS-6.10-x86_64-Everything/

## 挂载
[root@localhost ~]# mount -o loop CentOS-6.10-x86_64-Everything.iso /mnt/centos6/

# 5、配置 yum 源
## 创建 local.repo 文件
[root@localhost ~]# vim /etc/yum.repos.d/centos6.repo
### 填入以下内容
[local]
name=local base
baseurl=file:///mnt/centos6/
gpgcheck=0
4、创建一个可用空间为 1G 的 RAID1 设备,文件系统为 ext4,有一个空闲盘,开机可自动挂载至 /backup 目录
# 1、添加三块硬盘 /dev/sdb,/dev/sdc,/dev/sdd,在两块硬盘上分别创建一个1G的分区
## /dev/sdb
[root@localhost ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x867d76df.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-41943039, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +1G
Partition 1 of type Linux and of size 1 GiB is set

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x867d76df

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     2099199     1048576   83  Linux

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

Calling ioctl() to re-read partition table.
Syncing disks.

## /dev/sdc
[root@localhost ~]# fdisk /dev/sdc
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x2f2a90c9.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-41943039, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +1G
Partition 1 of type Linux and of size 1 GiB is set

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

Calling ioctl() to re-read partition table.
Syncing disks.

# /dev/sdd
[root@localhost ~]# fdisk /dev/sdd
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x29e81709.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-41943039, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +1G
Partition 1 of type Linux and of size 1 GiB is set

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

Calling ioctl() to re-read partition table.
Syncing disks.

# 2、创建 RAID
[root@localhost ~]# mdadm -C /dev/md0 -a yes -l 1 -n 2 /dev/sd{b,c}1 -x 1 /dev/sdd1

# 3、格式化分区
[root@localhost ~]# mkfs.ext4 /dev/md0

# 添加挂载
[root@localhost ~]# mkdir /backup
[root@localhost ~]# echo /dev/md0 /backup ext4 defaults 0 0  >> /etc/fstab
[root@localhost ~]# mount -a remount
5、创建由三块硬盘组成的可用空间为 2G 的 RAID5 设备,要求其 chunk 大小为 256k,文件系统为 ext4,开机可自动挂载至 /mydata 目录
# 创建3个分区 /dev/sdb2,/dev/sec2,/dev/sdd2,步骤略

# 创建 RAID
[root@localhost ~]# mdadm -C /dev/md1 -a yes -l 5 -n 3 -c 256 /dev/sd{b,c,d}2
[root@localhost ~]# mkfs.ext4 /dev/md1
[root@localhost ~]# echo /dev/md1 /mydata ext4 defaults 0 0 >> /etc/fstab
[root@localhost ~]# mount -a remount
6、创建一个至少有两个 PV 组成的大小为 20G 的名为 testvg 的 VG;要求 PE 大小为 16MB, 而后在卷组中创建大小为 5G 的逻辑卷testlv;挂载至 /users 目录
# 1、准备硬盘,此处准备两块 /dev/sdb,/dev/sdc

# 2、在 /dev/sdb 创建一个10G的分区,分区ID改为8e
[root@centos7 ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-41943039, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +10G
Partition 1 of type Linux and of size 10 GiB is set

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

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

Calling ioctl() to re-read partition table.
Syncing disks.

# 3、在 /dev/sdc 创建一个10G的分区,分区ID改为8e,操作方法同上
[root@centos7 ~]# fdisk /dev/sdc

# 4、查看分区
[root@centos7 ~]# lsblk
NAME                    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                       8:0    0  100G  0 disk 
├─sda1                    8:1    0    1G  0 part /boot
└─sda2                    8:2    0   99G  0 part 
  ├─centos_centos7-root 253:0    0   50G  0 lvm  /
  ├─centos_centos7-swap 253:1    0    2G  0 lvm  [SWAP]
  └─centos_centos7-home 253:2    0   47G  0 lvm  /home
sdb                       8:16   0   20G  0 disk 
└─sdb1                    8:17   0   10G  0 part 
sdc                       8:32   0   20G  0 disk 
└─sdc1                    8:33   0   10G  0 part 
sr0                      11:0    1  4.4G  0 rom  

# 5、创建 pv
[root@centos7 ~]# pvcreate /dev/sdb1 /dev/sdc1
  Physical volume "/dev/sdb1" successfully created.
  Physical volume "/dev/sdc1" successfully created.
  
## 查看 pv 信息
[root@centos7 ~]# pvs
  PV         VG             Fmt  Attr PSize   PFree
  /dev/sda2  centos_centos7 lvm2 a--  <99.00g 4.00m
  /dev/sdb1  testvg         lvm2 a--    9.98g 9.98g
  /dev/sdc1  testvg         lvm2 a--    9.98g 9.98g

# 6、创建 vg
[root@centos7 ~]# vgcreate -s 16M testvg /dev/sdb1 /dev/sdc1
  Volume group "testvg" successfully created
  
## 查看 vg 信息
[root@centos7 ~]# vgs
  VG             #PV #LV #SN Attr   VSize   VFree  
  centos_centos7   1   3   0 wz--n- <99.00g   4.00m
  testvg           2   1   0 wz--n- <19.97g <19.97g

# 7、创建 lv
[root@centos7 ~]# lvcreate -L 5G -n testlv testvg
  Logical volume "testlv" created.
  
## 查看 lv 信息 
[root@centos7 ~]# lvs
LV     VG             Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
home   centos_centos7 -wi-ao---- 46.99g                                                    
root   centos_centos7 -wi-ao---- 50.00g                                                    
swap   centos_centos7 -wi-ao----  2.00g                                                    
testlv testvg         -wi-a-----  5.00g 

# 8、格式化分区
[root@centos7 ~]# mkfs.ext4 /dev/testvg/testlv
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done 

# 9、查看分区信息
[root@centos7 ~]# blkid
/dev/mapper/centos_centos7-root: UUID="458d1621-f955-41da-9c61-4cda5b7543b0" TYPE="xfs" 
/dev/sda2: UUID="uv2ZRK-hGZh-0Nfa-3yAz-QXqy-ONu1-AbCOIj" TYPE="LVM2_member" 
/dev/sda1: UUID="f7cd1056-9888-4bfd-ae48-0eccace210e3" TYPE="xfs" 
/dev/sdb1: UUID="ZQiO6V-WdlI-aDYd-FSDP-0kjX-y5tm-2DdY6a" TYPE="LVM2_member" 
/dev/sr0: UUID="2019-09-11-18-50-31-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos" 
/dev/mapper/centos_centos7-swap: UUID="a0f7086a-edeb-446b-8c21-377f124045ea" TYPE="swap" 
/dev/mapper/centos_centos7-home: UUID="67154c0f-af61-46be-87ae-f0ef0620190a" TYPE="xfs" 
/dev/sdc1: UUID="rzKRqQ-qNhx-PCoD-hc7Z-XFD5-xx3b-2OvKDQ" TYPE="LVM2_member" 
/dev/mapper/testvg-testlv: UUID="d4c744a0-96b7-4eae-9572-df84a3547379" TYPE="ext4"

# 10、将挂载信息写入到 /etc/fstab
echo /dev/testvg/testlv /users ext4 defaults 0 0 >> /etc/fstab

# 11、创建挂载目录,立即生效挂载信息,查看挂载信息
[root@centos7 ~]# mkdir /users
[root@centos7 ~]# mount -a remount
[root@centos7 ~]# df -h
Filesystem                       Size  Used Avail Use% Mounted on
devtmpfs                         894M     0  894M   0% /dev
tmpfs                            910M     0  910M   0% /dev/shm
tmpfs                            910M   11M  900M   2% /run
tmpfs                            910M     0  910M   0% /sys/fs/cgroup
/dev/mapper/centos_centos7-root   50G  5.4G   45G  11% /
/dev/sda1                       1014M  193M  822M  20% /boot
/dev/mapper/centos_centos7-home   47G   39M   47G   1% /home
tmpfs                            182M   12K  182M   1% /run/user/42
tmpfs                            182M     0  182M   0% /run/user/1003
tmpfs                            182M     0  182M   0% /run/user/0
/dev/mapper/testvg-testlv        4.8G   20M  4.6G   1% /users
7、新建用户 archlinux,要求其家目录为 /users/archlinux,而后 su 切换至 archlinux 用户,复制 /etc/pam.d 目录至自己的家目录
[root@localhost ~]# useradd -d /users/archlinux archlinux
[root@localhost ~]# su - archlinux
[archlinux@localhost ~]$ cp -r /etc/pam.d/ .
[archlinux@localhost ~]$ ll
total 4
drwxr-xr-x. 2 archlinux archlinux 4096 May 30 17:18 pam.d
8、扩展 testlv 至 7G,要求 archlinux 用户的文件不能丢失
[root@localhost ~]# lvextend -r -L +2G /dev/testvg/testlv
[root@localhost ~]# ll /users/archlinux/
total 4
drwxr-xr-x. 2 archlinux archlinux 4096 May 30 17:18 pam.d
9、收缩 testlv 至 3G,要求 archlinux 用户的文件不能丢失
# 临时取消挂载,如果 umount 失败,可重启一下
[root@localhost ~]# umount /users
[root@localhost ~]# lvresize -r -L -4G /dev/testvg/testlv
[root@localhost ~]# mount -a
[archlinux@localhost ~]$ ll /users/archlinux/
total 4
drwxr-xr-x. 2 archlinux archlinux 4096 May 30 21:21 pam.d
10、对 testlv 创建快照,并尝试基于快照备份数据,验证快照的功能
# 创建快照
[root@localhost ~]# lvcreate -n testlv_snapshot -s -L 1G -p r /dev/testvg/testlv

# 挂载快照
[root@localhost ~]# mkdir /mnt/snapshot
[root@localhost ~]# mount -o ro /dev/testvg/testlv_snapshot /mnt/snapshot/
[root@localhost ~]# ll /mnt/snapshot/
total 20
drwx------. 6 archlinux archlinux  4096 May 30 21:23 archlinux
drwx------. 2 root      root      16384 May 30 21:15 lost+found

# 恢复快照(如果 umount 失败,可重启一下)
[root@localhost ~]# umount /mnt/snapshot
[root@localhost ~]# umount /users
[root@localhost ~]# lvconvert --merge /dev/testvg/testlv_snapshot2
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值