# cd /var/instances
# mount -o loop ubuntu.img /mnt
# chroot /mnt
接下来怎么操作就不用我说了吧。。。如果chroot之后系统提示Cannot open /dev/urandom for reading: No such file or directory,那么你在chroot之前需要执行以下命令mount --bind /dev /mnt/dev,其他相似问题类似处理方法。
提取文件系统
在上一步你挂载镜像时是否遇到了问题?(mount: you must specify the filesystem type),原因很简单,你当时用这个镜像安装操作系统时肯定分了其他分区,并且还有偏移量。你必须将根分区(/)提取出来,然后就可以直接挂载了。
挂载有偏移量的镜像
# cd /var/instances
# sfdisk -l -uS ubuntu.img
-------- output of sfdisk command --------
Disk ubuntu.img: cannot get geometry
Disk ubuntu.img: 652 cylinders, 255 heads, 63 sectors/track
Warning: extended partition does not start at a cylinder boundary.
DOS and Linux will interpret the contents differently.
Units = sectors of 512 bytes, counting from 0
Device Boot Start End #sectors Id System
ubuntu.img1 * 2048 9437183 9435136 83 Linux
ubuntu.img2 9439230 10483711 1044482 5 Extended
ubuntu.img3 0 - 0 0 Empty
ubuntu.img4 0 - 0 0 Empty
ubuntu.img5 9439232 10483711 1044480 82 Linux swap / Solaris
从以上输出可以看到这个镜像其实包含了5个分区!!!
我们要想挂载第一个分区,需要知道它的偏移量为512*2048=1048576,然后挂载mount -o loop,offset=1048576 ubuntu.img /mnt/。
注: 偏移量是柱面的偏移数,实际偏移的字节数等于512*柱面偏移数,sfdisk命令输出的第三列即为柱面偏移数。
提取根(/)文件系统
# cd /var/instances
# losetup -f ubuntu.img
# losetup -a
/dev/loop0: [0811]:56885251 (/var/instances/ubuntu.img)
# sfdisk -l -uS /dev/loop0
Disk /dev/loop0: cannot get geometry
Disk /dev/loop0: 652 cylinders, 255 heads, 63 sectors/track
Warning: extended partition does not start at a cylinder boundary.
DOS and Linux will interpret the contents differently.
Units = sectors of 512 bytes, counting from 0
Device Boot Start End #sectors Id System
/dev/loop0p1 * 2048 9437183 9435136 83 Linux
/dev/loop0p2 9439230 10483711 1044482 5 Extended
/dev/loop0p3 0 - 0 0 Empty
/dev/loop0p4 0 - 0 0 Empty
/dev/loop0p5 9439232 10483711 1044480 82 Linux swap / Solaris
# losetup -d /dev/loop0
# losetup -f -o 1048576 ubuntu.img
# losetup -a
/dev/loop0: [0811]:56885251 (/var/instances/ubuntu.img), offset 1048576
# dd if=/dev/loop0 of=ubuntu.final.img
# mount -o loop ubuntu.final.img /mnt
# chroot /mnt
以上这些操作就是提取根(/)文件系统的步骤。首先找到ubuntu.img镜像的根所在的分区的偏移量,并用losetup命令挂载成loop设备,然后使用dd命令将该loop设备(/dev/loop0)转换成文件,而这个文件就是最终的镜像文件。
将内核文件及ramdisk提取出来:
# cd /var/instances
# mount -o loop ubuntu.final.img /mnt
# cp /mnt/boot/vmlinuz-3.2.0-23-generic kernel
# cp /mnt/boot/initrd.img-3.2.0-23-generic ramdisk 镜像制作2
当你看了之前的制作镜像步骤后会发现很麻烦,现在介绍一种比较简单的制作方法。
# qemu-img create ubuntu-12.10.img 10G
# mkfs.ext4 -F -q ubuntu-12.10.img
# kvm -hda ubuntu-12.10.img -cdrom ubuntu-12.10-server-amd64.iso -boot d -m 1024 -vnc 0.0.0.0:20
# mount -o loop ubuntu-12.10.img /mnt
# cp /mnt/boot/initrd.img-3.5.0-17-generic ramdisk
# cp /mnt/boot/vmlinuz-3.5.0-17-generic kernel
# tune2fs -L new-lable ubuntu-12.10.img
# vim /mnt/etc/fstab
edit to the format you like
LABEL=new-lable / ext4 defaults 0 0
/dev/vda / ext4 defaults 0 0
# umount /mnt
以上几条命令的意思是将系统安装在整个磁盘上,即磁盘不分区,也就意味着这需要你自己指定内核来启动系统,详情请参见instance-hostname.xml文件中的配置。
vcpu属性表示分配给虚拟机实例的最大CPU个数。其中cpuset表示该vcpu可以运行在哪个物理CPU上面,一般如果设置cpuset,那么placement就设置成static。current的意思是是否允许虚拟机使用较少的CPU个数(current can be used to specify whether fewer than the maximum number of virtual CPUs should be enabled)。vcpu下面的这几个属性貌似只在kvm与qemu中有。