本文主要介绍如何在活动的Linux系统中迁移Linux目录、文件系统,涉及到以下两个方面:
1) 将一个文件系统由某个分区迁移至新分区;
2) 将一个文件系统的某个目录迁移至新分区作为一个独立的文件系统;
在日常运维过程中,如果出现了Linux磁盘、系统故障或文件系统已满的情况,那么可能需要创建新的分区和文件系统,并保持原有目录或文件系统的文件权限、所有权及可能存在的命名管道等,这时就需要对原有的目录或文件系统进行迁移。
NO.1 创建新分区
可以使用新的硬盘或原有硬盘的剩余空间来创建新的磁盘分区,这里使用fdisk工具在原有硬盘上创建。
- [root@localhost ~]# fdisk /dev/sda
- Command (m for help): n
- First cylinder (1443-2610, default 1443):
- Using default value 1443
- Last cylinder or +size or +sizeM or +sizeK (1443-2610, default 2610): +5000M
- Command (m for help): a
- Partition number (1-7): 6
- 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.
- Syncing disks.
复制代码
创建新的分区以备稍后测试用。
参数n 创建新的分区 a 激活新建分区 w 将结果写入分区表,保存并退出fdisk
创建完后需重启系统。
NO.2 创建新的文件系统
使用mkfs命令格式化新分区为ext3格式
[root@localhost ~]# mkfs.ext3 /dev/sda6
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
611648 inodes, 1222940 blocks
61147 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1254096896
38 block groups
32768 blocks per group, 32768 fragments per group
16096 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 20 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
复制代码
NO.3 将文件系统由旧分区迁移至新分区(以/home为例)
方法:可以在/mnt下建立一个临时目录,把新分区挂载到临时目录下,然后将旧分区/home文件系统迁移至临时目录,最后卸载旧分区挂载新分区。
[root@localhost ~]# mkdir /mnt/home.tmp
[root@localhost ~]# mount /dev/sda6 /mnt/home.tmp/
[root@localhost ~]# cd /home
[root@localhost home]# cp -pr * /mnt/home.tmp/
[root@localhost /]# umount /dev/sda6
[root@localhost /]# umount /home
[root@localhost /]# mount /dev/sda6 /home
cp时注意要将原文件系统的目录结构、权限等内容一并copy到新的文件系统。
NO.4 将目录迁移至新的分区作为一个独立的文件系统(以/usr为例)
方法:可以在/mnt下建立一个临时目录,把新分区挂载到临时目录下,然后将目录/usr内的文件迁移到临时目录,然后将/usr重命名并新建一个/usr(新建的/usr权限必须与老的/usr相同),然后将新分区设备卸载后挂到/usr下
[root@localhost ~]# cd /mnt
[root@localhost mnt]# mkdir /mnt/usr.tmp
[root@localhost mnt]# mount /dev/sda7 /mnt/usr.tmp
[root@localhost mnt]# cp -rp /usr/* /mnt/usr.tmp
[root@localhost mnt]# mv /usr /usr.old
[root@localhost mnt]# mkdir /usr
[root@localhost mnt]# chmod xxx /usr (设置/usr 权限与/usr.old相同)
[root@localhost mnt]# umount /dev/sda7
[root@localhost mnt]# mount /dev/sda7 /usr
复制代码
/usr和/usr.old属性
drwxr-xr-x 15 root root 4096 Mar 19 15:22 usr
drwxr-xr-x 14 root root 4096 Mar 19 15:07 usr.old
NO.5 将新的分区设备信息加入/etc/fstab以便随系统启动
在/etc/fstab内容后加上以下两行内容
/dev/sda6 /home ext3 defaults 1 2
/dev/sda7 /usr ext3 defaults 1 2
在添加新的信息时需要删除重复的老的文件系统信息或者直接将老的文件系统信息更改为新的信息
NO.6 关于一个问题的说明
在网上看到,在迁移老的目录或文件系统到新的文件系统时,可以使用cp或者tar,
使用tar时,先将目录或老的文件系统打成tar包,然后再将tar恢复到新的文件系统。
不过我用Vmware虚拟机做的测试,tar起来会特别的慢 所以这种方法就没试。
另外,在使用cp时可能会出现不能正确复制某些文件、符号链接及其他特殊类型的文件,所以如果条件允许还是使用tar吧。
文章评论