1、创建一个2G的文件系统,块大小为2048byte,预留1%可用空间,文件系统ext4,卷标为TEST,要求此分区开机后自动挂载至/test目录,且默认有acl挂载选项。
1)创建磁盘分区
[root@localhost ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x702945b2.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-2610, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +2G
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
2)格式化分区
[root@localhost ~]# mke2fs -t ext4 -b 2048 -m 1 -L "TEST" /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=TEST
OS type: Linux
Block size=2048 (log=1)
Fragment size=2048 (log=1)
Stride=0 blocks, Stripe width=0 blocks
131560 inodes, 1052240 blocks
10522 blocks (1.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=538968064
65 block groups
16384 blocks per group, 16384 fragments per group
2024 inodes per group
Superblock backups stored on blocks:
16384, 49152, 81920, 114688, 147456, 409600, 442368, 802816
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 31 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
3)加入到/etc/fstab文件中开机自动挂载
[root@localhost ~]# vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Mon Jul 25 09:00:01 2016
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/vg0-root / ext4 defaults 1 1
UUID=35e3b2e9-d658-4c9a-a97a-8e01e42321e4 /boot ext4 defaults 1 2
/dev/mapper/vg0-usr /usr ext4 defaults 1 2
/dev/mapper/vg0-var /var ext4 defaults 1 2
/dev/mapper/vg0-swap swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
LABEL="TEST" /test ext4 defaults,acl 0 0
4)查看磁盘分区信息
[root@localhost ~]# df -TH
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/vg0-root ext4 22G 393M 20G 2% /
tmpfs tmpfs 515M 0 515M 0% /dev/shm
/dev/sda1 ext4 199M 35M 154M 19% /boot
/dev/mapper/vg0-usr ext4 11G 2.1G 7.9G 21% /usr
/dev/mapper/vg0-var ext4 22G 122M 20G 1% /var
/dev/sdb1 ext4 2.1G 9.5M 2.1G 1% /test
2、写一个脚本,完成如下功能:
(1) 列出当前系统识别到的所有磁盘设备
(2) 如磁盘数量为1,则显示其空间使用信息,否则,显示最后一个磁盘上的空间使用信息
shell脚本:
#!/bin/bash
declare -a disk
disk=(`fdisk -l | sed -rn 's@Disk (/dev/sd[^0-9]{1,}):.*@\1@p'`)
disks=${#disk[@]}
cat /proc/partitions
if [ $disks -eq 1 ];then
echo -e "The it's frist Disk: ${disk[0]}"
df -h ${disk[0]}
else
echo -e "The it's last Disk: ${disk[$disks-1]}"
df -h ${disk[$disks-1]}
fi
执行结果:
[root@localhost 0826]# bash disk.sh
major minor #blocks name
8 0 104857600 sda
8 1 204800 sda1
8 2 62914560 sda2
8 16 20971520 sdb
8 32 20971520 sdc
8 48 20971520 sdd
8 64 20971520 sde
8 65 10490413 sde1
8 66 9446220 sde2
8 80 20971520 sdf
8 96 20971520 sdg
253 0 20971520 dm-0
253 1 2097152 dm-1
253 2 10485760 dm-2
253 3 20971520 dm-3
The it's last Disk: /dev/sdg
Filesystem Size Used Avail Use% Mounted on
- 477M 268K 477M 1% /dev
3、创建一个可用空间为1G的RAID1设备,要求其chunk大小为128k,文件系统为ext4,有一个空闲盘,开机可自动挂载至/backup目录。
(1)创建RAID1
[root@localhost 0826]# mdadm -C -c 128 -l 1 -x 1 -n 2 /dev/md1 /dev/sd{d,e,c}1
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
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md1 started.
(2)格式化/dev/md1
[root@localhost 0826]# mke2fs -t ext4 /dev/md1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
66240 inodes, 264800 blocks
13240 blocks (5.00%) reserved for the super user
First data block=0
(3)编辑/etc/fstab文件,开机自动挂载
[root@localhost 0826]# vim /etc/fstab
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
UUID="c0c2a27c-5c2d-4e7e-968a-3328d2143607" /backup ext4 defaults 0 0
(4)创建挂载目录,并挂载查看信息
[root@localhost 0826]# mkdir /backup
[root@localhost 0826]# mount -a
[root@localhost 0826]# df -TH
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/vg0-root ext4 22G 352M 20G 2% /
tmpfs tmpfs 515M 0 515M 0% /dev/shm
/dev/sda1 ext4 199M 35M 154M 19% /boot
/dev/mapper/vg0-usr ext4 11G 2.1G 7.9G 21% /usr
/dev/mapper/vg0-var ext4 22G 122M 20G 1% /var
/dev/sdb1 ext4 2.1G 3.2M 2.1G 1% /test
/dev/md1 ext4 1.1G 1.4M 979M 1% /backup
4、创建由三块硬盘组成的可用空间为2G的RAID5设备,要求其chunk大小为256k,文件系统为ext4,开机可自动挂载至/mydata目录。
(1)创建RAID5设备
[root@localhost 0826]# mdadm -C -c 256 -l 5 -n 3 /dev/md5 /dev/sd{b,c,d}1 mdadm: /dev/sdb1 appears to contain an ext2fs file system size=2104480K mtime=Mon Jul 25 13:14:42 2016 Continue creating array? y mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md5 started. [root@localhost 0826]# cat /proc/mdstat Personalities : [raid1] [raid6] [raid5] [raid4] md5 : active raid5 sdd1[3] sdc1[1] sdb1[0] ............
(2)格式化
[root@localhost 0826]# mke2fs -t ext4 /dev/md5 mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=64 blocks, Stripe width=128 blocks 132464 inodes, 529536 blocks 26476 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=545259520 17 block groups
(3)创建挂载目录并把挂载信息写在/etc/fstab文件中开机自动挂载
[root@localhost 0826]# mkdir /mydata [root@localhost 0826]# vim /etc/fstab # # /etc/fstab # Created by anaconda on Mon Jul 25 09:00:01 2016 ......... UUID="c0c2a27c-5c2d-4e7e-968a-3328d2143607" /backup ext4 defaults 0 0 UUID="4afb82dd-8360-47e6-bd7f-48ae4b725ac6" /mydata ext4 defaults 0 0
(4)查看挂载前与挂载后信息
[root@localhost 0826]# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/vg0-root 20511356 343140 19119640 2% / tmpfs 502068 0 502068 0% /dev/shm /dev/sda1 194241 34074 149927 19% /boot /dev/mapper/vg0-usr 10190136 1961752 7704096 21% /usr /dev/mapper/vg0-var 20511356 118400 19344380 1% /var ########################################################### [root@localhost 0826]# mount -a ########################################################### [root@localhost 0826]# df -TH Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/vg0-root ext4 22G 352M 20G 2% / tmpfs tmpfs 515M 0 515M 0% /dev/shm /dev/sda1 ext4 199M 35M 154M 19% /boot /dev/mapper/vg0-usr ext4 11G 2.1G 7.9G 21% /usr /dev/mapper/vg0-var ext4 22G 122M 20G 1% /var /dev/md5 ext4 2.1G 3.2M 2.0G 1% /mydata
(5)查看md5设备信息
[root@localhost 0826]# mdadm -D /dev/md5 /dev/md5: Version : 1.2 Creation Time : Mon Jul 25 15:31:13 2016 Raid Level : raid5 Array Size : 2118144 (2.02 GiB 2.17 GB) Used Dev Size : 1059072 (1034.25 MiB 1084.49 MB) Raid Devices : 3 Total Devices : 3 Persistence : Superblock is persistent Update Time : Mon Jul 25 15:35:50 2016 State : clean Active Devices : 3 Working Devices : 3 Failed Devices : 0 Spare Devices : 0 Layout : left-symmetric Chunk Size : 256K Name : localhost.localdomain:5 (local to host localhost.localdomain) UUID : 5e34ed0b:cde14ecb:71d73a72:ee68e848 Events : 18 Number Major Minor RaidDevice State 0 8 17 0 active sync /dev/sdb1 1 8 33 1 active sync /dev/sdc1 3 8 49 2 active sync /dev/sdd1
(6)将创建好的RAID设备信息写入配置文件
[root@localhost 0826]# mdadm -Ds > /etc/mdadm.conf [root@localhost 0826]# cat /etc/mdadm.conf ARRAY /dev/md5 metadata=1.2 name=localhost.localdomain:5 UUID=5e34ed0b:cde14ecb:71d73a72:ee68e848
五、创建lv
(1)创建物理卷(pv)
[root@localhost 0826]# pvcreate /dev/sdd Physical volume "/dev/sdb" successfully created Physical volume "/dev/sdc" successfully created Physical volume "/dev/sdd" successfully created Physical volume "/dev/sde" successfully created [root@localhost 0826]# pvs //查看物理卷 PV VG Fmt Attr PSize PFree /dev/sda2 vg0 lvm2 a--u 59.99g 7.99g //系统自带 /dev/sdb lvm2 ---- 20.00g 20.00g /dev/sdc lvm2 ---- 20.00g 20.00g /dev/sdd lvm2 ---- 20.00g 20.00g /dev/sde lvm2 ---- 20.00g 20.00g
(2).创建卷组(vg)
root@localhost 0826]# vgcreate vg1 /dev/sd{b,c,d,e} Volume group "vg1" successfully created [root@localhost 0826]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 vg0 lvm2 a--u 59.99g 7.99g /dev/sdb vg1 lvm2 a--u 20.00g 20.00g /dev/sdc vg1 lvm2 a--u 20.00g 20.00g /dev/sdd vg1 lvm2 a--u 20.00g 20.00g /dev/sde vg1 lvm2 a--u 20.00g 20.00g
(3)创建逻辑卷(lv)
[root@localhost 0826]# lvcreate -L 30G -n lv1 vg1 Logical volume "lv1" created. [root@localhost 0826]# lvs /dev/vg1/lv1 //查看lv1, 比之前多了20G LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv1 vg1 -wi-a----- 30.00g //此为创建的 [root@localhost 0826]#
-L:指定创建lv的大小
-n:指定lv的名称
vg1为在指定的vg组中创建lv
(4)、查看容量
[root@localhost 0826]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 vg0 lvm2 a--u 59.99g 7.99g /dev/sdb vg1 lvm2 a--u 20.00g 0 /dev/sdc vg1 lvm2 a--u 20.00g 9.99g /dev/sdd vg1 lvm2 a--u 20.00g 20.00g /dev/sde vg1 lvm2 a--u 20.00g 20.00g [root@localhost 0826]# lvs /dev/vg1/lv1 LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv1 vg1 -wi-a----- 30.00g [root@localhost 0826]# vgs VG #PV #LV #SN Attr VSize VFree vg0 1 4 0 wz--n- 59.99g 7.99g vg1 4 1 0 wz--n- 79.98g 49.98g
六、扩展lv
[root@localhost 0826]# lvextend -L +20G /dev/vg1/lv1 Size of logical volume vg1/lv1 changed from 30.00 GiB (7680 extents) to 50.00 GiB (12800 extents). Logical volume lv1 successfully resized. [root@localhost 0826]# lvs /dev/vg1/lv1 //查看lv1, 比之前多了20G LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv1 vg1 -wi-a----- 50.00g -L:添加指定容量,使用+numG格式,注意后面的lv1设备必须写全地址
七、扩展卷组
[root@localhost 0826]# vgs VG #PV #LV #SN Attr VSize VFree vg0 1 4 0 wz--n- 59.99g 7.99g vg1 4 1 0 wz--n- 79.98g 29.98g [root@localhost 0826]# vgextend vg1 /dev/sdf Physical volume "/dev/sdf" successfully created Volume group "vg1" successfully extended [root@localhost 0826]# vgs VG #PV #LV #SN Attr VSize VFree vg0 1 4 0 wz--n- 59.99g 7.99g vg1 5 1 0 wz--n- 99.98g 49.98g
所有硬盘大小:
[root@localhost 0826]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sr0 11:0 1 1024M 0 rom sda 8:0 0 100G 0 disk ├─sda1 8:1 0 200M 0 part /boot └─sda2 8:2 0 60G 0 part ├─vg0-root (dm-0) 253:0 0 20G 0 lvm / ├─vg0-swap (dm-1) 253:1 0 2G 0 lvm [SWAP] ├─vg0-usr (dm-2) 253:2 0 10G 0 lvm /usr └─vg0-var (dm-3) 253:3 0 20G 0 lvm /var sdb 8:16 0 20G 0 disk └─vg1-lv1 (dm-4) 253:4 0 50G 0 lvm sdc 8:32 0 20G 0 disk └─vg1-lv1 (dm-4) 253:4 0 50G 0 lvm sdd 8:48 0 20G 0 disk └─vg1-lv1 (dm-4) 253:4 0 50G 0 lvm sde 8:64 0 20G 0 disk sdf 8:80 0 20G 0 dis
原创文章,作者:Lii,如若转载,请注明出处:http://www.178linux.com/41217