The officially official Devuan Forum!

You are not logged in.

#1 2020-12-27 12:25:57

dice
Member
Registered: 2020-11-22
Posts: 559  
Website

dd command inside script.

hi, ive got a brain freeze on this and cant firgure it out, ive done something similar awhile ago.

what i want to do is fill in the values after the command as i want to put this in a script somehow.

dd if=/dev/urandom of=$1 bs=$2 count=$3 iflag=fullblock

so $1 will be say value.img and $2 will be 100M or whatever size the img should be and lastly $3 counts $2 incrementally.

??

so on the command line it would look similar to this before its run.

dd if=/dev/urandom of=$1 bs=$2 count=$3 iflag=fullblock value.img 100M 1

Last edited by dice (2020-12-27 12:33:59)

Offline

#2 2020-12-27 12:53:15

Head_on_a_Stick
Member
From: London
Registered: 2019-03-24
Posts: 3,125  
Website

Re: dd command inside script.

Not sure I understand your question but $1, $2 & $3 and the first, second and third arguments applied to a script. These are known as positional parameters.

Example script:

$ cat example
#!/bin/sh
echo "$1" "$2" "$3"
$ ./example value.img 100M 1
value.img 100M 1
$

Brianna Ghey — Rest In Power

Offline

#3 2020-12-27 12:55:27

fsmithred
Administrator
Registered: 2016-11-25
Posts: 2,486  

Re: dd command inside script.

dd if=/dev/urandom of=$1 bs=$2 count=$3 iflag=fullblock value.img 100M 1

Thinking about how that will expand is making my head hurt. Wouldn't $1 = 'if=/dev/urandom'? And so on...

I like to redefine positional paramaters at the beginning of the script to give them meaningful names and to make sure I know what they really are.

outfile="$1"
blocksize="$2"
blockcount="$3"

dd if=/dev/urandom of=$outfile bs=$blocksize count=$blockcount iflag=fullblock

Then call the script with

myscript.sh value.img 100M 1

Offline

#4 2020-12-27 12:59:56

dice
Member
Registered: 2020-11-22
Posts: 559  
Website

Re: dd command inside script.

Thanks both, positional parameters is what i need to learn. fsmithred, that looks like the way to go.

Offline

#5 2020-12-27 15:04:08

dice
Member
Registered: 2020-11-22
Posts: 559  
Website

Re: dd command inside script.

this is the script im working on, im stuck on the dd command as its giving me  dd: invalid number: ‘test.img’  when i run
sudo ./script.sh -C test.img 10 1
This has to be run via root or sudo, could be an issue there.

Im hoping to turn those crypt_open and crypt_close functions for the .img into positional parameters somehow.

EDIT: oops i need to also figure out cryptsetup creation. Ignore this post, keep it here for learning purposes.

cryptsetup options luksFormat device.img
cryptsetup open device.img name
mkfs.fstype /dev/mapper/name
cryptsetup open device.img name

#!/bin/bash

outfile="$2"
blocksize="$3"

create () {
        dd if=/dev/urandom of=$outfile bs=$blocksize count=1 iflag=fullblock
}

crypt_open () {
        losetup /dev/loop0 /home/$USER/ecrypt.img
        cryptsetup open /dev/loop0 ecryptfs
        mount -t ext4 /dev/mapper/ecryptfs /home/$USER/crypt/

}

crypt_close () {
        umount /home/$USER/crypt
        cryptsetup close ecryptfs
        losetup -d /dev/loop0

}

while getopts ":ocC" opt; do
  case ${opt} in
    o ) crypt_open
      ;;
    c ) crypt_close
      ;;
    C ) create $2 $3
      ;;
    \? ) echo "Usage: cmd [-o] [-c] [-C]"
      ;;
  esac
done

EDIT2; fixed dd function for getopts

Last edited by dice (2020-12-27 16:07:14)

Offline

#6 2020-12-27 21:16:31

chris2be8
Member
Registered: 2018-08-11
Posts: 307  

Re: dd command inside script.

My first step would be to add:

set -x

near the start of the script. This would tell bash to display the dd command as it will be executed after variable substitution. See the man page for bash for details.

Also look at set -n which tells bash to read commands but not execute them. This could be useful to syntax check the script before letting it do anything.

Chris

Offline

#7 2020-12-27 23:19:14

fsmithred
Administrator
Registered: 2016-11-25
Posts: 2,486  

Re: dd command inside script.

If you somehow end up with more than one loop device, you might use something like this:

losetup -f > /tmp/nextloop
LOOPDEV=$(cat /tmp/nextloop)

losetup ${LOOPDEV} ${LOOP_FILENAME}

That's taken from refracta2usb which can make an encrypted loopback filesystem for live-usb persistence.

Offline

#8 2020-12-28 03:03:21

dice
Member
Registered: 2020-11-22
Posts: 559  
Website

Re: dd command inside script.

thanks chris, set -x is quite helpful.

thanks fsmithred, that is exactly where i am stuck right now having two loop devices.

This not anything serious btw, just trying to learn. Think i would rather use tomb to be quite honest wink

Last edited by dice (2020-12-28 03:03:39)

Offline

#9 2020-12-28 09:59:47

dice
Member
Registered: 2020-11-22
Posts: 559  
Website

Re: dd command inside script.

Managed to cobble this together, not in anyway portable but ill keep trying.
something interesting i found out, luks header keys takes up 16 mb of space so you cant have have a small img like 10mb.

NAME           MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
loop0            7:0    0    20M  0 loop
└─cryptfs      254:1    0     4M  0 crypt /home/$u/crypt

Need to look into better command structure using more positional parameters, especially with the use of the crypt.img?
Find out how to use getopts usage better.

#!/bin/bash
set -x
u="<username>"
outfile="$2"
blocksize="$3"

crypt_img () {
	dd if=/dev/urandom of=$outfile bs=$blocksize count=1 iflag=fullblock
}

crypt_create () {
	losetup --find
	losetup /dev/loop0 crypt.img
	cryptsetup luksFormat crypt.img
	cryptsetup open crypt.img cryptfs
	mkfs.ext4 /dev/mapper/cryptfs
}

crypt_open () {
	cryptsetup open crypt.img cryptfs
}

crypt_mount () {
	mount -t ext4 /dev/mapper/cryptfs /home/$u/crypt
}

crypt_umount () {
	umount /home/$u/crypt
	cryptsetup close cryptfs
	losetup -d /dev/loop0
}

while getopts ":iComu" opt; do
  case ${opt} in
    i ) crypt_img $2 $3
      ;;
    C ) crypt_create
      ;;
    o ) crypt_open
      ;;
    m ) crypt_mount
      ;;
    u ) crypt_umount
      ;;
    \? ) echo "Usage: cmd [-i] [-C] [-o] [-m] [-u]"
      ;;
  esac
done

Last edited by dice (2020-12-28 10:09:42)

Offline

#10 2020-12-28 12:40:25

Head_on_a_Stick
Member
From: London
Registered: 2019-03-24
Posts: 3,125  
Website

Re: dd command inside script.

dice wrote:
    i ) crypt_img $2 $3

I think that should be

    i) crypt_img "$@"

Otherwise $2 & $3 will be interpreted as the third and forth arguments applied to the script.

Not sure about the encryption stuff though, I don't use that.


Brianna Ghey — Rest In Power

Offline

#11 2020-12-28 13:19:24

dice
Member
Registered: 2020-11-22
Posts: 559  
Website

Re: dd command inside script.

fsmithred wrote:

If you somehow end up with more than one loop device, you might use something like this:

losetup -f > /tmp/nextloop
LOOPDEV=$(cat /tmp/nextloop)

losetup ${LOOPDEV} ${LOOP_FILENAME}

That's taken from refracta2usb which can make an encrypted loopback filesystem for live-usb persistence.

Hi fsmithred, if you see this would you say this is the right way to implement loopdev?

here is just the code to the cryptsetup commands, im yet to figure out how to use that in the main script.

#!/bin/bash
set -x
LOOPDEV=$(cat /tmp/nextloop)
img="$2"

crypt_create () {
        losetup -f > /tmp/nextloop
        losetup ${LOOPDEV} $img
        cryptsetup luksFormat $img
        cryptsetup open $img cryptfs
        mkfs.ext4 /dev/mapper/cryptfs
}

while getopts ":c" opt; do
    case "${opt}" in
        c ) crypt_create $2
        ;;
       \? ) echo "Usage: cmd [c]"
        ;;
    esac
done

seems to work although set -x throws some errors?

sudo ./ccreate.sh -c test.img

++ cat /tmp/nextloop
cat: /tmp/nextloop: No such file or directory
+ LOOPDEV=
+ img=test.img
+ getopts :c opt
+ case "${opt}" in
+ crypt_create test.img
+ crypt_create test.img
+ losetup -f
+ losetup test.img
losetup: test.img: failed to use device: No such device
+ cryptsetup luksFormat test.img

WARNING!
========
This will overwrite data on test.img irrevocably.

Are you sure? (Type 'yes' in capital letters): YES
Enter passphrase for test.img:
Verify passphrase:
+ cryptsetup open test.img cryptfs
Enter passphrase for test.img:
+ mkfs.ext4 /dev/mapper/cryptfs
mke2fs 1.45.6 (20-Mar-2020)
Creating filesystem with 4096 1k blocks and 1024 inodes

Allocating group tables: done
Writing inode tables: done
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done

+ getopts :c opt

Offline

#12 2020-12-28 13:24:03

dice
Member
Registered: 2020-11-22
Posts: 559  
Website

Re: dd command inside script.

Head_on_a_Stick wrote:
dice wrote:
    i ) crypt_img $2 $3

I think that should be

    i) crypt_img "$@"

Otherwise $2 & $3 will be interpreted as the third and forth arguments applied to the script.

Not sure about the encryption stuff though, I don't use that.

Yes that seems to work as well. Im having a hard time wrapping my head around this stuff, thanks.

Offline

#13 2020-12-28 14:42:24

Head_on_a_Stick
Member
From: London
Registered: 2019-03-24
Posts: 3,125  
Website

Re: dd command inside script.

You should also be quoting your variables to avoid globbing and word substitution.

https://mywiki.wooledge.org/Quotes


Brianna Ghey — Rest In Power

Offline

#14 2020-12-28 14:56:13

fsmithred
Administrator
Registered: 2016-11-25
Posts: 2,486  

Re: dd command inside script.

You got this output for the line that says losetup ${LOOPDEV} $img but there's nothing for $LOOPDEV in the output. "No such device." The error also shows up in the first lines of the output where it shows that cat failed and LOOPDEV is empty.

+ losetup test.img
losetup: test.img: failed to use device: No such device

I think you need to define LOOPDEV after you run 'losetup -f'.

crypt_create () {
        losetup -f > /tmp/nextloop
        LOOPDEV=$(cat /tmp/nextloop)
        losetup ${LOOPDEV} $img
        cryptsetup luksFormat $img
        cryptsetup open $img cryptfs
        mkfs.ext4 /dev/mapper/cryptfs
}

Offline

#15 2020-12-28 15:20:51

dice
Member
Registered: 2020-11-22
Posts: 559  
Website

Re: dd command inside script.

still showing two loop devices, thanks for your help though.

 ~ $ > sudo ./ccreate.sh -c test.img
+ img=test.img
+ getopts :c opt
+ case "${opt}" in
+ crypt_create test.img
+ losetup -f
++ cat /tmp/nextloop
+ LOOPDEV=/dev/loop0
+ losetup /dev/loop0 test.img
+ cryptsetup luksFormat test.img

WARNING!
========
This will overwrite data on test.img irrevocably.

Are you sure? (Type 'yes' in capital letters): YES
Enter passphrase for test.img:
Verify passphrase:
+ cryptsetup open test.img cryptfs
Enter passphrase for test.img:
+ mkfs.ext4 /dev/mapper/cryptfs
mke2fs 1.45.6 (20-Mar-2020)
Creating filesystem with 4096 1k blocks and 1024 inodes

Allocating group tables: done
Writing inode tables: done
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done

+ getopts :c opt
 ~ $ > lsblk
NAME           MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
loop0            7:0    0    20M  0 loop
loop1            7:1    0    20M  0 loop
└─cryptfs      254:1    0     4M  0 crypt
#!/bin/bash
set -x
img="$2"

crypt_create () {
        losetup -f > /tmp/nextloop
        LOOPDEV=$(cat /tmp/nextloop)
        losetup ${LOOPDEV} $img
        cryptsetup luksFormat $img
        cryptsetup open $img cryptfs
        mkfs.ext4 /dev/mapper/cryptfs
}

while getopts ":c" opt; do
    case "${opt}" in
        c ) crypt_create $2
        ;;
       \? ) echo "Usage: cmd [c]"
        ;;
    esac
done

Offline

#16 2020-12-28 18:03:32

fsmithred
Administrator
Registered: 2016-11-25
Posts: 2,486  

Re: dd command inside script.

It looks like it worked. The previous error is gone. Maybe loop1 is left from before? I think you can just delete it manually.

Offline

#17 2020-12-28 19:33:39

OalSkuul
Member
Registered: 2020-12-23
Posts: 13  

Re: dd command inside script.

Admin, please delete.

Last edited by OalSkuul (2020-12-28 22:23:47)

Offline

#18 2020-12-28 21:52:11

Head_on_a_Stick
Member
From: London
Registered: 2019-03-24
Posts: 3,125  
Website

Re: dd command inside script.

OalSkuul wrote:

what is it for?

See random(4). If you have any other questions then please open a new thread rather than hijack this one, thanks.


Brianna Ghey — Rest In Power

Offline

#19 2020-12-29 01:03:15

dice
Member
Registered: 2020-11-22
Posts: 559  
Website

Re: dd command inside script.

fsmithred wrote:

It looks like it worked. The previous error is gone. Maybe loop1 is left from before? I think you can just delete it manually.

that seems to be the case. Thanks again.

Offline

#20 2020-12-29 10:31:56

Head_on_a_Stick
Member
From: London
Registered: 2019-03-24
Posts: 3,125  
Website

Re: dd command inside script.

Thought it better to post this here rather than pollute your new thread but just to let you know that https://shellcheck.net thinks you should double-quote the variables:

Line 13:
        mkdir -p /home/${u}/${dir}
                       ^-- SC2086: Double quote to prevent globbing and word splitting.
                            ^-- SC2086: Double quote to prevent globbing and word splitting.

Did you mean: (apply this, apply all SC2086)
        mkdir -p /home/"${u}"/"${dir}"
 
Line 19:
        dd if=/dev/urandom of=${outfile} bs=${blocksize} count=1 iflag=fullblock
                              ^-- SC2086: Double quote to prevent globbing and word splitting.
                                            ^-- SC2086: Double quote to prevent globbing and word splitting.

Did you mean: (apply this, apply all SC2086)
        dd if=/dev/urandom of="${outfile}" bs="${blocksize}" count=1 iflag=fullblock
 
Line 27:
        losetup ${LOOPDEV} ${img}
                ^-- SC2086: Double quote to prevent globbing and word splitting.
                           ^-- SC2086: Double quote to prevent globbing and word splitting.

Did you mean: (apply this, apply all SC2086)
        losetup "${LOOPDEV}" "${img}"
 
Line 28:
        cryptsetup luksFormat ${img}
                              ^-- SC2086: Double quote to prevent globbing and word splitting.

Did you mean: (apply this, apply all SC2086)
        cryptsetup luksFormat "${img}"
 
Line 29:
        cryptsetup open ${img} ${name}
                        ^-- SC2086: Double quote to prevent globbing and word splitting.
                               ^-- SC2086: Double quote to prevent globbing and word splitting.

Did you mean: (apply this, apply all SC2086)
        cryptsetup open "${img}" "${name}"
 
Line 30:
        mkfs.ext4 /dev/mapper/${name}
                              ^-- SC2086: Double quote to prevent globbing and word splitting.

Did you mean: (apply this, apply all SC2086)
        mkfs.ext4 /dev/mapper/"${name}"
 
Line 36:
        cryptsetup open ${img} ${name}
                        ^-- SC2086: Double quote to prevent globbing and word splitting.
                               ^-- SC2086: Double quote to prevent globbing and word splitting.

Did you mean: (apply this, apply all SC2086)
        cryptsetup open "${img}" "${name}"
 
Line 42:
        mount -t ext4 /dev/mapper/${name} /home/${u}/${dir}
                                  ^-- SC2086: Double quote to prevent globbing and word splitting.
                                                ^-- SC2086: Double quote to prevent globbing and word splitting.
                                                     ^-- SC2086: Double quote to prevent globbing and word splitting.

Did you mean: (apply this, apply all SC2086)
        mount -t ext4 /dev/mapper/"${name}" /home/"${u}"/"${dir}"
 
Line 49:
        umount /home/${u}/${dir}
                     ^-- SC2086: Double quote to prevent globbing and word splitting.
                          ^-- SC2086: Double quote to prevent globbing and word splitting.

Did you mean: (apply this, apply all SC2086)
        umount /home/"${u}"/"${dir}"
 
Line 50:
        cryptsetup close ${name}
                         ^-- SC2086: Double quote to prevent globbing and word splitting.

Did you mean: (apply this, apply all SC2086)
        cryptsetup close "${name}"

Oh, and I'm pleased to see that you've dropped bloaty old bash — POSIX sh ftw! :-)


Brianna Ghey — Rest In Power

Offline

#21 2020-12-29 11:56:01

dice
Member
Registered: 2020-11-22
Posts: 559  
Website

Re: dd command inside script.

Thanks head on a stick, i forgot to do that. This has been a good learning experience and its good to make mistakes and learn from them. I will definitely be looking into more posix orientated scripts in the future. Updated the script as per shellcheck.

Offline

Board footer