Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Any way to provide network setting via kernel cmdline?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Networking & Security
View previous topic :: View next topic  
Author Message
bastibasti
Guru
Guru


Joined: 27 Nov 2006
Posts: 568

PostPosted: Tue Feb 07, 2017 6:58 am    Post subject: Any way to provide network setting via kernel cmdline? Reply with quote

Hi,

Is there any way to provide network config via cmdline? for example eth0=192.168.1.10 etc??
Im using the stock network (net.lo) script
Back to top
View user's profile Send private message
charles17
Advocate
Advocate


Joined: 02 Mar 2008
Posts: 2583

PostPosted: Tue Feb 07, 2017 7:10 am    Post subject: Reply with quote

Try ifconfig or ip address.
Back to top
View user's profile Send private message
cboldt
l33t
l33t


Joined: 24 Aug 2005
Posts: 829

PostPosted: Tue Feb 07, 2017 7:33 am    Post subject: Reply with quote

Nothing in /usr/src/linux/Documentation/kernel-parameters.txt

But that file does refer the reader to driver files for network interface hardware.
Back to top
View user's profile Send private message
eccerr0r
Watchman
Watchman


Joined: 01 Jul 2004
Posts: 7051
Location: almost Mile High in the USA

PostPosted: Tue Feb 07, 2017 7:43 am    Post subject: Reply with quote

There is a kernel option:

ip=ipaddress:optional_nfs_root_ipaddress:gateway_ipaddress:netmask:hostname:ether_device:off:dns0_addr:dns1_addr

so something like...

ip=124.22.114.26::124.22.114.1:255.255.255.0:myhostname:eth0:off:8.8.8.8:4.4.4.4

Note: this is a guess, I've never actually used it. Reference located in: $KERNEL/Documentation/filesystems/nfs/nfsroot.txt
_________________
Intel Core i7 2700K@ 4.1GHz/HD3000 graphics/8GB DDR3/180GB SSD
What am I supposed watching?
Back to top
View user's profile Send private message
krinn
Watchman
Watchman


Joined: 02 May 2003
Posts: 6968

PostPosted: Tue Feb 07, 2017 8:48 am    Post subject: Reply with quote

as easy as
ifconfig eth0 192.168.0.4
route add default gw 192.168.0.100

or even easier "dhcpcd eth0"
Back to top
View user's profile Send private message
khayyam
Watchman
Watchman


Joined: 07 Jun 2012
Posts: 6228
Location: Room 101

PostPosted: Tue Feb 07, 2017 9:02 am    Post subject: Re: Any way to provide network setting via kernel cmdline? Reply with quote

bastibasti wrote:
Is there any way to provide network config via cmdline? for example eth0=192.168.1.10 etc?? Im using the stock network (net.lo) script

bastibasti ... basically all you need do is bring the interface up, add an ip/route, and configure dns resolution:

Code:
# ip link set dev eth0 up
# ip addr add 192.168.1.10/24 brd + dev eth0
# ip route add default via 192.168.1.254
# echo "nameserver 192.168.1.254" >> /etc/resolv.conf

However, this will not provide the 'net' target (because you're configuring network outside of openrc/netifrc), so should something 'need net' some service (ie, net.eth0, or dhcpcd) will be started that 'provide[s] net'. For that reason you are better off doing the following:

/etc/conf.d/net:
config_eth0="192.168.1.10/24"
routes_eth0="default via 192.168.1.254"

Code:
# ln -s /etc/init.d/net.lo /etc/init.d/net.eth0
# /etc/init.d/net.eth0 start

You might find net.eth0 is started automatically at boot, prehaps due to 'hotplug', or because some service in the 'default' runlevel 'need[s] net', if you don't want this (which, I assume is your reason for asking the above) you could do the following:

/etc/rc.conf:
rc_dhcpcd_provide="!net"
rc_sshd_need="!net"

EDIT: just re-read the subject line, so ignore the above. All you need to is tell init what runlevel it should start by adding 'softlevel=<runlevel>', and if 'net.eth0' is in that runlevel it will be started.

HTH & best ... khay


Last edited by khayyam on Tue Feb 07, 2017 9:09 am; edited 1 time in total
Back to top
View user's profile Send private message
NeddySeagoon
Administrator
Administrator


Joined: 05 Jul 2003
Posts: 42592
Location: 56N 3W

PostPosted: Tue Feb 07, 2017 9:05 am    Post subject: Reply with quote

bastibasti,

What do you really want to do?
Tell us your problem rather than your perceived solution.

If you want to mount root over the network, for a diskless system, eccerr0r is pointing the way.
Check the syntax, its a long time since I've used it.
You still need to start networking with all the same parameters during boot or things that depend on networking won't start.

Then there is the network console. Its not secure, so don't use it over a hostile network.
You need a kernel option or two and its documented in /usr/scr/linux/Documentation
You still need to start networking normally too.

You can put anything you want on the kernel command line and parse it yourself out of /proc/cmdline.
Just make sure nothing else grabs it.
bastibasti_ip=
bastibasti_netmask=
and so on.
You need to write the script to parse /proc/cmdline and do whatever you want with it.
As a worked example, see how the init script deals with root=
_________________
Regards,

NeddySeagoon

Computer users fall into two groups:-
those that do backups
those that have never had a hard drive fail.
Back to top
View user's profile Send private message
bastibasti
Guru
Guru


Joined: 27 Nov 2006
Posts: 568

PostPosted: Tue Feb 07, 2017 2:28 pm    Post subject: Reply with quote

as stated in the thread title I'm talking about kernel cmdline, not bash
I know howto bring up the network on a console.


Code:
You can put anything you want on the kernel command line and parse it yourself out of /proc/cmdline.
Just make sure nothing else grabs it.
bastibasti_ip=
bastibasti_netmask=
and so on.


that was my intention. I wanted to know whether the stock rc scripts already include any functionality to parse the cmdline.

Seems like no. I use following script to run from rc.local

#netip_eth0=dhcp <- configure eth0 using dhcp
#netip_eth0=192.168.1.10,255.255.255.0,192.168.1.1,192.168.1.1
# dev IP MASK DefaultGW Nameserver

Code:


COMMANDLINE="/proc/cmdline"

##Get number of kernel arguments
num_args=$(awk '{print NF}' "$COMMANDLINE" | sort -nu | tail -n 1)
cmdline=$(cat "$COMMANDLINE")

#netip_eth0=192.168.1.10,255.255.255.0,192.168.1.1,192.168.1.1
#ip,mask,gw,nameserver

##Parse for settings
for ((coloumn=1;coloumn<=$num_args;coloumn++)); do

#get argument
arg=$(echo "$cmdline"|cut -f$coloumn -d ' ')
arg1=$(echo "$arg"|cut -f1 -d '=')
arg2=$(echo "$arg1"|cut -f1 -d '_')

if [ $arg2 == "netip" ]; then

interface=$(echo "$arg1"|cut -f2 -d '_')
val=$(echo "$arg"|cut -f2 -d '=')
ip=$(echo "$val"|cut -f1 -d ',')
mask=$(echo "$val"|cut -f2 -d ',')
gw=$(echo "$val"|cut -f3 -d ',')
nameserver=$(echo "$val"|cut -f4 -d ',')

if [ $ip == "dhcp" ]; then
echo "Network settings for ""$interface"" found in kernel command line."
echo "Calling DHCP, as requested"
dhcpcd "$interface"
fi

if [ $ip != "dhcp" ]; then
echo "Network settings for ""$interface"" found in kernel command line."
echo "Applying following settings:"
echo "IP: ""$ip"
echo "MASK: ""$mask"
echo "GATEWAY: ""$gw"
echo "NAMESERVER: ""$nameserver"

ifconfig "$interface" up
sleep 1
ifconfig "$interface" "$ip" netmask "$mask"
sleep 1
route add default gw "$gw"
echo "nameserver ""$nameserver" >>/etc/resolv.conf
fi

echo
fi

done



Back to top
View user's profile Send private message
khayyam
Watchman
Watchman


Joined: 07 Jun 2012
Posts: 6228
Location: Room 101

PostPosted: Tue Feb 07, 2017 2:54 pm    Post subject: Reply with quote

bastibasti wrote:
that was my intention. I wanted to know whether the stock rc scripts already include any functionality to parse the cmdline. Seems like no.

bastibasti ... all you need do is create a runlevel, and provide 'softlevel=<runlevel>' on the kernel command line:

Code:
# mkdir /etc/runlevels/online
# rc-update -s add default online # the '-s' for 'stacked' runlevel
# rc-update add net.eth0 online

... and then as a kernel parameter: 'softlevel=online'.

best ... khay
Back to top
View user's profile Send private message
bastibasti
Guru
Guru


Joined: 27 Nov 2006
Posts: 568

PostPosted: Tue Feb 07, 2017 3:04 pm    Post subject: Reply with quote

mmhh maybe i do need to explain what this is for. Sorry. My mistake on trying to focus on the problem but when youre focussing on an issue you dont see that more info is needed.

The rootfs is a ramdisk I dont want to change to ramdisk for changing the network settings.

Instead want to edit the network setting from the boot loader. It uses openrc and gentoo as a base including all other scripts for booting. My script works nicely in the ramdisk now. All I thought was that I wanted to prevent using another script if this functionality was already included in the gentoo net.lo init script
Back to top
View user's profile Send private message
NeddySeagoon
Administrator
Administrator


Joined: 05 Jul 2003
Posts: 42592
Location: 56N 3W

PostPosted: Tue Feb 07, 2017 3:15 pm    Post subject: Reply with quote

bastibasti,

The net.lo init script includes four hooks. Pre/Post UP and Pre/Post down.
They are documented at the end of /usr/share/doc/netifrc-*/net.example.bz2

I guess using preup is just a different way of doing what you are already doing.
_________________
Regards,

NeddySeagoon

Computer users fall into two groups:-
those that do backups
those that have never had a hard drive fail.
Back to top
View user's profile Send private message
khayyam
Watchman
Watchman


Joined: 07 Jun 2012
Posts: 6228
Location: Room 101

PostPosted: Tue Feb 07, 2017 3:31 pm    Post subject: Reply with quote

bastibasti wrote:
The rootfs is a ramdisk I dont want to change to ramdisk for changing the network settings. Instead want to edit the network setting from the boot loader. It uses openrc and gentoo as a base including all other scripts for booting. My script works nicely in the ramdisk now. All I thought was that I wanted to prevent using another script if this functionality was already included in the gentoo net.lo init script

bastibasti ... I see, the problem I see with this is that your script (run from local.d) will not 'provide net', you are better to use 'preup()' (in /etc/conf.d/net) and parse those values from /proc/cmdline to provide values for 'config_${IFACE}', etc.

best ... khay
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Networking & Security All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum