1995-04-28 23:57:04 +00:00
|
|
|
/*
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
* "THE BEER-WARE LICENSE" (Revision 42):
|
2002-03-25 13:52:45 +00:00
|
|
|
* <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
|
1995-04-28 23:57:04 +00:00
|
|
|
* can do whatever you want with this stuff. If we meet some day, and you think
|
|
|
|
* this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2001-09-30 21:16:57 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1995-04-28 23:57:04 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
1995-05-12 18:50:00 +00:00
|
|
|
#include <ctype.h>
|
1995-12-07 10:34:59 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdarg.h>
|
2001-11-11 12:16:50 +00:00
|
|
|
#include <sys/param.h>
|
1995-04-29 07:21:14 +00:00
|
|
|
#include <sys/disklabel.h>
|
|
|
|
#include <sys/diskslice.h>
|
2002-10-08 12:13:19 +00:00
|
|
|
#ifdef PC98
|
|
|
|
#include <sys/diskpc98.h>
|
|
|
|
#else
|
2002-10-01 07:24:55 +00:00
|
|
|
#include <sys/diskmbr.h>
|
2002-10-08 12:13:19 +00:00
|
|
|
#endif
|
2001-11-11 12:16:50 +00:00
|
|
|
#include <sys/mount.h>
|
1995-05-12 18:50:00 +00:00
|
|
|
#include <sys/stat.h>
|
2001-10-10 07:46:04 +00:00
|
|
|
#include <sys/sysctl.h>
|
1999-12-15 08:33:56 +00:00
|
|
|
#include <grp.h>
|
2000-06-28 22:28:50 +00:00
|
|
|
#include <paths.h>
|
1999-12-15 08:33:56 +00:00
|
|
|
#include <pwd.h>
|
1995-04-28 23:57:04 +00:00
|
|
|
#include "libdisk.h"
|
|
|
|
|
2003-01-10 19:25:38 +00:00
|
|
|
struct chunk *New_Chunk(void);
|
|
|
|
|
1995-12-07 10:34:59 +00:00
|
|
|
static int
|
2003-02-04 17:26:45 +00:00
|
|
|
Fixup_FreeBSD_Names(struct chunk *c)
|
1995-12-07 10:34:59 +00:00
|
|
|
{
|
2002-10-04 18:10:39 +00:00
|
|
|
struct chunk *c1, *c3;
|
2003-02-04 17:26:45 +00:00
|
|
|
uint j;
|
1995-12-07 10:34:59 +00:00
|
|
|
|
2002-11-15 13:24:29 +00:00
|
|
|
if (!strcmp(c->name, "X"))
|
|
|
|
return 0;
|
1995-12-07 10:34:59 +00:00
|
|
|
|
2002-10-04 18:10:39 +00:00
|
|
|
/* reset all names to "X" */
|
|
|
|
for (c1 = c->part; c1; c1 = c1->next) {
|
|
|
|
c1->oname = c1->name;
|
|
|
|
c1->name = malloc(12);
|
2002-11-15 13:24:29 +00:00
|
|
|
if (!c1->name)
|
|
|
|
return -1;
|
2002-10-04 18:10:39 +00:00
|
|
|
strcpy(c1->name,"X");
|
|
|
|
}
|
1995-12-07 10:34:59 +00:00
|
|
|
|
2002-10-04 18:10:39 +00:00
|
|
|
/* Allocate the first swap-partition we find */
|
|
|
|
for (c1 = c->part; c1; c1 = c1->next) {
|
2002-11-15 13:24:29 +00:00
|
|
|
if (c1->type == unused)
|
|
|
|
continue;
|
|
|
|
if (c1->subtype != FS_SWAP)
|
|
|
|
continue;
|
2002-10-04 18:10:39 +00:00
|
|
|
sprintf(c1->name, "%s%c", c->name, SWAP_PART + 'a');
|
|
|
|
break;
|
|
|
|
}
|
1995-12-07 10:34:59 +00:00
|
|
|
|
2002-10-04 18:10:39 +00:00
|
|
|
/* Allocate the first root-partition we find */
|
|
|
|
for (c1 = c->part; c1; c1 = c1->next) {
|
2002-11-15 13:24:29 +00:00
|
|
|
if (c1->type == unused)
|
|
|
|
continue;
|
|
|
|
if (!(c1->flags & CHUNK_IS_ROOT))
|
|
|
|
continue;
|
2002-10-04 18:10:39 +00:00
|
|
|
sprintf(c1->name, "%s%c", c->name, 0 + 'a');
|
|
|
|
break;
|
|
|
|
}
|
1995-12-07 10:34:59 +00:00
|
|
|
|
2002-10-04 18:10:39 +00:00
|
|
|
/* Try to give them the same as they had before */
|
|
|
|
for (c1 = c->part; c1; c1 = c1->next) {
|
2002-11-15 13:24:29 +00:00
|
|
|
if (strcmp(c1->name, "X"))
|
|
|
|
continue;
|
|
|
|
for (c3 = c->part; c3 ; c3 = c3->next)
|
|
|
|
if (c1 != c3 && !strcmp(c3->name, c1->oname))
|
|
|
|
goto newname;
|
2002-10-04 18:10:39 +00:00
|
|
|
strcpy(c1->name, c1->oname);
|
2002-11-15 13:24:29 +00:00
|
|
|
newname:
|
|
|
|
;
|
2002-10-04 18:10:39 +00:00
|
|
|
}
|
2002-11-15 13:24:29 +00:00
|
|
|
|
2002-10-04 18:10:39 +00:00
|
|
|
/* Allocate the rest sequentially */
|
|
|
|
for (c1 = c->part; c1; c1 = c1->next) {
|
2002-10-29 07:37:11 +00:00
|
|
|
const char order[] = "defghab";
|
2002-11-15 13:24:29 +00:00
|
|
|
|
|
|
|
if (c1->type == unused)
|
|
|
|
continue;
|
|
|
|
if (strcmp("X", c1->name))
|
|
|
|
continue;
|
1995-12-07 10:34:59 +00:00
|
|
|
|
2002-11-15 13:24:29 +00:00
|
|
|
for (j = 0; j < strlen(order); j++) {
|
2002-10-04 18:10:39 +00:00
|
|
|
sprintf(c1->name, "%s%c", c->name, order[j]);
|
2002-11-15 13:24:29 +00:00
|
|
|
for (c3 = c->part; c3 ; c3 = c3->next)
|
2002-10-04 18:10:39 +00:00
|
|
|
if (c1 != c3 && !strcmp(c3->name, c1->name))
|
|
|
|
goto match;
|
|
|
|
break;
|
2002-11-15 13:24:29 +00:00
|
|
|
match:
|
2002-10-04 18:10:39 +00:00
|
|
|
strcpy(c1->name, "X");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (c1 = c->part; c1; c1 = c1->next) {
|
|
|
|
free(c1->oname);
|
|
|
|
c1->oname = 0;
|
1995-05-03 22:36:52 +00:00
|
|
|
}
|
2002-10-04 18:10:39 +00:00
|
|
|
return 0;
|
1995-04-29 07:21:14 +00:00
|
|
|
}
|
|
|
|
|
2002-10-23 21:05:42 +00:00
|
|
|
#ifndef PC98
|
2002-10-22 09:13:02 +00:00
|
|
|
static int
|
2003-02-04 17:26:45 +00:00
|
|
|
Fixup_Extended_Names(struct chunk *c)
|
1995-04-29 07:21:14 +00:00
|
|
|
{
|
2002-10-04 18:10:39 +00:00
|
|
|
struct chunk *c1;
|
2002-11-15 13:24:29 +00:00
|
|
|
int j = 5;
|
1995-12-07 10:34:59 +00:00
|
|
|
|
2002-10-04 18:10:39 +00:00
|
|
|
for (c1 = c->part; c1; c1 = c1->next) {
|
2002-11-15 13:24:29 +00:00
|
|
|
if (c1->type == unused)
|
|
|
|
continue;
|
2002-10-04 18:10:39 +00:00
|
|
|
free(c1->name);
|
|
|
|
c1->name = malloc(12);
|
2002-11-15 13:24:29 +00:00
|
|
|
if (!c1->name)
|
|
|
|
return -1;
|
2003-02-04 17:26:45 +00:00
|
|
|
sprintf(c1->name, "%ss%d", c->disk->chunks->name, j++);
|
2002-10-04 18:10:39 +00:00
|
|
|
if (c1->type == freebsd)
|
2003-02-04 17:26:45 +00:00
|
|
|
if (Fixup_FreeBSD_Names(c1) != 0)
|
2002-10-04 18:10:39 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
1995-04-29 07:21:14 +00:00
|
|
|
}
|
2002-10-23 21:05:42 +00:00
|
|
|
#endif
|
1995-04-29 07:21:14 +00:00
|
|
|
|
2001-05-13 20:08:54 +00:00
|
|
|
int
|
1995-04-29 07:21:14 +00:00
|
|
|
Fixup_Names(struct disk *d)
|
2002-11-11 04:46:39 +00:00
|
|
|
{
|
2002-10-04 18:10:39 +00:00
|
|
|
struct chunk *c1, *c2;
|
2002-11-11 04:46:39 +00:00
|
|
|
#if defined(__i386__) || defined(__ia64__)
|
2002-10-04 18:10:39 +00:00
|
|
|
struct chunk *c3;
|
|
|
|
int j;
|
2002-11-11 04:46:39 +00:00
|
|
|
#endif
|
2002-10-04 18:10:39 +00:00
|
|
|
|
|
|
|
c1 = d->chunks;
|
2002-11-15 13:24:29 +00:00
|
|
|
for (c2 = c1->part; c2 ; c2 = c2->next) {
|
2002-10-04 18:10:39 +00:00
|
|
|
if (c2->type == unused)
|
|
|
|
continue;
|
|
|
|
if (strcmp(c2->name, "X"))
|
|
|
|
continue;
|
2002-11-11 04:46:39 +00:00
|
|
|
#if defined(__i386__) || defined(__ia64__)
|
2002-10-04 18:10:39 +00:00
|
|
|
c2->oname = malloc(12);
|
2002-11-15 13:24:29 +00:00
|
|
|
if (!c2->oname)
|
|
|
|
return -1;
|
|
|
|
for (j = 1; j <= NDOSPART; j++) {
|
2002-10-04 18:10:39 +00:00
|
|
|
sprintf(c2->oname, "%ss%d", c1->name, j);
|
2002-11-15 13:24:29 +00:00
|
|
|
for (c3 = c1->part; c3; c3 = c3->next)
|
2002-10-04 18:10:39 +00:00
|
|
|
if (c3 != c2 && !strcmp(c3->name, c2->oname))
|
|
|
|
goto match;
|
|
|
|
free(c2->name);
|
|
|
|
c2->name = c2->oname;
|
|
|
|
c2->oname = 0;
|
|
|
|
break;
|
|
|
|
match:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (c2->oname)
|
|
|
|
free(c2->oname);
|
1999-01-08 00:32:19 +00:00
|
|
|
#else
|
2002-10-04 18:10:39 +00:00
|
|
|
free(c2->name);
|
|
|
|
c2->name = strdup(c1->name);
|
2001-04-01 11:37:31 +00:00
|
|
|
#endif /*__i386__*/
|
1995-04-29 07:21:14 +00:00
|
|
|
}
|
2002-11-15 13:24:29 +00:00
|
|
|
for (c2 = c1->part; c2; c2 = c2->next) {
|
2002-10-04 18:10:39 +00:00
|
|
|
if (c2->type == freebsd)
|
2003-02-04 17:26:45 +00:00
|
|
|
Fixup_FreeBSD_Names(c2);
|
2000-03-29 15:10:28 +00:00
|
|
|
#ifndef PC98
|
2003-02-04 17:26:45 +00:00
|
|
|
else if (c2->type == extended)
|
|
|
|
Fixup_Extended_Names(c2);
|
2000-03-29 15:10:28 +00:00
|
|
|
#endif
|
2002-10-04 18:10:39 +00:00
|
|
|
}
|
|
|
|
return 0;
|
1995-04-29 07:21:14 +00:00
|
|
|
}
|
|
|
|
|
1995-04-28 23:57:04 +00:00
|
|
|
int
|
2002-11-15 13:24:29 +00:00
|
|
|
Create_Chunk(struct disk *d, u_long offset, u_long size, chunk_e type,
|
|
|
|
int subtype, u_long flags, const char *sname)
|
1995-04-28 23:57:04 +00:00
|
|
|
{
|
2002-10-04 18:10:39 +00:00
|
|
|
int i;
|
|
|
|
u_long l;
|
|
|
|
|
2002-11-15 13:24:29 +00:00
|
|
|
if (!(flags & CHUNK_FORCE_ALL)) {
|
2000-03-29 15:10:28 +00:00
|
|
|
#ifdef PC98
|
2002-10-04 18:10:39 +00:00
|
|
|
/* Never use the first cylinder */
|
|
|
|
if (!offset) {
|
|
|
|
offset += (d->bios_sect * d->bios_hd);
|
|
|
|
size -= (d->bios_sect * d->bios_hd);
|
|
|
|
}
|
2000-03-29 15:10:28 +00:00
|
|
|
#else
|
2002-10-04 18:10:39 +00:00
|
|
|
/* Never use the first track */
|
|
|
|
if (!offset) {
|
|
|
|
offset += d->bios_sect;
|
|
|
|
size -= d->bios_sect;
|
|
|
|
}
|
2001-04-01 11:37:31 +00:00
|
|
|
#endif /* PC98 */
|
2002-10-04 18:10:39 +00:00
|
|
|
|
|
|
|
/* Always end on cylinder boundary */
|
|
|
|
l = (offset+size) % (d->bios_sect * d->bios_hd);
|
|
|
|
size -= l;
|
|
|
|
}
|
1995-12-07 10:34:59 +00:00
|
|
|
|
2002-10-04 18:10:39 +00:00
|
|
|
i = Add_Chunk(d, offset, size, "X", type, subtype, flags, sname);
|
|
|
|
Fixup_Names(d);
|
|
|
|
return i;
|
1995-04-28 23:57:04 +00:00
|
|
|
}
|
1995-05-10 05:57:04 +00:00
|
|
|
|
|
|
|
struct chunk *
|
2003-01-10 19:25:38 +00:00
|
|
|
Create_Chunk_DWIM(struct disk *d, struct chunk *parent, u_long size,
|
2002-11-15 13:24:29 +00:00
|
|
|
chunk_e type, int subtype, u_long flags)
|
1995-05-10 05:57:04 +00:00
|
|
|
{
|
2002-10-04 18:10:39 +00:00
|
|
|
int i;
|
|
|
|
struct chunk *c1;
|
2003-02-04 17:26:45 +00:00
|
|
|
long offset;
|
2002-10-04 18:10:39 +00:00
|
|
|
|
|
|
|
if (!parent)
|
|
|
|
parent = d->chunks;
|
2003-01-10 19:25:38 +00:00
|
|
|
|
|
|
|
if (parent->type == freebsd && type == part && parent->part == NULL) {
|
|
|
|
c1 = New_Chunk();
|
|
|
|
if (c1 == NULL)
|
|
|
|
return (NULL);
|
|
|
|
c1->disk = parent->disk;
|
|
|
|
c1->offset = parent->offset;
|
|
|
|
c1->size = parent->size;
|
|
|
|
c1->end = parent->offset + parent->size - 1;
|
|
|
|
c1->type = unused;
|
|
|
|
if (parent->sname != NULL)
|
|
|
|
c1->sname = strdup(parent->sname);
|
|
|
|
c1->name = strdup("-");
|
|
|
|
parent->part = c1;
|
|
|
|
}
|
|
|
|
|
2002-11-15 13:24:29 +00:00
|
|
|
for (c1 = parent->part; c1; c1 = c1->next) {
|
|
|
|
if (c1->type != unused)
|
|
|
|
continue;
|
|
|
|
if (c1->size < size)
|
|
|
|
continue;
|
2002-10-04 18:10:39 +00:00
|
|
|
offset = c1->offset;
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
return 0;
|
2002-11-15 13:24:29 +00:00
|
|
|
found:
|
2002-10-04 18:10:39 +00:00
|
|
|
i = Add_Chunk(d, offset, size, "X", type, subtype, flags, "-");
|
|
|
|
if (i)
|
|
|
|
return 0;
|
|
|
|
Fixup_Names(d);
|
2002-11-15 13:24:29 +00:00
|
|
|
for (c1 = parent->part; c1; c1 = c1->next)
|
2002-10-04 18:10:39 +00:00
|
|
|
if (c1->offset == offset)
|
|
|
|
return c1;
|
|
|
|
/* barfout(1, "Serious internal trouble"); */
|
1995-05-10 05:57:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|