Skip to content
Browse files

simple LVM defragment tool

  • Loading branch information...
1 parent 0b6f676 commit 824e8abf2709bc658fc2bcfcd79b8e31d3062ced @tomato42 committed
Showing with 105 additions and 2 deletions.
  1. +5 −2 Makefile
  2. +100 −0 lvmdefrag.c
View
7 Makefile
@@ -3,7 +3,7 @@ CC=gcc
CFLAGS=-std=gnu99 -Wall -pthread -ggdb3 -lm -O1
LFLAGS=-llvm2cmd -pthread -lconfuse
-all: lvmtscd lvmtscat lvmls lvmtsd
+all: lvmtscd lvmtscat lvmls lvmtsd lvmdefrag
lvmtsd: lvmtsd.c lvmls.o extents.o volumes.o activity_stats.o config.o
$(CC) $(CFLAGS) lvmtsd.c lvmls.o extents.o volumes.o activity_stats.o config.o $(LFLAGS) -o lvmtsd
@@ -23,6 +23,9 @@ lvmls.o: lvmls.c
lvmls: lvmls.c
$(CC) $(CFLAGS) lvmls.c $(LFLAGS) -DSTANDALONE -o lvmls
+lvmdefrag: lvmdefrag.c
+ $(CC) $(CFLAGS) lvmdefrag.c lvmls.o $(LFLAGS) -o lvmdefrag
+
lvmtscd: lvmtscd.c activity_stats.o
$(CC) $(CFLAGS) lvmtscd.c activity_stats.o -o lvmtscd
@@ -33,7 +36,7 @@ activity_stats.o: activity_stats.c
$(CC) $(CFLAGS) -c activity_stats.c
clean:
- rm -f lvmtscd lvmtscat lvmls lvmtsd activity_stats_test *.o
+ rm -f lvmtscd lvmtscat lvmls lvmtsd activity_stats_test lvmdefrag *.o
test: activity_stats_test
./activity_stats_test
View
100 lvmdefrag.c
@@ -0,0 +1,100 @@
+/*
+ * Copyright (C) 2012 Hubert Kario <kario@wsisiz.edu.pl>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <string.h>
+#include <lvm2cmd.h>
+#include "lvmls.h"
+
+int
+main(int argc, char **argv)
+{
+ if (argc != 4) {
+ fprintf(stderr, "Tool to defragment LogicalVolume on selected PhysicalVolume\n");
+ fprintf(stderr, "Usage: lvmdefrag VolumeGroup LogicalVolume PhysicalVolume\n");
+ return EXIT_FAILURE;
+ }
+
+ char *vg_name = argv[1];
+ char *lv_name = argv[2];
+ char *pv_name = argv[3];
+
+ struct program_params pp = { .lvm2_handle = NULL };
+ init_le_to_pe(&pp);
+
+ struct le_info first_le;
+
+ first_le = get_first_LE_info(vg_name, lv_name, pv_name);
+
+ for(size_t i=0; i < pv_segments_num; i ++)
+ if( !strcmp(pv_segments[i].lv_name, lv_name) &&
+ !strcmp(pv_segments[i].vg_name, vg_name) &&
+ !strcmp(pv_segments[i].pv_name, pv_name)) {
+
+ long int optimal_pos;
+ optimal_pos = first_le.pe + pv_segments[i].lv_start - first_le.le;
+
+ if (pv_segments[i].pv_start == optimal_pos)
+ continue;
+
+ struct le_info optimal;
+ long int move_extent = 0;
+
+ for (long int j=optimal_pos;
+ j < optimal_pos + pv_segments[i].pv_length;
+ j++) {
+
+ optimal = get_PE_allocation(vg_name, pv_name, j);
+
+ if (optimal.dev == NULL) {
+ printf("# Optimal position for LE %li-%li on %s is after end of "
+ "the device\n",
+ pv_segments[i].lv_start,
+ pv_segments[i].lv_start + pv_segments[i].pv_length,
+ pv_name);
+ break;
+ } else if (strcmp(optimal.lv_name, "free")) {
+ printf("# Optimal position for LE %li-%li is used by %s LE %li\n",
+ pv_segments[i].lv_start,
+ pv_segments[i].lv_start + pv_segments[i].pv_length,
+ optimal.lv_name, optimal.le);
+ break;
+ }
+
+ move_extent++;
+ }
+
+ if (move_extent) {
+ printf("pvmove -i1 --alloc anywhere %s:%li-%li %s:%li-%li # LE %li (size: %li)\n",
+ pv_name,
+ pv_segments[i].pv_start,
+ pv_segments[i].pv_start + move_extent - 1,
+ pv_name,
+ optimal_pos,
+ optimal_pos + move_extent - 1,
+ pv_segments[i].lv_start,
+ move_extent);
+ }
+ }
+
+ le_to_pe_exit(&pp);
+ lvm2_exit(pp.lvm2_handle);
+
+ return EXIT_SUCCESS;
+}

0 comments on commit 824e8ab

Please sign in to comment.
Something went wrong with that request. Please try again.