Ticket #734: mkfs.patch

File mkfs.patch, 8.5 KB (added by tradewind, 16 years ago)

patch for mkfs -- CLT for file system volume initialization

  • build/jam/HaikuImage

    diff --git a/build/jam/HaikuImage b/build/jam/HaikuImage
    index 7b0ad68..c4c8889 100644
    a b BEOS_BIN = "[" addattr alert arp base64 basename bc beep bootman bzip2 cal cat  
    3535    ideinfo idestatus ifconfig <bin>install installsound iroster isvolume join
    3636    keymap kill less lessecho lesskey link listarea listattr listdev listimage
    3737    listport listres listsem ln locate logger login logname ls lsindex m4 make
    38     makebootable md5sum merge mimeset mkdos mkdir mkfifo mkindex modifiers mount
     38    makebootable md5sum merge mimeset mkdos mkdir mkfifo mkfs mkindex modifiers mount
    3939    mount_nfs mountvolume mv nc netstat nl nohup od open passwd paste patch
    4040    pathchk pc ping play playfile playsound playwav pr prio printenv printf ps
    4141    ptx pwd query quit readlink release renice rescan rlog rm rmattr rmindex
  • src/bin/Jamfile

    diff --git a/src/bin/Jamfile b/src/bin/Jamfile
    index 208ff4e..ccbe218 100644
    a b SubInclude HAIKU_TOP src bin make ;  
    181181SubInclude HAIKU_TOP src bin makebootable ;
    182182#SubInclude HAIKU_TOP src bin makeudfimage ;
    183183SubInclude HAIKU_TOP src bin mkdos ;
     184SubInclude HAIKU_TOP src bin mkfs ;
    184185SubInclude HAIKU_TOP src bin multiuser ;
    185186SubInclude HAIKU_TOP src bin patch ;
    186187SubInclude HAIKU_TOP src bin pc ;
  • new file src/bin/mkfs/FsCreator.cpp

    diff --git a/src/bin/mkfs/FsCreator.cpp b/src/bin/mkfs/FsCreator.cpp
    new file mode 100644
    index 0000000..6115611
    - +  
     1/*
     2 * Copyright 2008 Haiku Inc. All rights reserved.
     3 * Distributed under the terms of the MIT License.
     4 *
     5 * Authors:
     6 *      Marco Minutoli, mminutoli@gmail.com
     7 */
     8
     9#include "FsCreator.h"
     10
     11#include <iostream>
     12
     13#include <DiskSystem.h>
     14
     15
     16FsCreator::FsCreator(const char* devPath, BString& type,
     17    BString& volumeName, const char* fsOpt, bool verbose)
     18    :fVolumeName(volumeName),fFsOptions(fsOpt), fVerbose(verbose)
     19{
     20    BDiskDeviceRoster roster;
     21
     22    if (type == "bfs" || type == "Be File System")
     23        fType = "Be File System";
     24
     25    status_t ret = roster.GetDeviceForPath(devPath, &fDevice);
     26    if (ret != B_OK) {
     27        std::cerr << "Error: Failed to get disk device for path "
     28                  << devPath << ": " << strerror(ret);
     29    }
     30}
     31
     32
     33bool
     34FsCreator::Run()
     35{
     36    // check that the device is writable
     37    if (fDevice.IsReadOnly()) {
     38        std::cerr << "Error: Can't Inizialize the device."
     39            "It is read only!!\n";
     40        return false;
     41    }
     42
     43    // check if the device is mounted
     44    if (fDevice.IsMounted()) {
     45        std::cerr << "Error: The device have to be unmounted before.\n";
     46        return false;
     47    }
     48
     49    BDiskSystem diskSystem;
     50    BDiskDeviceRoster dDRoster;
     51    bool found = false;
     52    while (dDRoster.GetNextDiskSystem(&diskSystem) == B_OK) {
     53        if (diskSystem.IsFileSystem() && diskSystem.SupportsInitializing()) {
     54            if (diskSystem.PrettyName() == fType) {
     55                found = true;
     56                break;
     57            }
     58        }
     59    }
     60
     61    if (!found) {
     62        std::cerr << "Error: Invalid file system type.\n";
     63        return false;
     64    }
     65
     66    // prepare the device for modifications
     67    status_t ret = fDevice.PrepareModifications();
     68    if (ret != B_OK) {
     69        std::cerr << "Error: A problem occurred preparing the device for the"
     70            "modifications\n";
     71        return false;
     72    }
     73    if (fVerbose)
     74        std::cout << "Preparing for modifications...\n\n";
     75
     76    // validate parameters
     77    BString name(fVolumeName);
     78    if (fDevice.ValidateInitialize(fType.String(),
     79            &fVolumeName, fFsOptions) != B_OK) {
     80        std::cerr << "Error: Parameters validation failed. "
     81            "Check what you wrote\n";
     82        std::cerr << ret;
     83        return false;
     84    }
     85    if (fVerbose)
     86        std::cout << "Parameters Validation...\n\n";
     87    if (name != fVolumeName)
     88        std::cout << "Volume name was adjusted to "
     89                  << fVolumeName.String() << std::endl;
     90
     91    // Initialize the partition
     92    ret = fDevice.Initialize(fType.String(), fVolumeName.String(), fFsOptions);
     93    if (ret != B_OK) {
     94        std::cerr << "Initialization failed: " <<  strerror(ret) << std::endl;
     95        return false;
     96    }
     97
     98    std::cout << "\nAre you sure you want to do this now?"
     99        "\nAll YOUR DATA in this partition will be lost forever\n";
     100    BString reply="";
     101    do {
     102        std::cout << "Continue? [yes|no]: ";
     103        reply = _ReadLine();
     104    } while (reply != "yes" && reply != "no");
     105
     106    if (reply == "yes") {
     107        ret = fDevice.CommitModifications();
     108        if (ret == B_OK) {
     109            if (fVerbose) {
     110                std::cout << "Volume " << fDevice.ContentName()
     111                          << " has been initialized successfully!\n";
     112            }
     113        } else {
     114            std::cout << "Error: Initialization of " << fDevice.ContentName()
     115                      << " failed !!\n";
     116            return false;
     117        }
     118    }
     119    return true;
     120}
     121
     122
     123inline BString
     124FsCreator::_ReadLine() {
     125    char line[255];
     126
     127    cin.getline(line, sizeof(line), '\n');
     128
     129    return line;
     130}
  • new file src/bin/mkfs/FsCreator.h

    diff --git a/src/bin/mkfs/FsCreator.h b/src/bin/mkfs/FsCreator.h
    new file mode 100644
    index 0000000..f1e281f
    - +  
     1/*
     2 * Copyright 2008 Haiku Inc. All rights reserved.
     3 * Distributed under the terms of the MIT License.
     4 *
     5 * Authors:
     6 *      Marco Minutoli, mminutoli@gmail.com
     7 */
     8#ifndef _FSCREATOR_H_
     9#define _FSCREATOR_H_
     10
     11#include <String.h>
     12
     13#include <DiskDevice.h>
     14#include <DiskDeviceRoster.h>
     15
     16class FsCreator {
     17public:
     18    FsCreator(const char* devPath, BString& type, BString& volumeName,
     19        const char* fsOpt, bool verbose);
     20
     21    bool Run();
     22private:
     23    inline BString _ReadLine();
     24
     25    BString fType;
     26    BString& fVolumeName;
     27    const char* fFsOptions;
     28    BDiskDevice fDevice;
     29    BPartition* fPartition;
     30    const bool fVerbose;
     31};
     32
     33#endif // _FSCREATOR_H_
  • new file src/bin/mkfs/Jamfile

    diff --git a/src/bin/mkfs/Jamfile b/src/bin/mkfs/Jamfile
    new file mode 100644
    index 0000000..112c398
    - +  
     1SubDir HAIKU_TOP src bin mkfs ;
     2
     3UsePrivateHeaders shared ;
     4UsePrivateHeaders storage ;
     5
     6BinCommand mkfs :
     7       main.cpp
     8       FsCreator.cpp
     9       : be libstdc++ ;
  • new file src/bin/mkfs/main.cpp

    diff --git a/src/bin/mkfs/main.cpp b/src/bin/mkfs/main.cpp
    new file mode 100644
    index 0000000..f0b4c45
    - +  
     1/*
     2 * Copyright 2004-2008 Haiku Inc. All rights reserved.
     3 * Distributed under the terms of the MIT License.
     4 *
     5 * Authors:
     6 *      Marco Minutoli, mminutoli@gmail.com
     7 */
     8
     9#include <stdio.h>
     10#include <iostream>
     11#include <stdlib.h>
     12#include <String.h>
     13#include <getopt.h>
     14#include "FsCreator.h"
     15
     16extern "C" const char* __progname;
     17static const char* kProgramName = __progname;
     18
     19static const char* kUsage =
     20"Usage: %s -t <fs> <options> <device> <volume name>\n"
     21"\n"
     22"Options:\n"
     23"  -t, --type       <fs>   - set type of filesystem to create\n\n"
     24"  -h, --help              - print this help text\n"
     25"  -o, --options    <opt>  - set fs specific options\n"
     26"  -v, --verbose           - set verbose output\n"
     27;
     28
     29
     30/*
     31 * Print program help on the right stream
     32 */
     33inline void
     34print_help(bool out)
     35{
     36    fprintf(out ? stdout : stderr, kUsage, kProgramName, kProgramName);
     37}
     38
     39
     40/*
     41 * Print program help and exit
     42 */
     43inline void
     44print_help_exit(bool out)
     45{
     46    print_help(out);
     47    exit(out ? 0 : 1);
     48}
     49
     50
     51int
     52main(int argc, char* const* argv)
     53{
     54    const struct option longOptions[] = {
     55        { "help", 0, NULL, 'h' },
     56        { "options", 0, NULL, 'o' },
     57        { "type", 1, NULL, 't' },
     58        { "verbose", 0, NULL, 'v' },
     59        { NULL, 0, NULL, 0 }
     60    };
     61    const char *shortOptions = "t:o:hv";
     62
     63    // parse argument list
     64    int32 nextOption;
     65    BString fsType;
     66    BString fsOptions;
     67    bool verbose = false;
     68
     69    nextOption = getopt_long(argc, argv, shortOptions, longOptions, NULL);
     70    if (nextOption == 't')
     71        fsType = optarg;
     72    else
     73        print_help_exit(nextOption == 'h' ? true : false);
     74
     75    do {
     76        nextOption = getopt_long(argc, argv, shortOptions, longOptions, NULL);
     77
     78        switch (nextOption) {
     79            case 't':   // -t or --type again?
     80                print_help_exit(false);
     81                break;
     82            case 'h':   // -h or --help
     83                print_help_exit(true);
     84                break;
     85            case 'v':   // -v or --verbose
     86                verbose = true;
     87                break;
     88            case 'o':   // -o or --options
     89                fsOptions << optarg;
     90                break;
     91            case '?':   // invalid option
     92                break;
     93            case -1:    // done with options
     94                break;
     95            default:    // everything else
     96                print_help(false);
     97                abort();
     98        }
     99    } while (nextOption != -1);
     100
     101    // the device name should be the first non-option element
     102    // right before the VolumeName
     103    if (optind != argc - 2)
     104        print_help_exit(false);
     105    const char* devPath = argv[optind];
     106    BString volName = argv[argc-1];
     107
     108    FsCreator* creator = new FsCreator(devPath, fsType, volName,
     109        fsOptions.String(), verbose);
     110    if (creator == NULL) {
     111        std::cerr << "Error: FsCreator can't be allocated\n";
     112        abort();
     113    }
     114
     115    return creator->Run();
     116}