全部博文(25)
分类: 虚拟化
2011-09-26 21:26:26
一.克隆配置好的REHL 6.1
期间也google,baidu了如何操作:
参考帖子
按照帖子的方法copy virtual machine disk(VMDK),为了避免硬盘文件冲突,在启动前还是先在Edit Settings里面删除Virtual Disk,然后再添加已存在的复制完的硬盘文件,如下图
不过还是在重命名VMDK的时候学到了几条命令,也算是值了。
我更喜欢用命令行克隆硬盘文件
用vSphere CLI编辑:
C:\Program Files\VMware\VMware vSphere CLI>vmkfstools.pl --server 10.100.100.170
-i /vmfs/volumes/datastore1/bidb1/bidb1.vmdk /vmfs/volumes/datastore1/bidb2/bid
b2.vmdk
Enter username: root
Enter password:
Attempting to clone disk [datastore1] bidb1/bidb1.vmdk
Successfully cloned disk [datastore1] bidb1/bidb1.vmdk to [datastore1] bidb2/bid
b2.vmdk
然后copy源*.vmx文件到bidb2目录下,重命名为bidb2.vmx.
下一步就是新建虚拟机的过程了,只不过不创建新的Virtual Disk,选existing disk.然后再改hostname and IP address.
Renaming a virtual machine disk (VMDK) via the vSphere Management Assistant (vMA) or vSphere CLI (vCLI)
VMA方式的话需要在vSphere端配置SSH启用,方法:
参考文档:To enable local or remote TSM from the Direct Console User Interface (DCUI):
1.At the DCUI of the ESXi host, press F2 and provide credentials when prompted.
2.Scroll to Troubleshooting Options, and press Enter.
3.If you want to enable local TSM, select Local Tech Support and press Enter once. This allows users to log in to the virtual console of the ESXi host.
Note: In ESXi 5.0, select Enable ESXi Shell and press Enter.
If you want to enable remote TSM, select Remote Tech Support (SSH) and press Enter once. This allows users to login via SSH on the virtual console of the ESXi host.
Note: In ESXi 5.0, select Enable SSH and press Enter.
4.Optionally, if you want to configure the timeout for TSM:
a.Select Modify Tech Support timeout and press Enter.
Note: In ESXi 5.0, select Modify ESXi Shell timeout and press Enter.
b.Enter the desired timeout value in minutes and press Enter.
6.Press Esc three times to return to the main DCUI screen.
To enable local or remote TSM from the vSphere Client:
1.Select the host and click the Configuration tab.
2.Click Security profile > Properties.
3.Click Local Tech Support or Remote Tech Support (SSH) and click Options.
Note: In ESXi 5.0, click SSH or ESXi Shell and click Options.
4.Choose the desired startup policy and click Start, then click OK.
5.Verify that the daemon selected in step 3 shows as running in the Services Properties window.
To configure the TSM timeout value using the vSphere Client:
1.Select the host and click the Configuration tab.
2.Click Advanced Settings.
3.Change the UserVars.TSMTimeOut field to the desired value in minutes.
Note: In ESXi 5.0, change the UserVars.ESXiShellTimeOut field to the desired value.
4.Click OK.
To access the local TSM:
1.At the main DCUI screen, press ALT+F1 simultaneously. This opens a virtual console window to the host.
2.Provide credentials when prompted.
Note: When typing the password, characters are not displayed on the console.
To access the remote TSM:
1.Open an SSH client.
2.Specify the IP address or domain name of the ESX host.
Notes:
?Directions may vary depending on what SSH client you are using. For more information, consult vendor documentation and support.
?By default, SSH works on TCP port 22.
用ESXi host console interface重命名VMDK,在此我开启了SSH,后用putty连接后更改,命令如下:
cd "/vmfs/volumes/datastore1/bidb2/"
vmkfstools -E bidb1.vmdk bidb2.vmdk
参考文档:Renaming using the virtual disk using the ESX/ESXi host console interface
To rename the disk and its files using the vmkfstools command in ESX/ESXi host's console interface:
1.Verify the virtual machine referring to the virtual machine disk is powered off and does not have outstanding snapshots.
2.Remove the virtual disk from the virtual machine's configuration:
a.Locate the virtual machine in the inventory using the vSphere Client.
b.Right-click the virtual machine select Edit Settings.
c.Select the virtual disk in question and take note of virtual device node (eg, SCSI 0:1) and the name of the datastore and directory in the Disk File field at the top-right.
d.Click the Remove button to disconnect the virtual disk from the virtual machine.
3.Open a console to the ESX or ESXi host. For more information, see Unable to connect to an ESX host using Secure Shell (SSH) (1003807) or Using Tech Support Mode in ESXi 4.1 (1017910).
4.Navigate to the virtual machine's directory using a command similar to:
cd "/vmfs/volumes/Datastore Name/Directory Name/"
5.Obtain a listing of the files within a directory using the command:
ls -l
For example:
total 320
-rw------- 1 root root 8684 Aug 30 10:53 examplevm.nvram
-rw------- 1 root root 21474836480 Aug 30 10:26 examplevm-flat.vmdk
-rw------- 1 root root 482 Aug 30 11:26 examplevm.vmdk
-rw------- 1 root root 0 Aug 30 10:33 examplevm.vmsd
-rwxr-xr-x 1 root root 2724 Aug 30 12:20 examplevm.vmx
-rw------- 1 root root 264 Aug 30 12:20 examplevm.vmxf
-rw-r--r-- 1 root root 39168 Aug 30 10:53 vmware.log
6.Rename a virtual disk using a command similar to:
vmkfstools -E OldName.vmdk NewName.vmdk
For example:
vmkfstools -E examplevm.vmdk examplevm-renamed.vmdk
Note: Specify the descriptor file; the associated extent file is renamed in the process.
7.Validate the files were renamed by listing the files within the directory using the command:
ls -l
For example:
total 320
-rw------- 1 root root 8684 Aug 30 10:53 examplevm.nvram
-rw------- 1 root root 21474836480 Aug 30 10:26 examplevm-renamed-flat.vmdk
-rw------- 1 root root 482 Aug 30 11:26 examplevm-renamed.vmdk
-rw------- 1 root root 0 Aug 30 10:33 examplevm.vmsd
-rwxr-xr-x 1 root root 2724 Aug 30 12:20 examplevm.vmx
-rw------- 1 root root 264 Aug 30 12:20 examplevm.vmxf
-rw-r--r-- 1 root root 39168 Aug 30 10:53 vmware.log
8.Re-add the virtual machine disk to the virtual machine's configuration.
a.Using the vSphere Client, select the virtual machine and click Edit Settings.
b.Click the Add... button above the virtual hardware list.
c.Select Hard Disk and Use an existing virtual disk.
d.Select the datastore and disk that was renamed.
e.Confirm that the same SCSI controller type and Device Node noted in step 2c.
f.Click the OK button to complete the configuration change.
正如前面提到的vShpere CLI模式,我也尝试过,不过不如host console interface容易用,操作步骤:
1.在安装VMware vSphere Client的电脑上安装vSphere CLI程序,默认在C盘,打开command prompt程序,后发现输入命令后不能识别,原因在命令的格式是*.pl
2.用CLI命令获得目录里文件的列表:vifs.pl --server
我输入的命令为:vifs.pl --server 10.100.100.170 -D '[datastore1] bidb2'
3.重命名一个虚拟硬盘:vmkfstools --server
4.校验重命名的文件命令:vifs.pl --server
详细命令可参考
二.用命令行工具克隆VMWare ESXi虚拟机
参考的第二个帖子:http://hi.baidu.com/hmilyyu8019/blog/item/72271b9608b83353d0135e48.html
首先,复制虚拟机配置文件:
进入vSphere Client:在左侧选择主机->右侧标签页上选择Summary->在DataStore项上单击右建->运行Browse DataStore在Datastore Browser里创建新虚拟机的文件夹,然后把要克隆的虚拟机文件夹中的.vmx文件复制过来
然后,用命令行执行克隆:
进入命令行界面(前提是在Server端开启了SSH,方法见参考文档:To enable local or remote TSM from the Direct Console User Interface (DCUI)): 可以用putty,也可以在主机上按Alt-F1,然后输入unsupported进入命令行。回来时按Alt-F2. 使用命令创建克隆虚拟机磁盘文件:
这个命令会显示进度,如果文件大的话时间会比较长。
这个命令是支持克隆快照的。如果要克隆某个快照(Snapshot)的话,以上BaseVM.vmdk就换成要克隆的那个Snapshot文件,如BaseVM-000008.vmdk,vmkfstools会最动生成合并的新文件。
在管理界面重新配置新虚拟机:
回到Datastore Browser界面,右键点击.vmx文件,选择Add to inventory,把这个新虚拟机加入管理界面。 在vSphere里面修改新虚拟机的配置:删除原来的Hard Disk,因为它是指向旧的虚拟机的磁盘文件,然后新加一个Hard Disk,并选择使用已有的磁盘文件,选择刚用命令行创建的那个文件。 启动新的虚拟机,完成。
至此,你就获得了一个一模一样的虚拟机。要注意的是,这个新的虚拟机与原虚拟机是完全一样的,包括硬盘里已经有的内容、配置等,因此如果同时开户有可能会有问题。比如说,如果是Linux的话,它的网络设备ID会是相同的,要解决这个问题,请参见:
上次克隆了虚拟机以后,发现新的虚拟机的网络设置会有点问题。这是因为克隆的虚拟机会与原来的那个有同样的MAC地址。当你启动它以后,vmware会自动为它分配一个新的MAC地址,而Linux会认为这是一个新的网络接口(或网卡),但机器里的配置文件可能还引用旧的网络接口。比如说,你的网络配置文件/etc/network/interfaces文件里可能会引用eth0,而虚拟机分配新的MAC地址以后,你的Linux里的网络接口可能就是eth1了。
检查是否有这个问题,可以用ifconfig看一下,列出的是否与配置文件中的一致。如果不同,则可以用下面的方法解决。
打开文件 /etc/udev/rules.d/70-persitent-net.rules
里面的内容看似这样:
可以看到这里了两个MAC地址。第一个是与原来那个虚拟机相同。那么要做的是把第一条删掉(或者要保险的话注释掉),第二条改成eth0。如下: