Skip to content
This repository was archived by the owner on Jan 22, 2026. It is now read-only.

Commit c292033

Browse files
committed
Add virt-install --disk 'cache' option (Ben Kochie)
1 parent a8f0b1b commit c292033

5 files changed

Lines changed: 51 additions & 6 deletions

File tree

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Patches also received from
3636
Andreas Schneider <anschneider-at-suse-dot-de>
3737
Michael Marineau <marineam-at-gentoo-dot-org>
3838
Tomas Hoger <thoger-at-redhat-dot-com>
39+
Ben Cochie <ben-at-nerp-dot-net>
3940

4041
...send patches and get your name here...
4142

man/en/virt-install.pod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@ size (in GB) to use if creating new storage
145145
whether to skip fully allocating newly created storage. Value is 'true' or
146146
'false'. Default is 'true' (do not fully allocate).
147147

148+
=item B<cache>
149+
150+
The cache mode to be used. The host pagecache provides cache memory.
151+
The cache value can be 'none', 'writethrough', or 'writeback'.
152+
'writethrough' provides read caching. 'writeback' provides
153+
read and write caching.
154+
148155
=back
149156

150157
See the examples section for some uses. This option deprecates C<--file>,

tests/validation.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@
138138
"vol-noexist")},
139139
{ 'conn' : testconn, 'volName' : ( 1234,
140140
"vol-noexist")},
141+
{ 'path' : 'valid', 'size' : 1,
142+
'driverCache' : 'invalid' },
141143
],
142144

143145
'valid' : [{ 'path' : '/dev/loop0' },
@@ -151,6 +153,8 @@
151153
{ 'conn' : testconn, 'path' : "/pool-exist/vol-noexist",
152154
'size' : 1 },
153155
{ 'conn' : testconn, 'volInstall': volinst},
156+
{ 'path' : 'nonexist', 'size' : 1,
157+
'driverCache' : 'writethrough' },
154158
]
155159
},
156160
'shareable' : {

virt-install

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ def parse_disk_option(guest, path, size):
8080
shared = False
8181
sparse = True
8282
bus = None
83+
cache = None
8384

8485
# Strip media type
8586
if path.startswith("path="):
@@ -130,6 +131,8 @@ def parse_disk_option(guest, path, size):
130131
sparse = False
131132
else:
132133
fail(_("Unknown '%s' value '%s'") % (opt_type, opt_val))
134+
elif opt_type == "cache":
135+
cache = opt_val
133136
else:
134137
fail(_("Unknown --disk option '%s'.") % (opt,))
135138

@@ -165,7 +168,7 @@ def parse_disk_option(guest, path, size):
165168

166169
if not devtype:
167170
devtype = virtinst.VirtualDisk.DEVICE_DISK
168-
ret = (abspath, voltuple, volinst, devtype, bus, ro, shared, size, sparse)
171+
ret = (abspath, voltuple, volinst, devtype, bus, ro, shared, size, sparse, cache)
169172
logging.debug("parse_disk: returning %s" % str(ret))
170173
return ret
171174

@@ -181,14 +184,15 @@ def get_disk(disk, size, sparse, guest, hvm, conn, is_file_path):
181184
else:
182185
(path, voltuple, volinst,
183186
device, bus, readOnly, shared,
184-
size, sparse) = parse_disk_option(guest, disk, size)
187+
size, sparse, cache) = parse_disk_option(guest, disk, size)
185188
if not sparse and volinst:
186189
volinst.allocation = volinst.capacity
187190

188191
d = virtinst.VirtualDisk(path=path, size=size, sparse=sparse,
189192
volInstall=volinst, volName=voltuple,
190193
readOnly=readOnly, shareable=shared,
191-
device=device, bus=bus, conn=guest.conn)
194+
device=device, bus=bus, conn=guest.conn,
195+
driverCache=cache)
192196
# Default file backed PV guests to tap driver
193197
if d.type == virtinst.VirtualDisk.TYPE_FILE \
194198
and not(hvm) and virtinst.util.is_blktap_capable():

virtinst/VirtualDisk.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ class VirtualDisk(VirtualDevice):
8686
driver_types = [DRIVER_TAP_RAW, DRIVER_TAP_QCOW,
8787
DRIVER_TAP_VMDK, DRIVER_TAP_VDISK]
8888

89+
CACHE_MODE_NONE = "none"
90+
CACHE_MODE_WRITETHROUGH = "writethrough"
91+
CACHE_MODE_WRITEBACK = "writeback"
92+
cache_types = [CACHE_MODE_NONE, CACHE_MODE_WRITETHROUGH,
93+
CACHE_MODE_WRITEBACK]
94+
8995
DEVICE_DISK = "disk"
9096
DEVICE_CDROM = "cdrom"
9197
DEVICE_FLOPPY = "floppy"
@@ -98,7 +104,8 @@ class VirtualDisk(VirtualDevice):
98104
def __init__(self, path=None, size=None, transient=False, type=None,
99105
device=DEVICE_DISK, driverName=None, driverType=None,
100106
readOnly=False, sparse=True, conn=None, volObject=None,
101-
volInstall=None, volName=None, bus=None, shareable=False):
107+
volInstall=None, volName=None, bus=None, shareable=False,
108+
driverCache=None):
102109
"""
103110
@param path: filesystem path to the disk image.
104111
@type path: C{str}
@@ -131,6 +138,8 @@ def __init__(self, path=None, size=None, transient=False, type=None,
131138
@type bus: C{str}
132139
@param shareable: If disk can be shared among VMs
133140
@type shareable: C{bool}
141+
@param driverCache: Disk cache mode (none, writethrough, writeback)
142+
@type driverCache: member of cache_types
134143
"""
135144

136145
VirtualDevice.__init__(self, conn=conn)
@@ -145,6 +154,7 @@ def __init__(self, path=None, size=None, transient=False, type=None,
145154
self._vol_install = None
146155
self._bus = None
147156
self._shareable = None
157+
self._driver_cache = None
148158

149159
# XXX: No property methods for these
150160
self.transient = transient
@@ -162,6 +172,7 @@ def __init__(self, path=None, size=None, transient=False, type=None,
162172
self._set_vol_install(volInstall, validate=False)
163173
self._set_bus(bus, validate=False)
164174
self._set_shareable(shareable, validate=False)
175+
self._set_driver_cache(driverCache, validate=False)
165176

166177
if volName:
167178
self.__lookup_vol_name(volName)
@@ -272,6 +283,16 @@ def _set_shareable(self, val, validate=True):
272283
self.__validate_wrapper("_shareable", val, validate)
273284
shareable = property(_get_shareable, _set_shareable)
274285

286+
def _get_driver_cache(self):
287+
return self._driver_cache
288+
def _set_driver_cache(self, val, validate=True):
289+
if val is not None:
290+
self._check_str(val, "cache")
291+
if val not in self.cache_types:
292+
raise ValueError, _("Unknown cache mode '%s'" % val)
293+
self.__validate_wrapper("_driver_cache", val, validate)
294+
driver_cache = property(_get_driver_cache, _set_driver_cache)
295+
275296
# Validation assistance methods
276297

277298
# Initializes attribute if it hasn't been done, then validates args.
@@ -657,13 +678,21 @@ def get_xml_config(self, disknode=None):
657678

658679
ret = " <disk type='%s' device='%s'>\n" % (self.type, self.device)
659680

681+
dname = self.driver_name
682+
if not dname and self.driver_cache:
683+
self.driver_name = "qemu"
684+
660685
if not self.driver_name is None:
661686
dtypexml = ""
662687
if not self.driver_type is None:
663688
dtypexml = " type='%s'" % self.driver_type
664689

665-
ret += " <driver name='%s'%s/>\n" % (self.driver_name,
666-
dtypexml)
690+
dcachexml = ""
691+
if not self.driver_cache is None:
692+
dcachexml = " cache='%s'" % self.driver_cache
693+
694+
ret += " <driver name='%s'%s%s/>\n" % (self.driver_name,
695+
dtypexml, dcachexml)
667696

668697
if path is not None:
669698
ret += " <source %s='%s'/>\n" % (typeattr, path)

0 commit comments

Comments
 (0)