2 * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
16 #include <sys/types.h>
17 #include <sys/ioctl.h>
18 #include <sys/mount.h>
19 #include <mtd/mtd-user.h>
29 #include <libubox/list.h>
30 #include <libubox/blob.h>
31 #include <libubox/md5.h>
33 #include "libfstools/libfstools.h"
34 #include "libfstools/volume.h"
35 #include "libfstools/snapshot.h"
38 config_write(int argc, char **argv)
40 struct volume *v = volume_find("rootfs_data");
46 ret = volatile_write(v, 0);
48 ret = sentinel_write(v, 0);
54 config_read(int argc, char **argv)
56 struct volume *v = volume_find("rootfs_data");
57 struct file_header conf, sentinel;
58 int next, block, ret = 0;
64 block = config_find(v, &conf, &sentinel);
65 next = snapshot_next_free(v, &seq);
66 if (is_config(&conf) && conf.seq == seq)
68 else if (!is_config(&sentinel) || sentinel.seq != seq)
71 unlink("/tmp/config.tar.gz");
72 ret = snapshot_read_file(v, block, "/tmp/config.tar.gz", CONF);
75 fprintf(stderr, "failed to read /tmp/config.tar.gz\n");
81 snapshot_write(int argc, char **argv)
83 struct volume *v = volume_find("rootfs_data");
90 block = snapshot_next_free(v, &seq);
94 ret = snapshot_write_file(v, block, "/tmp/snapshot.tar.gz", seq + 1, DATA);
96 fprintf(stderr, "failed to write /tmp/snapshot.tar.gz\n");
98 fprintf(stderr, "wrote /tmp/snapshot.tar.gz\n");
104 snapshot_mark(int argc, char **argv)
106 __be32 owrt = cpu_to_be32(OWRT);
111 fprintf(stderr, "This will remove all snapshot data stored on the system. Are you sure? [N/y]\n");
112 if (getchar() != 'y')
115 v = volume_find("rootfs_data");
117 fprintf(stderr, "no rootfs_data was found\n");
121 fd = open(v->blk, O_WRONLY);
122 fprintf(stderr, "%s - marking with 0x%08x\n", v->blk, owrt);
124 fprintf(stderr, "opening %s failed\n", v->blk);
128 sz = write(fd, &owrt, sizeof(owrt));
132 fprintf(stderr, "writing %s failed: %s\n", v->blk, strerror(errno));
140 snapshot_read(int argc, char **argv)
142 struct volume *v = volume_find("rootfs_data");;
143 int block = 0, ret = 0;
150 block = atoi(argv[2]);
151 if (block >= (v->size / v->block_size)) {
152 fprintf(stderr, "invalid block %d > %llu\n", block, v->size / v->block_size);
155 snprintf(file, sizeof(file), "/tmp/snapshot/block%d.tar.gz", block);
157 ret = snapshot_read_file(v, block, file, DATA);
162 snprintf(file, sizeof(file), "/tmp/snapshot/block%d.tar.gz", block);
163 block = snapshot_read_file(v, block, file, DATA);
173 struct volume *v = volume_find("rootfs_data");
174 struct file_header hdr = { 0 }, conf;
180 fprintf(stderr, "sectors:\t%llu, block_size:\t%dK\n", v->size / v->block_size, v->block_size / 1024);
182 if (volume_read(v, &hdr, block * v->block_size, sizeof(struct file_header))) {
183 fprintf(stderr, "scanning for next free block failed\n");
189 if (hdr.magic != OWRT)
192 if (hdr.type == DATA)
193 fprintf(stderr, "block %d:\tsnapshot entry, size: %d, sectors: %d, sequence: %d\n", block, hdr.length, pad_file_size(v, hdr.length) / v->block_size, hdr.seq);
194 else if (hdr.type == CONF)
195 fprintf(stderr, "block %d:\tvolatile entry, size: %d, sectors: %d, sequence: %d\n", block, hdr.length, pad_file_size(v, hdr.length) / v->block_size, hdr.seq);
197 if (hdr.type == DATA && !valid_file_size(hdr.length))
198 block += pad_file_size(v, hdr.length) / v->block_size;
199 } while (hdr.type == DATA);
200 block = config_find(v, &conf, &hdr);
202 fprintf(stderr, "block %d:\tsentinel entry, size: %d, sectors: %d, sequence: %d\n", block, hdr.length, pad_file_size(v, hdr.length) / v->block_size, hdr.seq);
207 int main(int argc, char **argv)
212 if (!strcmp(argv[1], "config_read"))
213 return config_read(argc, argv);
214 if (!strcmp(argv[1], "config_write"))
215 return config_write(argc, argv);
216 if (!strcmp(argv[1], "read"))
217 return snapshot_read(argc, argv);
218 if (!strcmp(argv[1], "write"))
219 return snapshot_write(argc, argv);
220 if (!strcmp(argv[1], "mark"))
221 return snapshot_mark(argc, argv);
222 if (!strcmp(argv[1], "info"))
223 return snapshot_info();