Skip to content

Commit 9441ea9

Browse files
committed
Support ramdisk as image directory
1 parent fa3314d commit 9441ea9

File tree

4 files changed

+42
-8
lines changed

4 files changed

+42
-8
lines changed

Diff for: easy_deploy.py

+18-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
BASEDIR = '/usr/local/share/libvirt'
1515
IMAGE_DIR = '/var/lib/libvirt/images'
16+
RAMDISK_DIR = ''
1617

1718
# bridge interface directly connected to the Internet (or your intranet)
1819
# It can be configured by the config file
@@ -89,12 +90,19 @@ def getImageFormat(image):
8990
return fmt.split(': ')[1]
9091

9192

92-
def copyImage(base, image):
93+
def copyImage(base, image, use_ramdisk=False):
9394
src_path = os.path.join(BASEIMAGE_DIR, base)
9495
dst_path = os.path.join(IMAGE_DIR, image)
96+
if use_ramdisk and RAMDISK_DIR:
97+
ramdisk_dir = RAMDISK_DIR
98+
else:
99+
ramdisk_dir = ''
95100
print 'Copying %s -> %s...' % (os.path.basename(base),
96101
os.path.basename(image))
97-
callCmdAsRoot(CMD_COPY_IMAGE, src_path, dst_path, direct_stderr=True)
102+
if ramdisk_dir:
103+
print '(using ramdisk dir %s)' % ramdisk_dir
104+
callCmdAsRoot(CMD_COPY_IMAGE, src_path, dst_path, ramdisk_dir,
105+
direct_stderr=True)
98106

99107

100108
def setHostnameToImage(image, name):
@@ -182,9 +190,12 @@ def parseArgs():
182190
parser.add_argument('-i', '--nic', action='append', default=[],
183191
help='NIC (bridge_name or "NAT").'
184192
' Specify multiple times if you want multiple vNICs.')
185-
parser.add_argument('--no-hostname', action='store_false',
193+
parser.add_argument('-H', '--no-hostname', action='store_false',
186194
dest='set_hostname',
187195
help='Do not set hostname of VM.')
196+
parser.add_argument('-r', '--use-ramdisk', action='store_true',
197+
dest='use_ramdisk',
198+
help='Place VM image to ramdisk (if ramdisk is configured).')
188199
args = parser.parse_args()
189200

190201
if args.BASEIMAGE == '?' or args.BASEIMAGE.upper() == 'LIST':
@@ -213,6 +224,9 @@ def loadConfig():
213224
if conf.has_option('default', 'public_bridge'):
214225
global PUBLIC_BRIDGE
215226
PUBLIC_BRIDGE = conf.get('default', 'public_bridge')
227+
if conf.has_option('default', 'ramdisk_dir'):
228+
global RAMDISK_DIR
229+
RAMDISK_DIR = conf.get('default', 'ramdisk_dir')
216230

217231

218232
def getAliasNames(name):
@@ -341,7 +355,7 @@ def main():
341355

342356
generateLibvirtXML(args, libvirt_xml)
343357
defineDomain(libvirt_xml)
344-
copyImage(base_abspath, dest_abspath)
358+
copyImage(base_abspath, dest_abspath, args.use_ramdisk)
345359
if args.set_hostname and image_fmt != 'raw':
346360
setHostnameToImage(dest_abspath, args.NAME)
347361
deleteLibvirtXML(libvirt_xml)

Diff for: remove_vm.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/bin/sh
22

33
IMAGE_DIR=/var/lib/libvirt/images
4+
RAMDISK_DIR=/mnt/ramdisk/images
45
FORCE=0
56

67
export LANG=C
@@ -37,4 +38,4 @@ if [ $? -eq 0 ]; then
3738
fi
3839

3940
virsh undefine $NAME
40-
sudo $RM_IMAGE $IMAGE_DIR ${NAME}.img
41+
sudo $RM_IMAGE $IMAGE_DIR ${NAME}.img $RAMDISK_DIR

Diff for: subcmds/copy_image.sh

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
#!/bin/sh
1+
#!/bin/sh -x
22

3-
#/usr/bin/pv < $1 > $2
4-
/bin/cp -v $1 $2
3+
SRC_PATH=$1
4+
DST_PATH=$2
5+
RAMDISK_DIR=$3
6+
7+
if [ -n "$RAMDISK_DIR" ]; then
8+
mkdir -p $RAMDISK_DIR
9+
DST_IMAGE=$RAMDISK_DIR/$(basename $DST_PATH)
10+
/bin/cp -v $1 $DST_IMAGE
11+
ln -s $DST_IMAGE $DST_PATH
12+
else
13+
#/usr/bin/pv < $1 > $2
14+
/bin/cp -v $1 $2
15+
fi

Diff for: subcmds/remove_image.sh

+8
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,13 @@ fi
77

88
IMAGE_DIR=$1
99
NAME=$2
10+
RAMDISK_DIR=$3
1011

1112
/bin/rm -v ${IMAGE_DIR}/${NAME}
13+
14+
if [ -n "$RAMDISK_DIR" ]; then
15+
RAM_IMAGE=$RAMDISK_DIR/$NAME
16+
if [ -f "$RAM_IMAGE" ]; then
17+
/bin/rm -v $RAM_IMAGE
18+
fi
19+
fi

0 commit comments

Comments
 (0)