Tuesday, June 21, 2016

how to create qume image (raw) from lxc container

***general information***
- there is a lxc container name: u-trusty-lxc
  u-trysty-lxc rootfs location: /var/lib/lxc/u-trusty-lxc/rootfs/

1- create a working directory
$ mkdir -p convertlxc/mountp && cd convertlxc

2- create a raw disk call test.raw with 40GB size
$ qemu-img create -f raw -o size=40G test.raw

3- create partition, sda1 (primary partition)=35GB sda5 (logical partition) = 4GB , use n to create new partition and then finish use w to write and quit from fdisk
$ fdisk test.raw

4- mount and create file system for each partition
$ sudo losetup -f --show test.raw 
$ sudo  kpartx -a /dev/loop0
$ sudo mkfs.ext4 /dev/mapper/loop0p1
$ sudo mkswap /dev/mapper/loop0p5

5- mount the primary partition and copy content from lxc to it
$ sudo mount /dev/mapper/loop0p1 /mnt
$ sudo rsync -a -v /var/lib/lxc/u-trusty-lxc/rootfs/* /mnt

6- mount bind dev run sys and proc and chroot to /mnt
$ suod mount -o bind /dev /mnt/dev/
$ suod mount -o bind /run /mnt/run/ 
$ suod mount -o bind /sys /mnt/sys/
$ suod mount -o bind /proc /mnt/proc/ 
$ sudo chroot /mnt

7- install necessary packages. lxc does not a complete linux, so it misses run as grub, linux image, init, upstart to we have to install it.

$ sudo apt-get update
$ sudo apt-get -o DPkg::Options::="--force-confmiss" install linux-image-3.13.0-24-generic grub2


$ sudo apt-get -o DPkg::Options::="--force-confmiss" --reinstall install adduser bzip2 console-setup dash dhcp3-client eject gnupg ifupdown initramfs-tools iproute iputils-ping kbd less locales lsb-release makedev mawk module-init-tools net-tools netbase netcat-openbsd ntpdate passwd procps python rsyslog sudo tasksel tzdata ubuntu-keyring udev upstart ureadahead vim-tiny whiptail

8- update file /etc/fstab as below
#/etc/fstab
/dev/sda1 / ext4 defaults 0 0
/dev/sda5 none swap defaults 0 0


9- unmount all bind directory, dev run sys proc
 - exit from chrom
$ exit
- unmount
$ sudo umount /mnt/sys
$ sudo umount /mnt/run
$ sudo umount /mnt/dev
$ sudo umount /mnt/proc

10- create sda sda1 sda2 and sda5 in /mnt/dev
$ sudo mknod /mnt/dev/sda  b 8 0
$ sudo mknod /mnt/dev/sda1  b 81
$ sudo mknod /mnt/dev/sda2  b 8 2
$ sudo mknod /mnt/dev/sda5  b 8 5 

11- install grub
$ sudo grub-install --target=i386-pc --boot-directory=/mnt/boot /dev/loop0

12- update grub menu file /mnt/boot/grub/grub.cfg, as below
the most import line is to change root to sda1
1.set root="hd0,1"
2.linux    /boot/vmlinuz-3.13.0-24-generic root=/dev/sda1 ro  quiet splash $vt_handoff


### BEGIN /etc/grub.d/00_header ###
if [ -s $prefix/grubenv ]; then
  set have_grubenv=true
  load_env
fi
if [ "${next_entry}" ] ; then
   set default="${next_entry}"
   set next_entry=
   save_env next_entry
   set boot_once=true
else
   set default="0"
fi

if [ x"${feature_menuentry_id}" = xy ]; then
  menuentry_id_option="--id"
else
  menuentry_id_option=""
fi

export menuentry_id_option

if [ "${prev_saved_entry}" ]; then
  set saved_entry="${prev_saved_entry}"
  save_env saved_entry
  set prev_saved_entry=
  save_env prev_saved_entry
  set boot_once=true
fi

function savedefault {
  if [ -z "${boot_once}" ]; then
    saved_entry="${chosen}"
    save_env saved_entry
  fi
}
function recordfail {
  set recordfail=1
  if [ -n "${have_grubenv}" ]; then if [ -z "${boot_once}" ]; then save_env recordfail; fi; fi
}
function load_video {
  if [ x$feature_all_video_module = xy ]; then
    insmod all_video
  else
    insmod efi_gop
    insmod efi_uga
    insmod ieee1275_fb
    insmod vbe
    insmod vga
    insmod video_bochs
    insmod video_cirrus
  fi
}

if [ x$feature_default_font_path = xy ] ; then
   font=unicode
else
insmod part_msdos
insmod ext2
if [ x$feature_platform_search_hint = xy ]; then
  search --no-floppy --fs-uuid --set=root  af90e7c8-2887-4371-bb5e-c6cd6aff092e
else
  search --no-floppy --fs-uuid --set=root af90e7c8-2887-4371-bb5e-c6cd6aff092e
fi
    font="/usr/share/grub/unicode.pf2"
fi

if loadfont $font ; then
  set gfxmode=auto
  load_video
  insmod gfxterm
  set locale_dir=$prefix/locale
  set lang=en_US
  insmod gettext
fi
terminal_output gfxterm
if [ "${recordfail}" = 1 ] ; then
  set timeout=30
else
  if [ x$feature_timeout_style = xy ] ; then
    set timeout_style=hidden
    set timeout=0
  # Fallback hidden-timeout code in case the timeout_style feature is
  # unavailable.
  elif sleep --interruptible 0 ; then
    set timeout=0
  fi
fi
### END /etc/grub.d/00_header ###

### BEGIN /etc/grub.d/05_debian_theme ###
set menu_color_normal=white/black
set menu_color_highlight=black/light-gray
### END /etc/grub.d/05_debian_theme ###

### BEGIN /etc/grub.d/10_linux ###
function gfxmode {
    set gfxpayload="${1}"
    if [ "${1}" = "keep" ]; then
        set vt_handoff=vt.handoff=7
    else
        set vt_handoff=
    fi
}
if [ "${recordfail}" != 1 ]; then
  if [ -e ${prefix}/gfxblacklist.txt ]; then
    if hwmatch ${prefix}/gfxblacklist.txt 3; then
      if [ ${match} = 0 ]; then
        set linux_gfx_mode=keep
      else
        set linux_gfx_mode=text
      fi
    else
      set linux_gfx_mode=text
    fi
  else
    set linux_gfx_mode=keep
  fi
else
  set linux_gfx_mode=text
fi
export linux_gfx_mode
menuentry 'Ubuntu' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-simple-af90e7c8-2887-4371-bb5e-c6cd6aff092e' {
    recordfail
    load_video
    gfxmode $linux_gfx_mode
    insmod gzio
    insmod part_msdos
    insmod ext2
    set root="hd0,1"
    linux    /boot/vmlinuz-3.13.0-24-generic root=/dev/sda1 ro  quiet splash $vt_handoff
    initrd    /boot/initrd.img-3.13.0-24-generic
}
submenu 'Advanced options for Ubuntu' $menuentry_id_option 'gnulinux-advanced-af90e7c8-2887-4371-bb5e-c6cd6aff092e' {
    menuentry 'Ubuntu, with Linux 3.13.0-24-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.13.0-24-generic-advanced-af90e7c8-2887-4371-bb5e-c6cd6aff092e' {
        recordfail
        load_video
        gfxmode $linux_gfx_mode
        insmod gzio
        insmod part_msdos
        insmod ext2
        set root=(hd0,1)
        echo    'Loading Linux 3.13.0-24-generic ...'
        linux    /boot/vmlinuz-3.13.0-24-generic root=/dev/sda1 ro  quiet splash $vt_handoff
        echo    'Loading initial ramdisk ...'
        initrd    /boot/initrd.img-3.13.0-24-generic
    }
    menuentry 'Ubuntu, with Linux 3.13.0-24-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.13.0-24-generic-recovery-af90e7c8-2887-4371-bb5e-c6cd6aff092e' {
        recordfail
        load_video
        insmod gzio
        insmod part_msdos
        insmod ext2
        set root=(hd0,1)
        echo    'Loading Linux 3.13.0-24-generic ...'
        linux    /boot/vmlinuz-3.13.0-24-generic root=/dev/sda1 ro single nomodeset
        echo    'Loading initial ramdisk ...'
        initrd    /boot/initrd.img-3.13.0-24-generic
    }
}

### END /etc/grub.d/10_linux ###



13- now we can use use the image with qemu with the following command
$ qemu-system-x86_64 -hda test.qcow2
-- screenshot


***note***
if you want to convert disk format to other format such as qcow2, use command below
$ qemu-img convert -f raw -O qcow2 test.raw test.qcow

No comments:

Post a Comment