[klibc] Custom kinit to find device by "label"

noneya biz user1233 at gmail.com
Tue Jul 24 06:53:53 PDT 2007

  I need to reliably boot a server from a USB device.  Since USB
device ordering can be unpredictable,  I wrote a simple
early-userspace "init" program to find the root FS on the correct USB
device & partition.  I have "labeled" the root ext2 FS on the USB
drive, and the program searches for the label by examining the bytes
at a specific offset from the beginning of the partition.  The program
is written in Pascal, and compiled using Free Pascal (fpc).  I wrote
it after studying how kinit and run-init work.  If you are interested
in looking at the code, it can be downloaded here:


    Although the program seems to work, it causes a lock-up later in
the boot process.  I have been testing on Gentoo Linux.  My program
launches the Gentoo init which runs fine for a while and then
eventually locks up for some unknown reason.  I have tested "kinit" on
Gentoo, and it works fine.

  Because of the crash issue, I want to rewrite the program by
modifying kinit and using C.  This is where I need help.  Here is the
main function that needs to be translated into C:

function FindRootDeviceByLabel(const RootFSLabel: string): boolean;
  DEV_MAJOR: dev_t = 8;
  DEV_MODE: mode_t = S_IFBLK or &0660;
  ActualFSLabel: string;
  FileDescriptor: cint;
  ReadBytes: TsSize;
  DeviceFound: boolean;
  dev_minor: dev_t;
  Result := False;
  SetLength(ActualFSLabel, Length(RootFSLabel));

  //loop through all devices that might have our root filesystem sda2 to sdp2
  dev_minor := 2;
  while dev_minor < 243 do begin // last one is 242
    if mknod(DEVICENAME, DEV_MODE, (DEV_MAJOR shl 8) or dev_minor) = 0 then
      WriteLn('Created device node "', DEVICENAME, '". dev_minor = ',
    else begin
      WriteError('mknod with dev_minor:  ' + IntToStr(dev_minor));
      EXIT; //exit w/ error
    dev_minor := dev_minor + 16;

    FileDescriptor := FpOpen(DEVICENAME, 0, 0);
    if FileDescriptor >= 0 then begin
      WriteLn('Opened device:  ', DEVICENAME);
      ReadBytes := FpPRead(FileDescriptor,
        PChar(ActualFSLabel), Length(ActualFSLabel), $478);
      if (ReadBytes = Length(ActualFSLabel)) then begin
        if (ActualFSLabel = RootFSLabel) then begin
          Result := True;
          DeviceFound := True;
      else WriteError('Reading from device:  ' + DEVICENAME);

      WriteLn('Closing device:  ', DEVICENAME);
      if FpClose(FileDescriptor) <> 0 then
        WriteError('Closing device:  ' + DEVICENAME);

      if DeviceFound then BREAK;
    else WriteError('Opening device:  ' + DEVICENAME);

    //Delete the device so we can try again
    WriteLn('Deleting device we created:  ', DEVICENAME);
    if Fpunlink(DEVICENAME) <> 0 then
      WriteError('Deleting device:  ' + DEVICENAME);
  end; // while dev_minor < 243

I pay someone to help me get this working.  Anyone interested?


Wayne Sherman
System Design Works

More information about the klibc mailing list