/usr/share/pyshared/mic/ec2convert/fs.py is in mic2 0.24.12-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | #!/usr/bin/python -tt
#
# fs.py: Convert a virtual appliance image in an EC2 AMI
#
# Copyright 2008, Red Hat Inc.
# Joseph Boggs <jboggs@redhat.com>
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import os
import subprocess
import logging
import sys
import shutil
import time
class LoopBackDiskImage:
def setup_fs(self,imagefile,tmpdir):
loop_devices = []
loop_partition_dict = {}
tmproot = tmpdir + "-tmproot"
tmpimage = tmpdir + "-tmpimage"
logging.debug("TMPDIR: " + tmpdir)
free_loop_dev = os.popen("/sbin/losetup -f")
loop_device = free_loop_dev.read().strip()
if not loop_device:
sys.exit(1)
os.system("/sbin/losetup %s %s" % (loop_device,imagefile))
os.system("/sbin/kpartx -a %s" % loop_device)
loop_partitions = os.popen("/sbin/kpartx -lv %s|awk {'print $1'}" % loop_device)
for dev in loop_partitions:
dev = dev.strip()
label = os.popen("e2label /dev/mapper/%s 2>&1 " % dev)
label = label.read().strip()
if label.startswith("e2label"):
logging.error("Unable to detect partition label on %s, continuing anyways, if %s is a swap partition, no action is needed" % (dev,dev))
else:
loop_partition_dict[dev] = label
logging.debug( dev + " : " + label)
dev = loop_partition_dict.values()
dev.sort()
os.mkdir(tmproot)
for value in dev:
for key in loop_partition_dict.keys():
if (value == loop_partition_dict[key]):
ld = os.popen("/sbin/losetup -f")
loop_partition_device = ld.read().strip()
if not loop_device:
logging.error("Please review your loopback device settings and remove unneeded ones")
sys.exit(1)
os.system("mount -o loop /dev/mapper/%s %s%s" % (key,tmproot,value))
tmp_disk_space = os.popen("du -s %s|awk {'print $1'}" % tmproot)
tmp_disk_space= int(tmp_disk_space.read()) / 1024
logging.info("Disk Space Required: %sM" % str(tmp_disk_space))
new_disk_space = int(tmp_disk_space + ((tmp_disk_space * 0.30) + 150))
logging.info("\nCreating a new disk image with additional freespace: " + str(new_disk_space) + "M total")
create_disk = os.system("dd if=/dev/zero of=%s/ec2-diskimage.img bs=1M count=%s" % (tmpimage,new_disk_space))
os.system("mke2fs -Fj %s/ec2-diskimage.img" % tmpimage)
if not loop_device:
logging.error("Please review your loopback device settings and remove unneeded ones")
sys.exit(1)
os.system("mount -o loop %s/ec2-diskimage.img %s" % (tmpimage,tmpdir))
logging.info("Performing rsync on all partitions to new root")
os.system("rsync -u -r -a %s/* %s" % (tmproot,tmpdir))
dev.sort(reverse=True)
for value in dev:
logging.info("Unmounting %s%s" % (tmpdir,value))
os.system("umount %s%s" % (tmproot,value))
logging.info("Freeing loopdevices")
os.system("kpartx -d %s" % loop_device)
os.system("losetup -d %s" % loop_device)
return
def unmount(self,tmpdir):
logging.debug("Unmounting directory %s" % tmpdir)
os.system("/bin/umount %s" % tmpdir)
return
def cleanup(self,tmpdir):
os.system("rm -rf %s/*" % tmpdir)
return
class DirectoryImage:
def setup_fs(self,imagefile,tmpdir):
tmproot = tmpdir + "-tmproot"
tmpimage = tmpdir + "-tmpimage"
logging.info("TMPDIR: " + tmpdir)
tmp_disk_space = os.popen("du -s %s|awk {'print $1'}" % imagefile)
tmp_disk_space= int(tmp_disk_space.read()) / 1024
logging.info("Disk Space Required: %sM" % str(tmp_disk_space))
new_disk_space = int(tmp_disk_space + ((tmp_disk_space * 0.30) + 150))
logging.info("Creating a new disk image with additional freespace: " + str(new_disk_space) + "M total")
create_disk = os.system("dd if=/dev/zero of=%s/ec2-diskimage.img bs=1M count=%s" % (tmpimage,new_disk_space))
os.system("mke2fs -Fj %s/ec2-diskimage.img" % tmpimage)
free_loop_dev = os.popen("/sbin/losetup -f")
loop_device = free_loop_dev.read().strip()
if not loop_device:
logging.error("Please review your loopback device settings and remove unneeded ones")
sys.exit(1)
os.system("mount -o loop %s/ec2-diskimage.img %s" % (tmpimage,tmpdir))
logging.info("Performing rsync on all partitions to new root")
os.system("rsync -u -r -a %s/* %s" % (imagefile,tmpdir))
return
def unmount(self,tmpdir):
logging.debug("Unmounting directory %s" % tmpdir)
os.system("/bin/umount %s" % tmpdir)
return
def cleanup(self,tmpdir):
os.system("rm -rf %s/*" % tmpdir)
return
class LoopbackFSImage:
def setup_fs(self,imagefile,tmpdir):
logging.debug("Mounting %s to %s" % (imagefile,tmpdir))
os.system("/bin/mount -o loop %s %s" % (imagefile,tmpdir))
return
def unmount(self,tmpdir):
logging.debug("Unmounting directory %s" % tmpdir)
os.system("/bin/umount %s" % tmpdir)
return
def cleanup(self,tmpdir):
os.system("rm -rf %s/*" % tmpdir)
|