1 案例1:新建一個(gè)邏輯卷
1.1 問題
本例要求沿用前一天案例,使用分區(qū) /dev/vdb1 構(gòu)建 LVM 存儲(chǔ)坡椒,相關(guān)要求如下:
- 新建一個(gè)名為 systemvg 的卷組
- 在此卷組中創(chuàng)建一個(gè)名為 vo 的邏輯卷扰路,大小為180MiB
- 將邏輯卷 vo 格式化為 EXT4 文件系統(tǒng)
- 將邏輯卷 vo 掛載到 /vo 目錄,并在此目錄下建立一個(gè)測(cè)試文件 votest.txt倔叼,內(nèi)容為“I AM KING.”
1.2 方案
LVM創(chuàng)建工具的基本用法:
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- vgcreate 卷組名 物理設(shè)備.. ..
- lvcreate -L 大小 -n 邏輯卷名 卷組名
</pre>
1.3 步驟
實(shí)現(xiàn)此案例需要按照如下步驟進(jìn)行汗唱。
步驟一:創(chuàng)建卷組
1)新建名為systemvg的卷組
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# vgcreate systemvg /dev/vdb1
- Physical volume "/dev/vdb1" successfully created
- Volume group "systemvg" successfully created
</pre>
2)確認(rèn)結(jié)果
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# vgscan
- Reading all physical volumes. This may take a while...
- Found volume group "systemvg" using metadata type lvm2
</pre>
步驟二:創(chuàng)建邏輯卷
1)新建名為vo的邏輯卷
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# lvcreate -L 180MiB -n vo systemvg
- Logical volume "vo" created
</pre>
2)確認(rèn)結(jié)果
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# lvscan
- ACTIVE '/dev/systemvg/vo' [180.00 MiB] inherit
</pre>
步驟三:格式化及掛載使用
1)格式化邏輯卷/dev/systemvg/vo
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# mkfs.ext4 /dev/systemvg/vo
- .. ..
- Allocating group tables: done
- Writing inode tables: done
- Creating journal (4096 blocks): done
- Writing superblocks and filesystem accounting information: done
</pre>
2)掛載邏輯卷/dev/systemvg/vo
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# mkdir /vo //創(chuàng)建掛載點(diǎn)
- [root@server0 ~]# mount /dev/systemvg/vo /vo //掛載
- [root@server0 ~]# df -hT /vo/ //檢查結(jié)果
- Filesystem Type Size Used Avail Use% Mounted on
- /dev/mapper/systemvg-vo ext4 171M 1.6M 157M 1% /vo
</pre>
3)訪問邏輯卷/dev/systemvg/vo
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# cat /vo/votest.txt
- I AM KING.
</pre>
2 案例2:調(diào)整現(xiàn)有磁盤的分區(qū)
2.1 問題
本例要求沿用前一天案例,對(duì)磁盤/dev/vdb的分區(qū)表進(jìn)行調(diào)整丈攒,要求如下:不更改原有分區(qū)哩罪,利用剩余空間新增三個(gè)分區(qū),大小依次為:500MiB巡验、2000MiB际插、512MiB
然后再基于剛建立的 2000MiB 分區(qū)構(gòu)建新的 LVM 存儲(chǔ):
- 新的邏輯卷命名為 database,大小為50個(gè)物理擴(kuò)展單元(Physical Extent)显设,屬于 datastore 卷組
- 在 datastore 卷組中的所有邏輯卷框弛,其物理擴(kuò)展單元(Physical Extent)的大小為16MiB
- 使用 EXT3 文件系統(tǒng)對(duì)邏輯卷 database 格式化,此邏輯卷應(yīng)該在開機(jī)時(shí)自動(dòng)掛載到 /mnt/database 目錄
2.2 方案
創(chuàng)建卷組時(shí)捕捂,可以通過-s選項(xiàng)指定PE的大小瑟枫。
在給新建的邏輯卷分配空間時(shí)斗搞,空間大小只能是PE大小的倍數(shù)。
2.3 步驟
實(shí)現(xiàn)此案例需要按照如下步驟進(jìn)行慷妙。
步驟一:調(diào)整現(xiàn)有磁盤分區(qū)
1)新建擴(kuò)展分區(qū)(使用剩余可用空間)
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
[root@server0 ~]# fdisk /dev/vdb
Command (m for help): p //確認(rèn)原有分區(qū)表
.. ..
Device Boot Start End Blocks Id System
/dev/vdb1 2048 411647 204800 8e Linux LVM
/dev/vdb2 411648 4507647 2048000 83 Linux
/dev/vdb3 4507648 6555647 1024000 83 Linux
Command (m for help): n //新建分區(qū)
Partition type:
p primary (3 primary, 0 extended, 1 free)
e extended
Select (default e): e //類型指定為e(擴(kuò)展分區(qū))
Selected partition 4 //只一個(gè)可用編號(hào)榜旦,自動(dòng)選取
First sector (6555648-20971519, default 6555648): //起始位置默認(rèn)
Using default value 6555648
Last sector, +sectors or +size{K,M,G} (6555648-20971519, default 20971519):
Using default value 20971519 //結(jié)束位置默認(rèn)
Partition 4 of type Extended and of size 6.9 GiB is set
Command (m for help): p
.. ..
Device Boot Start End Blocks Id System
/dev/vdb1 2048 411647 204800 8e Linux LVM
/dev/vdb2 411648 4507647 2048000 83 Linux
/dev/vdb3 4507648 6555647 1024000 83 Linux
/dev/vdb4 6555648 20971519 7207936 5 Extended
</pre>
2)在擴(kuò)展分區(qū)中新建3個(gè)邏輯分區(qū)
創(chuàng)建第1個(gè)邏輯卷(由于主分區(qū)編號(hào)已用完,分區(qū)類型自動(dòng)選l邏輯分區(qū)):
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- Command (m for help): n
- All primary partitions are in use
- Adding logical partition 5 //分區(qū)編號(hào)5
- First sector (6557696-20971519, default 6557696): //起始位置默認(rèn)
- Using default value 6557696
- Last sector, +sectors or +size{K,M,G} (6557696-20971519, default 20971519): +500M
- //結(jié)束位置默認(rèn)
- Partition 5 of type Linux and of size 500 MiB is set
</pre>
創(chuàng)建第2個(gè)邏輯卷:
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- Command (m for help): n
- All primary partitions are in use
- Adding logical partition 6 //分區(qū)編號(hào)6
- First sector (7583744-20971519, default 7583744): //起始位置默認(rèn)
- Using default value 7583744
- Last sector, +sectors or +size{K,M,G} (7583744-20971519, default 20971519): +2000M
- //結(jié)束位置默認(rèn)
- Partition 6 of type Linux and of size 2 GiB is set
</pre>
創(chuàng)建第3個(gè)邏輯卷:
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- Command (m for help): n
- All primary partitions are in use
- Adding logical partition 7 //分區(qū)編號(hào)7
- First sector (11681792-20971519, default 11681792): //起始位置默認(rèn)
- Using default value 11681792
- Last sector, +sectors or +size{K,M,G} (11681792-20971519, default 20971519): +512M
- //結(jié)束位置默認(rèn)
- Partition 7 of type Linux and of size 512 MiB is set
</pre>
根據(jù)預(yù)計(jì)的用途調(diào)整分區(qū)類型(可選):
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
Command (m for help): t //修改
Partition number (1-7, default 7): 5 //第5個(gè)分區(qū)
Hex code (type L to list all codes): 8e //類型為8e(LVM)
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): t //修改
Partition number (1-7, default 7): 6 //第6個(gè)分區(qū)
Hex code (type L to list all codes): 8e //類型為8e(LVM)
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): t //修改
Partition number (1-7, default 7): 7 //第7個(gè)分區(qū)
Hex code (type L to list all codes): 82 //類型為82(交換分區(qū))
Changed type of partition 'Linux' to 'Linux swap / Solaris'
</pre>
確認(rèn)分區(qū)結(jié)果并保存:
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
Command (m for help): p
.. ..
Device Boot Start End Blocks Id System
/dev/vdb1 2048 411647 204800 8e Linux LVM
/dev/vdb2 411648 4507647 2048000 83 Linux
/dev/vdb3 4507648 6555647 1024000 83 Linux
/dev/vdb4 6555648 20971519 7207936 5 Extended
/dev/vdb5 6557696 7581695 512000 8e Linux LVM
/dev/vdb6 7583744 11679743 2048000 8e Linux LVM
/dev/vdb7 11681792 12730367 524288 82 Linux swap / Solaris
Command (m for help): w //保存退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks. //提示重啟
</pre>
3)刷新分區(qū)表
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# partprobe /dev/vdb
- [root@server0 ~]# reboot
</pre>
步驟二:新建卷組景殷、邏輯卷
1)新建卷組datastore,指定PE大小為16MiB
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# vgcreate -s 16MiB datastore /dev/vdb6
- Volume group "datastore" successfully created
- [root@server0 ~]# vgscan //確認(rèn)新建的卷組
- Reading all physical volumes. This may take a while...
- Found volume group "systemvg" using metadata type lvm2
- Found volume group "datastore" using metadata type lvm2
</pre>
2)新建邏輯卷database澡屡,大小設(shè)置為50個(gè)PE
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# lvcreate -l 50 -n database datastore
- Logical volume "database" created
- [root@server0 ~]# lvscan //確認(rèn)新建的邏輯卷
- ACTIVE '/dev/systemvg/vo' [180.00 MiB] inherit
- ACTIVE '/dev/datastore/database' [800.00 MiB] inherit
</pre>
步驟三:格式化及使用邏輯卷
1)格式化邏輯卷/dev/datastore/database
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# mkfs.ext3 /dev/datastore/database
- .. ..
- Allocating group tables: done
- Writing inode tables: done
- Creating journal (4096 blocks): done
- Writing superblocks and filesystem accounting information: done
</pre>
2)配置開機(jī)掛載
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# mkdir /mnt/database //創(chuàng)建掛載點(diǎn)
- [root@server0 ~]# vim /etc/fstab
- .. ..
- /dev/datastore/database /mnt/database ext3 defaults 0 0
</pre>
3)驗(yàn)證掛載配置
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# mount -a
- [root@server0 ~]# df -hT /mnt/database/ //確認(rèn)掛載點(diǎn)設(shè)備
- Filesystem Type Size Used Avail Use% Mounted on
- /dev/mapper/datastore-database ext3 772M 828K 715M 1% /mnt/database
</pre>
3 案例3:擴(kuò)展邏輯卷的大小
3.1 問題
本例要求沿用練習(xí)一猿挚,將邏輯卷 vo 的大小調(diào)整為 300MiB,要求如下:
- 原文件系統(tǒng)中的內(nèi)容必須保持完整
- 必要時(shí)可使用之前準(zhǔn)備的分區(qū) /dev/vdb5 來補(bǔ)充空間
- 注意:分區(qū)大小很少能完全符合要求的大小驶鹉,所以大小在270MiB和300MiB之間都是可以接受的
3.2 方案
對(duì)于已經(jīng)格式化好的邏輯卷绩蜻,在擴(kuò)展大小以后,必須通知內(nèi)核新大小室埋。
如果此邏輯卷上的文件系統(tǒng)是EXT3/EXT4類型办绝,需要使用resize2fs工具;
如果此邏輯卷上的文件系統(tǒng)是XFS類型姚淆,需要使用xfs_growfs孕蝉。
3.3 步驟
實(shí)現(xiàn)此案例需要按照如下步驟進(jìn)行。
步驟一:確認(rèn)邏輯卷vo的信息
1)找出邏輯卷所在卷組
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# lvscan
- ACTIVE '/dev/systemvg/vo' [180.00 MiB] inherit
- ACTIVE '/dev/datastore/database' [800.00 MiB] inherit
</pre>
2)查看該卷組的剩余空間是否可滿足擴(kuò)展需要
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# vgdisplay systemvg
- --- Volume group ---
- VG Name systemvg
- System ID
- Format lvm2
- Metadata Areas 1
- Metadata Sequence No 2
- VG Access read/write
- VG Status resizable
- MAX LV 0
- Cur LV 1
- Open LV 0
- Max PV 0
- Cur PV 1
- Act PV 1
- VG Size 196.00 MiB //卷組總大小
- PE Size 4.00 MiB
- Total PE 49
- Alloc PE / Size 45 / 180.00 MiB
- Free PE / Size 4 / 16.00 MiB //剩余空間大小
- VG UUID czp8IJ-jihS-Ddoh-ny38-j521-5X8J-gqQfUN
</pre>
此例中卷組systemvg的總大小都不夠300MiB腌逢、剩余空間才16MiB降淮,因此必須先擴(kuò)展卷組疗隶。只有剩余空間足夠哼御,才可以直接擴(kuò)展邏輯卷大小。
步驟二:擴(kuò)展卷組
1)將提前準(zhǔn)備的分區(qū)/dev/vdb5添加到卷組systemvg
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# vgextend systemvg /dev/vdb5
- Physical volume "/dev/vdb5" successfully created
- Volume group "systemvg" successfully extended
</pre>
2)確認(rèn)卷組新的大小
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# vgdisplay systemvg
- --- Volume group ---
- VG Name systemvg
- .. ..
- VG Size 692.00 MiB //總大小已變大
- PE Size 4.00 MiB
- Total PE 173
- Alloc PE / Size 45 / 180.00 MiB
- Free PE / Size 128 / 512.00 MiB //剩余空間已達(dá)512MiB
- VG UUID czp8IJ-jihS-Ddoh-ny38-j521-5X8J-gqQfUN
</pre>
步驟三:擴(kuò)展邏輯卷大小
1)將邏輯卷/dev/systemvg/vo的大小調(diào)整為300MiB
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# lvextend -L 300MiB /dev/systemvg/vo
- Extending logical volume vo to 300.00 MiB
- Logical volume vo successfully resized
</pre>
2)確認(rèn)調(diào)整結(jié)果
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# lvscan
- ACTIVE '/dev/systemvg/vo' [300.00 MiB] inherit
- ACTIVE '/dev/datastore/database' [800.00 MiB] inherit
</pre>
3)刷新文件系統(tǒng)大小
確認(rèn)邏輯卷vo上的文件系統(tǒng)類型:
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# blkid /dev/systemvg/vo
- /dev/systemvg/vo: UUID="d4038749-74c3-4963-a267-94675082a48a" TYPE="ext4"
</pre>
選擇合適的工具刷新大信锱狻:
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# resize2fs /dev/systemvg/vo
- resize2fs 1.42.9 (28-Dec-2013)
- Resizing the filesystem on /dev/systemvg/vo to 307200 (1k) blocks.
- The filesystem on /dev/systemvg/vo is now 307200 blocks long.
</pre>
確認(rèn)新大忻教琛(約等于300MiB):
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# mount /dev/systemvg/vo /vo/
- [root@server0 ~]# df -hT /vo
- Filesystem Type Size Used Avail Use% Mounted on
- /dev/mapper/systemvg-vo ext4 287M 2.1M 266M 1% /vo
</pre>
4 案例4:查找并處理文件
4.1 問題
本例要求采用不少于兩種方法完成以下任務(wù):
- 找出所有用戶 student 擁有的文件
- 把它們拷貝到 /root/findfiles/ 文件夾中
4.2 步驟
實(shí)現(xiàn)此案例需要按照如下步驟進(jìn)行系吩。
步驟一:確認(rèn)能找到指定的文件
1)確認(rèn)新版內(nèi)核的下載地址
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# find / -user student -type f
- find: ‘/proc/1853/task/1853/fdinfo/6’: 沒有那個(gè)文件或目錄
- find: ‘/proc/1853/fdinfo/6’: 沒有那個(gè)文件或目錄
- /var/spool/mail/student
- /home/student/.bash_logout
- /home/student/.bash_profile
- /home/student/.bashrc
- /home/student/.ssh/authorized_keys
- /home/student/.config/gnome-initial-setup-done
- /home/student/.config/monitors.xml
</pre>
對(duì)于上述操作中出現(xiàn)的/proc信息忽略即可。
步驟二:處理找到的文件
1)創(chuàng)建目標(biāo)文件夾
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# mkdir /root/findfiles
</pre>
2)拷貝找到的文件到目標(biāo)文件夾
以下兩種方法任選一種:
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# find / -user student -type f -exec cp -p {} /root/findfiles/ ;
- .. ..
- 或者
- [root@server0 ~]# \cp -p $(find / -user student -type f) /root/findfiles/
- .. ..
</pre>
3)確認(rèn)拷貝結(jié)果
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# ls -lhA /root/findfiles/
- 總用量 24K
- -rw-------. 1 student student 1.7K 7月 11 2014 authorized_keys
- -rw-r--r--. 1 student student 18 1月 29 2014 .bash_logout
- -rw-r--r--. 1 student student 193 1月 29 2014 .bash_profile
- -rw-r--r--. 1 student student 231 1月 29 2014 .bashrc
- -rw-r--r--. 1 student student 4 7月 11 2014 gnome-initial-setup-done
- -rw-r--r--. 1 student student 1.5K 7月 11 2014 monitors.xml
- -rw-rw----. 1 student mail 0 7月 11 2014 student
</pre>
5 案例5:Linux管理員 綜合測(cè)試
5.1 問題
根據(jù)本文提供的練習(xí)步驟完成所有練習(xí)案例妒蔚。
5.2 方案
開始練習(xí)之前穿挨,先依次重置虛擬機(jī)環(huán)境。
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@room9pc13 ~]# rht-vmctl reset classroom
- [root@room9pc13 ~]# rht-vmctl reset server
</pre>
5.3 步驟
實(shí)現(xiàn)此案例需要按照如下步驟進(jìn)行面睛。
步驟01:配置一個(gè)用戶
案例概述:
創(chuàng)建一個(gè)名為alex的用戶絮蒿,用戶ID是 3456。密碼是flectrag
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# useradd -u 3456 alex
- [root@server0 ~]# echo flectrag | passwd --stdin alex
</pre>
步驟02:創(chuàng)建用戶賬號(hào)和組
案例概述:
創(chuàng)建下列用戶叁鉴、組以及和組的成員關(guān)系:
- 一個(gè)名為adminuser的組
- 一個(gè)名為natasha的用戶土涝,其屬于adminuser,這個(gè)組是該用戶的從屬組
- 一個(gè)名為harry的用戶幌墓,屬于adminuser但壮,這個(gè)組是該用戶的從屬組
- 一個(gè)名為sarah的用戶冀泻,其在系統(tǒng)中沒有可交互的shell,并且不是adminuser組的成員用戶
- natasha蜡饵、harry弹渔、和sarah的密碼都要設(shè)置為flectrag
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
[root@server0 ~]# groupadd adminuser //添加組
[root@server0 ~]# useradd -G adminuser natasha //添加用戶
[root@server0 ~]# useradd -G adminuser harry
[root@server0 ~]# useradd -s /sbin/nologin sarah
[root@server0 ~]# echo flectrag | passwd --stdin natasha //設(shè)置密碼
[root@server0 ~]# echo flectrag | passwd --stdin harry
[root@server0 ~]# echo flectrag | passwd --stdin sarah
</pre>
步驟03:配置文件 /var/tmp/fstab 的權(quán)限
案例概述:
拷貝文件/etc/fstab到/var/tmp/fstab,配置文件/var/tmp/fstab的權(quán)限:
- 文件/var/tmp/fstab的擁有者是root用戶
- 文件/var/tmp/fstab屬于root組
- 文件/var/tmp/fstab對(duì)任何人都不可執(zhí)行
- 用戶natasha 能夠?qū)ξ募?var/tmp/fstab執(zhí)行讀和寫操作
- 用戶harry 對(duì)文件/var/tmp/fstab既不能讀溯祸,也不能寫
- 所有其他用戶(當(dāng)前的和將來的)能夠?qū)ξ募?var/tmp/fstab進(jìn)行讀操作
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# cp /etc/fstab /var/tmp/fstab //復(fù)制文件
- [root@server0 ~]# setfacl -m u:natasha:rw /var/tmp/fstab //添加個(gè)別用戶權(quán)限
- [root@server0 ~]# setfacl -m u:harry:- /var/tmp/fstab
</pre>
步驟04:配置一個(gè) cron 任務(wù)
案例概述:
為用戶natasha配置一個(gè)定時(shí)任務(wù)肢专,每天在本地時(shí)間14:23時(shí)執(zhí)行以下命令:
/bin/echo hiya
解題參考:
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
[root@server0 ~]# systemctl restart crond
[root@server0 ~]# systemctl enable crond
[root@server0 ~]# crontab -e -u natasha
23 14 * * * /bin/echo hiya
</pre>
步驟05:創(chuàng)建一個(gè)共享目錄
案例概述:
創(chuàng)建一個(gè)共享目錄/home/admins ,特性如下:
- /home/admins目錄的組所有權(quán)是adminuser
- adminuser組的成員對(duì)目錄有讀寫和執(zhí)行的權(quán)限焦辅。除此之外的其他所有用戶沒有任何權(quán)限(root 用戶能夠訪問系統(tǒng)中的所有文件和目錄)
- 在/home/admins目錄中創(chuàng)建的文件博杖,其組所有權(quán)會(huì)自動(dòng)設(shè)置為屬于adminuser組
- [注]此處所謂的共享目錄并不是指網(wǎng)絡(luò)共享,只是某個(gè)組成員共用
解題參考:
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# mkdir /home/admins
- [root@server0 ~]# chown :adminuser /home/admins
- [root@server0 ~]# chmod ug+rwx,o-rwx /home/admins //調(diào)整權(quán)限
- [root@server0 ~]# chmod g+s /home/admins //設(shè)置Set UID權(quán)限
</pre>
步驟06:安裝內(nèi)核的升級(jí)
案例概述:
新版內(nèi)核文件從以下地址獲瓤甑恰:
http://classroom.example.com/content/rhel7.0/x86_64/errata/Packages/
- 升級(jí)你的系統(tǒng)的內(nèi)核版本剃根,同時(shí)要滿足下列要求:
- 當(dāng)系統(tǒng)重新啟動(dòng)之后升級(jí)的內(nèi)核要作為默認(rèn)的內(nèi)核
- 原來的內(nèi)核要被保留,并且仍然可以正常啟動(dòng)
解題參考:
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# firefox \
- http://classroom.example.com/content/rhel7.0/x86_64/errata/Packages/
- //根據(jù)所給地址找到內(nèi)核文件前方,復(fù)制其下載地址
- [root@server0 ~]# wget \
- http://classroom.example.com/content/rhel7.0/x86_64/errata/Packages/kernel-3.10.0-123.1.2.el7.x86_64.rpm
- [root@server0 ~]# rpm -ivh kernel-3.10*.rpm //安裝新內(nèi)核(耐心等...)
- [root@server0 ~]# reboot //重啟以使新內(nèi)核生效
- [root@server0 ~]# uname -r
- 3.10.0-123.1.2.el7.x86_64 //確認(rèn)新內(nèi)核版本
</pre>
步驟07:綁定到外部驗(yàn)證服務(wù)
案例概述:
系統(tǒng) classroom.example.com 提供了一個(gè) LDAP 驗(yàn)證服務(wù)狈醉。您的系統(tǒng)需要按照以下要求綁定到這個(gè)服務(wù)上:
- 驗(yàn)證服務(wù)器的基本 DN 是:dc=example,dc=com
- 帳戶信息和驗(yàn)證信息都是由 LDAP 提供的
- 連接要使用證書進(jìn)行加密,證書可以在下面的鏈接中下載 :
- http://classroom.example.com/pub/example-ca.crt
- 當(dāng)正確完成配置后惠险,用戶 ldapuser0 應(yīng)該能夠登錄到您的系統(tǒng)中苗傅,但是沒有主目錄。當(dāng)您完成 autofs的題目之后班巩,才能生成主目錄
- 用戶ldapuser0的密碼是password
解題參考:
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# yum -y install sssd
- [root@server0 ~]# authconfig-tui //使用簡易配置工具
</pre>
根據(jù)提示完成用戶和認(rèn)證方式設(shè)置 ——
User Information:[*] Use LDAP
Authentication Method:[*] Use LDAP Authentication
根據(jù)提示選中 [*] Use TLS金吗,并設(shè)置下列參數(shù) ——
Server:classroom.example.com
Base DN:dc=example,dc=com
提示下載證書到 /etc/openldap/cacerts 目錄時(shí),另開一終端執(zhí)行:
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# cd /etc/openldap/cacerts/ //進(jìn)入CA機(jī)構(gòu)證書目錄
- [root@server0 ~]# wget http://classroom.example.com/pub/example-ca.crt
</pre>
然后回到 authconfig-tui 工具確認(rèn)趣竣,稍等片刻即可摇庙。
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# systemctl restart sssd
- [root@server0 ~]# systemctl enable sssd
- [root@server0 ~]# id ldapuser0 //驗(yàn)證LDAP用戶可用
- uid=1700(ldapuser0) gid=1700(ldapuser0) groups=1700(ldapuser0)
</pre>
步驟08:家目錄漫游
案例概述:
按照下述要求配置手動(dòng)掛載 LDAP 用戶的主目錄:
- classroom.example.com(172.25.254.254)通過 NFS 輸出 /home/guests 目錄到您的系統(tǒng),這個(gè)文件系統(tǒng)包含了用戶ldapuser0的主目錄遥缕,并且已經(jīng)預(yù)先配置好了
- ldapuser0用戶的主目錄是 classroom.example.com:/home/guests/ldapuser0
- ldapuser0的主目錄應(yīng)該掛載到本地的/home/guests/ldapuser0 目錄下
- 用戶對(duì)其主目錄必須是可寫的
- ldapuser0用戶的密碼是password
解題參考:
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
[root@server0 ~]# mkdir /home/guest/ldapuser0
[root@server0 ~]# mount classroom.example.com:/home/guests/ldapuser0 /home/guests/ldapuser0 //掛載LDAP家目錄
[root@server0 ~]# su - ldapuser0 -c 'pwd' //驗(yàn)證結(jié)果
/home/guests/ldapuser0
</pre>
步驟09:配置NTP網(wǎng)絡(luò)時(shí)間客戶端
案例概述:
配置您的系統(tǒng)卫袒,讓其作為一個(gè) classroom.example.com 的 NTP 客戶端
解題參考:
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# yum -y install chrony
- [root@server0 ~]# vim /etc/chrony.conf
-
server 0.rhel.pool.ntp.org iburst //注釋掉不可用server配置,
-
server 1.rhel.pool.ntp.org iburst
-
server 2.rhel.pool.ntp.org iburst
-
server 3.rhel.pool.ntp.org iburst
- server classroom.example.com iburst //添加新的配置
- .. ..
- [root@server0 ~]# systemctl restart chronyd
- [root@server0 ~]# systemctl enable chronyd
</pre>
步驟10:查找文件
案例概述:
找出所有用戶student擁有的文件单匣,并且把它們拷貝到/root/findfiles 目錄中
解題參考:
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# mkdir /root/findfiles
- [root@server0 ~]# find / -user student -type f -exec cp -p {} /root/findfiles/ ;
</pre>
步驟11:查找一個(gè)字符串
案例概述:
在文件/usr/share/dict/words中查找到所有包含字符串seismic的行:
- 將找出的行按照原文的先后順序拷貝到/root/wordlist文件中
- /root/wordlist文件不要包含空行夕凝,并且其中的所有行的內(nèi)容都必須是 /usr/share/dict/words文件中原始行的準(zhǔn)確副本
解題參考:
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# grep 'seismic' /usr/share/dict/words > /root/wordlist
</pre>
步驟12:創(chuàng)建一個(gè)歸檔
案例概述:
創(chuàng)建一個(gè)名為 /root/backup.tar.bz2 的歸檔文件,其中包含 /usr/local 目錄中的內(nèi)容户秤,tar 歸檔必須使用 bzip2 進(jìn)行壓縮
解題參考:
<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">
- [root@server0 ~]# tar -jcf /root/backup.tar.bz2 /usr/local/
</pre>