在linux qemu环境中,一台主机服务器运行两台虚拟机。我想让两个VM将数据写入主机上的同一个块设备。因此,我运行以下两个命令将主机块设备暴露给两个VM。
try {
JSONObject baseJsonResponse = new JSONObject(freeNaugetJson);
JSONArray dataArray = baseJsonResponse.getJSONArray("data");
//put it here so you won't get a new array for each comment in the loop
**ArrayList<Comment> comments = new ArrayList<Comment>();**
// If there are results in the data array
for (int i = 0; i < dataArray.length(); i++){
String title = dataArray.getJSONObject(i).getString("title");
String body = dataArray.getJSONObject(i).getString("desc");
String totalComments = dataArray.getJSONObject(i).getString("no_comment");
String image = dataArray.getJSONObject(i).getString("image");
int id = dataArray.getJSONObject(i).getInt("id");
//here after every comment check its making a new comment ArrayList for each comment and filling it out so this can be the cause of the bug! bcz its in the loop
// ArrayList<Comment> comments = new ArrayList<Comment>();
//fetch each comment detail
if (Integer.parseInt(totalComments) > 0) {
JSONArray commentArray = dataArray.getJSONObject(i).getJSONArray("comments");
for (int j = 0; j < commentArray.length(); j++) {
String userName = commentArray.getJSONObject(j).getString("userName");
String comment_image = commentArray.getJSONObject(j).getString("userPhoto");
String comment = commentArray.getJSONObject(j).getString("comment");
String date = commentArray.getJSONObject(j).getString("date_commented");
comments.add(new Comment(userName, comment_image, comment, date));
}
}
// Create a new nauget object
naugets.add(new nauget(title, body, image, totalComments, comments, id));
}
} catch (JSONException e) {
Log.e(LOG_TAG, "Problem parsing the nauget JSON results", e);
}
return naugets;
现在,两个VM可以看到块设备并写入它。我想知道这种方法是否运作良好。由于两个VM同时将数据写入同一个块设备,我怀疑块设备是否可以存储正确的数据?
答案 0 :(得分:0)
你是对的。要共享这样的块设备,您必须采取某些步骤来防止来宾VM覆盖彼此的块。为每个guest虚拟机使用独立分区,或使用共享磁盘文件系统使用该设备。有关详细信息,请参阅群集文件系统上的Wikipedia页面:https://en.wikipedia.org/wiki/Clustered_file_system