實驗案例一:遷移/home分區

實驗案例一:遷移/home分區

(一)實驗環境

公司原來安裝的RHEL6服務器採用了自動分區的方案,隨着使用服務器的系統用戶數量不斷增多,根據分區經常面臨磁盤空間耗盡的情況,偶爾有幾次還導致系統無法啓動。爲了解決這些問題,現需要爲服務器新增加一塊SCSI硬盤,並將用戶目錄“/home”中的數據遷移到該硬盤中,要求遷移不能影響原有用戶賬號的使用。

(二)需求描述

  • 在虛擬機中添加一塊80G的SCSI磁盤。

  • 在新硬盤中建立一個20G的分區,用於存放所有普通用戶的宿主文件夾

  • 新建的分區仍然掛載到“/home”目錄下,需要導入系統中原有用戶的數據

  • 服務器在每次開機後能夠自動掛載該分區

(三)推薦步驟

1. 關機後添加新硬盤,重新開機進入RHEL6系統
2. 分區並格式化
    1. 使用fdisk命令對新硬盤進行分區,建立一個20GB的主分區(“/dev/sdb1”)
    2. 執行“partprobe”更新分區表信息,使用mkfs命令將上一步新建的分區格式化爲ext4文件系統

3. 遷移“/home”分區
    1. 轉移原有“/home"目錄中的內容並做好備份
    2. 修改“/etc/fstab”文件中的分區掛載設置。
    3. 按“/etc/fstab”文件中的設置將新建的分區掛載到“/home”目錄下。
    4. 將本步驟1中備份的數據複製到新掛載的“/home”文件系統。



實施過程

實驗案例:遷移/home分區

實驗環境

公司原來安裝的RHEL6服務器採用了自動分區的方案,隨着使用服務器的系統用戶數量不斷增多,根據分區經常面臨磁盤空間耗盡的情況,偶爾有幾次還導致系統無法啓動。爲了解決這些問題,現需要爲服務器新增加一塊SCSI硬盤,並將用戶目錄“/home”中的數據遷移到該硬盤中,要求遷移不能影響原有用戶賬號的使用。

需求描述

在虛擬機中添加一塊80G的SCSI磁盤。

選擇添加硬盤
Hardware Type

選擇硬盤類型
Select a Disk Type


選擇硬盤
Select a Disk


選擇硬盤容量
Specify Disk Capacity


指定存儲路徑
Specify disk file


添加硬盤完成
VM settings


在初始界面可以看到新增加的硬盤
Devices

查看硬盤信息:

[root@test2 jason]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000d0a36

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        2611    20458496   8e  Linux LVM

Disk /dev/sdb: 85.9 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xa8f24694

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        2612    20980858+  83  Linux
/dev/sdb2            2613        5224    20980890   83  Linux
/dev/sdb3            5225       10443    41921617+  83  Linux

Disk /dev/sdc: 85.9 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/mapper/vg_jason-lv_root: 18.8 GB, 18798870528 bytes
255 heads, 63 sectors/track, 2285 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/mapper/vg_jason-lv_swap: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

[root@test2 jason]#

新建的分區:





在新硬盤中建立一個20G的分區,用於存放所有普通用戶的宿主文件夾


分區前查看sdc信息:

[root@test2 jason]# 
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x764db371.
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): 

**Disk /dev/sdc: 85.9 GB, 85899345920 bytes**
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x764db371

   Device Boot      Start         End      Blocks   Id  System

截圖:

分區;查看信息;保存:

[root@test2 jason]# fdisk /dev/sdc
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x8967e34e.
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):                 
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-10443, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-10443, default 10443): +20G

Command (m for help):                 

Disk /dev/sdc: 85.9 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x8967e34e

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1        2612    20980858+  83  Linux

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

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

格式化成ext4:

[root@test2 jason]# 
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
1313760 inodes, 5245214 blocks
262260 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
161 block groups
32768 blocks per group, 32768 fragments per group
8160 inodes per group
Superblock backups stored on blocks:
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
    4096000

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

This filesystem will be automatically checked every 23 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@test2 jason]#

截圖:

探測/dev/sdc1:

[root@test2 jason]# 
sdc   sdc1
[root@test2 jason]# partprobe /dev/sdc

查看

[root@test2 jason]# fdisk -l /dev/sdc1

Disk /dev/sdc1: 21.5 GB, 21484399104 bytes
255 heads, 63 sectors/track, 2611 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

[root@test2 jason]#

截圖:





新建的分區仍然掛載到“/home”目錄下,需要導入系統中原有用戶的數據

備份/home

具體方式tar czvf home.bak.tar.gzip /root/Documents

[root@test2 ~]# tar czvf home.bak.tar.gzip /home/*

截圖:

查看備份後的信息:

[root@test2 ~]# ls
anaconda-ks.cfg  Downloads          install.log         man.txt    Pictures   Videos
Desktop          etc.tar.gz         install.log.syslog  Music      Public
Documents        home.bak.tar.gzip  man2.txt            mvtotrash  Templates
[root@test2 ~]#


截圖:





服務器在每次開機後能夠自動掛載該分區

修改/etc/fstab

[root@test2 Documents]# vi /etc/fstab

#
# /etc/fstab
# Created by anaconda on Wed May 18 04:04:40 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/vg_jason-lv_root /                       ext4    defaults        1 1
UUID=12a13375-c595-4876-9fc2-3158f535861d /boot                   ext4    defaults        1 2
/dev/mapper/vg_jason-lv_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

~
~
~
~
~
~
~
~
~
~
~
~
~
~

截圖:

掛載,mount

[root@test2 Documents]# mount /home

查看mount信息:

[root@test2 Documents]# mount
/dev/mapper/vg_jason-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
vmware-vmblock on /var/run/vmblock-fuse type fuse.vmware-vmblock (rw,nosuid,nodev,default_permissions,allow_other)
gvfs-fuse-daemon on /root/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev)
/dev/sr0 on /media/CentOS_6.5_Final type iso9660 (ro,nosuid,nodev,uhelper=udisks,uid=0,gid=0,iocharset=utf8,mode=0400,dmode=0500)

[root@test2 Documents]#

截圖:

掛載後home目錄下只有lost+found:

[root@test2 Documents]# cd /home
[root@test2 home]# ls
lost+found
[root@test2 home]#

截圖:

恢復/home:

[root@test2 ~]# tar xzvf home.bak.tar.gzip -C /home

截圖:

恢復結果:

[root@test2 ~]# cd /home
[root@test2 home]# ls
home  lost+found
[root@test2 home]#

截圖:

分析:可能是恢復路徑的問題

恢復路徑得寫/:

[root@test2 Documents]# cd /home
[root@test2 home]# ls
home
[root@test2 home]# rm -rf home
[root@test2 home]# ls

繼續恢復:

[root@test2 ~]# tar xzvf home.bak.tar.gzip -c /

恢復完成:

[root@test2 ~]# cd /home
[root@test2 home]# ls
backup  jason

截圖:




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章