linux_swap.c revision b5e694267142042228a6cac99ecad6c4b4ef8759
2880N/A/*
2880N/A * volume_id - reads filesystem label and uuid
2880N/A *
2880N/A * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
2880N/A *
2880N/A * This program is free software; you can redistribute it and/or modify it
2880N/A * under the terms of the GNU General Public License as published by the
2880N/A * Free Software Foundation version 2 of the License.
2880N/A */
2880N/A
2880N/A#ifndef _GNU_SOURCE
2880N/A#define _GNU_SOURCE 1
2880N/A#endif
2880N/A
2880N/A#ifdef HAVE_CONFIG_H
2880N/A# include <config.h>
2880N/A#endif
2880N/A
2880N/A#include <stdio.h>
2880N/A#include <stdlib.h>
2880N/A#include <unistd.h>
2880N/A#include <string.h>
2880N/A#include <errno.h>
2880N/A#include <ctype.h>
2880N/A
2880N/A#include "libvolume_id.h"
2880N/A#include "util.h"
2880N/A
2880N/Astruct swap_header_v1_2 {
2880N/A uint8_t bootbits[1024];
2880N/A uint32_t version;
2880N/A uint32_t last_page;
2880N/A uint32_t nr_badpages;
2880N/A uint8_t uuid[16];
2880N/A uint8_t volume_name[16];
2880N/A} PACKED *sw;
3661N/A
3661N/A#define LARGEST_PAGESIZE 0x4000
3778N/A
3778N/Aint volume_id_probe_linux_swap(struct volume_id *id, uint64_t off)
3778N/A{
2880N/A const uint8_t *buf;
2880N/A unsigned int page;
2880N/A
2880N/A dbg("probing at offset 0x%llx", (unsigned long long) off);
2880N/A
2880N/A /* the swap signature is at the end of the PAGE_SIZE */
2880N/A for (page = 0x1000; page <= LARGEST_PAGESIZE; page <<= 1) {
2880N/A buf = volume_id_get_buffer(id, off + page-10, 10);
2880N/A if (buf == NULL)
2880N/A return -1;
2880N/A
2880N/A if (memcmp(buf, "SWAP-SPACE", 10) == 0) {
2880N/A strcpy(id->type_version, "1");
2880N/A goto found;
2880N/A }
2880N/A
2880N/A if (memcmp(buf, "SWAPSPACE2", 10) == 0) {
2880N/A sw = (struct swap_header_v1_2 *) volume_id_get_buffer(id, off, sizeof(struct swap_header_v1_2));
2880N/A if (sw == NULL)
2880N/A return -1;
2880N/A strcpy(id->type_version, "2");
2880N/A volume_id_set_label_raw(id, sw->volume_name, 16);
2880N/A volume_id_set_label_string(id, sw->volume_name, 16);
volume_id_set_uuid(id, sw->uuid, UUID_DCE);
goto found;
}
}
return -1;
found:
volume_id_set_usage(id, VOLUME_ID_OTHER);
id->type = "swap";
return 0;
}