Thursday, October 6, 2016

create ubuntu vmdk image

1- create vmdk image with space of roughly 8GB with the command below
$ vboxmanage createmedium disk --format VMDK --filename yakkety.vmdk --size 8000

2- map the yakkety.vmdk to /dev/nbd0 with the following command
$ sudo modprobe nbd max_part=16
$ sudo qemu-nbd -c /dev/nbd0 yakkety.vmdk

3- now we can use fdisk to create partition on device /dev/nbd0 that is map to our vmdk file,
- create primary partition, id 1 : space +7G
- create extended partition, id 2: with remaining space
- create logical partition, id 5 : with the whole extended partition space

4- format the partition 1 with ext4 file system
- list all the partitions
$ ls /dev/nbd0*
/dev/nbd0  /dev/nbd0p1  /dev/nbd0p2  /dev/nbd0p5


- formation /dev/nbd0p1 with ext4
$ sudo mkfs.ext4 /dev/nbd0p1


5- create working directory and create the ubuntu bootstrap for yakkety amd64
$ mkdir -p yakketydir
$ mount /dev/nbd0p1 yakketydir
$ sudo debootstrap --arch=amd64 yakkety yakketydir http://us.archive.ubuntu.com/ubuntu/


6- preparing for chroot the bootstrap
$ sudo mount --bind /dev yakketydir/dev
$ sudo cp /etc/hosts yakketydir/etc/hosts
$ sudo cp /etc/resolv.conf yakketydir/etc/resolv.conf


7- chroot the bootstrap and install packages
$ sudo chroot chroot
$ mount none -t proc /proc
$ mount none -t sysfs /sys
$ mount none -t devpts /dev/pts
$ export HOME=/
$ export LC_ALL=C
$ apt-get update
$ apt-get install --yes dbus
$ dbus-uuidgen > /var/lib/dbus/machine-id
$ dpkg-divert --local --rename --add /sbin/initctl
$ apt-get install --yes ubuntu-standard
$ apt-get install --yes linux-generic  


8- create user and set root password
$ passwd
$ useradd -m user1
$ passwd user1

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


10- cleanup before exit chroot
$ dpkg-divert --rename --remove /sbin/initctl
$ apt-get clean
$ rm -rf /tmp/*
$ umount -lf /proc
$ umount -lf /sys
$ umount -lf /dev/pts
$ exit


- after exit the chroot unmount the /dev directory
 $ sudo unmount chroot/dev

11- install grub boot loader and update grub configuration
$ sudo grub-install --target=i386-pc --boot-directory=yakketydir/boot /dev/nbd0

- yakketydir/boot/grub/grub.cfg wiil look like below
### 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 root=/dev/sda1 ro  $vt_handoff
    initrd    /boot/initrd.img
}
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 root=/dev/sda1 ro  $vt_handoff
        echo    'Loading initial ramdisk ...'
        initrd    /boot/initrd.img
    }
 }

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



- create link of vmlinuz and initrd.img example below if the kernel version is 4.4.0
$ sudo ln -s yakketydir/boot/vmlinuz-4.4.0-9136-generic yakketydir/boot/vmlinuz
$ sudo ln -s yakketydir/boot/initrd.img-4.4.0-9136-generic yakketydir/boot/initrd.img



5- now unmount everything, and the yakkety.vmdk will be bootable with yakkety installed in it.



$ sudo umount yakketydir
$ sudo qemu-nbd -d /dev/nbd0


and you can create new virtual machine with the disk file we just created,

- snapshot of boot with the image file and login as user user1


***tips
- with qemu the read/write speed of disk file is very slow, to speed up will can mount use vmware-mount, if you have vmware installed on the machine, to mount disk and unmount disk use the command below
$ sudo vmware-mount yakkety.vmdk 1 trusty/ -o exec,dev
$ sudo vmware-mount -d trusty/

No comments:

Post a Comment