yeoman / generator

Rails-inspired generator system that provides scaffolding for your apps
http://yeoman.io
BSD 2-Clause "Simplified" License
1.2k stars 298 forks source link

issue with spawnCommandSync and (linux) mount during generator run #1507

Closed deostroll closed 6 months ago

deostroll commented 6 months ago

Here is a stub from my custom generator file:

    this._msg(`Mounting ${chalk.green('disks/config.img')} to ${chalk.green('.mnt')} ...`);
    this._runCmd('mount', ['disks/config.img', '.mnt']);

The _runCmd is defined as follows:

  _runCmd(...args) {
    this.log(args);
    let res = this.spawnCommandSync(...args);
    if(res.status === null) {
      throw res.error
    } else if(res.status !== 0) {
      throw new Error(`${arguments[0]} exited with non-zero code: ${res.status}`);
    }
  }

When this generator is run (as root), this command always fails. Below is the error output of failure:

mount: .mnt: failed to setup loop device for /home/osboxes/code/cloud-init-projects/proj1/disks/config.img

For an isolated test, I created a fresh nodejs project npm init -y install yeoman-generator (v3.2.0), and, prepared the below code:

const yg = require('yeoman-generator');
const execa = require('execa');
// console.log(yg.prototype.spawnCommandSync);
const printEmpty = () => console.log('');

let { spawnCommandSync } = yg.prototype;
spawnCommandSync('mkdir' , ['.mnt', 'disks']);
printEmpty();
spawnCommandSync('dd', ['if=/dev/zero', 'of=disks/config.img', 'bs=1', 'count=0', 'seek=2M']);
printEmpty();
spawnCommandSync('mkfs.vfat',['-n', 'cidata', 'disks/config.img']);
printEmpty();
spawnCommandSync('mount', ['disks/config.img', '.mnt']);

The above test does not error.

Additionally, the custom generator I was authoring earlier assumes there is a src folder in the directory (where the generator if finally run) with files to be copied over to the "mount" location.

What could be the reason for the failure of the mount command when run within the generator?

Code available here: https://gist.github.com/deostroll/3f7907b673ea7c544b385f2a399c45fa

mshima commented 6 months ago

yeoman-generator v3.2.0 is more than 5 years old. We only support latest version.