linux – 只写Delta时用图像更新块设备,是否可能?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了linux – 只写Delta时用图像更新块设备,是否可能?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2913字,纯文字阅读大概需要5分钟。
内容图文
![linux – 只写Delta时用图像更新块设备,是否可能?](/upload/InfoBanner/zyjiaocheng/957/ada6983012b44cfbad3cc8f797b41057.jpg)
我广泛使用dd作为软件配置控制的手段.这些映像通常部署在闪存盘上以更新设备.我发现我经常对图像文件进行小的增量更新,然后必须将整个图像重新复制到块设备上.这相当耗时,因为图像的大小通常为8GB.为了解决问题,图像(一旦组装)不是易于安装的格式.换句话说,不可能直接在块上进行更改.
我试图确定是否有方法将图像文件与块设备进行比较,并仅更新需要更新的块.我怀疑这比写入整个磁盘要快得多,这可能相当于图像文件中的10kb delta.
解决方法:
以下是一个小型C程序的快速入侵,能够以块方式比较两个文件(file1,file2),如果块不同,则将相应的块从file1复制到file2.适用于文件和块设备.用它做你想要的,但风险自负!
/*
Small program to blockwise compare two files and write different
blocks from file1 to file2.
Arguments: file1, file2, blocksize in bytes
If blocksize is not given, it is set to 512 (minimum)
No error checking, no intensive tests run - use at your own risk!
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main(argc, argv)
int argc;
char *argv[];
{
char *fnamein; /* Input file name */
char *fnameout; /* Output file name */
char *bufin; /* Input buffer */
char *bufout; /* Output buffer */
int bufsize; /* Buffer size (blocksize) */
int fdin; /* Input file descriptor*/
int fdout; /* Output file descriptor*/
int cnt; /* Current block # */
/* Argument processing */
if (argc < 3 || argc > 4) {
fprintf(stderr,"Usage: %s infile outfile [bufsize]\n", argv[0]);
exit(1);
}
fnamein = argv[1];
fnameout = argv[2];
if (argc == 4) {
bufsize = atoi(argv[3]);
if (bufsize < 512) {
fprintf(stderr,"Error: Illegal value for [bufsize]: %s\n", argv[3]);
exit(1);
}
} else {
bufsize = 512;
}
fprintf(stderr, "Copying differing blocks from '%s' to '%s', blocksize is %i\n", fnamein, fnameout, bufsize);
if (! ((bufin = malloc(bufsize)) && (bufout = malloc(bufsize)))) {
fprintf(stderr,"Error: Can't allocate buffers: %i\n", bufsize);
exit(1);
}
fdin = open(fnamein, O_RDONLY);
if (fdin < 0) {
fprintf(stderr,"Error: Can't open input file: %s\n", fnamein);
exit(1);
}
fdout = open(fnameout, O_RDWR | O_SYNC);
if (fdout < 0) {
fprintf(stderr,"Error: Can't open ouput file: %s\n", fnameout);
exit(1);
}
cnt = 0;
while (read(fdin, bufin, bufsize) == bufsize) {
if (read(fdout, bufout, bufsize) == bufsize) {
if (memcmp(bufin, bufout, bufsize) != 0) {
fprintf(stderr, "Differing blocks at block # %i; writing block to %s\n", cnt, fnameout);
if (lseek(fdout, -bufsize, SEEK_CUR) > -1) {
if (write(fdout, bufin, bufsize) != bufsize) {
fprintf(stderr,"Error: Unable to write to output file %s block # %i\n", fnameout, cnt);
exit(1);
}
} else {
fprintf(stderr,"Error: Unable to seek to output file %s block # %i\n", fnameout, cnt);
exit(1);
}
}
} else {
fprintf(stderr,"Error: Unable to read from ouput file %s block # %i\n", fnameout, cnt);
exit(1);
}
cnt++;
}
exit(0);
}
内容总结
以上是互联网集市为您收集整理的linux – 只写Delta时用图像更新块设备,是否可能?全部内容,希望文章能够帮你解决linux – 只写Delta时用图像更新块设备,是否可能?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。