cpio解压方法:
    1.  # gunzip  XXX.cpio.gz
2. cpio -idmv < XXX.cpio
制作cpio格式文件系统的方法:
1. 执行gen_initramfs_list.sh脚本:
# gen_initramfs_list.sh ./Filesystem/ >filelist  
其中Filesystem文件夹是由上一步解压出来的cpio文件系统目录
2. 执行gen_init_cpio产生cpio文件:
#  gen_init_cpio filelist >rootfs.cpio
3. 压缩cpio生成cpio.gz文件:
    #  gzip rootfs.cpio
所使用文件说明:
gen_initramfs_list.sh:用于产生filelist
gen_init_cpio.c :工具gen_init_cpio的源码,编译后可以用
gen_init_cpio:用于产生cpio格式文件系统的可执行文件
gen_initramfs_list.sh的源码:
[plain] view plaincopyprint?
  1. #!/bin/sh
  2. # Copyright (C) Martin Schlemmer <azarah@nosferatu.za.org>
  3. # Copyright (C) 2006 Sam Ravnborg <sam@ravnborg.org>
  4. #
  5. # Released under the terms of the GNU GPL
  6. #
  7. # Generate a cpio packed initramfs. It uses gen_init_cpio to generate
  8. # the cpio archive, and then compresses it.
  9. # The script may also be used to generate the inputfile used for gen_init_cpio
  10. # This script assumes that gen_init_cpio is located in usr/ directory
  11. # error out on errors
  12. set -e
  13. usage() {
  14. cat << EOF
  15. Usage:
  16. $0 [-o <file>] [-u <uid>] [-g <gid>] {-d | <cpio_source>} ...
  17. -o <file>      Create compressed initramfs file named <file> using
  18. gen_init_cpio and compressor depending on the extension
  19. -u <uid>       User ID to map to user ID 0 (root).
  20. <uid> is only meaningful if <cpio_source> is a
  21. directory.  "squash" forces all files to uid 0.
  22. -g <gid>       Group ID to map to group ID 0 (root).
  23. <gid> is only meaningful if <cpio_source> is a
  24. directory.  "squash" forces all files to gid 0.
  25. <cpio_source>  File list or directory for cpio archive.
  26. If <cpio_source> is a .cpio file it will be used
  27. as direct input to initramfs.
  28. -d             Output the default cpio list.
  29. All options except -o and -l may be repeated and are interpreted
  30. sequentially and immediately.  -u and -g states are preserved across
  31. <cpio_source> options so an explicit "-u 0 -g 0" is required
  32. to reset the root/group mapping.
  33. EOF
  34. }
  35. # awk style field access
  36. # $1 - field number; rest is argument string
  37. field() {
  38. shift $1 ; echo $1
  39. }
  40. list_default_initramfs() {
  41. # echo usr/kinit/kinit
  42. :
  43. }
  44. default_initramfs() {
  45. cat <<-EOF >> ${output}
  46. # This is a very simple, default initramfs
  47. dir /dev 0755 0 0
  48. nod /dev/console 0600 0 0 c 5 1
  49. dir /root 0700 0 0
  50. # file /kinit usr/kinit/kinit 0755 0 0
  51. # slink /init kinit 0755 0 0
  52. EOF
  53. }
  54. filetype() {
  55. local argv1="$1"
  56. # symlink test must come before file test
  57. if [ -L "${argv1}" ]; then
  58. echo "slink"
  59. elif [ -f "${argv1}" ]; then
  60. echo "file"
  61. elif [ -d "${argv1}" ]; then
  62. echo "dir"
  63. elif [ -b "${argv1}" -o -c "${argv1}" ]; then
  64. echo "nod"
  65. elif [ -p "${argv1}" ]; then
  66. echo "pipe"
  67. elif [ -S "${argv1}" ]; then
  68. echo "sock"
  69. else
  70. echo "invalid"
  71. fi
  72. return 0
  73. }
  74. list_print_mtime() {
  75. :
  76. }
  77. print_mtime() {
  78. local my_mtime="0"
  79. if [ -e "$1" ]; then
  80. my_mtime=$(find "$1" -printf "%T@\n" | sort -r | head -n 1)
  81. fi
  82. echo "# Last modified: ${my_mtime}" >> ${output}
  83. echo "" >> ${output}
  84. }
  85. list_parse() {
  86. [ ! -L "$1" ] && echo "$1 \\" || :
  87. }
  88. # for each file print a line in following format
  89. # <filetype> <name> <path to file> <octal mode> <uid> <gid>
  90. # for links, devices etc the format differs. See gen_init_cpio for details
  91. parse() {
  92. local location="$1"
  93. local name="/${location#${srcdir}}"
  94. # change '//' into '/'
  95. name=$(echo "$name" | sed -e 's://*:/:g')
  96. local mode="$2"
  97. local uid="$3"
  98. local gid="$4"
  99. local ftype=$(filetype "${location}")
  100. # remap uid/gid to 0 if necessary
  101. [ "$root_uid" = "squash" ] && uid=0 || [ "$uid" -eq "$root_uid" ] && uid=0
  102. [ "$root_gid" = "squash" ] && gid=0 || [ "$gid" -eq "$root_gid" ] && gid=0
  103. local str="${mode} ${uid} ${gid}"
  104. [ "${ftype}" = "invalid" ] && return 0
  105. [ "${location}" = "${srcdir}" ] && return 0
  106. case "${ftype}" in
  107. "file")
  108. str="${ftype} ${name} ${location} ${str}"
  109. ;;
  110. "nod")
  111. local dev=`LC_ALL=C ls -l "${location}"`
  112. local maj=`field 5 ${dev}`
  113. local min=`field 6 ${dev}`
  114. maj=${maj%,}
  115. [ -b "${location}" ] && dev="b" || dev="c"
  116. str="${ftype} ${name} ${str} ${dev} ${maj} ${min}"
  117. ;;
  118. "slink")
  119. local target=`readlink "${location}"`
  120. str="${ftype} ${name} ${target} ${str}"
  121. ;;
  122. *)
  123. str="${ftype} ${name} ${str}"
  124. ;;
  125. esac
  126. echo "${str}" >> ${output}
  127. return 0
  128. }
  129. unknown_option() {
  130. printf "ERROR: unknown option \"$arg\"\n" >&2
  131. printf "If the filename validly begins with '-', " >&2
  132. printf "then it must be prefixed\n" >&2
  133. printf "by './' so that it won't be interpreted as an option." >&2
  134. printf "\n" >&2
  135. usage >&2
  136. exit 1
  137. }
  138. list_header() {
  139. :
  140. }
  141. header() {
  142. printf "\n#####################\n# $1\n" >> ${output}
  143. }
  144. # process one directory (incl sub-directories)
  145. dir_filelist() {
  146. ${dep_list}header "$1"
  147. srcdir=$(echo "$1" | sed -e 's://*:/:g')
  148. dirlist=$(find "${srcdir}" -printf "%p %m %U %G\n")
  149. # If $dirlist is only one line, then the directory is empty
  150. if [  "$(echo "${dirlist}" | wc -l)" -gt 1 ]; then
  151. ${dep_list}print_mtime "$1"
  152. echo "${dirlist}" | \
  153. while read x; do
  154. ${dep_list}parse ${x}
  155. done
  156. fi
  157. }
  158. # if only one file is specified and it is .cpio file then use it direct as fs
  159. # if a directory is specified then add all files in given direcotry to fs
  160. # if a regular file is specified assume it is in gen_initramfs format
  161. input_file() {
  162. source="$1"
  163. if [ -f "$1" ]; then
  164. ${dep_list}header "$1"
  165. is_cpio="$(echo "$1" | sed 's/^.*\.cpio\..∗\?/cpio/')"
  166. if [ $2 -eq 0 -a ${is_cpio} = "cpio" ]; then
  167. cpio_file=$1
  168. echo "$1" | grep -q '^.*\.cpio\..*' && is_cpio_compressed="compressed"
  169. [ ! -z ${dep_list} ] && echo "$1"
  170. return 0
  171. fi
  172. if [ -z ${dep_list} ]; then
  173. print_mtime "$1" >> ${output}
  174. cat "$1"         >> ${output}
  175. else
  176. echo "$1 \\"
  177. cat "$1" | while read type dir file perm ; do
  178. if [ "$type" = "file" ]; then
  179. echo "$file \\";
  180. fi
  181. done
  182. fi
  183. elif [ -d "$1" ]; then
  184. dir_filelist "$1"
  185. else
  186. echo "  ${prog}: Cannot open '$1'" >&2
  187. exit 1
  188. fi
  189. }
  190. prog=$0
  191. root_uid=0
  192. root_gid=0
  193. dep_list=
  194. cpio_file=
  195. cpio_list=
  196. output="/dev/stdout"
  197. output_file=""
  198. is_cpio_compressed=
  199. compr="gzip -n -9 -f"
  200. arg="$1"
  201. case "$arg" in
  202. "-l")   # files included in initramfs - used by kbuild
  203. dep_list="list_"
  204. echo "deps_initramfs := $0 \\"
  205. shift
  206. ;;
  207. "-o")   # generate compressed cpio image named $1
  208. shift
  209. output_file="$1"
  210. cpio_list="$(mktemp ${TMPDIR:-/tmp}/cpiolist.XXXXXX)"
  211. output=${cpio_list}
  212. echo "$output_file" | grep -q "\.gz$" && compr="gzip -n -9 -f"
  213. echo "$output_file" | grep -q "\.bz2$" && compr="bzip2 -9 -f"
  214. echo "$output_file" | grep -q "\.lzma$" && compr="lzma -9 -f"
  215. echo "$output_file" | grep -q "\.xz$" && \
  216. compr="xz --check=crc32 --lzma2=dict=1MiB"
  217. echo "$output_file" | grep -q "\.lzo$" && compr="lzop -9 -f"
  218. echo "$output_file" | grep -q "\.cpio$" && compr="cat"
  219. shift
  220. ;;
  221. esac
  222. while [ $# -gt 0 ]; do
  223. arg="$1"
  224. shift
  225. case "$arg" in
  226. "-u")   # map $1 to uid=0 (root)
  227. root_uid="$1"
  228. shift
  229. ;;
  230. "-g")   # map $1 to gid=0 (root)
  231. root_gid="$1"
  232. shift
  233. ;;
  234. "-d")   # display default initramfs list
  235. default_list="$arg"
  236. ${dep_list}default_initramfs
  237. ;;
  238. "-h")
  239. usage
  240. exit 0
  241. ;;
  242. *)
  243. case "$arg" in
  244. "-"*)
  245. unknown_option
  246. ;;
  247. *)  # input file/dir - process it
  248. input_file "$arg" "$#"
  249. ;;
  250. esac
  251. ;;
  252. esac
  253. done
  254. # If output_file is set we will generate cpio archive and compress it
  255. # we are careful to delete tmp files
  256. if [ ! -z ${output_file} ]; then
  257. if [ -z ${cpio_file} ]; then
  258. timestamp=
  259. if test -n "$KBUILD_BUILD_TIMESTAMP"; then
  260. timestamp="$(date -d"$KBUILD_BUILD_TIMESTAMP" +%s || :)"
  261. if test -n "$timestamp"; then
  262. timestamp="-t $timestamp"
  263. fi
  264. fi
  265. cpio_tfile="$(mktemp ${TMPDIR:-/tmp}/cpiofile.XXXXXX)"
  266. usr/gen_init_cpio $timestamp ${cpio_list} > ${cpio_tfile}
  267. else
  268. cpio_tfile=${cpio_file}
  269. fi
  270. rm ${cpio_list}
  271. if [ "${is_cpio_compressed}" = "compressed" ]; then
  272. cat ${cpio_tfile} > ${output_file}
  273. else
  274. (cat ${cpio_tfile} | ${compr}  - > ${output_file}) \
  275. || (rm -f ${output_file} ; false)
  276. fi
  277. [ -z ${cpio_file} ] && rm ${cpio_tfile}
  278. fi
  279. exit 0

gen_init_cpio.c的源码:

[cpp] view plaincopyprint?
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <time.h>
  8. #include <fcntl.h>
  9. #include <errno.h>
  10. #include <ctype.h>
  11. #include <limits.h>
  12. /*
  13. * Original work by Jeff Garzik
  14. *
  15. * External file lists, symlink, pipe and fifo support by Thayne Harbaugh
  16. * Hard link support by Luciano Rocha
  17. */
  18. #define xstr(s) #s
  19. #define str(s) xstr(s)
  20. static unsigned int offset;
  21. static unsigned int ino = 721;
  22. static time_t default_mtime;
  23. struct file_handler {
  24. const char *type;
  25. int (*handler)(const char *line);
  26. };
  27. static void push_string(const char *name)
  28. {
  29. unsigned int name_len = strlen(name) + 1;
  30. fputs(name, stdout);
  31. putchar(0);
  32. offset += name_len;
  33. }
  34. static void push_pad (void)
  35. {
  36. while (offset & 3) {
  37. putchar(0);
  38. offset++;
  39. }
  40. }
  41. static void push_rest(const char *name)
  42. {
  43. unsigned int name_len = strlen(name) + 1;
  44. unsigned int tmp_ofs;
  45. fputs(name, stdout);
  46. putchar(0);
  47. offset += name_len;
  48. tmp_ofs = name_len + 110;
  49. while (tmp_ofs & 3) {
  50. putchar(0);
  51. offset++;
  52. tmp_ofs++;
  53. }
  54. }
  55. static void push_hdr(const char *s)
  56. {
  57. fputs(s, stdout);
  58. offset += 110;
  59. }
  60. static void cpio_trailer(void)
  61. {
  62. char s[256];
  63. const char name[] = "TRAILER!!!";
  64. sprintf(s, "%s%08X%08X%08lX%08lX%08X%08lX"
  65. "%08X%08X%08X%08X%08X%08X%08X",
  66. "070701",       /* magic */
  67. 0,          /* ino */
  68. 0,          /* mode */
  69. (long) 0,       /* uid */
  70. (long) 0,       /* gid */
  71. 1,          /* nlink */
  72. (long) 0,       /* mtime */
  73. 0,          /* filesize */
  74. 0,          /* major */
  75. 0,          /* minor */
  76. 0,          /* rmajor */
  77. 0,          /* rminor */
  78. (unsigned)strlen(name)+1, /* namesize */
  79. 0);         /* chksum */
  80. push_hdr(s);
  81. push_rest(name);
  82. while (offset % 512) {
  83. putchar(0);
  84. offset++;
  85. }
  86. }
  87. static int cpio_mkslink(const char *name, const char *target,
  88. unsigned int mode, uid_t uid, gid_t gid)
  89. {
  90. char s[256];
  91. if (name[0] == '/')
  92. name++;
  93. sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX"
  94. "%08X%08X%08X%08X%08X%08X%08X",
  95. "070701",       /* magic */
  96. ino++,          /* ino */
  97. S_IFLNK | mode,     /* mode */
  98. (long) uid,     /* uid */
  99. (long) gid,     /* gid */
  100. 1,          /* nlink */
  101. (long) default_mtime,   /* mtime */
  102. (unsigned)strlen(target)+1, /* filesize */
  103. 3,          /* major */
  104. 1,          /* minor */
  105. 0,          /* rmajor */
  106. 0,          /* rminor */
  107. (unsigned)strlen(name) + 1,/* namesize */
  108. 0);         /* chksum */
  109. push_hdr(s);
  110. push_string(name);
  111. push_pad();
  112. push_string(target);
  113. push_pad();
  114. return 0;
  115. }
  116. static int cpio_mkslink_line(const char *line)
  117. {
  118. char name[PATH_MAX + 1];
  119. char target[PATH_MAX + 1];
  120. unsigned int mode;
  121. int uid;
  122. int gid;
  123. int rc = -1;
  124. if (5 != sscanf(line, "%" str(PATH_MAX) "s %" str(PATH_MAX) "s %o %d %d", name, target, &mode, &uid, &gid)) {
  125. fprintf(stderr, "Unrecognized dir format '%s'", line);
  126. goto fail;
  127. }
  128. rc = cpio_mkslink(name, target, mode, uid, gid);
  129. fail:
  130. return rc;
  131. }
  132. static int cpio_mkgeneric(const char *name, unsigned int mode,
  133. uid_t uid, gid_t gid)
  134. {
  135. char s[256];
  136. if (name[0] == '/')
  137. name++;
  138. sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX"
  139. "%08X%08X%08X%08X%08X%08X%08X",
  140. "070701",       /* magic */
  141. ino++,          /* ino */
  142. mode,           /* mode */
  143. (long) uid,     /* uid */
  144. (long) gid,     /* gid */
  145. 2,          /* nlink */
  146. (long) default_mtime,   /* mtime */
  147. 0,          /* filesize */
  148. 3,          /* major */
  149. 1,          /* minor */
  150. 0,          /* rmajor */
  151. 0,          /* rminor */
  152. (unsigned)strlen(name) + 1,/* namesize */
  153. 0);         /* chksum */
  154. push_hdr(s);
  155. push_rest(name);
  156. return 0;
  157. }
  158. enum generic_types {
  159. GT_DIR,
  160. GT_PIPE,
  161. GT_SOCK
  162. };
  163. struct generic_type {
  164. const char *type;
  165. mode_t mode;
  166. };
  167. static struct generic_type generic_type_table[] = {
  168. [GT_DIR] = {
  169. .type = "dir",
  170. .mode = S_IFDIR
  171. },
  172. [GT_PIPE] = {
  173. .type = "pipe",
  174. .mode = S_IFIFO
  175. },
  176. [GT_SOCK] = {
  177. .type = "sock",
  178. .mode = S_IFSOCK
  179. }
  180. };
  181. static int cpio_mkgeneric_line(const char *line, enum generic_types gt)
  182. {
  183. char name[PATH_MAX + 1];
  184. unsigned int mode;
  185. int uid;
  186. int gid;
  187. int rc = -1;
  188. if (4 != sscanf(line, "%" str(PATH_MAX) "s %o %d %d", name, &mode, &uid, &gid)) {
  189. fprintf(stderr, "Unrecognized %s format '%s'",
  190. line, generic_type_table[gt].type);
  191. goto fail;
  192. }
  193. mode |= generic_type_table[gt].mode;
  194. rc = cpio_mkgeneric(name, mode, uid, gid);
  195. fail:
  196. return rc;
  197. }
  198. static int cpio_mkdir_line(const char *line)
  199. {
  200. return cpio_mkgeneric_line(line, GT_DIR);
  201. }
  202. static int cpio_mkpipe_line(const char *line)
  203. {
  204. return cpio_mkgeneric_line(line, GT_PIPE);
  205. }
  206. static int cpio_mksock_line(const char *line)
  207. {
  208. return cpio_mkgeneric_line(line, GT_SOCK);
  209. }
  210. static int cpio_mknod(const char *name, unsigned int mode,
  211. uid_t uid, gid_t gid, char dev_type,
  212. unsigned int maj, unsigned int min)
  213. {
  214. char s[256];
  215. if (dev_type == 'b')
  216. mode |= S_IFBLK;
  217. else
  218. mode |= S_IFCHR;
  219. if (name[0] == '/')
  220. name++;
  221. sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX"
  222. "%08X%08X%08X%08X%08X%08X%08X",
  223. "070701",       /* magic */
  224. ino++,          /* ino */
  225. mode,           /* mode */
  226. (long) uid,     /* uid */
  227. (long) gid,     /* gid */
  228. 1,          /* nlink */
  229. (long) default_mtime,   /* mtime */
  230. 0,          /* filesize */
  231. 3,          /* major */
  232. 1,          /* minor */
  233. maj,            /* rmajor */
  234. min,            /* rminor */
  235. (unsigned)strlen(name) + 1,/* namesize */
  236. 0);         /* chksum */
  237. push_hdr(s);
  238. push_rest(name);
  239. return 0;
  240. }
  241. static int cpio_mknod_line(const char *line)
  242. {
  243. char name[PATH_MAX + 1];
  244. unsigned int mode;
  245. int uid;
  246. int gid;
  247. char dev_type;
  248. unsigned int maj;
  249. unsigned int min;
  250. int rc = -1;
  251. if (7 != sscanf(line, "%" str(PATH_MAX) "s %o %d %d %c %u %u",
  252. name, &mode, &uid, &gid, &dev_type, &maj, &min)) {
  253. fprintf(stderr, "Unrecognized nod format '%s'", line);
  254. goto fail;
  255. }
  256. rc = cpio_mknod(name, mode, uid, gid, dev_type, maj, min);
  257. fail:
  258. return rc;
  259. }
  260. static int cpio_mkfile(const char *name, const char *location,
  261. unsigned int mode, uid_t uid, gid_t gid,
  262. unsigned int nlinks)
  263. {
  264. char s[256];
  265. char *filebuf = NULL;
  266. struct stat buf;
  267. long size;
  268. int file = -1;
  269. int retval;
  270. int rc = -1;
  271. int namesize;
  272. int i;
  273. mode |= S_IFREG;
  274. file = open (location, O_RDONLY);
  275. if (file < 0) {
  276. fprintf (stderr, "File %s could not be opened for reading\n", location);
  277. goto error;
  278. }
  279. retval = fstat(file, &buf);
  280. if (retval) {
  281. fprintf(stderr, "File %s could not be stat()'ed\n", location);
  282. goto error;
  283. }
  284. filebuf = malloc(buf.st_size);
  285. if (!filebuf) {
  286. fprintf (stderr, "out of memory\n");
  287. goto error;
  288. }
  289. retval = read (file, filebuf, buf.st_size);
  290. if (retval < 0) {
  291. fprintf (stderr, "Can not read %s file\n", location);
  292. goto error;
  293. }
  294. size = 0;
  295. for (i = 1; i <= nlinks; i++) {
  296. /* data goes on last link */
  297. if (i == nlinks) size = buf.st_size;
  298. if (name[0] == '/')
  299. name++;
  300. namesize = strlen(name) + 1;
  301. sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX"
  302. "%08lX%08X%08X%08X%08X%08X%08X",
  303. "070701",       /* magic */
  304. ino,            /* ino */
  305. mode,           /* mode */
  306. (long) uid,     /* uid */
  307. (long) gid,     /* gid */
  308. nlinks,         /* nlink */
  309. (long) buf.st_mtime,    /* mtime */
  310. size,           /* filesize */
  311. 3,          /* major */
  312. 1,          /* minor */
  313. 0,          /* rmajor */
  314. 0,          /* rminor */
  315. namesize,       /* namesize */
  316. 0);         /* chksum */
  317. push_hdr(s);
  318. push_string(name);
  319. push_pad();
  320. if (size) {
  321. if (fwrite(filebuf, size, 1, stdout) != 1) {
  322. fprintf(stderr, "writing filebuf failed\n");
  323. goto error;
  324. }
  325. offset += size;
  326. push_pad();
  327. }
  328. name += namesize;
  329. }
  330. ino++;
  331. rc = 0;
  332. error:
  333. if (filebuf) free(filebuf);
  334. if (file >= 0) close(file);
  335. return rc;
  336. }
  337. static char *cpio_replace_env(char *new_location)
  338. {
  339. char expanded[PATH_MAX + 1];
  340. char env_var[PATH_MAX + 1];
  341. char *start;
  342. char *end;
  343. for (start = NULL; (start = strstr(new_location, "${")); ) {
  344. end = strchr(start, '}');
  345. if (start < end) {
  346. *env_var = *expanded = '\0';
  347. strncat(env_var, start + 2, end - start - 2);
  348. strncat(expanded, new_location, start - new_location);
  349. strncat(expanded, getenv(env_var), PATH_MAX);
  350. strncat(expanded, end + 1, PATH_MAX);
  351. strncpy(new_location, expanded, PATH_MAX);
  352. } else
  353. break;
  354. }
  355. return new_location;
  356. }
  357. static int cpio_mkfile_line(const char *line)
  358. {
  359. char name[PATH_MAX + 1];
  360. char *dname = NULL; /* malloc'ed buffer for hard links */
  361. char location[PATH_MAX + 1];
  362. unsigned int mode;
  363. int uid;
  364. int gid;
  365. int nlinks = 1;
  366. int end = 0, dname_len = 0;
  367. int rc = -1;
  368. if (5 > sscanf(line, "%" str(PATH_MAX) "s %" str(PATH_MAX)
  369. "s %o %d %d %n",
  370. name, location, &mode, &uid, &gid, &end)) {
  371. fprintf(stderr, "Unrecognized file format '%s'", line);
  372. goto fail;
  373. }
  374. if (end && isgraph(line[end])) {
  375. int len;
  376. int nend;
  377. dname = malloc(strlen(line));
  378. if (!dname) {
  379. fprintf (stderr, "out of memory (%d)\n", dname_len);
  380. goto fail;
  381. }
  382. dname_len = strlen(name) + 1;
  383. memcpy(dname, name, dname_len);
  384. do {
  385. nend = 0;
  386. if (sscanf(line + end, "%" str(PATH_MAX) "s %n",
  387. name, &nend) < 1)
  388. break;
  389. len = strlen(name) + 1;
  390. memcpy(dname + dname_len, name, len);
  391. dname_len += len;
  392. nlinks++;
  393. end += nend;
  394. } while (isgraph(line[end]));
  395. } else {
  396. dname = name;
  397. }
  398. rc = cpio_mkfile(dname, cpio_replace_env(location),
  399. mode, uid, gid, nlinks);
  400. fail:
  401. if (dname_len) free(dname);
  402. return rc;
  403. }
  404. static void usage(const char *prog)
  405. {
  406. fprintf(stderr, "Usage:\n"
  407. "\t%s [-t <timestamp>] <cpio_list>\n"
  408. "\n"
  409. "<cpio_list> is a file containing newline separated entries that\n"
  410. "describe the files to be included in the initramfs archive:\n"
  411. "\n"
  412. "# a comment\n"
  413. "file <name> <location> <mode> <uid> <gid> [<hard links>]\n"
  414. "dir <name> <mode> <uid> <gid>\n"
  415. "nod <name> <mode> <uid> <gid> <dev_type> <maj> <min>\n"
  416. "slink <name> <target> <mode> <uid> <gid>\n"
  417. "pipe <name> <mode> <uid> <gid>\n"
  418. "sock <name> <mode> <uid> <gid>\n"
  419. "\n"
  420. "<name>       name of the file/dir/nod/etc in the archive\n"
  421. "<location>   location of the file in the current filesystem\n"
  422. "             expands shell variables quoted with ${}\n"
  423. "<target>     link target\n"
  424. "<mode>       mode/permissions of the file\n"
  425. "<uid>        user id (0=root)\n"
  426. "<gid>        group id (0=root)\n"
  427. "<dev_type>   device type (b=block, c=character)\n"
  428. "<maj>        major number of nod\n"
  429. "<min>        minor number of nod\n"
  430. "<hard links> space separated list of other links to file\n"
  431. "\n"
  432. "example:\n"
  433. "# A simple initramfs\n"
  434. "dir /dev 0755 0 0\n"
  435. "nod /dev/console 0600 0 0 c 5 1\n"
  436. "dir /root 0700 0 0\n"
  437. "dir /sbin 0755 0 0\n"
  438. "file /sbin/kinit /usr/src/klibc/kinit/kinit 0755 0 0\n"
  439. "\n"
  440. "<timestamp> is time in seconds since Epoch that will be used\n"
  441. "as mtime for symlinks, special files and directories. The default\n"
  442. "is to use the current time for these entries.\n",
  443. prog);
  444. }
  445. struct file_handler file_handler_table[] = {
  446. {
  447. .type    = "file",
  448. .handler = cpio_mkfile_line,
  449. }, {
  450. .type    = "nod",
  451. .handler = cpio_mknod_line,
  452. }, {
  453. .type    = "dir",
  454. .handler = cpio_mkdir_line,
  455. }, {
  456. .type    = "slink",
  457. .handler = cpio_mkslink_line,
  458. }, {
  459. .type    = "pipe",
  460. .handler = cpio_mkpipe_line,
  461. }, {
  462. .type    = "sock",
  463. .handler = cpio_mksock_line,
  464. }, {
  465. .type    = NULL,
  466. .handler = NULL,
  467. }
  468. };
  469. #define LINE_SIZE (2 * PATH_MAX + 50)
  470. int main (int argc, char *argv[])
  471. {
  472. FILE *cpio_list;
  473. char line[LINE_SIZE];
  474. char *args, *type;
  475. int ec = 0;
  476. int line_nr = 0;
  477. const char *filename;
  478. default_mtime = time(NULL);
  479. while (1) {
  480. int opt = getopt(argc, argv, "t:h");
  481. char *invalid;
  482. if (opt == -1)
  483. break;
  484. switch (opt) {
  485. case 't':
  486. default_mtime = strtol(optarg, &invalid, 10);
  487. if (!*optarg || *invalid) {
  488. fprintf(stderr, "Invalid timestamp: %s\n",
  489. optarg);
  490. usage(argv[0]);
  491. exit(1);
  492. }
  493. break;
  494. case 'h':
  495. case '?':
  496. usage(argv[0]);
  497. exit(opt == 'h' ? 0 : 1);
  498. }
  499. }
  500. if (argc - optind != 1) {
  501. usage(argv[0]);
  502. exit(1);
  503. }
  504. filename = argv[optind];
  505. if (!strcmp(filename, "-"))
  506. cpio_list = stdin;
  507. else if (!(cpio_list = fopen(filename, "r"))) {
  508. fprintf(stderr, "ERROR: unable to open '%s': %s\n\n",
  509. filename, strerror(errno));
  510. usage(argv[0]);
  511. exit(1);
  512. }
  513. while (fgets(line, LINE_SIZE, cpio_list)) {
  514. int type_idx;
  515. size_t slen = strlen(line);
  516. line_nr++;
  517. if ('#' == *line) {
  518. /* comment - skip to next line */
  519. continue;
  520. }
  521. if (! (type = strtok(line, " \t"))) {
  522. fprintf(stderr,
  523. "ERROR: incorrect format, could not locate file type line %d: '%s'\n",
  524. line_nr, line);
  525. ec = -1;
  526. break;
  527. }
  528. if ('\n' == *type) {
  529. /* a blank line */
  530. continue;
  531. }
  532. if (slen == strlen(type)) {
  533. /* must be an empty line */
  534. continue;
  535. }
  536. if (! (args = strtok(NULL, "\n"))) {
  537. fprintf(stderr,
  538. "ERROR: incorrect format, newline required line %d: '%s'\n",
  539. line_nr, line);
  540. ec = -1;
  541. }
  542. for (type_idx = 0; file_handler_table[type_idx].type; type_idx++) {
  543. int rc;
  544. if (! strcmp(line, file_handler_table[type_idx].type)) {
  545. if ((rc = file_handler_table[type_idx].handler(args))) {
  546. ec = rc;
  547. fprintf(stderr, " line %d\n", line_nr);
  548. }
  549. break;
  550. }
  551. }
  552. if (NULL == file_handler_table[type_idx].type) {
  553. fprintf(stderr, "unknown file type line %d: '%s'\n",
  554. line_nr, line);
  555. }
  556. }
  557. if (ec == 0)
  558. cpio_trailer();
  559. exit(ec);
  560. }

cpio文件系统的解压和制作方法相关推荐

  1. linux下 tar解压 gz解压 bz2等各种解压文件使用方法

    点击打开链接 linux下 tar解压 gz解压 bz2等各种解压文件使用方法                .tar 解包:tar xvf FileName.tar 打包:tar cvf FileN ...

  2. Ubuntu解压文件的方法

    一般通过默认安装的ubuntu是不能解压rar文件的,只有在安装了rar解压工具之后,才可以解压.其实在ubuntu下安装rar解压工具是非常简单的,只需要两个步骤就可以迅速搞定. ubuntu 下r ...

  3. tar gz bz bz2 等各种解压文件使用方法

    .tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) --------------- .gz 解压 ...

  4. 测试掌握的Linux解压,轻松掌握Linux压缩/解压文件的方法

    对于在Linux下解压大型的*.zip文件,相信大家一般都会通过使用winrar直接在smb中来进行解压的操作,虽然说最终可能能够解压但有时候会存在解压时间长或者网络原因出错等故障的情况出现.那么有没 ...

  5. python遍历目录压缩文件夹_Python实现多级目录压缩与解压文件的方法

    本文实例讲述了Python实现多级目录压缩与解压文件的方法.分享给大家供大家参考,具体如下: 咱向来就是拿来主意,也发个东西供同行"拿来"使用吧 咱信奉的就是少量的代码完成大量的工 ...

  6. Arch Linux下解决zip解压乱码的方法

    Arch Linux下解决zip解压乱码的方法 完全转载于一个大佬的论坛发帖,感觉很有用但没有搜到CSDN的相关文章所以转过来,瑟瑟发抖 安装unarchiver,用unar解压zip文件.(给kde ...

  7. cpio.gz的解压方式

    操作系统:Redhat 6.4 安装软件:Oracle 10.2.0.1 今天在安装10G的时候,同事给了一个这样的安装介质:10201_database_linux_x86_64.cpio.gz 跟 ...

  8. 解压命令linux tgz,Linux_centos_redhat下tar命令解压tgz文件方法

    .tar.gz,或者.tgz的文件一般是在Linux下用tar和gnuzip压缩的文件. 1.在Linux下展开.tar.gz文件:tar zxvf filename.tar.gz filename ...

  9. cpio.gz文件解压方法

    2019独角兽企业重金招聘Python工程师标准>>> 下载了 10201_database_linux_x86_64.cpio.gz 文件,解压方法如下: 1. gunzip 10 ...

最新文章

  1. 如何快速而准确的获取生物体的遗传信息一直是生命科学 中的一个非常重要的研究点
  2. ODBC访问EXCEL
  3. oracle 度量 预警,度量阀值预警总结
  4. Linux编程简介——动态链接库
  5. Swoole-2.1.2 进程池模块的使用
  6. 【机器视觉】 dev_inspect_ctrl算子
  7. java外部接口图解_java代码实现访问网络外部接口并获取数据的工具类详解
  8. python 操作mongodb数据库参考文档
  9. JAVA并发,线程异常捕获
  10. vscode生成代码图片_vs Code 快速生成代码
  11. node中使用shell脚本
  12. win10 Python开发环境搭建 PyCharm IDE安装
  13. python难学吗-Python为什么那么受欢迎?学习Python难不难?
  14. Firefox扩展IE Tab Plus内置功能导致浏览所有网页加载superfish.com脚本
  15. 怎样更改itunes备份位置_妙招更改iTunes备份地址释放C盘空间
  16. html input限制输入小数,限制input输入小数只能到3位或者只能输入正整数(兼容ios)...
  17. MWorks建模、仿真、分析优化平台
  18. SkyWalking Agent数据采集和上报原理浅析
  19. 灰狼算法(GWO)优化长短期记忆神经网络的数据回归预测,GWO-LSTM回归预测,多输入单输出模型。
  20. Centos7安装trac手册

热门文章

  1. Python之闰年算法
  2. ElementUI Table边框css修改
  3. halcon学习笔记(14)——模板匹配
  4. 计算机网络系列——网络分层
  5. 网络跑满 限制可保留宽带 基于策略的Qos
  6. 网络访问相关知识(入门)
  7. 基于Unity3D的Android游戏添加google广告的方法——使用AdMob
  8. uniapp | 开发中遇到的兼容性问题
  9. Javascript 构造函数深入理解
  10. 神机百炼2.41-BFS