You are not logged in.
Pages: 1
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
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
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
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
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
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
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
Last edited by dice (2020-12-28 03:03:39)
Offline
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
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
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
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
You should also be quoting your variables to avoid globbing and word substitution.
Brianna Ghey — Rest In Power
Offline
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
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
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
Admin, please delete.
Last edited by OalSkuul (2020-12-28 22:23:47)
Offline
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
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
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
Pages: 1