From f387a3fcba910b45d8dc3c0443d8351eedc0bf6e Mon Sep 17 00:00:00 2001 From: phk Date: Sun, 29 Dec 2002 15:17:11 +0000 Subject: Make fdisk work on active GEOM devices. --- sbin/fdisk/fdisk.c | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) (limited to 'sbin') diff --git a/sbin/fdisk/fdisk.c b/sbin/fdisk/fdisk.c index e10929a..22ebd74 100644 --- a/sbin/fdisk/fdisk.c +++ b/sbin/fdisk/fdisk.c @@ -83,7 +83,8 @@ struct mboot { }; static struct mboot mboot; -static int fd; +static int fd, fdw; + #define ACTIVE 0x80 #define BOOT_MAGIC 0xAA55 @@ -694,7 +695,10 @@ static int open_disk(int flag) { struct stat st; + int rwmode, p; + char *s; + fdw = -1; if (stat(disk, &st) == -1) { if (errno == ENOENT) return -2; @@ -703,10 +707,26 @@ open_disk(int flag) } if ( !(st.st_mode & S_IFCHR) ) warnx("device %s is not character special", disk); - if ((fd = open(disk, - a_flag || I_flag || B_flag || flag ? O_RDWR : O_RDONLY)) == -1) { - if(errno == ENXIO) - return -2; + rwmode = a_flag || I_flag || B_flag || flag ? O_RDWR : O_RDONLY; + fd = open(disk, rwmode); + if (fd == -1 && errno == ENXIO) + return -2; + if (fd == -1 && errno == EPERM && rwmode == O_RDWR) { + fd = open(disk, O_RDONLY); + if (fd == -1) + return -3; + for (p = 1; p < 5; p++) { + asprintf(&s, "%ss%d", disk, p); + fdw = open(s, O_RDONLY); + free(s); + if (fdw == -1) + continue; + break; + } + if (fdw == -1) + return -4; + } + if (fd == -1) { warnx("can't open device %s", disk); return -1; } @@ -740,9 +760,14 @@ read_disk(off_t sector, void *buf) static ssize_t write_disk(off_t sector, void *buf) { - lseek(fd,(sector * 512), 0); - /* write out in the size that the read_disk found worked */ - return write(fd, buf, secsize); + + if (fdw != -1) { + return ioctl(fdw, DIOCSMBR, buf); + } else { + lseek(fd,(sector * 512), 0); + /* write out in the size that the read_disk found worked */ + return write(fd, buf, secsize); + } } static int -- cgit v1.1