第六章作业

2016.6.13 实验案例:迁移/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”文件系统。

4. 其他操作练习

 参照理论讲解部分的内容及示例,练习fdisk、mkfs、mount、umount命令的相关用法、扩展交换空间的操作步骤,以及/etc/fstab文件中挂载记录的格式和设置方法。

在虚拟机中添加一块80G的SCSI磁盘

在新硬盘中建立一个20G的分区,用于存放所有普通用户的宿主文件夹

当前系统中硬盘的信息:

    [root@test1 jason]# fdisk -l

    Disk /dev/sdd: 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: 0x00000000

    Disk /dev/sdc: 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: 0xf5a3b5cd

       Device Boot  Start End  Blocks   Id  System
    /dev/sdc1   1130610490413+  8e  Linux LVM
    /dev/sdc21307261010474380   8e  Linux LVM

    Disk /dev/sdb: 42.9 GB, 42949672960 bytes
    255 heads, 63 sectors/track, 5221 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: 0xe1ca40dd

       Device Boot  Start End  Blocks   Id  System
    /dev/sdb1   1130610490413+  83  Linux
    /dev/sdb21307261210490445   8e  Linux LVM

    Disk /dev/sde: 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/sda: 42.9 GB, 42949672960 bytes
    255 heads, 63 sectors/track, 5221 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: 0x0003aa34

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

    Disk /dev/mapper/vg_test-lv_root: 38.1 GB, 38126223360 bytes
    255 heads, 63 sectors/track, 4635 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_test-lv_swap: 4294 MB, 4294967296 bytes
    255 heads, 63 sectors/track, 522 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/mail_store-mbox: 26.8 GB, 26843545600 bytes
    255 heads, 63 sectors/track, 3263 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@test1 jason]#

截图

使用fdisk /dev/sde开始对新增的80G硬盘进行分区 [root@test1 jason]# fdisk /dev/sde Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0x1ed32520. 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): p

Disk /dev/sde: 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: 0x1ed32520

   Device Boot      Start         End      Blocks   Id  System

创建primary partition,并分配20G的空间:


Command (m for help): n
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): p

Disk /dev/sde: 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: 0x1ed32520

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

保存分区信息并退出:


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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@test1 jason]#

主要部分截图:

使用fdisk -l再次查看分区信息:


[root@test1 jason]# fdisk -l

Disk /dev/sdd: 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: 0x00000000

Disk /dev/sdc: 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: 0xf5a3b5cd

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1        1306    10490413+  8e  Linux LVM
/dev/sdc2            1307        2610    10474380   8e  Linux LVM

Disk /dev/sdb: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 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: 0xe1ca40dd

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1306    10490413+  83  Linux
/dev/sdb2            1307        2612    10490445   8e  Linux LVM

Disk /dev/sde: 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: 0x1ed32520

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

Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 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: 0x0003aa34

   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        5222    41430016   8e  Linux LVM

Disk /dev/mapper/vg_test-lv_root: 38.1 GB, 38126223360 bytes
255 heads, 63 sectors/track, 4635 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_test-lv_swap: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 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/mail_store-mbox: 26.8 GB, 26843545600 bytes
255 heads, 63 sectors/track, 3263 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@test1 jason]#

截图:

检测电脑是否已经识别:

[root@test1 jason]# partprobe /dev/sde
sde   sde1
[root@test1 jason]# partprobe /dev/sde

截图:

格式化的两种方法: 方法一,tab两次可以看到可以使用的命令,mkfs.ext4

[root@test1 jason]# mkfs
mkfs          mkfs.ext2     mkfs.ext4     mkfs.msdos
mkfs.cramfs   mkfs.ext3     mkfs.ext4dev  mkfs.vfat
[root@test1 jason]# mkfs

方法二, mkfs -t ext4 /dev/sde1

[root@test1 jason]# mkfs -t ext4 /dev/sde1
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 25 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@test1 jason]#

截图:

备份/home:

失败的尝试:

[root@test1 Documents]# tar ztvf home-bak.tar.gzip /home
tar (child): home-bak.tar.gzip: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
[root@test1 Documents]# tar ctvf home-bak.tar.gzip /home
tar: You may not specify more than one `-Acdtrux' or `--test-label' option
Try `tar --help' or `tar --usage' for more information.
[root@test1 Documents]# tar ctvf home.tar.gzip /home
tar: You may not specify more than one `-Acdtrux' or `--test-label' option
Try `tar --help' or `tar --usage' for more information.
[root@test1 Documents]#

截图: 原因:c-create, z-gzip,t-list。

成功备份:

[root@test1 Documents]# tar czvf home-bak.tar.gzip /home
tar: Removing leading `/' from member names
/home/
/home/bourn/
/home/bourn/.bash_logout
/home/bourn/.bash_profile
/home/bourn/.bashrc
...
[root@test1 Documents]#

核实: [root@test1 Documents]# ls -l home-bak.tar.gzip -rw-r--r-- 1 root root 66599 Jun 14 20:56 home-bak.tar.gzip [root@test1 Documents]#

截图:

* 新建的分区仍然挂载到“/home”目录下,需要导入系统中原有用户的数据
* 服务器在每次开机后能够自动挂载该分区

编辑/etc/fstab:

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

#
# /etc/fstab
# Created by anaconda on Tue May 24 00:02:03 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_test-lv_root /                       ext4    defaults        1 1
UUID=a844ed1c-d5cf-4898-90f7-e9f040f4426e /boot                   ext4    defaults        1 2
/dev/mapper/vg_test-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
/dev/sr0        /mnt    iso9660 defaults 0 0
UUID=8c475c04-aae8-4b2c-8e91-f93b9f26cf59       /mnt     ext4   defaults        0 0
/dev/sde1       /home   ext4    defaults        0 0
~
~
~
~
~
~
~
~
~
~
~
~
:wq

截图: 解释:

* /dev/sde1就是想要挂载的设备
* /home是挂载点
* ext4是文件系统类型
* defaults表示默认参数
* 第一个“0”是不进行dump备份
* 第二个“0”是开机的时候不进行硬盘检测

当前的mount信息:

[root@test1 Documents]# mount
/dev/mapper/vg_test-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)
/dev/sr0 on /mnt type iso9660 (ro)
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)
[root@test1 Documents]#

截图: 里面没有sde1的挂载信息

重启:

[root@test1 Documents]# reboot

Broadcast message from jason@test1
    (/dev/pts/0) at 21:09 ...

The system is going down for reboot NOW!
[root@test1 Documents]#

重启之后jason无法登陆:

Connecting to 192.168.0.111:23...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
CentOS release 6.5 (Final)
Kernel 2.6.32-431.el6.x86_64 on an x86_64
login: jason
Password:
Last login: Tue Jun 14 21:11:14 from 192.168.0.1
No directory /home/jason!
Logging in with home = "/".
-bash-4.1$

自动挂载是成功的:

恢复/home:

恢复完成:

再次测试,发现问题依旧,home下面的文件并没有恢复,原因是上面使用的是“ztvf”:

继续犯错: 分析应该使用xzvf, 还有要指定dir 即“-C”

继续测试+失败: 分析:上面过程中可以看出来,是把备份恢复到了home下面,而不是替换了home本身

继续,tar xzvf home-bak.tar.gzip -C / 分析:备份文件没有在home当中,而是/root/Documents路径下面 理解:extract gzip verbosely archivefile

go on 在/root/Documents路径下输入上面的命令后: 能够使用jason登录:

Connecting to 192.168.0.111:23...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
CentOS release 6.5 (Final)
Kernel 2.6.32-431.el6.x86_64 on an x86_64
login: jason
Password:
Last login: Tue Jun 14 21:13:34 on tty1
[jason@test1 ~]$ su

检查home目录下的文件:

[root@test1 jason]# cd /home
[root@test1 home]# ls
alice  bourn  home  jason  lost+found  sherry  teacher  test  test1  test2  test3  test81
[root@test1 home]#

results matching ""

    No results matching ""