Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1811886
  • 博文数量: 241
  • 博客积分: 9862
  • 博客等级: 中将
  • 技术积分: 5206
  • 用 户 组: 普通用户
  • 注册时间: 2005-02-18 23:23
文章分类
文章存档

2011年(14)

2010年(61)

2009年(48)

2008年(118)

我的朋友

分类: LINUX

2009-04-27 17:34:13

如果在android启动时要加载一个压缩的文件系统镜像或者一个ext3的文件系统镜像,不可避免的要使用loop设备,可是android本省的init没有提供losetup功能,而且其mount功能中也没有loop设备相关处理,因此要自己动手添加一个losetup功能。
要修改的代码在 system/core/init目录下。
 
keywords.h中添加int do_losetup(int nargs, char **args);和 KEYWORD(losetup,     COMMAND, 2, do_losetup)两行。
 
parser.c中的lookup_keyword函数case 'l'部分添加一行 if (!strcmp(s, "osetup")) return K_losetup;
 
在builtins.c中添加do_losetup函数和setloop函数。
 
int do_losetup(int nargs, char **args) {
 /* max 2 args,  no option*/
    if (nargs != 3)
        return -1;
 if (setloop(args[1], args[2], 0) < 0)
  return -2;
 return 0;
}
static int setloop(char *device, const char *file, unsigned long long offset)
{
 bb_loop_info loopinfo;
 struct stat statbuf;
 int dfd, ffd, mode, rc = -1;
 /* Open the file. */
 mode = O_RDONLY;
 ffd = open(file, mode);
 if (ffd < 0)
  return -errno;
 /* Ran out of block devices, return failure.  */
 if (stat(device, &statbuf) || !S_ISBLK(statbuf.st_mode)) {
  return -errno;
  }
 /* Open the sucker and check its loopiness.  */
 dfd = open(device, mode);
 if (dfd < 0)
  return -errno;
 rc = ioctl(dfd, BB_LOOP_GET_STATUS, &loopinfo);
 /* If device is free, claim it.  */
 if (rc && errno == ENXIO) {
  memset(&loopinfo, 0, sizeof(loopinfo));
  strncpy((char *)loopinfo.lo_file_name, file, LO_NAME_SIZE);
  loopinfo.lo_offset = offset;
  /* Associate free loop device with file.  */
  if (!ioctl(dfd, LOOP_SET_FD, ffd)) {
   if (!ioctl(dfd, BB_LOOP_SET_STATUS, &loopinfo))
    rc = 0;
   else
    ioctl(dfd, LOOP_CLR_FD, 0);
  }
    }
 else
    return -errno;
 close(dfd);
 close(ffd);
 return rc;
}
重新编译,现在可以在init.rc中使用losetup功能。
阅读(1815) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~