实验案例一:迁移/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磁盘。
选择添加硬盘
选择硬盘类型
选择硬盘选择硬盘容量指定存储路径添加硬盘完成在初始界面可以看到新增加的硬盘查看硬盘信息:
[root@test2 jason]# fdisk -lDisk /dev/sda: 21.5 GB, 21474836480 bytes255 heads, 63 sectors/track, 2610 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x000d0a36 Device Boot Start End Blocks Id System/dev/sda1 * 1 64 512000 83 LinuxPartition 1 does not end on cylinder boundary./dev/sda2 64 2611 20458496 8e Linux LVMDisk /dev/sdb: 85.9 GB, 85899345920 bytes255 heads, 63 sectors/track, 10443 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk 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 LinuxDisk /dev/sdc: 85.9 GB, 85899345920 bytes255 heads, 63 sectors/track, 10443 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00000000Disk /dev/mapper/vg_jason-lv_root: 18.8 GB, 18798870528 bytes255 heads, 63 sectors/track, 2285 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00000000Disk /dev/mapper/vg_jason-lv_swap: 2147 MB, 2147483648 bytes255 heads, 63 sectors/track, 261 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00000000[root@test2 jason]#
新建的分区:
在新硬盘中建立一个20G的分区,用于存放所有普通用户的宿主文件夹
分区前查看sdc信息:
[root@test2 jason]# Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabelBuilding 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 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x764db371 Device Boot Start End Blocks Id System
截图:
分区;查看信息;保存:
[root@test2 jason]# fdisk /dev/sdcDevice contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabelBuilding 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)pPartition number (1-4): 1First cylinder (1-10443, default 1):Using default value 1Last cylinder, +cylinders or +size{K,M,G} (1-10443, default 10443): +20GCommand (m for help): Disk /dev/sdc: 85.9 GB, 85899345920 bytes255 heads, 63 sectors/track, 10443 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x8967e34e Device Boot Start End Blocks Id System/dev/sdc1 1 2612 20980858+ 83 LinuxCommand (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: LinuxBlock size=4096 (log=2)Fragment size=4096 (log=2)Stride=0 blocks, Stripe width=0 blocks1313760 inodes, 5245214 blocks262260 blocks (5.00%) reserved for the super userFirst data block=0Maximum filesystem blocks=4294967296161 block groups32768 blocks per group, 32768 fragments per group8160 inodes per groupSuperblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000Writing inode tables: doneCreating journal (32768 blocks): doneWriting superblocks and filesystem accounting information: doneThis filesystem will be automatically checked every 23 mounts or180 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/sdc1Disk /dev/sdc1: 21.5 GB, 21484399104 bytes255 heads, 63 sectors/track, 2611 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk 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 ~]# lsanaconda-ks.cfg Downloads install.log man.txt Pictures VideosDesktop etc.tar.gz install.log.syslog Music PublicDocuments 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 1UUID=12a13375-c595-4876-9fc2-3158f535861d /boot ext4 defaults 1 2/dev/mapper/vg_jason-lv_swap swap swap defaults 0 0tmpfs /dev/shm tmpfs defaults 0 0devpts /dev/pts devpts gid=5,mode=620 0 0sysfs /sys sysfs defaults 0 0proc /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]# lslost+found[root@test2 home]#
截图:
恢复/home:
[root@test2 ~]# tar xzvf home.bak.tar.gzip -C /home
截图:
恢复结果:
[root@test2 ~]# cd /home[root@test2 home]# lshome lost+found[root@test2 home]#
截图:
分析:可能是恢复路径的问题
恢复路径得写/:
[root@test2 Documents]# cd /home[root@test2 home]# lshome[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]# lsbackup jason