2

I am trying to build a disk image as an output from my build system (currently SCons). Creating disk images from a directory structure is very easy to do, for example with genext2fs (http://genext2fs.sourceforge.net/).

But what I would like to do is to create an image that contains several partitions from multiple directories. I am currently having to do this by gluing together dd, losetup, kpartx, mount and copying files over. This requires root access.

Is there any convenient way to do this as a non-root user? Any application out there, suitable for inclusion into a build system that can perform this task efficiently?

7 Answers 7

1

I hate to say it, but I don't know of an easy way to do this.

It's quite probable you could make something work by creating an image file with dd, then partitioning it with fdisk, then creating a second image file with dd, and formatting it directly (or using something like genext2fs), then dd'ing the second filesystem image into the first partitioned image file at the correct offset. . . however, that's going to be difficult and complicated.

I'm afraid I don't know of any good way to accomplish this without root access, though.

1

Depending on why you want to avoid root access, sudo might be a solution.

Write a script that does all the stuff you need to do for the task, making sure users can't misuse it with "creative" input, and then give the user in question sudo rights for exactly that script and nothing else.

1

encountered similar problem recently, the main reason is that operating loop device files requires root privileges. got same idea as MikeyB's answer

and here's my code:

create_vdiskn() {
local path=$1
local dsize=$2
local fstype=$3
local imghead=img-head-$$
local imgtail=img-tail-$$
local fn=${FUNCNAME[0]}

echo -e "\n[$fn:info] creating disk and partition"
dd if=/dev/null of=$path bs=1${dsize//[0-9]/} seek=${dsize//[^0-9]/}
printf "o\nn\np\n1\n\n\nw\n" | fdisk "$path"
partprobe "$path"

read pstart psize < <( LANG=C parted -s $path unit B print | sed 's/B//g' |
    awk -v P=1 '/^Number/{start=1;next}; start {if ($1==P) {print $2, $4}}' )
echo -e "\n[$fn:info] split disk head and partition($pstart:$psize)"
dd if=$path of=$imghead bs=${pstart} count=1
truncate --size=${psize} $imgtail

echo -e "\n[$fn:info] making fs($fstype)"
mkfs.$fstype $MKFS_OPT "$imgtail"

echo -e "\n[$fn:info] concat image-head and partition"
cat $imghead $imgtail >$path
rm -vf $imghead $imgtail
}

[[ $# -lt 3 ]] && {
cat <<-COMM
Usage: [MKFS_OPT=xxx] $0 <image> <size> <fstype>
Examples:
  $0 usb.img 256M vfat
  $0 ext4.img 4G ext4
  MKFS_OPT="-f -i attr=2,size=512" $0 xfs.img 4G xfs
COMM
exit 1
}
create_vdiskn "$@"
0

you can try with mkisofs...

mkisofs -l -iso-level 4 -o file.iso folder

0

I haven't tried this, but have you tried fakeroot? It's commonly used on Debian-based distributions to bootstrap the initial set of files needed for an install (or in the case of a 64-bit system, a chroot with 32-bit files). You didn't say what OS you're using, fakeroot might not work for you.

I've seen this problem solved another way, use a virtual machine in the build system. In the VM you can be root without messing up the OS on your actual build machine, you can snapshot it so everytime it powers on it's at the same clean state.

0

Are the various partitions that you're creating known sizes?

You could work around the problem by creating a template 'disk image' with the partitions at known byte offsets, then splice the real filesystems into that image using dd.

0
  1. https://serverfault.com/a/332114/100216;
  2. if ISO9660 is generally fine with you, see also isohybrid script in syslinux distribution.

You must log in to answer this question.