- windows
- 准备虚拟机
安装配置好一个用来制作模版的虚拟机 - 导出windows的mbr到文件(仅适用于mbr分区表的windows操作系统),基于lvm(基于文件需要使用loop设备)
dd if=/dev/mapper/vg_lv_name of=/path/to/windows_tpl.mbr count=1 bs=512
- 生成模板磁盘的分区设备
kpartx -av /dev/mapper/vg_lv_name
4.复制模板数据到文件(或分区)
ntfsclone -r -O windows_tpl.img /dev/mapper/vg_lv_namep1
5.删除分区设备
kpartx -dv /dev/mapper/vg_lv_name
- linux(仅适用于半虚拟化)
1.打包整个系统tar cpzf test.tar.gz --exclude=/os_tpl.tar.gz /
2.建立虚拟磁盘
dd if=/dev/zero of=os_tpl.img bs=1 count=1 seek=10G mkfs.ext4 test.img mount -o loop test.img /mnt
3.释放系统文件到虚拟磁盘
tar -zxSf os_tpl.tar.gz -C /mnt/os_tpl
4.挂载系统所需目录(无此目录可创建),然后chroot
sudo mkdir -p /mnt/os_tpl/{proc,sys,dev,dev/pts} sudo mount -t proc proc /mnt/os_tpl/proc/ sudo mount -t sysfs sys /mnt/os_tpl/sys/ sudo mount -o bind /dev /mnt/os_tpl/dev/ sudo mount -o bind /dev/pts /mnt/os_tpl/dev/pts/ sudo chroot /mnt/os_tpl
5.chroot后更新系统并安装linux-virtual内核,当然,不要忘了更新 grub(centos同理)
apt-get update & apt-get upgrade apt-get install linux-virtual apt-get purge grub2 grub-pc apt-get install grub update-grub
6.换了内核后还需要更新和配置几个系统文件,menu.lst,fstab,hvc0.conf,以便新内核能在 Xen 虚拟环境里正常启动
# vi /boot/grub/menu.lst Kernel command line: root=/dev/xvda1 xencons=tty console=tty1 console=hvc0 nosep nodevfs ramdisk_size=32768 ip_conntrack.hashsize=8192 nf_conntrack.hashsize=8192 ro devtmpfs.mount=1
# vi /etc/fstab /dev/xvda1 / ext4 noatime,errors=remount-ro 0 1 /dev/xvda2 none swap sw 0 0 tmpfs /dev/shm tmpfs nodev,nosuid,noexec,mode=1777 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/xvdb /home ext4 noatime,errors=remount-ro 0 1
# vi /etc/init/hvc0.conf # hvc0 - getty # # This service maintains a getty on hvc0 from the point the system is # started until it is shut down again. start on stopped rc RUNLEVEL=[2345] stop on runlevel [!2345] respawn exec /sbin/getty -8 38400 hvc0
7.退出chroot并逆顺序卸载刚才挂载的目录
umount /mnt/os_tpl/proc/ umount /mnt/os_tpl/sys/ umount /mnt/os_tpl/dev/pts/ umount /mnt/os_tpl/dev/ umount /mnt/os_tpl
# xl create test.cfg
- 准备虚拟机
- windows
1.创建磁盘/磁盘镜像lvcreate -L 20G -n xxx vg
2.从模版mbr恢复mbr到磁盘
dd if=2003_tpl.mbr of=/dev/mapper/vg_lv_name count=1 bs=512
3.创建分区设备
kpartx -av /dev/mapper/vg_lv_name
4.复制模版数据到分区设备
ntfsclone -r -O /dev/mapper/vg_lv_namep1 windows_tpl.img
4.根据需要在分区设备执行resize
ntfsresize -f -v /dev/mapper/vg_lv_namep1
5.删除分区设备
kpartx -dv /dev/mapper/vg_lv_name
- Linux
1.建立虚拟磁盘dd if=/dev/zero of=test.img bs=1 count=1 seek=10G
2.格式化虚拟磁盘
mkfs.ext4 test.img
3.挂载虚拟磁盘
mount -o loop test.img /mnt
4.释放模版文件到目标位置
tar -zxSf test.tar.gz -C /mnt/
# vi /etc/xen/test.cfg
bootloader = "pygrub"
root = "/dev/xvda ro"
name = "test"
vcpus = "1"
memory = "2048"
disk = [ "file:/root/test.img,xvda,w","file:/root/test.swp,xvdb,w" ]
vif = [ "bridge=xenbr0" ]
原文地址:http://blog.51cto.com/13187574/2084555
时间: 2024-10-02 18:06:41