Mounting KVM image file for open-iscsi modification

Last month I installed Ubuntu 12.04 LTS on KVM, but the VM cannot mount any iSCSI partition after successful installation. After googling, I made a solution, mounting kvm image file, modifying a network script file, and then re-create the initial ramdisk file. Hereby is my notes with  script.

A script to (un-)mount kvm image

case “$1” in
losetup /dev/loop0 ~sammy/mykvm.img
kpartx -av /dev/loop0
mount /dev/mapper/loop0p1 /mnt/
mount -o bind /dev /mnt/dev
mount -o bind /proc /mnt/proc
chroot /mnt/
umount /mnt/dev
umount /mnt/proc
umount /mnt/
kpartx -dv /dev/loop0
losetup -d /dev/loop0

Mount iSCSI partition with open-iscsi:

$ ip addr add dev eth0
$ ip link set dev eth0 up
$ route add default gw

$ modprobe iscsi-tcp
$ iscsid -f &
$ iscsiadm -m discovery
$ iscsiadm -m node -T iqn.123313133:myvol-1234-5678-90 -p –login

$ mount /dev/sdb1 /mymountpt


Viewing existing initial RAMdisk content

$ cp /boot/initrd.img /boot/initrd.gz
$ gunzip /boot/initrd.gz
$ mkdir -p /tmp2; cd /tmp2
$ cpio -id < /boot/initrd

Re-generating an initial RAMdisk file:

$ vim /usr/share/initramfs-tools/scripts/local-top/iscsi
$ mkinitramfs -o /boot/sammy-initrd.img

Notes to static IP required hosts, you can add the following lines before configure_networking at try_configure_networking()

# eth1
ip addr add dev eth0
ip link set dev eth0 up
touch /tmp/net-eth0.conf