update create_virtualimage script

This commit is contained in:
lrusak 2012-11-20 01:46:09 -08:00
parent 5e2be356b9
commit 34854f95b6

View File

@ -20,29 +20,55 @@
# http://www.gnu.org/copyleft/gpl.html # http://www.gnu.org/copyleft/gpl.html
################################################################################ ################################################################################
# usage: sudo ./create_virtualmachine <path> # usage: sudo ./create_virtualmachine <path> <size(MB)> [<type>]
# example: sudo ./create_virtualmachine /home/test/VM # example: sudo ./create_virtualmachine /home/test/VM 512 [vdi]
if [ "$(id -u)" != "0" ]; then if [ "$(id -u)" != "0" ]; then
clear
echo "###########################################################"
echo "# please execute with 'sudo' or -DANGEROUS!!!- as root #"
echo "# example: #"
echo "# sudo ./create_virtualmachine <path> <size(MB)> [<type>] #"
echo "###########################################################"
exit 1
fi
if [ -z "$1" -o -z "$2" ]; then
clear
echo "###########################################################"
echo "# please execute as follows #"
echo "# example: #"
echo "# sudo ./create_virtualmachine <path> <size(MB)> [<type>] #"
echo "###########################################################"
exit 1
fi
if [ "$2" -lt "200" -o "$2" -gt "2048" ]; then
clear clear
echo "#########################################################" echo "#########################################################"
echo "# please execute with 'sudo' or -DANGEROUS!!!- as root #" echo "# use a value between 200MB and 2048MB (2GB) #"
echo "# example: sudo ./create_virtualmachine <path> #" echo "# example: #"
echo "# sudo ./create_virtualmachine /home/test/VM 512 #"
echo "#########################################################" echo "#########################################################"
exit 1 exit 1
fi fi
if [ -z "$1" ]; then if [ ! -z "$3" -a "$3" != "vdi" -a "$3" != "vmdk" ]; then
clear clear
echo "#########################################################" echo "#########################################################"
echo "# please execute with target folder drive as option #" echo "# only vdi or vmdk types are supported #"
echo "# example: sudo ./create_virtualmachine /home/test/VM/ #" echo "# example: #"
echo "# sudo ./create_virtualmachine /home/test/VM 512 [vdi] #"
echo "#########################################################" echo "#########################################################"
exit 1 exit 1
elif [ "$3" = "vdi" ]; then
TYPE="vdi"
elif [ -z "$3" -o "$3" = "vmdk" ]; then
TYPE="vmdk"
fi fi
DISK="$1/OpenELEC.img" DISK="$1/OpenELEC.img"
VMDK="$1/OpenELEC.vmdk" IMAGE="$1/OpenELEC.$TYPE"
LOOP=$(losetup -f) LOOP=$(losetup -f)
clear clear
@ -109,7 +135,7 @@ echo "#########################################################"
exit 1 exit 1
fi fi
# this is needed fo convert harddisk image to vmdk format # this is needed fo convert harddisk image to vmdk or vdi format
which qemu-img > /dev/null which qemu-img > /dev/null
if [ "$?" = "1" ]; then if [ "$?" = "1" ]; then
clear clear
@ -159,7 +185,7 @@ echo "#########################################################"
# create an image # create an image
echo "creating new empty harddisk image: $DISK..." echo "creating new empty harddisk image: $DISK..."
dd if=/dev/zero of="$DISK" bs=1M count=512 dd if=/dev/zero of="$DISK" bs=1M count=1024
# write a disklabel # write a disklabel
echo "creating new partition table: $DISK..." echo "creating new partition table: $DISK..."
@ -168,16 +194,28 @@ echo "#########################################################"
# create partition1 # create partition1
echo "creating partition1 on $DISK..." echo "creating partition1 on $DISK..."
parted -s "$LOOP" -a min unit s mkpart primary ext4 64 262208 parted -s "$LOOP" unit cyl mkpart primary ext2 -- 0 16
# create partition2 # create partition2
echo "creating partition2 on $DISK..." echo "creating partition2 on $DISK..."
parted -s "$LOOP" -a min unit s mkpart primary ext4 262209 100% parted -s "$LOOP" unit cyl mkpart primary ext2 -- 16 -2
# make partition1 active (bootable) # make partition1 active (bootable)
echo "marking partition1 active..." echo "marking partition1 active..."
parted -s "$LOOP" set 1 boot on parted -s "$LOOP" set 1 boot on
echo "telling kernel we have a new partition table..."
partprobe "$LOOP"
# create filesystem on partition1
echo "creating filesystem on partition1..."
mkfs.ext4 "${LOOP}p1" -L System
# create filesystem on partition2
echo "creating filesystem on partition2..."
mkfs.ext4 "${LOOP}p2" -L Storage
sync
# write mbr # write mbr
echo "writing mbr..." echo "writing mbr..."
if [ -f /usr/lib/syslinux/mbr.bin ]; then if [ -f /usr/lib/syslinux/mbr.bin ]; then
@ -192,18 +230,10 @@ echo "#########################################################"
cat "$MBR" > "$LOOP" cat "$MBR" > "$LOOP"
fi fi
# create filesystem on partition1
echo "creating filesystem on partition1..."
losetup -d "$LOOP"
losetup -o 32768 --sizelimit 134218240 "$LOOP" "$DISK"
mke2fs -t ext4 -m 0 "$LOOP"
tune2fs -U random -L "System" "$LOOP"
sync
# mount partition # mount partition
echo "mounting partition1 on /tmp/vmware_install..." echo "mounting partition1 on /tmp/vmware_install..."
mkdir -p /tmp/vmware_install mkdir -p /tmp/vmware_install
mount "$LOOP" /tmp/vmware_install mount "${LOOP}p1" /tmp/vmware_install
# create bootloader configuration # create bootloader configuration
echo "creating bootloader configuration..." echo "creating bootloader configuration..."
@ -213,7 +243,7 @@ echo "#########################################################"
echo " " >> /tmp/vmware_install/syslinux.cfg echo " " >> /tmp/vmware_install/syslinux.cfg
echo "LABEL linux" >> /tmp/vmware_install/syslinux.cfg echo "LABEL linux" >> /tmp/vmware_install/syslinux.cfg
echo " KERNEL /KERNEL" >> /tmp/vmware_install/syslinux.cfg echo " KERNEL /KERNEL" >> /tmp/vmware_install/syslinux.cfg
echo " APPEND boot=LABEL=System disk=LABEL=Storage quiet ssh" >> /tmp/vmware_install/syslinux.cfg echo " APPEND boot=LABEL=System disk=LABEL=Storage ssh debugging nosplash" >> /tmp/vmware_install/syslinux.cfg
# install extlinux # install extlinux
echo "installing extlinux to partition1..." echo "installing extlinux to partition1..."
@ -230,15 +260,7 @@ echo "#########################################################"
# unmount partition1 # unmount partition1
echo "unmounting partition1..." echo "unmounting partition1..."
umount "$LOOP" umount "${LOOP}p1"
sync
# create filesystem on partition2
echo "creating filesystem on partition2..."
losetup -d "$LOOP"
losetup -o 134251008 "$LOOP" "$DISK"
mke2fs -t ext4 -m 0 "$LOOP"
tune2fs -U random -L "Storage" "$LOOP"
sync sync
# detach loop0 # detach loop0
@ -248,9 +270,10 @@ echo "#########################################################"
echo "cleaning tempdir..." echo "cleaning tempdir..."
rm -rf /tmp/vmware_install rm -rf /tmp/vmware_install
# convert image to vmdk # convert image to vmdk or vdi
echo "converting $DISK to vmdk format..." echo "converting $DISK to $TYPE format..."
qemu-img convert -O vmdk "$DISK" "$VMDK" qemu-img convert -O $TYPE "$DISK" "$IMAGE"
rm -f "$DISK" rm -f "$DISK"
echo "...installation finished" echo "...installation finished"