Linux备份系统镜像并对镜像中LVM分区文件的修改

Linux系统下对系统镜像的简单备份所用的命令就是dd命令了。

但是对备份的系统文件可能并不适合新的硬盘。


比如如下的应用环境:

1、目前在一个母板的SSD上已经安装好了一个Linux系统,并在Linux系统上部署好了具体的应用;

2、将母板上的SSD硬盘镜像备份出来,一则为了保护当前的Linux系统,防止系统损坏或丢失后的重复部署;

      二则可以将此份备份的镜像文件写入到另外的SSD硬盘中,克隆另外一份系统,以实现快速部署;

3、需要考虑的问题,就是母版中的网络配置。

  Linux在自动加载设备硬件时,使用的是udev来协助管理系统中各设备名称。

而克隆后的文件系统,在加载到新的主板上后,系统启动时则会出现网络接口名称错乱的问题。

这个问题就是udev服务在加载时,首先加载已经存在的网络接口的的配置文件

(/etc/udev/rules.d/70-persistent-net.rules),对不对应的接口再进行设备序号的添加,

最终导致克隆后的硬盘启动后网络接口是从1或2开始排序,而不是从eth0开始。

对于这种问题,可以有两种解决方法:

第一、直接修改母板中的70-persistent-net.rules、ifcfg-eth0/ifcfg-eth1配置文件,然后备份出所需的镜像,最后再修改回去;

第二、首先备份出母板中的镜像,然后对镜像进行修改,修改成通用的

(即:注释掉所有的70-persistent-net.rules、ifcfg-eth0/ifcfg-eth1网络接口文件)。


为了不改变母板中的系统,我采用了第二种方法,具体的操作步骤及命令如下:

###     将母板中的系统镜像备份到移动硬盘中   ### 

[root@localhost ~]# fdisk -l

Disk /dev/sda: 31.7 GB, 31675383808 bytes
255 heads, 63 sectors/track, 3850 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: 0x56af7af6

   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        3851    30419968   8e  Linux LVM

Disk /dev/mapper/VolGroup-lv_root: 28.0 GB, 27980201984 bytes
255 heads, 63 sectors/track, 3401 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/VolGroup-lv_swap: 3166 MB, 3166699520 bytes
255 heads, 63 sectors/track, 384 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@localhost ~]# dd if=/dev/sda of=/media/Box-20161202.img


###     卸载移动硬盘,并将移动硬盘上的母板镜像复制到其他的Linux系统中   ### 

这里用的系统是ubuntu,为了方便直接通过sudo -i命令切换到了root用户

login as: leekwen
leekwen@192.168.1.180's password:
Welcome to Ubuntu 12.04.4 LTS (GNU/Linux 3.11.0-15-generic i686)

 * Documentation:  https://help.ubuntu.com/

New release '14.04.1 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

Last login: Fri Dec  2 16:49:02 2016 from thinkpadx64.lan
leekwen@leekwen:~$ sudo -i
[sudo] password for leekwen:

root@leekwen:~# fdisk -l /dev/sdb

Disk /dev/sdb: 1000.2 GB, 1000204886016 bytes
255 heads, 63 sectors/track, 121601 cylinders, total 1953525168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00055e54

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   629147647   314572800   83  Linux
/dev/sdb2       629147648  1258293247   314572800   83  Linux
/dev/sdb3      1258293248  1953525167   347615960    b  W95 FAT32
root@leekwen:~# mount /dev/sdb2 /mnt/
root@leekwen:~# ls /mnt/
binary/                             profile-tail1.txt                   rpmforge.list
root@leekwen:~# ls /mnt/binary/Box-20161202.img -l
-rw-r--r--. 1 root root 31675383808 Dec  2 16:42 /mnt/binary/Box-20161202.img
root@leekwen:~# cp /mnt/binary/Box-20161202.img .
root@leekwen:~# fdisk -l Box-20161202.img

Disk Box-20161202.img: 31.7 GB, 31675383808 bytes
255 heads, 63 sectors/track, 3850 cylinders, total 61865984 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x56af7af6

           Device Boot      Start         End      Blocks   Id  System
Box-20161202.img1   *        2048     1026047      512000   83  Linux
Box-20161202.img2         1026048    61865983    30419968   8e  Linux LVM

看到这行:
              Device Boot                   Start      End            Blocks      Id      System
Box-20161202.img1       *        2048     1026047    512000     83     Linux

发现linux的文件系统是从第2048块开始的,所以挂载的时候应该从这个地方开始挂载,

由这条信息可以得出,扇区大小为512

Units = sectors of 1 * 512 = 512 bytes

所以我们需要从512*2048=1048576出开始挂载

root@leekwen:~#  echo $((512*2048))
1048576
root@leekwen:~# mount -o loop,offset=1048576  Box-20161202.img /mnt
root@leekwen:~# cd /mnt/
root@leekwen:/mnt# ls
config-2.6.32-431.el6.i686  initramfs-2.6.32-431.el6.i686.img    initrd-3.10.28kdump.img         System.map                      vmlinuz
efi                         initramfs-3.10.28.img                lost+found                      System.map-2.6.32-431.el6.i686  vmlinuz-2.6.32-431.el6.i686
grub                        initrd-2.6.32-431.el6.i686kdump.img  symvers-2.6.32-431.el6.i686.gz  System.map-3.10.28              vmlinuz-3.10.28
root@leekwen:/mnt# ls -l
total 54082
-rw-r--r--. 1 root root   109953 Nov 22  2013 config-2.6.32-431.el6.i686
drwxr-xr-x. 3 root root     1024 Nov 24 11:41 efi
drwxr-xr-x. 2 root root     1024 Dec  2 16:13 grub
-rw-------. 1 root root 16260076 Nov 24 11:52 initramfs-2.6.32-431.el6.i686.img
-rw-------. 1 root root 17004428 Nov 25 12:26 initramfs-3.10.28.img
-rw-------. 1 root root  4730164 Nov 24 14:51 initrd-2.6.32-431.el6.i686kdump.img
-rw-------. 1 root root  4884466 Nov 25 11:06 initrd-3.10.28kdump.img
drwx------. 2 root root    12288 Nov 24 11:23 lost+found
-rw-r--r--. 1 root root   190104 Nov 22  2013 symvers-2.6.32-431.el6.i686.gz
lrwxrwxrwx. 1 root root       24 Nov 25 11:03 System.map -> /boot/System.map-3.10.28
-rw-r--r--. 1 root root  1982877 Nov 22  2013 System.map-2.6.32-431.el6.i686
-rw-r--r--. 1 root root  2075204 Nov 25 11:03 System.map-3.10.28
lrwxrwxrwx. 1 root root       21 Nov 25 11:03 vmlinuz -> /boot/vmlinuz-3.10.28
-rwxr-xr-x. 1 root root  4002656 Nov 22  2013 vmlinuz-2.6.32-431.el6.i686
-rw-r--r--. 1 root root  4106432 Nov 25 11:03 vmlinuz-3.10.28

已经挂载成功,并看到系统盘中的数据,这个ID为83直接是系统的镜像,挂载相对不复杂。

而系统镜像Box-20161202.img2的格式却是LVM格式,这个系统的挂载就需要借助于工具了。

root@leekwen:~#  echo $((512*1026048))
525336576

root@leekwen:~# losetup /dev/loop0 Box-20161202.img -o 525336576
无pvscan命令,需要安装lvm2工具进行挂载:
root@leekwen:~# pvscan
The program 'pvscan' is currently not installed.  You can install it by typing:
apt-get install lvm2
root@leekwen:~# apt-get install lvm2
上面是安装lvm2工具
root@leekwen:~# pvscan
  PV /dev/loop0   VG VolGroup   lvm2 [29.01 GiB / 0    free]
  Total: 1 [29.01 GiB] / in use: 1 [29.01 GiB] / in no VG: 0 [0   ]

root@leekwen:~# vgchange -ay
  2 logical volume(s) in volume group "VolGroup" now active

root@leekwen:~# mount /dev/mapper/
control           VolGroup-lv_root  VolGroup-lv_swap
root@leekwen:~# mount /dev/mapper/VolGroup-lv_root /mnt/
root@leekwen:~# cd /mnt/
root@leekwen:/mnt# ls -l
total 124
dr-xr-xr-x.   2 root root  4096 Nov 28 09:27 bin
drwxr-xr-x.   2 root root  4096 Nov 24 11:24 boot
drwxr-xr-x.   2 root root  4096 Nov 24 11:24 dev
drwxr-xr-x. 120 root root 12288 Dec  2 16:20 etc
drwxr-xr-x.   3 root root  4096 Nov 24 12:32 home
dr-xr-xr-x.  18 root root 12288 Nov 28 09:27 lib
drwx------.   2 root root 16384 Nov 24 11:23 lost+found
drwxr-xr-x.   5 root root  4096 Dec  2 16:20 media
drwxr-xr-x.   2 root root  4096 Nov 23  2013 misc
drwxr-xr-x.   2 root root  4096 Sep 23  2011 mnt
drwxr-xr-x.   2 root root  4096 Nov 23  2013 net
-rw-r--r--.   1 root root     0 Nov 25 11:47 null
drwxr-xr-x.   4 root root  4096 Dec  2 15:33 opt
drwxr-xr-x.   2 root root  4096 Nov 24 11:24 proc
-rw-r--r--.   1 root root   137 Dec  2 15:38 profilerc
dr-xr-x---.  34 root root  4096 Dec  2 16:31 root
dr-xr-xr-x.   2 root root 12288 Nov 28 09:27 sbin
drwxr-xr-x.   2 root root  4096 Nov 24 11:25 selinux
drwxr-xr-x.   2 root root  4096 Sep 23  2011 srv
drwxr-xr-x.   2 root root  4096 Nov 24 11:24 sys
drwxrwxrwt.  33 root root  4096 Dec  2 16:15 tmp
drwxr-xr-x.  12 root root  4096 Nov 24 11:25 usr
drwxr-xr-x.  22 root root  4096 Nov 24 11:50 var

注释掉母板中的配置
root@leekwen:/mnt/etc/udev/rules.d# cat 70-persistent-net.rules
# This file was automatically generated by the /lib/udev/write_net_rules
# program, run by the persistent-net-generator.rules rules file.
#
# You can modify it, as long as you keep each rule on a single
# line, and change only the value of the NAME= key.

# PCI device 0x10ec:0x8168 (r8169)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:18:7d:a5:35:31", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

# PCI device 0x10ec:0x8168 (r8169)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:18:7d:a5:35:32", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"
root@leekwen:/mnt/etc/udev/rules.d# vim 70-persistent-net.rules
root@leekwen:/mnt/etc/udev/rules.d# cat 70-persistent-net.rules
# This file was automatically generated by the /lib/udev/write_net_rules
# program, run by the persistent-net-generator.rules rules file.
#
# You can modify it, as long as you keep each rule on a single
# line, and change only the value of the NAME= key.

# PCI device 0x10ec:0x8168 (r8169)
#SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:18:7d:a5:35:31", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

# PCI device 0x10ec:0x8168 (r8169)
#SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:18:7d:a5:35:32", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"

查看母板中的网络接口配置文件:
root@leekwen:/mnt# cat etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
HWADDR=00:18:7D:A5:35:31
TYPE=Ethernet
UUID=c1843b9a-490a-46f6-87c9-bc734d016f9d
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=dhcp
root@leekwen:/mnt# cat etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
HWADDR=00:18:7D:A5:35:32
TYPE=Ethernet
UUID=ac6f78b1-f75e-4ba4-b379-5b9353ad008d
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=dhcp

修改网络接口配置文件,如下:
root@leekwen:/mnt# cat etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
#HWADDR=00:18:7D:A5:35:31
TYPE=Ethernet
#UUID=c1843b9a-490a-46f6-87c9-bc734d016f9d
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=dhcp
root@leekwen:/mnt# cat etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
#HWADDR=00:18:7D:A5:35:32
TYPE=Ethernet
#UUID=ac6f78b1-f75e-4ba4-b379-5b9353ad008d
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=dhcp



卸载挂载、激活的LVM分区:
root@leekwen:~# vgchange -an VolGroup
  0 logical volume(s) in volume group "VolGroup" now active
root@leekwen:~# losetup -d /dev/loop0
root@leekwen:~#

按照上述步骤,系统修改完成了。

下步操作就是直接将目前的修改好的这个镜像,写入到新的SSD硬盘上。

1、linux系统下就用dd命令了;

2、windows下就可以用Win32DiskImager.exe,自己写入吧,这里就不赘述了。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Leekwen

您的鼓励,是我坚持更新的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值